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.
This commit is contained in:
Andreas Troelsen 2017-02-18 03:27:10 +01:00
parent 51f907218e
commit d2fd8b4fc2
4 changed files with 67 additions and 12 deletions

View File

@ -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;
}
}
}

View File

@ -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() + "!");

View File

@ -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<String> items = section.getStringList("items");

View File

@ -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();
}
}
/**