Fix Item Cube functionality when first inserting an item to a newly

unactivated Item Cube. Made a new helper function to check it the item
is an Item Cube.
master_event
sigonasr2 11 years ago
parent c4108da16d
commit 3b8d4195e4
  1. 145
      BankEconomyMod/src/me/kaZep/Base/PlayerListener.java

@ -9685,10 +9685,13 @@ implements Listener
} }
if (event.getInventory().getType()==InventoryType.CRAFTING /*|| event.getInventory().getType()==InventoryType.CHEST*//*Buggy for some reason. We can't open chests in chests.*/) { if (event.getInventory().getType()==InventoryType.CRAFTING /*|| event.getInventory().getType()==InventoryType.CHEST*//*Buggy for some reason. We can't open chests in chests.*/) {
if (event.getCurrentItem()!=null) { if (event.getCurrentItem()!=null) {
if ((event.getCurrentItem().getType()==Material.CHEST || event.getCurrentItem().getType()==Material.TRAPPED_CHEST || event.getCurrentItem().getType()==Material.ENDER_CHEST) && event.getClick()==ClickType.RIGHT) { if ((event.getCurrentItem().getType()==Material.CHEST || event.getCurrentItem().getType()==Material.TRAPPED_CHEST || event.getCurrentItem().getType()==Material.ENDER_CHEST) && event.getClick()==ClickType.RIGHT && event.getCurrentItem().hasItemMeta()) {
viewItemCube(p, event.getCurrentItem()); if (isItemCube(event.getCurrentItem())) {
event.setCancelled(true); //Only cancel the event and view the Item Cube if it actually is one.
return; viewItemCube(p, event.getCurrentItem());
event.setCancelled(true);
return;
}
} }
} }
}else }else
@ -11832,27 +11835,29 @@ implements Listener
} }
} }
public void viewItemCube(Player p, ItemStack item_cube) { public boolean isItemCube(ItemStack item_cube) {
//This function will figure out what type of Item Cube this item is and then display the correct inventory on-screen, also setting up the identifier. if (item_cube.hasItemMeta() && item_cube.getItemMeta().getLore()!=null) {
//Note that this function does not check if the item *is* an item cube. It assumes it is.
Cube cube_type = null;
int identifier=-1;
if (item_cube.getItemMeta().getLore()!=null) {
//Check to see if the Lore contains anything. //Check to see if the Lore contains anything.
for (int i=0;i<item_cube.getItemMeta().getLore().size();i++) { for (int i=0;i<item_cube.getItemMeta().getLore().size();i++) {
if (item_cube.getItemMeta().getLore().get(i).equalsIgnoreCase(ChatColor.AQUA+"Contains 9 item slots.")) { if (item_cube.getItemMeta().getLore().get(i).equalsIgnoreCase(ChatColor.AQUA+"Contains 9 item slots.")) {
cube_type = Cube.SMALL; return true;
} }
if (item_cube.getItemMeta().getLore().get(i).equalsIgnoreCase(ChatColor.AQUA+"Contains 54 item slots.")) { if (item_cube.getItemMeta().getLore().get(i).equalsIgnoreCase(ChatColor.AQUA+"Contains 54 item slots.")) {
cube_type = Cube.LARGE; return true;
} }
if (item_cube.getItemMeta().getLore().get(i).equalsIgnoreCase(ChatColor.AQUA+"Contains 27 item slots.")) { if (item_cube.getItemMeta().getLore().get(i).equalsIgnoreCase(ChatColor.AQUA+"Contains 27 item slots.")) {
cube_type = Cube.ENDER; return true;
}
if (item_cube.getItemMeta().getLore().get(i).contains("ID#")) {
identifier=Integer.valueOf(item_cube.getItemMeta().getLore().get(i).replace("ID#", ""));
} }
} }
}
return false;
}
public void viewItemCube(Player p, ItemStack item_cube) {
//This function will figure out what type of Item Cube this item is and then display the correct inventory on-screen, also setting up the identifier.
Cube cube_type = null;
int identifier=-1;
if (isItemCube(item_cube)) {
if (identifier==-1) { if (identifier==-1) {
//This doesn't have an identifier yet. Create a new one. //This doesn't have an identifier yet. Create a new one.
identifier=this.plugin.getConfig().getInt("item-cube-numb"); identifier=this.plugin.getConfig().getInt("item-cube-numb");
@ -12024,68 +12029,66 @@ implements Listener
f.set("item-"+i, new ItemStack(Material.AIR)); f.set("item-"+i, new ItemStack(Material.AIR));
} }
f.set("item-0", insert_item); f.set("item-0", insert_item);
insert_item.setType(Material.AIR);
f.set("created", Boolean.valueOf(true)); f.set("created", Boolean.valueOf(true));
return insert_item; //return insert_item;
} else { }
//We need to see if we have the inventory opened already...If so we have to add it to THAT one. //We need to see if we have the inventory opened already...If so we have to add it to THAT one.
Inventory thisinven=Bukkit.createInventory(p, slots, heading+"Item Cube #"+identifier); Inventory thisinven=Bukkit.createInventory(p, slots, heading+"Item Cube #"+identifier);
boolean changeinven=false; boolean changeinven=false;
if (p.getOpenInventory().getTopInventory().getTitle().contains(heading+"Item Cube") && p.getOpenInventory().getTopInventory().getTitle().length()>0) { if (p.getOpenInventory().getTopInventory().getTitle().contains(heading+"Item Cube") && p.getOpenInventory().getTopInventory().getTitle().length()>0) {
if (Integer.valueOf(p.getOpenInventory().getTopInventory().getTitle().substring(p.getOpenInventory().getTopInventory().getTitle().indexOf("#")).replace("#", ""))==identifier) { if (Integer.valueOf(p.getOpenInventory().getTopInventory().getTitle().substring(p.getOpenInventory().getTopInventory().getTitle().indexOf("#")).replace("#", ""))==identifier) {
thisinven=p.getOpenInventory().getTopInventory(); thisinven=p.getOpenInventory().getTopInventory();
changeinven=true; changeinven=true;
}
}
if (!changeinven) {
for (int i=0;i<slots;i++) {
//items.add(f.getItemStack("item-"+i));
if (f.contains("item-"+i)) {
thisinven.addItem(f.getItemStack("item-"+i));
}
}
} }
int countinven = countSpace(thisinven,insert_item); }
if (countinven>=insert_item.getAmount()) { if (!changeinven) {
//We can simply add it in no problem. for (int i=0;i<slots;i++) {
thisinven.addItem(insert_item); //items.add(f.getItemStack("item-"+i));
for (int i=0;i<thisinven.getContents().length;i++) { if (f.contains("item-"+i)) {
f.set("item-"+i, thisinven.getItem(i)); thisinven.addItem(f.getItemStack("item-"+i));
} }
this.plugin.saveItemCubeConfig(f, identifier); }
}
int countinven = countSpace(thisinven,insert_item);
if (countinven>=insert_item.getAmount()) {
//We can simply add it in no problem.
thisinven.addItem(insert_item);
for (int i=0;i<thisinven.getContents().length;i++) {
f.set("item-"+i, thisinven.getItem(i));
}
this.plugin.saveItemCubeConfig(f, identifier);
if (size == Cube.ENDER) {
//This is an Ender Item Cube. We have to check if any other players are looking at the correspnding Ender Item Cube inventory.
ItemCube_addSameEnderCube(insert_item, identifier, p);
}
return new ItemStack(Material.AIR);
} else
if (countinven>0) {
//We can at least fit a few.
int fit = insert_item.getAmount()-countinven;
//Leave behind this many.
ItemStack thisitem = insert_item, thisitem2 = insert_item;
thisitem.setAmount(fit);
//Bukkit.getPlayer("sigonasr2").sendMessage("Cursor gets "+thisitem.getAmount());
thisitem2.setAmount(countinven);
thisinven.addItem(thisitem2);
if (size == Cube.ENDER) { if (size == Cube.ENDER) {
//This is an Ender Item Cube. We have to check if any other players are looking at the correspnding Ender Item Cube inventory. //This is an Ender Item Cube. We have to check if any other players are looking at the correspnding Ender Item Cube inventory.
ItemCube_addSameEnderCube(insert_item, identifier, p); ItemCube_addSameEnderCube(thisitem2, identifier, p);
}
return new ItemStack(Material.AIR);
} else
if (countinven>0) {
//We can at least fit a few.
int fit = insert_item.getAmount()-countinven;
//Leave behind this many.
ItemStack thisitem = insert_item, thisitem2 = insert_item;
thisitem.setAmount(fit);
//Bukkit.getPlayer("sigonasr2").sendMessage("Cursor gets "+thisitem.getAmount());
thisitem2.setAmount(countinven);
thisinven.addItem(thisitem2);
if (size == Cube.ENDER) {
//This is an Ender Item Cube. We have to check if any other players are looking at the correspnding Ender Item Cube inventory.
ItemCube_addSameEnderCube(thisitem2, identifier, p);
}
//Bukkit.getPlayer("sigonasr2").sendMessage("Item Cube gets "+thisitem2.getAmount());
for (int i=0;i<thisinven.getContents().length;i++) {
f.set("item-"+i, thisinven.getItem(i));
}
this.plugin.saveItemCubeConfig(f, identifier);
p.updateInventory();
return thisitem;
} }
else { //Bukkit.getPlayer("sigonasr2").sendMessage("Item Cube gets "+thisitem2.getAmount());
//Well, we can't do anything, just treat it as an item swap. for (int i=0;i<thisinven.getContents().length;i++) {
this.plugin.saveItemCubeConfig(f, identifier); f.set("item-"+i, thisinven.getItem(i));
return new ItemStack(insert_item);
} }
} this.plugin.saveItemCubeConfig(f, identifier);
p.updateInventory();
return thisitem;
}
else {
//Well, we can't do anything, just treat it as an item swap.
this.plugin.saveItemCubeConfig(f, identifier);
return new ItemStack(insert_item);
}
} }
public ItemStack insertIntoItemCube(Player p, ItemStack item_cube, ItemStack insert_item) { public ItemStack insertIntoItemCube(Player p, ItemStack item_cube, ItemStack insert_item) {

Loading…
Cancel
Save