This is a dev question. I'm not requesting/offering any service.
Hello, I have no idea on where to post this. I think MCM is my only solution because spigot doesn't give any support for 1.7 builds.
So basically I'm using this API AnvilGUI. Everything is working fine, only exception the items do not display in the anvil when I set them.
I did some debug (broadcast the item name) when I click the INPUT_LEFT slot. And the item name is broadcasted. But no items can be seen in the anvil.
Thank you for any help. I'm stuck for now 1 week.
Hello, I have no idea on where to post this. I think MCM is my only solution because spigot doesn't give any support for 1.7 builds.
So basically I'm using this API AnvilGUI. Everything is working fine, only exception the items do not display in the anvil when I set them.
Code:
private class AnvilContainer extends ContainerAnvil {
public AnvilContainer(EntityHuman entity) {
super(entity.inventory, entity.world, 0, 0, 0, entity);
}
@Override
public boolean a(EntityHuman entityhuman) {
return true;
}
}
public enum AnvilSlot {
INPUT_LEFT(0),
INPUT_RIGHT(1),
OUTPUT(2);
private int slot;
AnvilSlot(int slot) {
this.slot = slot;
}
public int getSlot() {
return slot;
}
public static AnvilSlot bySlot(int slot) {
for (AnvilSlot anvilSlot : values()) {
if (anvilSlot.getSlot() == slot) {
return anvilSlot;
}
}
return null;
}
}
public class AnvilClickEvent {
private AnvilSlot slot;
private String name;
private boolean close = true;
private boolean destroy = true;
public AnvilClickEvent(AnvilSlot slot, String name) {
this.slot = slot;
this.name = name;
}
public AnvilSlot getSlot() {
return slot;
}
public String getName() {
return name;
}
public boolean getWillClose() {
return close;
}
public void setWillClose(boolean close) {
this.close = close;
}
public boolean getWillDestroy() {
return destroy;
}
public void setWillDestroy(boolean destroy) {
this.destroy = destroy;
}
}
public interface AnvilClickEventHandler {
void onAnvilClick(AnvilClickEvent event);
}
private Player player;
@SuppressWarnings("unused")
private AnvilClickEventHandler handler;
private HashMap<AnvilSlot, ItemStack> items = new HashMap<>();
private Inventory inv;
private Listener listener;
public AnvilGUI(Player player, final AnvilClickEventHandler handler) {
this.player = player;
this.handler = handler;
this.listener = new Listener() {
@SuppressWarnings("unused")
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (event.getWhoClicked() instanceof Player) {
Player clicker = (Player) event.getWhoClicked();
if (event.getInventory().equals(inv)) {
event.setCancelled(true);
ItemStack item = event.getCurrentItem();
int slot = event.getRawSlot();
String name = "";
if (item != null) {
if (item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
if (meta.hasDisplayName()) {
name = meta.getDisplayName();
}
}
}
AnvilClickEvent clickEvent = new AnvilClickEvent(AnvilSlot.bySlot(slot), name);
handler.onAnvilClick(clickEvent);
if (clickEvent.getWillClose()) {
event.getWhoClicked().closeInventory();
}
if (clickEvent.getWillDestroy()) {
destroy();
}
}
}
}
@SuppressWarnings("unused")
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (event.getPlayer() instanceof Player) {
Player player = (Player) event.getPlayer();
Inventory inv = event.getInventory();
if (inv.equals(AnvilGUI.this.inv)) {
inv.clear();
destroy();
}
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
if (event.getPlayer().equals(getPlayer())) {
destroy();
}
}
};
Bukkit.getPluginManager().registerEvents(listener, Main.getInstance()); //Replace with instance of main class
}
public Player getPlayer() {
return player;
}
public void setSlot(AnvilSlot slot, ItemStack item) {
items.put(slot, item);
}
public void open() {
EntityPlayer p = ((CraftPlayer) player).getHandle();
AnvilContainer container = new AnvilContainer(p);
//Set the items to the items from the inventory given
inv = container.getBukkitView().getTopInventory();
for (AnvilSlot slot : items.keySet()) {
inv.setItem(slot.getSlot(), items.get(slot));
}
//Counter stuff that the game uses to keep track of inventories
int c = p.nextContainerCounter();
//Send the packet
p.playerConnection.sendPacket(new PacketPlayOutOpenWindow(container.windowId, type.getType(), title, 9, true));
//Set their active container to the container
p.activeContainer = container;
//Set their active container window id to that counter stuff
p.activeContainer.windowId = c;
//Add the slot listener
p.activeContainer.addSlotListener(p);
}
public void destroy() {
player = null;
handler = null;
items = null;
HandlerList.unregisterAll(listener);
listener = null;
}
}
I did some debug (broadcast the item name) when I click the INPUT_LEFT slot. And the item name is broadcasted. But no items can be seen in the anvil.
Thank you for any help. I'm stuck for now 1 week.
