From be6693e85a5c50d8ec0963ee2dcd9277c70229c0 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 22 Dec 2019 16:38:51 +0100 Subject: [PATCH] Refactoring of KitSystem and adding of potion effects Signed-off-by: Lixfel --- .../src/de/steamwar/fightsystem/kit/Kit.java | 60 ++++++++++---- .../steamwar/fightsystem/kit/KitManager.java | 82 +++++++------------ FightSystem_Main/src/plugin.yml | 5 +- 3 files changed, 74 insertions(+), 73 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/kit/Kit.java b/FightSystem_Main/src/de/steamwar/fightsystem/kit/Kit.java index e54c323..22a3247 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/kit/Kit.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/kit/Kit.java @@ -1,22 +1,44 @@ package de.steamwar.fightsystem.kit; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; + +import java.util.Collection; +import java.util.Objects; public class Kit { - private String name; - private boolean leaderAllowed; - private boolean memberAllowed; - private ItemStack[] inventory; - private ItemStack[] armor; + private final String name; + private final boolean leaderAllowed; + private final boolean memberAllowed; + private final ItemStack[] inventory; + private final ItemStack[] armor; + private final Collection effects; - Kit(String name, boolean leaderAllowed, boolean memberAllowed, ItemStack[] inventory, ItemStack[] armor) { + Kit(String name, Player player) { this.name = name; - this.leaderAllowed = leaderAllowed; - this.memberAllowed = memberAllowed; - this.inventory = inventory; - this.armor = armor; + this.leaderAllowed = true; + this.memberAllowed = true; + this.inventory = player.getInventory().getContents(); + this.armor = player.getInventory().getArmorContents(); + this.effects = player.getActivePotionEffects(); + } + + Kit(ConfigurationSection kit){ + name = kit.getName(); + inventory = Objects.requireNonNull(kit.getList("Items")).toArray(new ItemStack[1]); + if(kit.isList("Armor")) + armor = Objects.requireNonNull(kit.getList("Armor")).toArray(new ItemStack[1]); + else + armor = null; + leaderAllowed = kit.getBoolean("LeaderAllowed"); + memberAllowed = kit.getBoolean("MemberAllowed"); + if(kit.isList("Effects")) + effects = (Collection) kit.getList("Effects"); + else + effects = null; } public String getName() { @@ -31,18 +53,20 @@ public class Kit { return memberAllowed; } - ItemStack[] getInventory() { - return inventory; - } - - ItemStack[] getArmor(){ - return armor; - } - public void loadToPlayer(Player player) { player.getInventory().setContents(inventory); if(armor != null) player.getInventory().setArmorContents(armor); player.updateInventory(); + player.addPotionEffects(effects); + } + + void saveKit(ConfigurationSection section){ + section.set("Items", inventory); + if(armor != null) + section.set("Armor", armor); + section.set("LeaderAllowed", leaderAllowed); + section.set("MemberAllowed", memberAllowed); + section.set("Effects", effects); } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/kit/KitManager.java b/FightSystem_Main/src/de/steamwar/fightsystem/kit/KitManager.java index 12b912e..ef1acf1 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/kit/KitManager.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/kit/KitManager.java @@ -5,7 +5,6 @@ import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; import java.io.File; import java.io.IOException; @@ -14,7 +13,6 @@ import java.util.List; import java.util.logging.Level; public class KitManager { - private KitManager(){} private static final String KITS_PATH = "plugins/" + FightSystem.getPlugin().getName() + "/kits.data"; @@ -24,60 +22,10 @@ public class KitManager { private static final ArrayList loadedKits = new ArrayList<>(); public static void saveInventory(String kitName, Player player){ - loadedKits.add(new Kit(kitName, true, true, player.getInventory().getContents(), player.getInventory().getArmorContents())); + loadedKits.add(new Kit(kitName, player)); saveAllKits(); } - private static Kit loadKitFromConfig(String name) { - String path = "Kits." + name; - if(kitData.contains(path)) { - List items = (List) kitData.getList(path + ".Items"); - - ItemStack[] armor = null; - if(kitData.isList(path + ".Armor")) - armor = kitData.getList(path + ".Armor").toArray(new ItemStack[1]); - - boolean leaderAllowed = kitData.getBoolean(path + ".LeaderAllowed"); - boolean memberAllowed = kitData.getBoolean(path + ".MemberAllowed"); - - return new Kit(name, leaderAllowed, memberAllowed, items.toArray(new ItemStack[1]), armor); - } - return null; - } - - public static void loadAllKits() { - if(!new File(KITS_PATH).exists()) { - saveAllKits(); - Bukkit.getLogger().log(Level.INFO, "kits.data erstellt!"); - FightSystem.shutdown(null); - return; - } - - if(kitData.getConfigurationSection("Kits") != null) { - for(String key : kitData.getConfigurationSection("Kits").getKeys(false)) { - loadedKits.add(loadKitFromConfig(key)); - } - } - } - - private static void saveAllKits(){ - YamlConfiguration yamlConfiguration = new YamlConfiguration(); - for(Kit k : loadedKits){ - String path = "Kits." + k.getName(); - yamlConfiguration.set(path + ".Items", k.getInventory()); - if(k.getArmor() != null) - yamlConfiguration.set(path + ".Armor", k.getArmor()); - yamlConfiguration.set(path + ".LeaderAllowed", k.isLeaderAllowed()); - yamlConfiguration.set(path + ".MemberAllowed", k.isMemberAllowed()); - } - - try { - yamlConfiguration.save(kits); - }catch(IOException e){ - Bukkit.getLogger().log(Level.SEVERE, "Failed to save kits.data", e); - } - } - public static Kit getKitByName(String kitName) { for(Kit kit : loadedKits) { if(kit.getName().equalsIgnoreCase(kitName)) @@ -99,4 +47,32 @@ public class KitManager { } return kits; } + + public static void loadAllKits() { + if(!new File(KITS_PATH).exists()) { + saveAllKits(); + Bukkit.getLogger().log(Level.INFO, "kits.data erstellt!"); + FightSystem.shutdown(null); + return; + } + + if(kitData.getConfigurationSection("Kits") != null) { + for(String key : kitData.getConfigurationSection("Kits").getKeys(false)) { + loadedKits.add(new Kit(kitData.getConfigurationSection("Kits." + key))); + } + } + } + + private static void saveAllKits(){ + YamlConfiguration yamlConfiguration = new YamlConfiguration(); + for(Kit k : loadedKits) + k.saveKit(yamlConfiguration.createSection("Kits." + k.getName())); + + try { + yamlConfiguration.save(kits); + }catch(IOException e){ + Bukkit.getLogger().log(Level.SEVERE, "Failed to save kits.data", e); + } + } + } diff --git a/FightSystem_Main/src/plugin.yml b/FightSystem_Main/src/plugin.yml index 3291afc..7835160 100644 --- a/FightSystem_Main/src/plugin.yml +++ b/FightSystem_Main/src/plugin.yml @@ -1,8 +1,9 @@ name: FightSystem -version: 1.0 -author: [Yaruma3341, Lixfel] +version: "1.0" +authors: [Yaruma3341, Lixfel] main: de.steamwar.fightsystem.FightSystem depend: [SpigotCore, WorldEdit, ProtocolLib] +api-version: "1.13" commands: ak: