Allow editing loadouts from chest UI

This commit is contained in:
Connor Monahan 2017-07-29 16:09:36 -05:00
parent 821c66c556
commit 47be664a93
6 changed files with 257 additions and 10 deletions

View File

@ -28,9 +28,11 @@ public abstract class ChestUI {
public abstract int getSize();
void processClick(ItemStack clicked) {
boolean processClick(ItemStack clicked, Inventory inventory) {
if (actions.containsKey(clicked)) {
War.war.getServer().getScheduler().runTask(War.war, actions.get(clicked));
return true;
}
return false;
}
}

View File

@ -0,0 +1,121 @@
package com.tommytony.war.ui;
import com.tommytony.war.Team;
import com.tommytony.war.War;
import com.tommytony.war.Warzone;
import com.tommytony.war.mapper.LoadoutYmlMapper;
import com.tommytony.war.utility.Loadout;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.HashMap;
import java.util.List;
/**
* Created by Connor on 7/29/2017.
*/
class EditLoadoutListUI extends ChestUI {
private final Warzone zone;
private final Team team;
EditLoadoutListUI(Warzone zone) {
super();
this.zone = zone;
this.team = null;
}
EditLoadoutListUI(Team team) {
super();
this.zone = null;
this.team = team;
}
@Override
public void build(final Player player, Inventory inv) {
List<Loadout> loadouts;
if (zone != null) {
loadouts = zone.getDefaultInventories().resolveNewLoadouts();
} else if (team != null) {
loadouts = team.getInventories().resolveNewLoadouts();
} else {
throw new IllegalStateException();
}
ItemStack item;
ItemMeta meta;
int i = 0;
item = new ItemStack(Material.GOLD_SPADE, 1);
meta = item.getItemMeta();
meta.setDisplayName(ChatColor.GREEN + "Create new loadout");
item.setItemMeta(meta);
this.addItem(inv, i++, item, new Runnable() {
@Override
public void run() {
War.war.getUIManager().getPlayerMessage(player, "Type the name for the new loadout (or type cancel):", new StringRunnable() {
@Override
public void run() {
if (this.getValue().equalsIgnoreCase("cancel")) {
return;
}
Loadout ldt;
if (zone != null) {
zone.getDefaultInventories().setLoadout(this.getValue(), new HashMap<Integer, ItemStack>());
ldt = zone.getDefaultInventories().getNewLoadout(this.getValue());
} else {
team.getInventories().setLoadout(this.getValue(), new HashMap<Integer, ItemStack>());
ldt = team.getInventories().getNewLoadout(this.getValue());
}
War.war.getUIManager().assignUI(player, new EditLoadoutUI(ldt, zone, team));
}
});
}
});
List<String> sortedNames = LoadoutYmlMapper.sortNames(Loadout.toLegacyFormat(loadouts));
for (String loadoutName : sortedNames) {
final Loadout ldt = Loadout.getLoadout(loadouts, loadoutName);
if (ldt == null) {
War.war.getLogger().warning("Failed to resolve loadout " + loadoutName);
continue;
}
item = new ItemStack(Material.CHEST);
meta = item.getItemMeta();
meta.setDisplayName(ChatColor.YELLOW + loadoutName);
item.setItemMeta(meta);
this.addItem(inv, i++, item, new Runnable() {
@Override
public void run() {
War.war.getUIManager().assignUI(player, new EditLoadoutUI(ldt, zone, team));
}
});
}
}
@Override
public String getTitle() {
if (zone != null) {
return ChatColor.RED + "Warzone \"" + zone.getName() + "\": Loadouts";
} else if (team != null) {
return ChatColor.BLUE + "Team \"" + team.getName() + "\": Loadouts";
}
return null;
}
@Override
public int getSize() {
int size = 0;
if (zone != null) {
size = zone.getDefaultInventories().getNewLoadouts().size() + 1;
} else if (team != null) {
size = team.getInventories().getNewLoadouts().size() + 1;
}
if (size % 9 == 0) {
return size / 9;
} else {
return size / 9 + 9;
}
}
}

View File

@ -0,0 +1,105 @@
package com.tommytony.war.ui;
import com.tommytony.war.Team;
import com.tommytony.war.Warzone;
import com.tommytony.war.config.TeamConfigBag;
import com.tommytony.war.config.WarzoneConfigBag;
import com.tommytony.war.utility.Loadout;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.HashMap;
/**
* Created by Connor on 7/29/2017.
*/
class EditLoadoutUI extends ChestUI {
private final Loadout loadout;
private final Warzone zone;
private final Team team;
EditLoadoutUI(Loadout ldt, Warzone zone, Team team) {
super();
this.loadout = ldt;
this.zone = zone;
this.team = team;
}
@Override
public void build(final Player player, final Inventory inv) {
HashMap<Integer, ItemStack> lc = loadout.getContents();
for (Integer slot : lc.keySet()) {
ItemStack item = lc.get(slot);
if (item == null || item.getType() == Material.AIR) {
continue;
}
if (slot == 100) {
inv.setItem(9 * 4, item.clone());
} else if (slot == 101) {
inv.setItem(9 * 4 + 1, item.clone());
} else if (slot == 102) {
inv.setItem(9 * 4 + 2, item.clone());
} else if (slot == 103) {
inv.setItem(9 * 4 + 3, item.clone());
} else {
inv.setItem(slot, item.clone());
}
}
ItemStack item = new ItemStack(Material.NETHER_STAR);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.GRAY + "Save");
item.setItemMeta(meta);
this.addItem(inv, getSize() - 2, item, new Runnable() {
@Override
public void run() {
HashMap<Integer, ItemStack> nc = new HashMap<Integer, ItemStack>();
for (int i = 0; i < 9 * 4 + 4; i++) {
int slot = i;
if (i >= 9 * 4) {
slot = i + 64;
}
ItemStack item = inv.getItem(i);
if (item != null && item.getType() != Material.AIR) {
nc.put(slot, item);
}
}
loadout.setContents(nc);
if (zone != null) {
WarzoneConfigBag.afterUpdate(zone, player, "loadout updated", false);
} else if (team != null) {
TeamConfigBag.afterUpdate(team, player, "loadout updated", false);
}
}
});
item = new ItemStack(Material.TNT);
meta = item.getItemMeta();
meta.setDisplayName(ChatColor.GRAY + "Delete");
item.setItemMeta(meta);
this.addItem(inv, getSize() - 1, item, new Runnable() {
@Override
public void run() {
if (zone != null) {
zone.getDefaultInventories().removeLoadout(loadout.getName());
WarzoneConfigBag.afterUpdate(zone, player, "loadout deleted", false);
} else if (team != null) {
team.getInventories().removeLoadout(loadout.getName());
TeamConfigBag.afterUpdate(team, player, "loadout deleted", false);
}
}
});
}
@Override
public String getTitle() {
return ChatColor.RED + "Updating loadout " + loadout.getName();
}
@Override
public int getSize() {
return 9 * 5;
}
}

View File

@ -16,10 +16,10 @@ import org.bukkit.inventory.meta.ItemMeta;
/**
* Created by Connor on 7/27/2017.
*/
public class EditTeamUI extends ChestUI {
class EditTeamUI extends ChestUI {
private final Team team;
public EditTeamUI(Team team) {
EditTeamUI(Team team) {
super();
this.team = team;
}
@ -31,7 +31,7 @@ public class EditTeamUI extends ChestUI {
int i = 0;
item = new ItemStack(Material.GOLD_SPADE, 1);
meta = item.getItemMeta();
meta.setDisplayName("Add additional spawn");
meta.setDisplayName(ChatColor.GREEN + "Add additional spawn");
item.setItemMeta(meta);
this.addItem(inv, i++, item, new Runnable() {
@Override
@ -44,6 +44,16 @@ public class EditTeamUI extends ChestUI {
}
}
});
item = new ItemStack(Material.CHEST, 1);
meta = item.getItemMeta();
meta.setDisplayName(ChatColor.YELLOW + "Loadouts");
item.setItemMeta(meta);
this.addItem(inv, i++, item, new Runnable() {
@Override
public void run() {
War.war.getUIManager().assignUI(player, new EditLoadoutListUI(team));
}
});
item = new ItemStack(Material.TNT, 1);
meta = item.getItemMeta();
meta.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Delete");

View File

@ -14,10 +14,10 @@ import org.bukkit.inventory.meta.ItemMeta;
/**
* Created by Connor on 7/27/2017.
*/
public class EditZoneUI extends ChestUI {
class EditZoneUI extends ChestUI {
private final Warzone zone;
public EditZoneUI(Warzone zone) {
EditZoneUI(Warzone zone) {
super();
this.zone = zone;
}
@ -50,6 +50,12 @@ public class EditZoneUI extends ChestUI {
meta = item.getItemMeta();
meta.setDisplayName(ChatColor.YELLOW + "Loadouts");
item.setItemMeta(meta);
this.addItem(inv, 2, item, new Runnable() {
@Override
public void run() {
War.war.getUIManager().assignUI(player, new EditLoadoutListUI(zone));
}
});
item = new ItemStack(Material.CHEST);
meta = item.getItemMeta();
meta.setDisplayName(ChatColor.YELLOW + "Structures");

View File

@ -59,11 +59,14 @@ public class UIManager implements Listener {
if (uiMap.containsKey(player)) {
ChestUI ui = uiMap.get(player);
if (inventory.getName().equals(ui.getTitle())) {
event.setCancelled(true);
ui.processClick(clicked);
player.closeInventory();
if (ui.processClick(clicked, inventory)) {
event.setCancelled(true);
player.closeInventory();
uiMap.remove(player);
}
} else {
uiMap.remove(player);
}
uiMap.remove(player);
}
}