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:
@@ -3,6 +3,8 @@ package com.garbagemule.MobArena;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import com.garbagemule.MobArena.framework.Arena;
|
||||||
|
import com.garbagemule.MobArena.framework.ArenaMaster;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -340,4 +342,40 @@ public class ArenaClass
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return lowercaseName.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -956,6 +956,7 @@ public class ArenaImpl implements Arena
|
|||||||
// And update the inventory as well.
|
// And update the inventory as well.
|
||||||
try {
|
try {
|
||||||
inventoryManager.storeInv(p);
|
inventoryManager.storeInv(p);
|
||||||
|
inventoryManager.clearInventory(p);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Messenger.severe("Failed to store inventory for player " + p.getName() + "!");
|
Messenger.severe("Failed to store inventory for player " + p.getName() + "!");
|
||||||
@@ -1017,6 +1018,7 @@ public class ArenaImpl implements Arena
|
|||||||
inventoryManager.clearInventory(p);
|
inventoryManager.clearInventory(p);
|
||||||
try {
|
try {
|
||||||
inventoryManager.restoreInv(p);
|
inventoryManager.restoreInv(p);
|
||||||
|
inventoryManager.clearCache(p);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Messenger.severe("Failed to restore inventory for player " + p.getName() + "!");
|
Messenger.severe("Failed to restore inventory for player " + p.getName() + "!");
|
||||||
|
|||||||
@@ -274,6 +274,9 @@ public class ArenaMasterImpl implements ArenaMaster
|
|||||||
for (String className : classNames) {
|
for (String className : classNames) {
|
||||||
loadClass(className);
|
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) {
|
private ArenaClass loadClass(String classname) {
|
||||||
ConfigurationSection section = config.getConfigurationSection("classes." + 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 the section doesn't exist, the class doesn't either.
|
||||||
if (section == null) {
|
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 + "'.");
|
Messenger.severe("Failed to load class '" + classname + "'.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -307,7 +316,9 @@ public class ArenaMasterImpl implements ArenaMaster
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create an ArenaClass with the config-file name.
|
// 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
|
// Parse the items-node
|
||||||
List<String> items = section.getStringList("items");
|
List<String> items = section.getStringList("items");
|
||||||
|
|||||||
@@ -48,21 +48,18 @@ public class InventoryManager
|
|||||||
config.set("armor", armor);
|
config.set("armor", armor);
|
||||||
config.save(file);
|
config.save(file);
|
||||||
|
|
||||||
// And clear the inventory
|
|
||||||
clearInventory(p);
|
|
||||||
p.updateInventory();
|
p.updateInventory();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void restoreInv(Player p) throws FileNotFoundException, IOException, InvalidConfigurationException {
|
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
|
// Try to grab the items from memory first
|
||||||
ItemStack[] items = this.items.remove(p);
|
ItemStack[] items = this.items.get(p);
|
||||||
ItemStack[] armor = this.armor.remove(p);
|
ItemStack[] armor = this.armor.get(p);
|
||||||
|
|
||||||
// If we can't restore from memory, restore from file
|
// If we can't restore from memory, restore from file
|
||||||
if (items == null || armor == null) {
|
if (items == null || armor == null) {
|
||||||
|
File file = new File(dir, p.getName());
|
||||||
|
|
||||||
YamlConfiguration config = new YamlConfiguration();
|
YamlConfiguration config = new YamlConfiguration();
|
||||||
config.load(file);
|
config.load(file);
|
||||||
|
|
||||||
@@ -78,9 +75,16 @@ public class InventoryManager
|
|||||||
// Set the player inventory contents
|
// Set the player inventory contents
|
||||||
p.getInventory().setContents(items);
|
p.getInventory().setContents(items);
|
||||||
p.getInventory().setArmorContents(armor);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user