SteamWar/FightSystem
Archiviert
13
1

Refactoring of KitSystem and adding of potion effects

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2019-12-22 16:38:51 +01:00
Ursprung c534a6b27b
Commit be6693e85a
3 geänderte Dateien mit 74 neuen und 73 gelöschten Zeilen

Datei anzeigen

@ -1,22 +1,44 @@
package de.steamwar.fightsystem.kit; package de.steamwar.fightsystem.kit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import java.util.Collection;
import java.util.Objects;
public class Kit { public class Kit {
private String name; private final String name;
private boolean leaderAllowed; private final boolean leaderAllowed;
private boolean memberAllowed; private final boolean memberAllowed;
private ItemStack[] inventory; private final ItemStack[] inventory;
private ItemStack[] armor; private final ItemStack[] armor;
private final Collection<PotionEffect> effects;
Kit(String name, boolean leaderAllowed, boolean memberAllowed, ItemStack[] inventory, ItemStack[] armor) { Kit(String name, Player player) {
this.name = name; this.name = name;
this.leaderAllowed = leaderAllowed; this.leaderAllowed = true;
this.memberAllowed = memberAllowed; this.memberAllowed = true;
this.inventory = inventory; this.inventory = player.getInventory().getContents();
this.armor = armor; 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<PotionEffect>) kit.getList("Effects");
else
effects = null;
} }
public String getName() { public String getName() {
@ -31,18 +53,20 @@ public class Kit {
return memberAllowed; return memberAllowed;
} }
ItemStack[] getInventory() {
return inventory;
}
ItemStack[] getArmor(){
return armor;
}
public void loadToPlayer(Player player) { public void loadToPlayer(Player player) {
player.getInventory().setContents(inventory); player.getInventory().setContents(inventory);
if(armor != null) if(armor != null)
player.getInventory().setArmorContents(armor); player.getInventory().setArmorContents(armor);
player.updateInventory(); 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);
} }
} }

Datei anzeigen

@ -5,7 +5,6 @@ import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -14,7 +13,6 @@ import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
public class KitManager { public class KitManager {
private KitManager(){} private KitManager(){}
private static final String KITS_PATH = "plugins/" + FightSystem.getPlugin().getName() + "/kits.data"; private static final String KITS_PATH = "plugins/" + FightSystem.getPlugin().getName() + "/kits.data";
@ -24,60 +22,10 @@ public class KitManager {
private static final ArrayList<Kit> loadedKits = new ArrayList<>(); private static final ArrayList<Kit> loadedKits = new ArrayList<>();
public static void saveInventory(String kitName, Player player){ 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(); saveAllKits();
} }
private static Kit loadKitFromConfig(String name) {
String path = "Kits." + name;
if(kitData.contains(path)) {
List<ItemStack> items = (List<ItemStack>) 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) { public static Kit getKitByName(String kitName) {
for(Kit kit : loadedKits) { for(Kit kit : loadedKits) {
if(kit.getName().equalsIgnoreCase(kitName)) if(kit.getName().equalsIgnoreCase(kitName))
@ -99,4 +47,32 @@ public class KitManager {
} }
return kits; 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);
}
}
} }

Datei anzeigen

@ -1,8 +1,9 @@
name: FightSystem name: FightSystem
version: 1.0 version: "1.0"
author: [Yaruma3341, Lixfel] authors: [Yaruma3341, Lixfel]
main: de.steamwar.fightsystem.FightSystem main: de.steamwar.fightsystem.FightSystem
depend: [SpigotCore, WorldEdit, ProtocolLib] depend: [SpigotCore, WorldEdit, ProtocolLib]
api-version: "1.13"
commands: commands:
ak: ak: