From 6b5d3895598d5ace9882eb5bcf4b726e645779c9 Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Sun, 3 Sep 2023 23:39:04 +0800 Subject: [PATCH] optimized-market --- .../api/data/user/OnlineUser.java | 6 -- .../api/manager/MarketManager.java | 2 - .../mechanic/game/AbstractGamingPlayer.java | 3 - .../command/sub/ItemCommand.java | 71 +------------------ .../mechanic/block/BlockManagerImpl.java | 5 +- .../mechanic/item/ItemManagerImpl.java | 65 +++++++++++++++++ .../mechanic/market/MarketGUI.java | 14 ++-- .../mechanic/market/MarketManagerImpl.java | 11 +-- 8 files changed, 85 insertions(+), 92 deletions(-) diff --git a/api/src/main/java/net/momirealms/customfishing/api/data/user/OnlineUser.java b/api/src/main/java/net/momirealms/customfishing/api/data/user/OnlineUser.java index 53df007c..2f52257c 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/data/user/OnlineUser.java +++ b/api/src/main/java/net/momirealms/customfishing/api/data/user/OnlineUser.java @@ -1,13 +1,7 @@ package net.momirealms.customfishing.api.data.user; -import net.momirealms.customfishing.api.data.EarningData; -import net.momirealms.customfishing.api.data.PlayerData; -import net.momirealms.customfishing.api.mechanic.bag.FishingBagHolder; -import net.momirealms.customfishing.api.mechanic.statistic.Statistics; import org.bukkit.entity.Player; -import java.util.UUID; - public interface OnlineUser extends OfflineUser { Player getPlayer(); } diff --git a/api/src/main/java/net/momirealms/customfishing/api/manager/MarketManager.java b/api/src/main/java/net/momirealms/customfishing/api/manager/MarketManager.java index 3faf2135..cfe571ca 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/manager/MarketManager.java +++ b/api/src/main/java/net/momirealms/customfishing/api/manager/MarketManager.java @@ -3,8 +3,6 @@ package net.momirealms.customfishing.api.manager; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import java.util.UUID; - public interface MarketManager { void openMarketGUI(Player player); diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java index 16d13c2a..a4fae0ac 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java @@ -8,9 +8,6 @@ import org.bukkit.Material; import org.bukkit.entity.FishHook; import org.bukkit.entity.Player; import org.bukkit.inventory.PlayerInventory; -import org.bukkit.potion.PotionEffectType; - -import java.util.concurrent.TimeUnit; public abstract class AbstractGamingPlayer implements GamingPlayer, Runnable { diff --git a/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java b/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java index ccafddae..493b666e 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java +++ b/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java @@ -8,11 +8,10 @@ import dev.jorel.commandapi.arguments.TextArgument; import net.momirealms.customfishing.adventure.AdventureManagerImpl; import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.common.Key; -import net.momirealms.customfishing.api.util.LogUtils; +import net.momirealms.customfishing.mechanic.item.ItemManagerImpl; import net.momirealms.customfishing.setting.Locale; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; import java.util.Collection; @@ -55,7 +54,7 @@ public class ItemCommand { int amount = (int) args.getOrDefault("amount", 1); ItemStack item = CustomFishingPlugin.get().getItemManager().build(player, namespace, id); if (item != null) { - int actual = giveCertainAmountOfItem(player, item, amount); + int actual = ItemManagerImpl.giveCertainAmountOfItem(player, item, amount); AdventureManagerImpl.getInstance().sendMessageWithPrefix(player, Locale.MSG_Get_Item.replace("{item}", id).replace("{amount}", String.valueOf(actual))); } else { AdventureManagerImpl.getInstance().sendMessageWithPrefix(player, Locale.MSG_Item_Not_Exists); @@ -75,7 +74,7 @@ public class ItemCommand { ItemStack item = CustomFishingPlugin.get().getItemManager().build(players.stream().findAny().get(), namespace, id); if (item != null) { for (Player player : players) { - int actual = giveCertainAmountOfItem(player, item, amount); + int actual = ItemManagerImpl.giveCertainAmountOfItem(player, item, amount); AdventureManagerImpl.getInstance().sendMessageWithPrefix(sender, Locale.MSG_Give_Item.replace("{item}", id).replace("{amount}", String.valueOf(actual)).replace("{player}", player.getName())); } } else { @@ -83,68 +82,4 @@ public class ItemCommand { } }); } - - private int giveCertainAmountOfItem(Player player, ItemStack itemStack, int amount) { - PlayerInventory inventory = player.getInventory(); - String metaStr = itemStack.getItemMeta().getAsString(); - int maxStackSize = itemStack.getMaxStackSize(); - - if (amount > maxStackSize * 100) { - LogUtils.warn("Detected too many items spawning. Lowering the amount to " + (maxStackSize * 100)); - amount = maxStackSize * 100; - } - - int actualAmount = amount; - - for (ItemStack other : inventory.getStorageContents()) { - if (other != null) { - if (other.getType() == itemStack.getType() && other.getItemMeta().getAsString().equals(metaStr)) { - if (other.getAmount() < maxStackSize) { - int delta = maxStackSize - other.getAmount(); - if (amount > delta) { - other.setAmount(maxStackSize); - amount -= delta; - } else { - other.setAmount(amount + other.getAmount()); - return actualAmount; - } - } - } - } - } - - if (amount > 0) { - for (ItemStack other : inventory.getStorageContents()) { - if (other == null) { - if (amount > maxStackSize) { - amount -= maxStackSize; - ItemStack cloned = itemStack.clone(); - cloned.setAmount(maxStackSize); - inventory.addItem(cloned); - } else { - ItemStack cloned = itemStack.clone(); - cloned.setAmount(amount); - inventory.addItem(cloned); - return actualAmount; - } - } - } - } - - if (amount > 0) { - for (int i = 0; i < amount / maxStackSize; i++) { - ItemStack cloned = itemStack.clone(); - cloned.setAmount(maxStackSize); - player.getWorld().dropItem(player.getLocation(), cloned); - } - int left = amount % maxStackSize; - if (left != 0) { - ItemStack cloned = itemStack.clone(); - cloned.setAmount(left); - player.getWorld().dropItem(player.getLocation(), cloned); - } - } - - return actualAmount; - } } diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/block/BlockManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/block/BlockManagerImpl.java index 5bced975..1f05cd97 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/block/BlockManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/block/BlockManagerImpl.java @@ -21,12 +21,13 @@ import org.bukkit.block.data.type.NoteBlock; import org.bukkit.block.data.type.TurtleEgg; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.*; +import org.bukkit.entity.Ageable; +import org.bukkit.entity.FallingBlock; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityChangeBlockEvent; -import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java index 3175186f..dc39c93d 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java @@ -26,6 +26,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -552,4 +553,68 @@ public class ItemManagerImpl implements ItemManager { return this; } } + + public static int giveCertainAmountOfItem(Player player, ItemStack itemStack, int amount) { + PlayerInventory inventory = player.getInventory(); + String metaStr = itemStack.getItemMeta().getAsString(); + int maxStackSize = itemStack.getMaxStackSize(); + + if (amount > maxStackSize * 100) { + LogUtils.warn("Detected too many items spawning. Lowering the amount to " + (maxStackSize * 100)); + amount = maxStackSize * 100; + } + + int actualAmount = amount; + + for (ItemStack other : inventory.getStorageContents()) { + if (other != null) { + if (other.getType() == itemStack.getType() && other.getItemMeta().getAsString().equals(metaStr)) { + if (other.getAmount() < maxStackSize) { + int delta = maxStackSize - other.getAmount(); + if (amount > delta) { + other.setAmount(maxStackSize); + amount -= delta; + } else { + other.setAmount(amount + other.getAmount()); + return actualAmount; + } + } + } + } + } + + if (amount > 0) { + for (ItemStack other : inventory.getStorageContents()) { + if (other == null) { + if (amount > maxStackSize) { + amount -= maxStackSize; + ItemStack cloned = itemStack.clone(); + cloned.setAmount(maxStackSize); + inventory.addItem(cloned); + } else { + ItemStack cloned = itemStack.clone(); + cloned.setAmount(amount); + inventory.addItem(cloned); + return actualAmount; + } + } + } + } + + if (amount > 0) { + for (int i = 0; i < amount / maxStackSize; i++) { + ItemStack cloned = itemStack.clone(); + cloned.setAmount(maxStackSize); + player.getWorld().dropItem(player.getLocation(), cloned); + } + int left = amount % maxStackSize; + if (left != 0) { + ItemStack cloned = itemStack.clone(); + cloned.setAmount(left); + player.getWorld().dropItem(player.getLocation(), cloned); + } + } + + return actualAmount; + } } \ No newline at end of file diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/market/MarketGUI.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/market/MarketGUI.java index 26ed607d..5e713b3c 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/market/MarketGUI.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/market/MarketGUI.java @@ -5,6 +5,7 @@ import net.momirealms.customfishing.api.data.EarningData; import net.momirealms.customfishing.api.mechanic.market.MarketGUIHolder; import net.momirealms.customfishing.api.util.InventoryUtils; import net.momirealms.customfishing.api.util.LogUtils; +import net.momirealms.customfishing.mechanic.item.ItemManagerImpl; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -41,12 +42,13 @@ public class MarketGUI { private void init() { int line = 0; for (String content : manager.getLayout()) { - if (content.length() != 9) { - LogUtils.warn("Please make sure that GUI layout has 9 elements in each row"); - return; - } for (int index = 0; index < 9; index++) { - char symbol = content.charAt(index); + char symbol; + if (index < content.length()) { + symbol = content.charAt(index); + } else { + symbol = ' '; + } MarketGUIElement element = itemsCharMap.get(symbol); if (element != null) { element.addSlot(index + line * 9); @@ -178,7 +180,7 @@ public class MarketGUI { for (int slot : itemElement.getSlots()) { ItemStack itemStack = inventory.getItem(slot); if (itemStack != null && itemStack.getType() != Material.AIR) { - owner.getInventory().addItem(itemStack); + ItemManagerImpl.giveCertainAmountOfItem(owner, itemStack, itemStack.getAmount()); inventory.setItem(slot, new ItemStack(Material.AIR)); } } diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/market/MarketManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/market/MarketManagerImpl.java index fbc95bc9..2ada2d2a 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/market/MarketManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/market/MarketManagerImpl.java @@ -1,10 +1,8 @@ package net.momirealms.customfishing.mechanic.market; -import com.willfp.eco.core.map.MutableListMap; import de.tr7zw.changeme.nbtapi.NBTItem; import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.data.EarningData; -import net.momirealms.customfishing.api.data.InventoryData; import net.momirealms.customfishing.api.data.user.OnlineUser; import net.momirealms.customfishing.api.manager.MarketManager; import net.momirealms.customfishing.api.mechanic.action.Action; @@ -23,7 +21,10 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.*; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -149,7 +150,7 @@ public class MarketManagerImpl implements MarketManager, Listener { if (event.isCancelled()) return; Inventory inventory = event.getInventory(); - if (!(inventory.getHolder() instanceof MarketGUIHolder holder)) + if (!(inventory.getHolder() instanceof MarketGUIHolder)) return; Player player = (Player) event.getWhoClicked(); MarketGUI gui = marketGUIMap.get(player.getUniqueId()); @@ -182,7 +183,7 @@ public class MarketManagerImpl implements MarketManager, Listener { if (clickedInv == null) return; Player player = (Player) event.getWhoClicked(); - if (!(event.getInventory().getHolder() instanceof MarketGUIHolder holder)) + if (!(event.getInventory().getHolder() instanceof MarketGUIHolder)) return; MarketGUI gui = marketGUIMap.get(player.getUniqueId());