From d2fd8b4fc2688cb89a7653358843cb71746df097 Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Sat, 18 Feb 2017 03:27:10 +0100 Subject: [PATCH] Add support for bringing your own items into the arena. This implementation uses a sublcass of ArenaClass to make an implicit My Items class, which restores the player's inventory when granting items. --- .../com/garbagemule/MobArena/ArenaClass.java | 38 +++++++++++++++++++ .../com/garbagemule/MobArena/ArenaImpl.java | 2 + .../garbagemule/MobArena/ArenaMasterImpl.java | 15 +++++++- .../util/inventory/InventoryManager.java | 24 +++++++----- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/garbagemule/MobArena/ArenaClass.java b/src/main/java/com/garbagemule/MobArena/ArenaClass.java index e27348a..808e39b 100644 --- a/src/main/java/com/garbagemule/MobArena/ArenaClass.java +++ b/src/main/java/com/garbagemule/MobArena/ArenaClass.java @@ -3,6 +3,8 @@ package com.garbagemule.MobArena; import java.util.*; import java.util.Map.Entry; +import com.garbagemule.MobArena.framework.Arena; +import com.garbagemule.MobArena.framework.ArenaMaster; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -340,4 +342,40 @@ public class ArenaClass public int hashCode() { return lowercaseName.hashCode(); } + + public static class MyItems extends ArenaClass { + private ArenaMaster am; + + public MyItems(double price, boolean unbreakableWeapons, boolean unbreakableArmor, ArenaMaster am) { + super("", price, unbreakableWeapons, unbreakableArmor); + this.am = am; + } + + @Override + public String getConfigName() { + return "My Items"; + } + + @Override + public String getLowercaseName() { + return "myitems"; + } + + @Override + public void grantItems(Player p) { + Arena arena = am.getArenaWithPlayer(p); + if (arena != null) { + try { + arena.getInventoryManager().restoreInv(p); + } catch (Exception e) { + Messenger.severe("Failed to give " + p.getName() + " their own items: " + e.getMessage()); + } + } + } + + @Override + public Location getClassChest() { + return null; + } + } } diff --git a/src/main/java/com/garbagemule/MobArena/ArenaImpl.java b/src/main/java/com/garbagemule/MobArena/ArenaImpl.java index 3beb8f7..add1502 100644 --- a/src/main/java/com/garbagemule/MobArena/ArenaImpl.java +++ b/src/main/java/com/garbagemule/MobArena/ArenaImpl.java @@ -956,6 +956,7 @@ public class ArenaImpl implements Arena // And update the inventory as well. try { inventoryManager.storeInv(p); + inventoryManager.clearInventory(p); } catch (Exception e) { e.printStackTrace(); Messenger.severe("Failed to store inventory for player " + p.getName() + "!"); @@ -1017,6 +1018,7 @@ public class ArenaImpl implements Arena inventoryManager.clearInventory(p); try { inventoryManager.restoreInv(p); + inventoryManager.clearCache(p); } catch (Exception e) { e.printStackTrace(); Messenger.severe("Failed to restore inventory for player " + p.getName() + "!"); diff --git a/src/main/java/com/garbagemule/MobArena/ArenaMasterImpl.java b/src/main/java/com/garbagemule/MobArena/ArenaMasterImpl.java index fbb7f85..698c99a 100644 --- a/src/main/java/com/garbagemule/MobArena/ArenaMasterImpl.java +++ b/src/main/java/com/garbagemule/MobArena/ArenaMasterImpl.java @@ -274,6 +274,9 @@ public class ArenaMasterImpl implements ArenaMaster for (String className : classNames) { loadClass(className); } + + // Add a class for "my items" + loadClass("My Items"); } /** @@ -281,10 +284,16 @@ public class ArenaMasterImpl implements ArenaMaster */ private ArenaClass loadClass(String classname) { ConfigurationSection section = config.getConfigurationSection("classes." + classname); - String lowercase = classname.toLowerCase(); + String lowercase = classname.toLowerCase().replace(" ", ""); // If the section doesn't exist, the class doesn't either. if (section == null) { + // We may not have a class entry for My Items, but that's fine + if (classname.equals("My Items")) { + ArenaClass myItems = new ArenaClass.MyItems(0, false, false, this); + classes.put(lowercase, myItems); + return myItems; + } Messenger.severe("Failed to load class '" + classname + "'."); return null; } @@ -307,7 +316,9 @@ public class ArenaMasterImpl implements ArenaMaster } // Create an ArenaClass with the config-file name. - ArenaClass arenaClass = new ArenaClass(classname, price, weps, arms); + ArenaClass arenaClass = classname.equals("My Items") + ? new ArenaClass.MyItems(price, weps, arms, this) + : new ArenaClass(classname, price, weps, arms); // Parse the items-node List items = section.getStringList("items"); diff --git a/src/main/java/com/garbagemule/MobArena/util/inventory/InventoryManager.java b/src/main/java/com/garbagemule/MobArena/util/inventory/InventoryManager.java index 213b4a1..7532a62 100644 --- a/src/main/java/com/garbagemule/MobArena/util/inventory/InventoryManager.java +++ b/src/main/java/com/garbagemule/MobArena/util/inventory/InventoryManager.java @@ -48,21 +48,18 @@ public class InventoryManager config.set("armor", armor); config.save(file); - // And clear the inventory - clearInventory(p); p.updateInventory(); } public void restoreInv(Player p) throws FileNotFoundException, IOException, InvalidConfigurationException { - // Grab the file on disk - File file = new File(dir, p.getName()); - // Try to grab the items from memory first - ItemStack[] items = this.items.remove(p); - ItemStack[] armor = this.armor.remove(p); + ItemStack[] items = this.items.get(p); + ItemStack[] armor = this.armor.get(p); // If we can't restore from memory, restore from file if (items == null || armor == null) { + File file = new File(dir, p.getName()); + YamlConfiguration config = new YamlConfiguration(); config.load(file); @@ -78,9 +75,16 @@ public class InventoryManager // Set the player inventory contents p.getInventory().setContents(items); p.getInventory().setArmorContents(armor); - - // Delete the file - file.delete(); + } + + public void clearCache(Player p) { + items.remove(p); + armor.remove(p); + + File file = new File(dir, p.getName()); + if (file.exists()) { + file.delete(); + } } /**