Skip to content

Instantly share code, notes, and snippets.

@aikar
Created July 16, 2014 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aikar/9e29371b5c8055e8ce09 to your computer and use it in GitHub Desktop.
Save aikar/9e29371b5c8055e8ce09 to your computer and use it in GitHub Desktop.
package com.empireminecraft.systems.chestui;
import com.empireminecraft.systems.EmpireChat;
import com.empireminecraft.util.BukkitUtil;
import com.empireminecraft.util.ItemUtil;
import com.google.common.base.Predicate;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import javax.annotation.Nullable;
public class ChestInterfaceItem extends ItemUtil.LoreBuilder {
private Player player;
private Inventory inventory;
private int slot;
private Predicate<Player> onClick = null;
/**
* Initiates a builder and sets the index to the first empty line after the lore
*
* @param item
*/
public ChestInterfaceItem(Player player, Inventory inventory, int slot, ItemStack item) {
super(item);
this.player = player;
this.inventory = inventory;
this.slot = slot;
}
public ItemStack save() {
ItemStack saved = super.save();
inventory.setItem(slot, saved);
return saved;
}
public ChestInterfaceItem onClick(Predicate<Player> run) {
this.onClick = run;
return this;
}
public ChestInterfaceItem onClick(final String cmd) {
this.onClick = new Predicate<Player>() {
@Override
public boolean apply(@Nullable Player input) {
EmpireChat.performCommand(player, cmd);
return true;
}
};
return this;
}
protected final void processClick() {
if (onClick != null) {
player.closeInventory();
BukkitUtil.runTaskNextTick(new Runnable() {
@Override
public void run() {
onClick.apply(player);
}
});
}
}
}
package com.empireminecraft.systems.chestui;
import com.empireminecraft.util.BukkitUtil;
import com.empireminecraft.util.Util;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import java.util.HashMap;
public class ChestUI extends BukkitUtil.Listener {
public static void initialize() {
new ChestUI();
}
@EventHandler(ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
final Inventory inventory = event.getInventory();
final HashMap<String, Object> meta = inventory.getMeta();
if (!meta.containsKey("ChestUI")) {
return;
}
event.setCancelled(true);
if (event.getAction() != InventoryAction.PICKUP_ALL) {
return;
}
if (event.getClickedInventory() != event.getView().getTopInventory()) {
return;
}
final Object o = meta.get(ChestUIInterface.getSlotLabel(event.getSlot()));
if (o instanceof ChestInterfaceItem) {
((ChestInterfaceItem) o).processClick();
}
}
public static void open(Player player, Class <? extends ChestUIInterface> ui) {
try {
open(player, ui.newInstance());
} catch (Exception e) {
Util.printException(e);
}
}
public static void open(Player player, ChestUIInterface ui) {
try {
final Inventory inventory = ui.initialize(player);
inventory.getMeta().put("ChestUI", 1);
player.openInventory(inventory);
} catch (Exception e) {
Util.printException(e);
}
}
}
package com.empireminecraft.systems.chestui;
import com.empireminecraft.util.ItemUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
public abstract class ChestUIInterface {
protected Player player;
protected Inventory inventory;
public static String getSlotLabel(int slot) {
return "ChestUI_Slot_" + slot;
}
public abstract int getSize();
public abstract String getTitle();
public abstract void init(Player player, Inventory inventory);
protected int slot = 0;
protected final Inventory initialize(Player player) {
this.player = player;
inventory = Bukkit.createInventory(player, getSize(), getTitle());
init(player, inventory);
return inventory;
}
public Inventory getInventory() {
return inventory;
}
public Player getPlayer() {
return player;
}
public ChestInterfaceItem add(int slot, Material material, short data) {
return add(slot, new ItemStack(material, 1, data));
}
public ChestInterfaceItem add(Material material, short data) {
return add(slot, new ItemStack(material, 1, data));
}
public ChestInterfaceItem add(Material material) {
return add(slot, new ItemStack(material));
}
public ChestInterfaceItem add(int slot, Material material) {
return add(slot, new ItemStack(material));
}
public ChestInterfaceItem add(ItemStack item) {
return add(slot, item);
}
public ChestInterfaceItem add(final int slot, ItemStack item) {
this.slot = slot+1;
final ChestInterfaceItem ciItem = new ChestInterfaceItem(player, inventory, slot, item);
final HashMap<String, Object> meta = inventory.getMeta();
meta.put(getSlotLabel(slot), ciItem);
return ciItem;
}
}
package com.empireminecraft.systems.chestui;
import com.empireminecraft.util.ItemUtil;
import com.empireminecraft.util.Util;
import com.google.common.base.Predicate;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import javax.annotation.Nullable;
public class ConfirmChestInterface extends ChestUIInterface {
private final String confirmTitle;
private final Predicate<Player> onConfirm;
private final Predicate<Player> onCancel;
private final String[] lines;
public ConfirmChestInterface(String confirmTitle, Predicate<Player> onConfirm, String... lines) {
this(confirmTitle, onConfirm, null, lines);
}
public ConfirmChestInterface(
String confirmTitle, Predicate<Player> onConfirm, Predicate<Player> onCancel, String... lines) {
this.confirmTitle = confirmTitle;
this.onConfirm = onConfirm;
this.onCancel = onCancel;
this.lines = lines;
}
@Override
public int getSize() {
return 9;
}
@Override
public String getTitle() {
return ChatColor.stripColor(Util.color(confirmTitle));
}
@Override
public void init(final Player player, Inventory inventory) {
final ItemUtil.LoreBuilder builder = add(Material.PAPER)
.setName(confirmTitle);
if (lines != null) {
for (String line : lines) {
builder.add(line);
}
}
builder
.empty()
.add("&eClick on the &2Green Wool&e to confirm.")
.add("&eClick on the &4Red Wool&e to cancel.")
.save();
add(3, Material.WOOL, (short) 13)
.onClick(new Predicate<Player>() {
@Override
public boolean apply(@Nullable Player input) {
player.closeInventory();
return onConfirm.apply(input);
}
})
.setName("&2Confirm")
.add("&2Click to Confirm")
.save();
add(5, Material.WOOL, (short) 14)
.onClick(new Predicate<Player>() {
@Override
public boolean apply(@Nullable Player input) {
player.closeInventory();
return onCancel == null || onCancel.apply(input);
}
})
.setName("&4Cancel")
.add("&4Click to Cancel")
.save();
}
}
package com.empireminecraft.systems.chestui;
import com.empireminecraft.config.EmpireServer;
import com.empireminecraft.util.ItemUtil;
import com.empireminecraft.util.Util;
import com.google.common.base.Predicate;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import javax.annotation.Nullable;
public class ServerSwitcher extends ChestUIInterface {
@Override
public int getSize() {
return 27;
}
@Override
public String getTitle() {
return "Server Switcher";
}
@Override
public void init(Player player, Inventory inventory) {
addServer(Material.WOOL, 0, EmpireServer.SMP1, "This is a Survival Server");
addServer(Material.WOOL, 1, EmpireServer.SMP2, "This is a Survival Server");
addServer(Material.WOOL, 2, EmpireServer.SMP3, "This is a Survival Server");
addServer(Material.WOOL, 3, EmpireServer.SMP4, "This is a Survival Server");
addServer(Material.WOOL, 4, EmpireServer.SMP5, "This is a Survival Server");
addServer(Material.WOOL, 5, EmpireServer.SMP6, "This is a Survival Server");
addServer(Material.WOOL, 6, EmpireServer.SMP7, "This is a Survival Server");
addServer(Material.WOOL, 7, EmpireServer.SMP8, "This is a Survival Server");
addServer(Material.WOOL, 8, EmpireServer.SMP9, "This is a Survival Server");
addServer(Material.GOLD_BLOCK, 0, EmpireServer.UTOPIA, "This is a Survival Server for", "Gold and Diamond Supporters");
if (Util.isModerator(player)) {
addServer(Material.FIREWORK, 0, EmpireServer.GAMES, "This is a Games Server");
addServer(Material.TNT, 0, EmpireServer.STAGE, "This is a Test Server");
}
}
void addServer(Material mat, int id, final EmpireServer server, String... extra) {
final ItemUtil.LoreBuilder loreBuilder = add(mat, (short) id)
.onClick(new Predicate<Player>() {
@Override
public boolean apply(@Nullable Player input) {
server.sendPlayerToServer(input);
return true;
}
})
.setName("&bSwitch to: &a" + server.extraLabel);
if (extra != null) {
for (String ex : extra) {
loreBuilder.add("&b" + ex);
}
loreBuilder.empty();
}
loreBuilder.add("&aCommand: &f/" + server.label).save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment