개인 자료란 (JE)

  서버 커뮤니티

Profile 개발자토미 정품인증자

Luni5 ae6ece18994b48deb33b7529b6f49b30

Profile

이해도 플러그인 
게임버전 (JE) 기타 

    @EventHandler

    public void onClickShop(InventoryClickEvent e) {

        if (isSetting) return;

        for (String x : ShopData.getShopList()) {

            if (e.getView().title().equals(Component.text(x + " 상점"))) {

                e.setCancelled(true);

                for (String s : ShopData.getSlotList(x)) {

                    if (Objects.equals(e.getCurrentItem(), ShopData.getItemWithLore(x, s))) {

                        if (e.isLeftClick()) {

                            if (ShopData.getItemBuy(x, s) == 0) return;

                            if (e.isShiftClick()) {

                                if (Main.getEconomy().getBalance((OfflinePlayer) e.getWhoClicked()) >= 64 * ShopData.getItemBuy(x, s)) {

                                    if (ShopData.isLimitedItem(x, s) && ShopData.getLimitedAmount(x, s) <= 0) {

                                        e.getWhoClicked().sendMessage(index + "§c이미 다 팔린 상품입니다.");

                                        return;

                                    } else if (ShopData.isLimitedItem(x, s) && ShopData.getLimitedAmount(x, s) - 64 < 0) {

                                        e.getWhoClicked().sendMessage(index + "§c64개를 구입할 수 없습니다.");

                                        return;

                                    } else {

                                        if (e.getWhoClicked().getInventory().firstEmpty() == -1) {

                                            e.getWhoClicked().sendMessage(index + "§c인벤토리 공간이 부족합니다.");

                                            return;

                                        }

                                        EconomyResponse response = Main.getEconomy().withdrawPlayer((OfflinePlayer) e.getWhoClicked(), 64 * ShopData.getItemBuy(x, s));

                                        if (response.transactionSuccess()) {

                                            ItemStack item = ShopData.getItemShopSlot(x, s);

                                            item.lore(null);

                                            item.setAmount(64);

                                            e.getWhoClicked().getInventory().addItem(item);

                                            if (ShopData.getChangePriceList().contains(x)) {

                                                ShopPrice.buy.put(x + ":" + s, ShopPrice.buy.getOrDefault(x + ":" + s, 0) + 64);

                                                ShopPrice.shop.add(x);

                                            }

                                            e.getWhoClicked().sendMessage(index + "성공적으로 " + ShopData.getItemShopSlot(x, s).getType() + "을 64개 구매했습니다.");

                                            if (ShopData.isLimitedItem(x, s)) {

                                                ShopData.shopData.set("shop.price." + x + "." + s + ".realAmount", ShopData.getLimitedAmount(x, s) - 64);

                                                ShopData.saveData();

                                                e.getWhoClicked().closeInventory();

                                                e.getWhoClicked().openInventory(Objects.requireNonNull(ShopData.setupShop(x)));


                                            }

                                        } else {

                                            e.getWhoClicked().sendMessage(index + "§c당신은 구매에 실패했습니다.");

                                            e.getWhoClicked().sendMessage(index + ChatColor.RED + response.errorMessage);

                                        }

                                    }

                                } else e.getWhoClicked().sendMessage(index + "§c돈이 부족합니다.");

                            } else {

                                if (Main.getEconomy().getBalance((OfflinePlayer) e.getWhoClicked()) >= ShopData.getItemBuy(x, s)) {

                                    if (ShopData.isLimitedItem(x, s) && ShopData.getLimitedAmount(x, s) <= 0) {

                                        e.getWhoClicked().sendMessage(index + "§c이미 다 팔린 상품입니다.");

                                        return;

                                    } else {

                                        if (e.getWhoClicked().getInventory().firstEmpty() == -1) {

                                            boolean canMerge = false;

                                            for (ItemStack item : e.getWhoClicked().getInventory().getContents()) {

                                                if (item == null) continue;

                                                if (item.getType() == ShopData.getItemShopSlot(x, s).getType()) {

                                                    if (item.getAmount() < 64) {

                                                        canMerge = true;

                                                        break;

                                                    }

                                                }

                                            }

                                            if (!canMerge) {

                                                e.getWhoClicked().sendMessage(index + "§c인벤토리 공간이 부족합니다.");

                                                return;

                                            }

                                        }

                                        EconomyResponse response = Main.getEconomy().withdrawPlayer((OfflinePlayer) e.getWhoClicked(), ShopData.getItemBuy(x, s));

                                        if (response.transactionSuccess()) {

                                            ItemStack item = ShopData.getItemShopSlot(x, s);

                                            item.lore(null);

                                            item.setAmount(1);

                                            e.getWhoClicked().getInventory().addItem(item);

                                            if (ShopData.getChangePriceList().contains(x)) {

                                                ShopPrice.buy.put(x + ":" + s, ShopPrice.buy.getOrDefault(x + ":" + s, 0) + 1);

                                                ShopPrice.shop.add(x);

                                            }

                                            e.getWhoClicked().sendMessage(index + "성공적으로 " + ShopData.getItemShopSlot(x, s).getType() + "을 1개 구매했습니다.");

                                            if (ShopData.isLimitedItem(x, s)) {

                                                ShopData.shopData.set("shop.price." + x + "." + s + ".realAmount", ShopData.getLimitedAmount(x, s) - 1);

                                                ShopData.saveData();

                                                e.getWhoClicked().closeInventory();

                                                e.getWhoClicked().openInventory(Objects.requireNonNull(ShopData.setupShop(x)));


                                            }

                                        } else {

                                            e.getWhoClicked().sendMessage(index + "§c당신은 구매에 실패했습니다.");

                                            e.getWhoClicked().sendMessage(index + ChatColor.RED + response.errorMessage);

                                        }

                                    }

                                } else e.getWhoClicked().sendMessage(index + "§c돈이 부족합니다.");

                            }

                        } else if (e.isRightClick()) {

                            if (ShopData.getItemSell(x, s) == 0) return;

                            if (e.isShiftClick()) {

                                if (ShopData.ConsumeItems((Player) e.getWhoClicked(), ShopData.getItemShopSlot(x, s), 64)) {

                                    EconomyResponse response = Main.getEconomy().depositPlayer((OfflinePlayer) e.getWhoClicked(), 64 * ShopData.getItemSell(x, s));

                                    if (response.transactionSuccess()) {

                                        e.getWhoClicked().sendMessage(index + "성공적으로 " + ShopData.getItemShopSlot(x, s).getType() + "을 64개 판매했습니다.");

                                        if (ShopData.getChangePriceList().contains(x)) {

                                            ShopPrice.sell.put(x + ":" + s, ShopPrice.sell.getOrDefault(x + ":" + s, 0) + 1);

                                            ShopPrice.shop.add(x);

                                        }

                                    } else {

                                        e.getWhoClicked().sendMessage(index + "§c당신은 판매에 실패했습니다.");

                                        e.getWhoClicked().sendMessage(index + ChatColor.RED + response.errorMessage);

                                        ItemStack item = ShopData.getItemShopSlot(x, s);

                                        item.setAmount(64);

                                        e.getWhoClicked().getInventory().addItem(item);

                                    }

                                } else e.getWhoClicked().sendMessage(index + "§c물건이 부족합니다.");

                            } else {

                                if (ShopData.ConsumeItems((Player) e.getWhoClicked(), ShopData.getItemShopSlot(x, s), 1)) {

                                    EconomyResponse response = Main.getEconomy().depositPlayer((OfflinePlayer) e.getWhoClicked(), ShopData.getItemSell(x, s));

                                    if (response.transactionSuccess()) {

                                        e.getWhoClicked().sendMessage(index + "성공적으로 " + ShopData.getItemShopSlot(x, s).getType() + "을 1개 판매했습니다.");

                                        if (ShopData.getChangePriceList().contains(x)) {

                                            ShopPrice.sell.put(x + ":" + s, ShopPrice.sell.getOrDefault(x + ":" + s, 0) + 1);

                                            ShopPrice.shop.add(x);

                                        }

                                    } else {

                                        e.getWhoClicked().sendMessage(index + "§c당신은 판매에 실패했습니다.");

                                        e.getWhoClicked().sendMessage(index + ChatColor.RED + response.errorMessage);

                                        ItemStack item = ShopData.getItemShopSlot(x, s);

                                        item.setAmount(1);

                                        e.getWhoClicked().getInventory().addItem(item);

                                    }

                                } else e.getWhoClicked().sendMessage(index + "§c물건이 부족합니다.");

                            }

                        } else if (e.getClick().isCreativeAction()) {

                            if (ShopData.getItemSell(x, s) != 0 && ShopData.itemAmount((Player) e.getWhoClicked(), ShopData.getItemShopSlot(x, s)) != 0) {

                                int amount = ShopData.itemAmount((Player) e.getWhoClicked(), ShopData.getItemShopSlot(x, s));

                                if (ShopData.ConsumeItems((Player) e.getWhoClicked(), ShopData.getItemShopSlot(x, s), amount)) {

                                    EconomyResponse response = Main.getEconomy().depositPlayer((OfflinePlayer) e.getWhoClicked(), ShopData.getItemSell(x, s) * amount);

                                    if (response.transactionSuccess()) {

                                        e.getWhoClicked().sendMessage(index + "성공적으로 " + ShopData.getItemShopSlot(x, s).getType() + "을 " + amount + "개 판매했습니다.");

                                        if (ShopData.getChangePriceList().contains(x)) {

                                            ShopPrice.sell.put(x + ":" + s, ShopPrice.sell.getOrDefault(x + ":" + s, 0) + amount);

                                            ShopPrice.shop.add(x);

                                        }

                                    } else {

                                        e.getWhoClicked().sendMessage(index + "§c당신은 판매에 실패했습니다.");

                                        e.getWhoClicked().sendMessage(index + ChatColor.RED + response.errorMessage);

                                        ItemStack item = ShopData.getItemShopSlot(x, s);

                                        item.setAmount(amount);

                                        e.getWhoClicked().getInventory().addItem(item);

                                    }

                                } else e.getWhoClicked().sendMessage(index + "§c물건이 부족합니다.");

                            }

                        }

                    }

                }

            } else if (e.getView().title().toString().contains("상점 시세변동!")) e.setCancelled(true);

        }

    }


    @EventHandler

    public void onCloseInventory(InventoryCloseEvent e) {


        if (!isSetting) return;


        for (String x : ShopData.getShopList()) {

            if (e.getView().title().equals(Component.text(x + " 상점"))) {

                int slot = 0;

                for (ItemStack item : e.getInventory().getContents()) {

                    if (item != null) ShopData.setItem(x, item, slot);

                    else ShopData.removeItem(x, String.valueOf(slot));

                    slot++;

                }

                isSetting = false;

                break;

            }

        }

    }


위에는 소스코드입니다.
상점을 열게 되면 if (e.getView().title().equals(Component.text(x + " 상점"))) 안쪽 부분이 실행됩니다.
그러나 Shift + 좌클릭을 하게 되면 상점 gui에 있던 아이템이 64개로 늘어납니다.

gui의 아이템을 변경할려면 ShopData.setItem() 말고는 변경할 수 있는 방법이 없습니다.

왜 자꾸 바뀌는 것일까요?

8개의 댓글

서재형
2021.10.09

음, 28번째줄에 setAmount(64)가 있네요

개발자토미
2021.10.09
@서재형

코드를 더 자세히 보시면 아시겠지만, item이라는 객체는 방금 새로 만들어진 객체고 플레이어 인벤토리에 추가되는 겁니다.

서재형
2021.10.09
@개발자토미

2칸 위를보면 item객체는 ShopData.getItemShopSlot(x, s)로 가져오는 객체네요. 또한 아래쪽에 setAmount(64)가 하나 더있는데 그것또한 ShopData.getItemShopSlot(x, s)으로 가져오는 객체고요.

ShopData.getItemShopSlot(x, s)부분을 ShopData.getItemShopSlot(x, s).clone()으로 바꾸시면 고쳐질거에요.

개발자토미
2021.10.09
@서재형

방금 테스트 해봤는데 진짜로 setAmount 값에 따라 바뀌네요.. 그럼 이걸 해결 할만한 대책이 없을까요?

서재형
2021.10.09
@개발자토미

아이템.clone()은 아이템을 말그대로 복사해서 가져오기때문에, 이거로 가져온걸 setAmount로 바꾸시면 원본 아이템은 안바뀔거에요

개발자토미
2021.10.09
@서재형

.done()이 작동하지 않네용..

서재형
2021.10.09
@개발자토미

done()이 아니라 c lone()입니당

개발자토미
2021.10.09
@서재형

감사합니다. 처음 안 사실이라 몰랐네요.

뉴스 및 창작물
/files/thumbnails/520/751/003/262x150.crop.jpg?20240328020349

레드스톤

마인크래프트 노트블록으로 만든 『 Bling‐Bang‐Bang‐Born 』 1

Sonttukk

2024-03-23

1

/files/thumbnails/467/742/003/262x150.crop.jpg?20240311163123

레드스톤

[노트블럭커버] MILGRAM -ミルグラム(밀그램)- / 아마네 「숙청 행진」 제 2심 2

리비온

2024-03-11

0

/files/thumbnails/846/741/003/262x150.crop.jpg?20240310221214

레드스톤

워든 vs 라마 200마리 1

GlassesFilm

2024-03-10

0

/files/thumbnails/542/739/003/262x150.crop.jpg?20240309070457

레드스톤

[고인의 명복을 빕니다][노트블럭]드래곤볼 GT 오프닝 - 점점 마음이 끌려

리비온

2024-03-09

0

/files/thumbnails/326/738/003/262x150.crop.jpg?20240328020414

레드스톤

마인크래프트 노트블록으로 만든 『 Build Our Machine 』

Sonttukk

2024-03-07

0