Merge pull request #587 from cmastudios/fix-colored-armor

Fix loadout issues + permission-restricted loadouts, thanks @cmastudios!!!
This commit is contained in:
taoneill 2013-05-04 10:02:49 -07:00
commit 856d697d79
13 changed files with 558 additions and 78 deletions

View File

@ -50,6 +50,7 @@ import com.tommytony.war.structure.HubLobbyMaterials;
import com.tommytony.war.structure.Monument;
import com.tommytony.war.structure.WarHub;
import com.tommytony.war.structure.ZoneLobby;
import com.tommytony.war.utility.Loadout;
import com.tommytony.war.utility.PlayerState;
import com.tommytony.war.utility.SizeCounter;
import com.tommytony.war.utility.WarLogFormatter;
@ -186,7 +187,7 @@ public class War extends JavaPlugin {
teamDefaultConfig.put(TeamConfig.TEAMSIZE, 10);
teamDefaultConfig.put(TeamConfig.PERMISSION, "war.player");
this.getDefaultInventories().getLoadouts().clear();
this.getDefaultInventories().clearLoadouts();
HashMap<Integer, ItemStack> defaultLoadout = new HashMap<Integer, ItemStack>();
ItemStack stoneSword = new ItemStack(Material.STONE_SWORD, 1, (byte) 8);
@ -339,6 +340,7 @@ public class War extends JavaPlugin {
public ItemStack copyStack(ItemStack originalStack) {
ItemStack copiedStack = new ItemStack(originalStack.getType(), originalStack.getAmount(), originalStack.getDurability(), new Byte(originalStack.getData().getData()));
copiedStack.setDurability(originalStack.getDurability());
copiedStack.setItemMeta(originalStack.getItemMeta());
copyEnchantments(originalStack, copiedStack);
return copiedStack;
@ -377,11 +379,15 @@ public class War extends JavaPlugin {
public String updateTeamFromNamedParams(Team team, CommandSender commandSender, String[] arguments) {
try {
Map<String, String> namedParams = new HashMap<String, String>();
Map<String, String> namedParams = new HashMap();
Map<String, String> thirdParameter = new HashMap();
for (String namedPair : arguments) {
String[] pairSplit = namedPair.split(":");
if (pairSplit.length == 2) {
namedParams.put(pairSplit[0].toLowerCase(), pairSplit[1]);
} else if (pairSplit.length == 3) {
namedParams.put(pairSplit[0].toLowerCase(), pairSplit[1]);
thirdParameter.put(pairSplit[0].toLowerCase(), pairSplit[2]);
}
}
@ -392,15 +398,15 @@ public class War extends JavaPlugin {
Player player = (Player) commandSender;
if (namedParams.containsKey("loadout")) {
String loadoutName = namedParams.get("loadout");
HashMap<Integer, ItemStack> loadout = team.getInventories().getLoadouts().get(loadoutName);
HashMap<Integer, ItemStack> loadout = team.getInventories().getLoadout(loadoutName);
if (loadout == null) {
// Check if any loadouts exist, if not gotta use the default inventories then add the newly created one
if(team.getInventories().getLoadouts().isEmpty()) {
if(!team.getInventories().hasLoadouts()) {
Warzone warzone = Warzone.getZoneByTeam(team);
for (String key : warzone.getDefaultInventories().resolveLoadouts().keySet()) {
HashMap<Integer, ItemStack> transferredLoadout = warzone.getDefaultInventories().resolveLoadouts().get(key);
if (transferredLoadout != null) {
team.getInventories().getLoadouts().put(key, transferredLoadout);
team.getInventories().setLoadout(key, transferredLoadout);
} else {
War.war.log("Failed to transfer loadout " + key + " down to team " + team.getName() + " in warzone " + warzone.getName(), Level.WARNING);
}
@ -408,16 +414,25 @@ public class War extends JavaPlugin {
}
loadout = new HashMap<Integer, ItemStack>();
team.getInventories().getLoadouts().put(loadoutName, loadout);
team.getInventories().setLoadout(loadoutName, loadout);
returnMessage.append(loadoutName + " respawn loadout added.");
} else {
returnMessage.append(loadoutName + " respawn loadout updated.");
}
this.inventoryToLoadout(player, loadout);
Loadout ldt = team.getInventories().getNewLoadout(loadoutName);
if (thirdParameter.containsKey("loadout")) {
String permission = thirdParameter.get("loadout");
ldt.setPermission(permission);
returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission set to ").append(permission).append('.');
} else if (ldt.requiresPermission()) {
ldt.setPermission(null);
returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission deleted.");
}
}
if (namedParams.containsKey("deleteloadout")) {
String loadoutName = namedParams.get("deleteloadout");
if (team.getInventories().getLoadouts().keySet().contains(loadoutName)) {
if (team.getInventories().containsLoadout(loadoutName)) {
team.getInventories().removeLoadout(loadoutName);
returnMessage.append(" " + loadoutName + " loadout removed.");
} else {
@ -440,11 +455,15 @@ public class War extends JavaPlugin {
public String updateZoneFromNamedParams(Warzone warzone, CommandSender commandSender, String[] arguments) {
try {
Map<String, String> namedParams = new HashMap<String, String>();
Map<String, String> namedParams = new HashMap();
Map<String, String> thirdParameter = new HashMap();
for (String namedPair : arguments) {
String[] pairSplit = namedPair.split(":");
if (pairSplit.length == 2) {
namedParams.put(pairSplit[0].toLowerCase(), pairSplit[1]);
} else if (pairSplit.length == 3) {
namedParams.put(pairSplit[0].toLowerCase(), pairSplit[1]);
thirdParameter.put(pairSplit[0].toLowerCase(), pairSplit[2]);
}
}
@ -473,32 +492,41 @@ public class War extends JavaPlugin {
Player player = (Player) commandSender;
if (namedParams.containsKey("loadout")) {
String loadoutName = namedParams.get("loadout");
HashMap<Integer, ItemStack> loadout = warzone.getDefaultInventories().getLoadouts().get(loadoutName);
HashMap<Integer, ItemStack> loadout = warzone.getDefaultInventories().getLoadout(loadoutName);
if (loadout == null) {
loadout = new HashMap<Integer, ItemStack>();
// Check if any loadouts exist, if not gotta use the default inventories then add the newly created one
if(warzone.getDefaultInventories().getLoadouts().isEmpty()) {
if(!warzone.getDefaultInventories().hasLoadouts()) {
for (String key : warzone.getDefaultInventories().resolveLoadouts().keySet()) {
HashMap<Integer, ItemStack> transferredLoadout = warzone.getDefaultInventories().resolveLoadouts().get(key);
if (transferredLoadout != null) {
warzone.getDefaultInventories().getLoadouts().put(key, transferredLoadout);
warzone.getDefaultInventories().setLoadout(key, transferredLoadout);
} else {
War.war.log("Failed to transfer loadout " + key + " down to warzone " + warzone.getName(), Level.WARNING);
}
}
}
warzone.getDefaultInventories().getLoadouts().put(loadoutName, loadout);
warzone.getDefaultInventories().setLoadout(loadoutName, loadout);
returnMessage.append(loadoutName + " respawn loadout added.");
} else {
returnMessage.append(loadoutName + " respawn loadout updated.");
}
this.inventoryToLoadout(player, loadout);
Loadout ldt = warzone.getDefaultInventories().getNewLoadout(loadoutName);
if (thirdParameter.containsKey("loadout")) {
String permission = thirdParameter.get("loadout");
ldt.setPermission(permission);
returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission set to ").append(permission).append('.');
} else if (ldt.requiresPermission()) {
ldt.setPermission(null);
returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission deleted.");
}
}
if (namedParams.containsKey("deleteloadout")) {
String loadoutName = namedParams.get("deleteloadout");
if (warzone.getDefaultInventories().getLoadouts().keySet().contains(loadoutName)) {
if (warzone.getDefaultInventories().containsLoadout(loadoutName)) {
warzone.getDefaultInventories().removeLoadout(loadoutName);
returnMessage.append(" " + loadoutName + " loadout removed.");
} else {
@ -619,11 +647,15 @@ public class War extends JavaPlugin {
public String updateFromNamedParams(CommandSender commandSender, String[] arguments) {
try {
Map<String, String> namedParams = new HashMap<String, String>();
Map<String, String> namedParams = new HashMap();
Map<String, String> thirdParameter = new HashMap();
for (String namedPair : arguments) {
String[] pairSplit = namedPair.split(":");
if (pairSplit.length == 2) {
namedParams.put(pairSplit[0].toLowerCase(), pairSplit[1]);
} else if (pairSplit.length == 3) {
namedParams.put(pairSplit[0].toLowerCase(), pairSplit[1]);
thirdParameter.put(pairSplit[0].toLowerCase(), pairSplit[2]);
}
}
@ -637,7 +669,7 @@ public class War extends JavaPlugin {
Player player = (Player) commandSender;
if (namedParams.containsKey("loadout")) {
String loadoutName = namedParams.get("loadout");
HashMap<Integer, ItemStack> loadout = this.getDefaultInventories().getLoadouts().get(loadoutName);
HashMap<Integer, ItemStack> loadout = this.getDefaultInventories().getLoadout(loadoutName);
if (loadout == null) {
loadout = new HashMap<Integer, ItemStack>();
this.getDefaultInventories().addLoadout(loadoutName, loadout);
@ -646,11 +678,20 @@ public class War extends JavaPlugin {
returnMessage.append(loadoutName + " respawn loadout updated.");
}
this.inventoryToLoadout(player, loadout);
Loadout ldt = this.getDefaultInventories().getNewLoadout(loadoutName);
if (thirdParameter.containsKey("loadout")) {
String permission = thirdParameter.get("loadout");
ldt.setPermission(permission);
returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission set to ").append(permission).append('.');
} else if (ldt.requiresPermission()) {
ldt.setPermission(null);
returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission deleted.");
}
}
if (namedParams.containsKey("deleteloadout")) {
String loadoutName = namedParams.get("deleteloadout");
if (this.getDefaultInventories().getLoadouts().keySet().contains(loadoutName)) {
if (this.getDefaultInventories().getLoadouts().keySet().size() > 1) {
if (this.getDefaultInventories().containsLoadout(loadoutName)) {
if (this.getDefaultInventories().getNewLoadouts().size() > 1) {
this.getDefaultInventories().removeLoadout(loadoutName);
returnMessage.append(" " + loadoutName + " loadout removed.");
} else {
@ -742,23 +783,27 @@ public class War extends JavaPlugin {
}
private String getLoadoutsString(InventoryBag invs) {
String loadoutsString = "";
StringBuilder loadoutsString = new StringBuilder();
ChatColor loadoutColor = ChatColor.GREEN;
ChatColor normalColor = ChatColor.WHITE;
if (invs.hasLoadouts()) {
String loadouts = "";
for (String loadoutName : invs.getLoadouts().keySet()) {
loadouts += loadoutName + ",";
StringBuilder loadouts = new StringBuilder();
for (Loadout ldt : invs.getNewLoadouts()) {
if (ldt.requiresPermission()) {
loadouts.append(ldt.getName()).append(":").append(ldt.getPermission()).append(",");
} else {
loadouts.append(ldt.getName()).append(",");
}
}
loadoutsString += " loadout:" + loadoutColor + loadouts + normalColor;
loadoutsString.append(" loadout:").append(loadoutColor).append(loadouts.toString()).append(normalColor);
}
if (invs.hasReward()) {
loadoutsString += " reward:" + loadoutColor + "default" + normalColor;
loadoutsString.append(" reward:").append(loadoutColor).append("default").append(normalColor);
}
return loadoutsString;
return loadoutsString.toString();
}
public String printConfig(Warzone zone) {

View File

@ -1,5 +1,6 @@
package com.tommytony.war;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@ -41,12 +42,14 @@ import com.tommytony.war.structure.WarzoneMaterials;
import com.tommytony.war.structure.ZoneLobby;
import com.tommytony.war.structure.ZoneWallGuard;
import com.tommytony.war.utility.Direction;
import com.tommytony.war.utility.Loadout;
import com.tommytony.war.utility.LoadoutSelection;
import com.tommytony.war.utility.PlayerState;
import com.tommytony.war.utility.PotionEffectHelper;
import com.tommytony.war.volume.BlockInfo;
import com.tommytony.war.volume.Volume;
import com.tommytony.war.volume.ZoneVolume;
import org.bukkit.inventory.meta.LeatherArmorMeta;
/**
*
@ -386,6 +389,9 @@ public class Warzone {
if (!this.getLoadoutSelections().keySet().contains(player.getName())) {
isFirstRespawn = true;
this.getLoadoutSelections().put(player.getName(), new LoadoutSelection(true, 0));
} else if (this.isReinitializing) {
isFirstRespawn = true;
this.getLoadoutSelections().get(player.getName()).setStillInSpawn(true);
} else {
this.getLoadoutSelections().get(player.getName()).setStillInSpawn(true);
}
@ -442,15 +448,11 @@ public class Warzone {
playerInv.setHelmet(new ItemStack(team.getKind().getMaterial(), 1, (short) 1, new Byte(team.getKind().getData())));
} else {
if (!helmetIsInLoadout) {
if (team.getKind() == TeamKind.GOLD) {
playerInv.setHelmet(new ItemStack(Material.GOLD_HELMET));
} else if (team.getKind() == TeamKind.DIAMOND) {
playerInv.setHelmet(new ItemStack(Material.DIAMOND_HELMET));
} else if (team.getKind() == TeamKind.IRON) {
playerInv.setHelmet(new ItemStack(Material.IRON_HELMET));
} else {
playerInv.setHelmet(new ItemStack(Material.LEATHER_HELMET));
}
ItemStack helmet = new ItemStack(Material.LEATHER_HELMET);
LeatherArmorMeta meta = (LeatherArmorMeta) helmet.getItemMeta();
meta.setColor(team.getKind().getBukkitColor());
helmet.setItemMeta(meta);
playerInv.setHelmet(helmet);
}
}
}
@ -1331,30 +1333,48 @@ public class Warzone {
if (selection != null && !this.isRespawning(player) && playerTeam.getPlayers().contains(player)) {
// Make sure that inventory resets dont occur if player has already tp'ed out (due to game end, or somesuch)
// - repawn timer + this method is why inventories were getting wiped as players exited the warzone.
HashMap<String, HashMap<Integer, ItemStack>> loadouts = playerTeam.getInventories().resolveLoadouts();
List<String> sortedNames = LoadoutYmlMapper.sortNames(loadouts);
List<Loadout> loadouts = playerTeam.getInventories().resolveNewLoadouts();
List<String> sortedNames = LoadoutYmlMapper.sortNames(Loadout.toLegacyFormat(loadouts));
sortedNames.remove("first");
for (Iterator<String> it = sortedNames.iterator(); it.hasNext();) {
String loadoutName = it.next();
Loadout ldt = Loadout.getLoadout(loadouts, loadoutName);
if (ldt.requiresPermission() && !player.hasPermission(ldt.getPermission())) {
it.remove();
}
}
if (sortedNames.isEmpty()) {
// Fix for zones that mistakenly only specify a `first' loadout, but do not add any others.
this.handlePlayerLeave(player, this.getTeleport(), true);
War.war.badMsg(player, "We couldn't find a loadout for you! Please alert the warzone maker to add a `default' loadout to this warzone.");
return;
}
int currentIndex = selection.getSelectedIndex();
Loadout firstLoadout = Loadout.getLoadout(loadouts, "first");
int i = 0;
Iterator<String> it = sortedNames.iterator();
while (it.hasNext()) {
String name = (String)it.next();
if (i == currentIndex) {
if (playerTeam.getTeamConfig().resolveBoolean(TeamConfig.PLAYERLOADOUTASDEFAULT) && name.equals("default")) {
// Use player's own inventory as loadout
this.resetInventory(playerTeam, player, this.getPlayerInventoryFromSavedState(player));
} else {
// Use the loadout from the list in the settings
this.resetInventory(playerTeam, player, loadouts.get(name));
}
while (it.hasNext()) {
String name = (String) it.next();
if (i == currentIndex) {
if (playerTeam.getTeamConfig().resolveBoolean(TeamConfig.PLAYERLOADOUTASDEFAULT) && name.equals("default")) {
// Use player's own inventory as loadout
this.resetInventory(playerTeam, player, this.getPlayerInventoryFromSavedState(player));
} else if (isFirstRespawn && firstLoadout != null && name.equals("default")
&& (firstLoadout.requiresPermission() ? player.hasPermission(firstLoadout.getPermission()) : true)) {
// Get the loadout for the first spawn
this.resetInventory(playerTeam, player, Loadout.getLoadout(loadouts, "first").getContents());
} else {
// Use the loadout from the list in the settings
this.resetInventory(playerTeam, player, Loadout.getLoadout(loadouts, name).getContents());
}
if (isFirstRespawn && playerTeam.getInventories().resolveLoadouts().keySet().size() > 1) {
War.war.msg(player, "Equipped " + name + " loadout (sneak to switch).");
} else if (isToggle) {
War.war.msg(player, "Equipped " + name + " loadout.");
}
}
i++;
}
}
i++;
}
}
}

View File

@ -7,10 +7,13 @@ import org.bukkit.inventory.ItemStack;
import com.tommytony.war.War;
import com.tommytony.war.Warzone;
import com.tommytony.war.utility.Loadout;
import java.util.ArrayList;
import java.util.List;
public class InventoryBag {
private HashMap<String, HashMap<Integer, ItemStack>> loadouts = new HashMap<String, HashMap<Integer,ItemStack>>();
private List<Loadout> loadouts = new ArrayList();
private HashMap<Integer, ItemStack> reward = null;
private Warzone warzone;
@ -24,11 +27,23 @@ public class InventoryBag {
}
public void addLoadout(String name, HashMap<Integer, ItemStack> loadout) {
this.loadouts.put(name, loadout);
this.loadouts.add(new Loadout(name, loadout, null));
}
public void addLoadout(Loadout loadout) {
this.loadouts.add(loadout);
}
public void removeLoadout(String name) {
this.loadouts.remove(name);
for (Loadout ldt : loadouts) {
if (ldt.getName().equals(name)) {
this.loadouts.remove(ldt);
}
}
}
public void removeLoadout(Loadout ldt) {
this.loadouts.remove(ldt);
}
public boolean hasLoadouts() {
@ -36,12 +51,20 @@ public class InventoryBag {
}
public HashMap<String, HashMap<Integer, ItemStack>> getLoadouts() {
return this.loadouts;
return Loadout.toLegacyFormat(loadouts);
}
public List<Loadout> getNewLoadouts() {
return loadouts;
}
public void setLoadouts(List<Loadout> loadouts) {
this.loadouts = loadouts;
}
public HashMap<String, HashMap<Integer, ItemStack>> resolveLoadouts() {
if (this.hasLoadouts()) {
return loadouts;
return this.getLoadouts();
} else if (warzone != null && warzone.getDefaultInventories().hasLoadouts()) {
return warzone.getDefaultInventories().resolveLoadouts();
} else if (War.war.getDefaultInventories().hasLoadouts()) {
@ -50,6 +73,18 @@ public class InventoryBag {
return new HashMap<String, HashMap<Integer, ItemStack>>();
}
}
public List<Loadout> resolveNewLoadouts() {
if (this.hasLoadouts()) {
return this.getNewLoadouts();
} else if (warzone != null && warzone.getDefaultInventories().hasLoadouts()) {
return warzone.getDefaultInventories().resolveNewLoadouts();
} else if (War.war.getDefaultInventories().hasLoadouts()) {
return War.war.getDefaultInventories().resolveNewLoadouts();
} else {
return new ArrayList();
}
}
public void setReward(HashMap<Integer, ItemStack> reward) {
this.reward = reward;
@ -76,4 +111,36 @@ public class InventoryBag {
public void clearLoadouts() {
this.loadouts.clear();
}
public HashMap<Integer, ItemStack> getLoadout(String loadoutName) {
for (Loadout ldt : loadouts) {
if (ldt.getName().equals(loadoutName)) {
return ldt.getContents();
}
}
return null;
}
public Loadout getNewLoadout(String loadoutName) {
for (Loadout ldt : loadouts) {
if (ldt.getName().equals(loadoutName)) {
return ldt;
}
}
return null;
}
public void setLoadout(String name, HashMap<Integer, ItemStack> contents) {
for (Loadout ldt : loadouts) {
if (ldt.getName().equals(name)) {
ldt.setContents(contents);
return;
}
}
loadouts.add(new Loadout(name, contents, null));
}
public boolean containsLoadout(String name) {
return this.getNewLoadout(name) != null;
}
}

View File

@ -94,6 +94,49 @@ public enum TeamKind {
return new Color(255,255,255);
}
}
/**
* Gets the color for Bukkit
* @return the color
*/
public org.bukkit.Color getBukkitColor() {
int colorCode = (int)this.data;
switch (colorCode) {
case 0:
return org.bukkit.Color.fromRGB(255,255,255);
case 1:
return org.bukkit.Color.fromRGB(255,128,0);
case 2:
return org.bukkit.Color.fromRGB(255,128,255);
case 3:
return org.bukkit.Color.fromRGB(0,0,255);
case 4:
return org.bukkit.Color.fromRGB(255,215,0);
case 5:
return org.bukkit.Color.fromRGB(0,255,0);
case 6:
return org.bukkit.Color.fromRGB(255,128,255);
case 7:
return org.bukkit.Color.fromRGB(100,100,100);
case 8:
return org.bukkit.Color.fromRGB(200,200,200);
case 9:
return org.bukkit.Color.fromRGB(128,255,255);
case 10:
return org.bukkit.Color.fromRGB(128,0,255);
case 11:
return org.bukkit.Color.fromRGB(0,0,128);
case 12:
return org.bukkit.Color.fromRGB(128,0,0);
case 13:
return org.bukkit.Color.fromRGB(0,128,0);
case 14:
return org.bukkit.Color.fromRGB(255,0,0);
case 15:
return org.bukkit.Color.fromRGB(0,0,0);
default:
return org.bukkit.Color.fromRGB(255,255,255);
}
}
public Material getMaterial() {
return this.material;

View File

@ -41,7 +41,10 @@ import com.tommytony.war.structure.Cake;
import com.tommytony.war.structure.WarHub;
import com.tommytony.war.structure.ZoneLobby;
import com.tommytony.war.utility.Direction;
import com.tommytony.war.utility.Loadout;
import com.tommytony.war.utility.LoadoutSelection;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @author tommytony, Tim Düsterhus
@ -231,6 +234,10 @@ public class WarPlayerListener implements Listener {
War.war.badMsg(player, "Can't use items while still in spawn.");
}
}
if (zone != null && event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.ENDER_CHEST) {
event.setCancelled(true);
War.war.badMsg(player, "Can't use ender chests while playing in a warzone!");
}
}
}
@ -823,7 +830,19 @@ public class WarPlayerListener implements Listener {
if (playerWarzone.getLoadoutSelections().keySet().contains(event.getPlayer().getName())
&& playerWarzone.getLoadoutSelections().get(event.getPlayer().getName()).isStillInSpawn()) {
LoadoutSelection selection = playerWarzone.getLoadoutSelections().get(event.getPlayer().getName());
int currentIndex = (selection.getSelectedIndex() + 1) % (playerTeam.getInventories().resolveLoadouts().keySet().size());
List<Loadout> loadouts = (List<Loadout>)new ArrayList(playerTeam.getInventories().resolveNewLoadouts()).clone();
for (Iterator<Loadout> it = loadouts.iterator(); it.hasNext();) {
Loadout ldt = it.next();
if ("first".equals(ldt.getName())) {
it.remove();
continue;
}
if (ldt.requiresPermission() && !event.getPlayer().hasPermission(ldt.getPermission())) {
it.remove();
continue;
}
}
int currentIndex = (selection.getSelectedIndex() + 1) % loadouts.size();
selection.setSelectedIndex(currentIndex);
playerWarzone.equipPlayerLoadoutSelection(event.getPlayer(), playerTeam, false, true);

View File

@ -10,24 +10,50 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import com.tommytony.war.War;
import com.tommytony.war.utility.Loadout;
import java.util.Collections;
import org.bukkit.Color;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
public class LoadoutYmlMapper {
public static void fromConfigToLoadouts(ConfigurationSection config, HashMap<String, HashMap<Integer, ItemStack>> loadouts) {
/**
* Deserializes loadouts from the configuration.
* Backwards compatibility: returns new-style loadouts and still modifies
* the loadouts parameter.
* @param config A configuration section that contains loadouts
* @param loadouts Map of the loadout names and the items. This will be
* cleared and written to by the method, cannot be final.
* @return list of new style loadouts
*/
public static List<Loadout> fromConfigToLoadouts(ConfigurationSection config, HashMap<String, HashMap<Integer, ItemStack>> loadouts) {
List<String> loadoutNames = config.getStringList("names");
loadouts.clear();
List<Loadout> ldts = new ArrayList();
for (String name : loadoutNames) {
HashMap<Integer, ItemStack> newLoadout = new HashMap<Integer, ItemStack>();
fromConfigToLoadout(config, newLoadout, name);
Loadout ldt = fromConfigToLoadout(config, newLoadout, name);
ldts.add(ldt);
loadouts.put(name, newLoadout);
}
Collections.sort(ldts);
return ldts;
}
public static void fromConfigToLoadout(ConfigurationSection config, HashMap<Integer, ItemStack> loadout, String loadoutName) {
/**
* Deserialize a loadout from the configuration.
* Backwards compatibility: returns new-style loadout and still modifies the
* loadout parameter.
* @param config A configuration section that contains loadouts
* @param loadout Map of slots and items in the loadout. Will be written to
* by the method, cannot be final.
* @param loadoutName The name of the loadout
* @return new style loadout
*/
public static Loadout fromConfigToLoadout(ConfigurationSection config, HashMap<Integer, ItemStack> loadout, String loadoutName) {
List<Integer> slots = config.getIntegerList(loadoutName + ".slots");
for (Integer slot : slots) {
String prefix = loadoutName + "." + slot + ".";
int id = config.getInt(prefix + "id");
byte data = (byte)config.getInt(prefix + "data");
int amount = config.getInt(prefix + "amount");
@ -47,9 +73,29 @@ public class LoadoutYmlMapper {
}
}
}
if (config.contains(prefix + "armorcolor")) {
int rgb = config.getInt(prefix + "armorcolor");
Color clr = Color.fromRGB(rgb);
LeatherArmorMeta meta = (LeatherArmorMeta) stack.getItemMeta();
meta.setColor(clr);
stack.setItemMeta(meta);
}
if (config.contains(prefix + "name")) {
String itemName = config.getString(prefix + "name");
ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(itemName);
stack.setItemMeta(meta);
}
if (config.contains(prefix + "lore")) {
List<String> itemLore = config.getStringList(prefix + "lore");
ItemMeta meta = stack.getItemMeta();
meta.setLore(itemLore);
stack.setItemMeta(meta);
}
loadout.put(slot, stack);
}
String permission = config.getString(loadoutName + ".permission", "");
return new Loadout(loadoutName, loadout, permission);
}
public static void fromLoadoutsToConfig(HashMap<String, HashMap<Integer, ItemStack>> loadouts, ConfigurationSection section) {
@ -60,6 +106,21 @@ public class LoadoutYmlMapper {
fromLoadoutToConfig(name, loadouts.get(name), section);
}
}
/**
* Serializes a list of new style loadouts to the configuration.
* @param loadouts List of new style loadouts
* @param section Section of the configuration to write to
*/
public static void fromLoadoutsToConfig(List<Loadout> loadouts, ConfigurationSection section) {
Collections.sort(loadouts);
List<String> names = new ArrayList();
for (Loadout ldt : loadouts) {
names.add(ldt.getName());
LoadoutYmlMapper.fromLoadoutToConfig(ldt, section);
}
section.set("names", names);
}
public static List<String> sortNames(HashMap<String, HashMap<Integer, ItemStack>> loadouts) {
List<String> sortedNames = new ArrayList<String>();
@ -86,6 +147,17 @@ public class LoadoutYmlMapper {
return list;
}
/**
* Serialize a new style loadout to the configuration
* @param loadout New style loadout
* @param section Section of the configuration to write to
*/
public static void fromLoadoutToConfig(Loadout loadout, ConfigurationSection section) {
LoadoutYmlMapper.fromLoadoutToConfig(loadout.getName(), loadout.getContents(), section);
if (loadout.requiresPermission()) {
section.set(loadout.getName() + ".permission", loadout.getPermission());
}
}
public static void fromLoadoutToConfig(String loadoutName, HashMap<Integer, ItemStack> loadout, ConfigurationSection section) {
ConfigurationSection loadoutSection = section.createSection(loadoutName);
@ -108,6 +180,20 @@ public class LoadoutYmlMapper {
}
slotSection.set("enchantments", enchantmentStringList);
}
if (stack.hasItemMeta() && stack.getItemMeta() instanceof LeatherArmorMeta
&& ((LeatherArmorMeta)stack.getItemMeta()).getColor() != null) {
LeatherArmorMeta meta = (LeatherArmorMeta)stack.getItemMeta();
int rgb = meta.getColor().asRGB();
slotSection.set("armorcolor", rgb);
}
if (stack.hasItemMeta() && stack.getItemMeta().hasDisplayName()) {
ItemMeta meta = stack.getItemMeta();
slotSection.set("name", meta.getDisplayName());
}
if (stack.hasItemMeta() && stack.getItemMeta().hasLore()) {
ItemMeta meta = stack.getItemMeta();
slotSection.set("lore", meta.getLore());
}
}
}
}

View File

@ -80,12 +80,12 @@ public class WarTxtMapper {
}
// defaultLoadout
War.war.getDefaultInventories().getLoadouts().clear();
War.war.getDefaultInventories().clearLoadouts();
String loadoutStr = warConfig.getString("defaultLoadout");
if (loadoutStr != null && !loadoutStr.equals("")) {
War.war.getDefaultInventories().addLoadout("default", new HashMap<Integer, ItemStack>());
LoadoutTxtMapper.fromStringToLoadout(loadoutStr, War.war.getDefaultInventories().getLoadouts().get("default"));
LoadoutTxtMapper.fromStringToLoadout(loadoutStr, War.war.getDefaultInventories().getLoadout("default"));
}
// defaultExtraLoadouts
@ -101,7 +101,7 @@ public class WarTxtMapper {
for (String extraName : extraLoadoutsSplit) {
if (extraName != null && !extraName.equals("")) {
String loadoutString = warConfig.getString(extraName + "Loadout");
HashMap<Integer, ItemStack> loadout = War.war.getDefaultInventories().getLoadouts().get(extraName);
HashMap<Integer, ItemStack> loadout = War.war.getDefaultInventories().getLoadout(extraName);
LoadoutTxtMapper.fromStringToLoadout(loadoutString, loadout);
}
}

View File

@ -70,7 +70,7 @@ public class WarYmlMapper {
// defaultLoadouts
ConfigurationSection loadoutsSection = warRootSection.getConfigurationSection("team.default.loadout");
LoadoutYmlMapper.fromConfigToLoadouts(loadoutsSection, War.war.getDefaultInventories().getLoadouts());
War.war.getDefaultInventories().setLoadouts(LoadoutYmlMapper.fromConfigToLoadouts(loadoutsSection, new HashMap()));
// defaultReward
ConfigurationSection rewardsSection = warRootSection.getConfigurationSection("team.default.reward");
@ -121,7 +121,7 @@ public class WarYmlMapper {
// defaultLoadouts
ConfigurationSection loadoutSection = teamDefault.createSection("loadout");
LoadoutYmlMapper.fromLoadoutsToConfig(War.war.getDefaultInventories().getLoadouts(), loadoutSection);
LoadoutYmlMapper.fromLoadoutsToConfig(War.war.getDefaultInventories().getNewLoadouts(), loadoutSection);
// defaultReward
ConfigurationSection rewardsSection = teamDefault.createSection("reward");

View File

@ -86,12 +86,12 @@ public class WarzoneTxtMapper {
}
// loadout
warzone.getDefaultInventories().getLoadouts().clear();
warzone.getDefaultInventories().clearLoadouts();
String loadoutStr = warzoneConfig.getString("loadout");
if (loadoutStr != null && !loadoutStr.equals("")) {
warzone.getDefaultInventories().getLoadouts().put("default", new HashMap<Integer, ItemStack>());
LoadoutTxtMapper.fromStringToLoadout(loadoutStr, warzone.getDefaultInventories().getLoadouts().get("default"));
warzone.getDefaultInventories().setLoadout("default", new HashMap<Integer, ItemStack>());
LoadoutTxtMapper.fromStringToLoadout(loadoutStr, warzone.getDefaultInventories().getLoadout("default"));
}
// extraLoadouts
@ -100,14 +100,14 @@ public class WarzoneTxtMapper {
for (String nameStr : extraLoadoutsSplit) {
if (nameStr != null && !nameStr.equals("")) {
warzone.getDefaultInventories().getLoadouts().put(nameStr, new HashMap<Integer, ItemStack>());
warzone.getDefaultInventories().setLoadout(nameStr, new HashMap<Integer, ItemStack>());
}
}
for (String extraName : extraLoadoutsSplit) {
if (extraName != null && !extraName.equals("")) {
String loadoutString = warzoneConfig.getString(extraName + "Loadout");
HashMap<Integer, ItemStack> loadout = warzone.getDefaultInventories().getLoadouts().get(extraName);
HashMap<Integer, ItemStack> loadout = warzone.getDefaultInventories().getLoadout(extraName);
LoadoutTxtMapper.fromStringToLoadout(loadoutString, loadout);
}
}

View File

@ -27,6 +27,7 @@ import com.tommytony.war.structure.Monument;
import com.tommytony.war.structure.WarzoneMaterials;
import com.tommytony.war.structure.ZoneLobby;
import com.tommytony.war.utility.Direction;
import com.tommytony.war.utility.Loadout;
import com.tommytony.war.volume.Volume;
import com.tommytony.war.volume.ZoneVolume;
@ -79,7 +80,7 @@ public class WarzoneYmlMapper {
// defaultLoadouts
if (warzoneRootSection.contains("team.default.loadout")) {
ConfigurationSection loadoutsSection = warzoneRootSection.getConfigurationSection("team.default.loadout");
LoadoutYmlMapper.fromConfigToLoadouts(loadoutsSection, warzone.getDefaultInventories().getLoadouts());
warzone.getDefaultInventories().setLoadouts(LoadoutYmlMapper.fromConfigToLoadouts(loadoutsSection, new HashMap()));
}
// defaultReward
@ -231,11 +232,11 @@ public class WarzoneYmlMapper {
if (warzoneRootSection.contains(teamLoadoutPrefix)) {
// team specific loadouts
ConfigurationSection loadoutsSection = warzoneRootSection.getConfigurationSection(teamLoadoutPrefix);
LoadoutYmlMapper.fromConfigToLoadouts(loadoutsSection, team.getInventories().getLoadouts());
team.getInventories().setLoadouts(LoadoutYmlMapper.fromConfigToLoadouts(loadoutsSection, new HashMap()));
} else if (warzoneRootSection.contains(teamLoadoutPrefix.toLowerCase())) {
// try lowercase instead
ConfigurationSection loadoutsSection = warzoneRootSection.getConfigurationSection(teamLoadoutPrefix.toLowerCase());
LoadoutYmlMapper.fromConfigToLoadouts(loadoutsSection, team.getInventories().getLoadouts());
team.getInventories().setLoadouts(LoadoutYmlMapper.fromConfigToLoadouts(loadoutsSection, new HashMap()));
}
String teamRewardPrefix = "team." + teamName + ".reward";
@ -539,7 +540,7 @@ public class WarzoneYmlMapper {
// defaultLoadouts
if (warzone.getDefaultInventories().hasLoadouts()) {
ConfigurationSection loadoutsSection = teamsSection.createSection("default.loadout");
LoadoutYmlMapper.fromLoadoutsToConfig(warzone.getDefaultInventories().getLoadouts(), loadoutsSection);
LoadoutYmlMapper.fromLoadoutsToConfig(warzone.getDefaultInventories().getNewLoadouts(), loadoutsSection);
}
// defaultReward
@ -558,7 +559,7 @@ public class WarzoneYmlMapper {
if (team.getInventories().hasLoadouts()) {
// team specific loadouts
ConfigurationSection loadoutsSection = teamsSection.createSection(team.getName() + ".loadout");
LoadoutYmlMapper.fromLoadoutsToConfig(team.getInventories().getLoadouts(), loadoutsSection);
LoadoutYmlMapper.fromLoadoutsToConfig(team.getInventories().getNewLoadouts(), loadoutsSection);
}
if (team.getInventories().hasReward()) {

View File

@ -0,0 +1,198 @@
package com.tommytony.war.utility;
import com.tommytony.war.War;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Color;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.material.MaterialData;
/**
* Represents a loadout of items
*
* @author cmastudios
*/
public class Loadout implements Comparable, ConfigurationSerializable {
private String name;
private HashMap<Integer, ItemStack> contents;
private String permission;
public Loadout(String name, HashMap<Integer, ItemStack> contents, String permission) {
this.name = name;
this.contents = contents;
this.permission = permission;
}
static {
ConfigurationSerialization.registerClass(Loadout.class);
}
public int compareTo(Object o) {
if (!(o instanceof Loadout)) {
throw new ClassCastException(this.getClass().getCanonicalName()
+ " is not comparable to a " + o.getClass().getCanonicalName());
}
Loadout ldt = (Loadout) o;
if ("default".equals(ldt.getName()) && !"default".equals(this.getName())) {
return -1;
} else if ("default".equals(this.getName()) && !"default".equals(ldt.getName())) {
return 1;
} else {
return 0;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HashMap<Integer, ItemStack> getContents() {
return contents;
}
public void setContents(HashMap<Integer, ItemStack> contents) {
this.contents = contents;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public boolean requiresPermission() {
return permission != null && !permission.isEmpty();
}
private List<Integer> toIntList(Set<Integer> keySet) {
List<Integer> list = new ArrayList<Integer>();
for (Integer key : keySet) {
list.add(key);
}
return list;
}
public static HashMap<String, HashMap<Integer, ItemStack>> toLegacyFormat(List<Loadout> loadouts) {
HashMap<String, HashMap<Integer, ItemStack>> oldLoadouts = new HashMap();
for (Loadout ldt : loadouts) {
oldLoadouts.put(ldt.getName(), ldt.getContents());
}
return oldLoadouts;
}
public static Loadout getLoadout(List<Loadout> loadouts, String name) {
for (Loadout ldt : loadouts) {
if (ldt.getName().equals(name)) {
return ldt;
}
}
return null;
}
// For future use
public Map<String, Object> serialize() {
Map<String, Object> config = new HashMap();
config.put("slots", this.toIntList(contents.keySet()));
for (Integer slot : contents.keySet()) {
Map<String, Object> slotConfig = new HashMap();
ItemStack stack = contents.get(slot);
slotConfig.put("id", stack.getTypeId());
slotConfig.put("data", stack.getData().getData());
slotConfig.put("amount", stack.getAmount());
slotConfig.put("durability", stack.getDurability());
if (stack.getEnchantments().keySet().size() > 0) {
List<String> enchantmentStringList = new ArrayList<String>();
for (Enchantment enchantment : stack.getEnchantments().keySet()) {
int level = stack.getEnchantments().get(enchantment);
enchantmentStringList.add(enchantment.getId() + "," + level);
}
slotConfig.put("enchantments", enchantmentStringList);
}
if (stack.hasItemMeta() && stack.getItemMeta() instanceof LeatherArmorMeta
&& ((LeatherArmorMeta) stack.getItemMeta()).getColor() != null) {
LeatherArmorMeta meta = (LeatherArmorMeta) stack.getItemMeta();
int rgb = meta.getColor().asRGB();
slotConfig.put("armorcolor", rgb);
}
if (stack.hasItemMeta() && stack.getItemMeta().hasDisplayName()) {
ItemMeta meta = stack.getItemMeta();
slotConfig.put("name", meta.getDisplayName());
}
if (stack.hasItemMeta() && stack.getItemMeta().hasLore()) {
ItemMeta meta = stack.getItemMeta();
slotConfig.put("lore", meta.getLore());
}
config.put(slot.toString(), slotConfig);
}
config.put("permission", permission);
return config;
}
public static Loadout deserialize(Map<String, Object> config) {
HashMap<Integer, ItemStack> contents = new HashMap();
List<Integer> slots = (List<Integer>) config.get("slots");
for (Integer slot : slots) {
Map<String, Object> slotConfig = (Map<String, Object>) config.get(slot.toString());
int id = (Integer) slotConfig.get("id");
byte data = (Byte) slotConfig.get("data");
int amount = (Integer) slotConfig.get("amount");
short durability = (Short) slotConfig.get("durability");
ItemStack stack = new ItemStack(id, amount, durability);
stack.setData(new MaterialData(id, data));
if (slotConfig.containsKey("enchantments")) {
List<String> enchantmentStringList = (List<String>) slotConfig.get("enchantments");
for (String enchantmentString : enchantmentStringList) {
String[] enchantmentStringSplit = enchantmentString.split(",");
if (enchantmentStringSplit.length == 2) {
int enchantId = Integer.parseInt(enchantmentStringSplit[0]);
int level = Integer.parseInt(enchantmentStringSplit[1]);
War.war.safelyEnchant(stack, Enchantment.getById(enchantId), level);
}
}
}
if (slotConfig.containsKey("armorcolor")) {
int rgb = (Integer) slotConfig.get("armorcolor");
Color clr = Color.fromRGB(rgb);
LeatherArmorMeta meta = (LeatherArmorMeta) stack.getItemMeta();
meta.setColor(clr);
stack.setItemMeta(meta);
}
if (slotConfig.containsKey("name")) {
String itemName = (String) slotConfig.get("name");
ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(itemName);
stack.setItemMeta(meta);
}
if (slotConfig.containsKey("lore")) {
List<String> itemLore = (List<String>) slotConfig.get("lore");
ItemMeta meta = stack.getItemMeta();
meta.setLore(itemLore);
stack.setItemMeta(meta);
}
contents.put(slot, stack);
}
String permission = "";
if (config.containsKey("permission")) {
permission = (String) config.get("permission");
}
return new Loadout(null, contents, permission);
}
}

View File

@ -6,8 +6,8 @@ public class LoadoutSelection {
private int selectedIndex;
public LoadoutSelection(boolean stillInSpawn, int selectedIndex) {
this.setStillInSpawn(stillInSpawn);
this.setSelectedIndex(selectedIndex);
this.stillInSpawn = stillInSpawn;
this.selectedIndex = selectedIndex;
}

View File

@ -197,6 +197,7 @@ commands:
/zonecfg teamsize:10 flagmustbehome:on flagpointsonly:off flagreturn:<both/flag/spawn> nohunger:off respawntimer:0 saturation:10 spawnstyle:<small/flat/big/invisible> \\
/zonecfg loadout:default -> sets the default respawn inventory to your current items. \\
/zonecfg loadout:<extra-loadout-name> -> adds an extra loadout (i.e. another player class) that the players can toggle to by sneaking while inside the spawn. \\
/zonecfg loadout:<extra-loadout-name>:<permission> -> add/update a loadout that requires a permission. \\
/zonecfg deleteloadout:<extra-loadout-name> -> removes the specified loadout from the choices. \\
/zonecfg playerloadoutasdefault:true -> the player can bring a copy of his outside-world inventory to fight, it replaces the default loadout. \\
/zonecfg reward:default -> sets the winner's reward to your current items.