diff --git a/README.md b/README.md index e0b2c9c..17e1c8d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # JKit + help plugin for YanyanQt https://www.youtube.com/watch?v=ZLzqiZbvmz4 diff --git a/pom.xml b/pom.xml index ac101ad..9cff2da 100644 --- a/pom.xml +++ b/pom.xml @@ -1,12 +1,12 @@ - 4.0.0 josscoder.jkit JKit - 1.0.0 + 1.0.1 1.8 diff --git a/src/main/java/josscoder/jkit/command/KitCommand.java b/src/main/java/josscoder/jkit/command/KitCommand.java index 0619e2a..c8fff6c 100644 --- a/src/main/java/josscoder/jkit/command/KitCommand.java +++ b/src/main/java/josscoder/jkit/command/KitCommand.java @@ -10,8 +10,6 @@ import josscoder.jkit.data.Kit; import josscoder.jkit.helper.Helper; -import java.time.Duration; - public class KitCommand extends Command { public KitCommand() { @@ -92,11 +90,16 @@ private void selectKit(Player player) { Helper helper = JKitPlugin.getInstance().getHelper(); - helper.getKitList().values().forEach(kit -> windowForm.addButton(kit.getId(), String.format( - "%s\n%s", - kit.getName(), - (kit.isAvailable(player) ? TextFormat.DARK_GREEN + "Available" : TextFormat.DARK_RED + "No available") - ), kit.getImage())); + helper.getKitList().values().forEach(kit -> { + String timeLeft = kit.getTimeLeftString(player); + String state = (kit.isAvailable(player) ? TextFormat.DARK_GREEN + "Available" : TextFormat.DARK_RED + (kit.hasCooldown(player) ? timeLeft : "No available")); + + windowForm.addButton(kit.getId(), String.format( + "%s\n%s", + kit.getName(), + state + ), kit.getImage()); + }); windowForm.addHandler(event -> { if (event.wasClosed()) { @@ -122,11 +125,8 @@ private void selectKit(Player player) { } if (kit.hasCooldown(player) && !player.isOp()) { - int secondsLeft = kit.getTimeLeft(player) - helper.getCurrentSeconds(); - Duration duration = Duration.ofSeconds(secondsLeft); - String durationString = JKitPlugin.getInstance().getHelper().formatDuration(duration); - - player.sendMessage(TextFormat.RED + String.format("You have to wait %s to equip this kit again!", durationString)); + String timeLeft = kit.getTimeLeftString(player); + player.sendMessage(TextFormat.RED + String.format("You have to wait %s to equip this kit again!", timeLeft)); return; } diff --git a/src/main/java/josscoder/jkit/data/Kit.java b/src/main/java/josscoder/jkit/data/Kit.java index 86a68df..9164559 100644 --- a/src/main/java/josscoder/jkit/data/Kit.java +++ b/src/main/java/josscoder/jkit/data/Kit.java @@ -3,14 +3,14 @@ import cn.nukkit.Player; import cn.nukkit.inventory.PlayerInventory; import cn.nukkit.item.Item; -import cn.nukkit.item.enchantment.Enchantment; import cn.nukkit.utils.ConfigSection; import cn.nukkit.utils.TextFormat; import josscoder.jkit.JKitPlugin; +import josscoder.jkit.helper.Helper; import lombok.Getter; +import java.time.Duration; import java.util.HashMap; -import java.util.List; import java.util.Map; @Getter @@ -40,43 +40,22 @@ public Kit(String id, String name, String permission, int cooldown, PlayerInvent this.itemList.putAll(playerInventory.getContents()); } - public Kit(String id, ConfigSection mainSection) { + public Kit(String id, ConfigSection mainSection, Helper helper) { this.id = id; this.image = mainSection.getString("image", "textures/blocks/barrier.png"); this.name = mainSection.getString("name", "No name"); this.permission = mainSection.getString("permission", "kit.permission"); this.cooldown = mainSection.getInt("cooldown"); - this.helmet = Item.get(Integer.parseInt(mainSection.getString("helmet"))); - this.chestPlate = Item.get(Integer.parseInt(mainSection.getString("chestPlate"))); - this.leggings = Item.get(Integer.parseInt(mainSection.getString("leggings"))); - this.boots = Item.get(Integer.parseInt(mainSection.getString("boots"))); + + this.helmet = helper.readItemDataFromSection(mainSection.getSection("helmet")); + this.chestPlate = helper.readItemDataFromSection(mainSection.getSection("chestPlate")); + this.leggings = helper.readItemDataFromSection(mainSection.getSection("leggings")); + this.boots = helper.readItemDataFromSection(mainSection.getSection("boots")); ConfigSection itemsSection = mainSection.getSection("items"); itemsSection.getSections().getKeys(false).forEach(key -> { ConfigSection currentItemSection = itemsSection.getSection(key); - String[] itemData = currentItemSection.getString("data").split(":"); - - Item item = Item.get(Integer.parseInt(itemData[0]), - itemData.length >= 2 ? Integer.parseInt(itemData[1]) : 0, - itemData.length == 3 ? Integer.parseInt(itemData[2]) : 1 - ); - - if (currentItemSection.exists("customName")) { - item.setCustomName(currentItemSection.getString("customName")); - } - - List enchantmentsList = currentItemSection.getStringList("enchantments"); - if (!enchantmentsList.isEmpty()) { - enchantmentsList.forEach(enchantment -> { - String[] split = enchantment.split(":"); - if (enchantment.length() > 0 && split.length > 0) { - Enchantment enchantment1 = Enchantment.getEnchantment(Integer.parseInt(split[0])) - .setLevel(split.length == 2 ? Integer.parseInt(split[1]) : 1, false); - item.addEnchantment(enchantment1); - } - }); - } - + Item item = helper.readItemDataFromSection(currentItemSection); itemList.put(Integer.parseInt(key), item); }); } @@ -101,6 +80,14 @@ public boolean isAvailable(Player player) { return !hasCooldown(player) || player.isOp() || player.hasPermission(permission); } + public String getTimeLeftString(Player player) { + Helper helper = JKitPlugin.getInstance().getHelper(); + + int secondsLeft = getTimeLeft(player) - helper.getCurrentSeconds(); + Duration duration = Duration.ofSeconds(secondsLeft); + return helper.formatDuration(duration); + } + public void give(Player player) { PlayerInventory inventory = player.getInventory(); diff --git a/src/main/java/josscoder/jkit/helper/Helper.java b/src/main/java/josscoder/jkit/helper/Helper.java index 01ff2e5..9ea3314 100644 --- a/src/main/java/josscoder/jkit/helper/Helper.java +++ b/src/main/java/josscoder/jkit/helper/Helper.java @@ -27,7 +27,7 @@ public Helper(Config config) { public void loadKits() { mainSection.getSections().getKeys(false).forEach(key -> - registerKit(new Kit(key, mainSection.getSection(key))) + registerKit(new Kit(key, mainSection.getSection(key), this)) ); } @@ -51,20 +51,27 @@ public void registerKit(Kit kit, boolean serialize) { mainSection.set("name", kit.getName()); mainSection.set("permission", kit.getPermission()); mainSection.set("cooldown", kit.getCooldown()); - mainSection.set("helmet", String.valueOf(kit.getHelmet().getId())); - mainSection.set("chestPlate", String.valueOf(kit.getChestPlate().getId())); - mainSection.set("leggings", String.valueOf(kit.getLeggings().getId())); - mainSection.set("boots", String.valueOf(kit.getBoots().getId())); + + ConfigSection helmet = new ConfigSection(); + writeItemDataIntoSection(kit.getHelmet(), helmet); + mainSection.set("helmet", helmet); + + ConfigSection chestPlate = new ConfigSection(); + writeItemDataIntoSection(kit.getChestPlate(), chestPlate); + mainSection.set("chestPlate", chestPlate); + + ConfigSection leggings = new ConfigSection(); + writeItemDataIntoSection(kit.getLeggings(), leggings); + mainSection.set("leggings", leggings); + + ConfigSection boots = new ConfigSection(); + writeItemDataIntoSection(kit.getBoots(), boots); + mainSection.set("boots", boots); ConfigSection itemsSection = new ConfigSection(); kit.getItemList().forEach((index, item) -> { ConfigSection currentItemSection = new ConfigSection(); - if (item.hasCustomName()) { - currentItemSection.set("customName", item.getCustomName()); - } - currentItemSection.set("data", itemToString(item)); - currentItemSection.set("enchantments", enchantmentsToStringList(item.getEnchantments())); - + writeItemDataIntoSection(item, currentItemSection); itemsSection.set(String.valueOf(index), currentItemSection); }); @@ -74,6 +81,41 @@ public void registerKit(Kit kit, boolean serialize) { config.save(); } + public void writeItemDataIntoSection(Item item, ConfigSection section) { + if (item.hasCustomName()) { + section.set("customName", item.getCustomName()); + } + section.set("data", itemToString(item)); + section.set("enchantments", enchantmentsToStringList(item.getEnchantments())); + } + + public Item readItemDataFromSection(ConfigSection section) { + String[] itemData = section.getString("data").split(":"); + + Item item = Item.get(Integer.parseInt(itemData[0]), + itemData.length >= 2 ? Integer.parseInt(itemData[1]) : 0, + itemData.length == 3 ? Integer.parseInt(itemData[2]) : 1 + ); + + if (section.exists("customName")) { + item.setCustomName(section.getString("customName")); + } + + List enchantmentsList = section.getStringList("enchantments"); + if (!enchantmentsList.isEmpty()) { + enchantmentsList.forEach(enchantment -> { + String[] split = enchantment.split(":"); + if (enchantment.length() > 0 && split.length > 0) { + Enchantment enchantment1 = Enchantment.getEnchantment(Integer.parseInt(split[0])) + .setLevel(split.length == 2 ? Integer.parseInt(split[1]) : 1, false); + item.addEnchantment(enchantment1); + } + }); + } + + return item; + } + public String itemToString(Item item) { return String.format("%s:%s:%s", item.getId(), item.getDamage(), item.getCount()); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b5495f4..85f3769 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -4,10 +4,26 @@ kits: image: "textures/items/iron_sword.png" permission: "gladiator.kit.permission" cooldown: 60 #in seconds - helmet: 0 - chestPlate: 0 - leggings: 0 - boots: 0 + helmet: + data: "1:0:1" + enchantments: + - "" + - "" + chestPlate: + data: "1:0:1" + enchantments: + - "" + - "" + leggings: + data: "1:0:1" + enchantments: + - "" + - "" + boots: + data: "1:0:1" + enchantments: + - "" + - "" items: "0": data: "1:0:1"