Fix colored names being ruined when placed in an anvil.

This commit is contained in:
sigonasr2 2016-06-18 16:08:33 -05:00
parent 338e17a4ff
commit 82cbf67562
3 changed files with 60 additions and 26 deletions

Binary file not shown.

View File

@ -37,7 +37,11 @@ public class RecyclingCenter {
} }
public Location getRandomNode() { public Location getRandomNode() {
return nodes.get((int)(Math.floor(Math.random()*nodes.size()))); if (nodes.size()>0) {
return nodes.get((int)(Math.floor(Math.random()*nodes.size())));
} else {
return null;
}
} }
public int getNumberOfNodes() { public int getNumberOfNodes() {

View File

@ -1658,6 +1658,32 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
} }
},1); },1);
if (ev.getInventory().getType()==InventoryType.ANVIL &&
ev.getRawSlot()==2) {
//The results slot was clicked. We should set the result's item name properly back to what it was.
if (ev.getCurrentItem()!=null &&
ev.getInventory().getItem(0)!=null &&
ev.getInventory().getItem(0).getItemMeta().hasDisplayName()) {
//It's possible we may have to fix the color code for this item. Check the first two characters.
String oldname = ev.getInventory().getItem(0).getItemMeta().getDisplayName();
ChatColor first = ChatColor.getByChar(oldname.charAt(1));
log("First character is "+first,4);
ChatColor second = ChatColor.getByChar(oldname.charAt(0));
log("Second character is "+second,4);
if (first!=null) {
if (second!=null) {
ItemMeta m = ev.getCurrentItem().getItemMeta();
m.setDisplayName(first+""+second+ev.getCurrentItem().getItemMeta().getDisplayName().substring(2));
ev.getCurrentItem().setItemMeta(m);
} else {
ItemMeta m = ev.getCurrentItem().getItemMeta();
m.setDisplayName(first+ev.getCurrentItem().getItemMeta().getDisplayName().substring(1));
ev.getCurrentItem().setItemMeta(m);
}
}
}
}
if (ev.getInventory().getTitle().equalsIgnoreCase("Death Loot")) { if (ev.getInventory().getTitle().equalsIgnoreCase("Death Loot")) {
//See how many items are in our inventory. Determine final balance. //See how many items are in our inventory. Determine final balance.
//Count the occupied slots. //Count the occupied slots.
@ -2193,31 +2219,35 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
if (Math.random()*100<=RECYCLECHANCE && if (Math.random()*100<=RECYCLECHANCE &&
TwosideRecyclingCenter.IsItemAllowed(i.getItemStack())) { TwosideRecyclingCenter.IsItemAllowed(i.getItemStack())) {
//Recycle allowed. Now figure out which node to go to. //Recycle allowed. Now figure out which node to go to.
Location rand_node=TwosideRecyclingCenter.getRandomNode(); if (TwosideRecyclingCenter.getNumberOfNodes()>0) {
Block b = Bukkit.getWorld("world").getBlockAt(rand_node); Location rand_node=TwosideRecyclingCenter.getRandomNode();
if (b!=null && b.getType()==Material.CHEST || Block b = Bukkit.getWorld("world").getBlockAt(rand_node);
b.getType()==Material.TRAPPED_CHEST) { if (b!=null && b.getType()==Material.CHEST ||
if (b.getState()!=null) { b.getType()==Material.TRAPPED_CHEST) {
Chest c = (Chest) b.getState(); if (b.getState()!=null) {
//Choose a random inventory slot and copy the vanished item into it. Chest c = (Chest) b.getState();
double chancer = 100.0; //Choose a random inventory slot and copy the vanished item into it.
for (int j=0;j<27;j++) { double chancer = 100.0;
if (c.getBlockInventory().getItem(j)!=null && c.getBlockInventory().getItem(j).getType()==i.getItemStack().getType()) { for (int j=0;j<27;j++) {
chancer-=RECYCLEDECAYAMT; if (c.getBlockInventory().getItem(j)!=null && c.getBlockInventory().getItem(j).getType()==i.getItemStack().getType()) {
} chancer-=RECYCLEDECAYAMT;
} }
int itemslot = (int)Math.floor(Math.random()*27); }
ItemStack oldItem = c.getBlockInventory().getItem(itemslot); int itemslot = (int)Math.floor(Math.random()*27);
//There is also a chance to move this item to another random spot. ItemStack oldItem = c.getBlockInventory().getItem(itemslot);
if (chancer>0 && Math.random()*100<chancer) { //There is also a chance to move this item to another random spot.
if (oldItem!=null && Math.random()*100<=RECYCLECHANCE) { if (chancer>0 && Math.random()*100<chancer) {
int itemslot2 = (int)Math.floor(Math.random()*27); if (oldItem!=null && Math.random()*100<=RECYCLECHANCE) {
c.getBlockInventory().setItem(itemslot2, oldItem); int itemslot2 = (int)Math.floor(Math.random()*27);
} c.getBlockInventory().setItem(itemslot2, oldItem);
c.getBlockInventory().setItem(itemslot, i.getItemStack()); }
log("Sent "+GenericFunctions.UserFriendlyMaterialName(i.getItemStack())+" to Recycling Center Node "+rand_node.toString(),2); c.getBlockInventory().setItem(itemslot, i.getItemStack());
} log("Sent "+GenericFunctions.UserFriendlyMaterialName(i.getItemStack())+" to Recycling Center Node "+rand_node.toString(),3);
} }
}
}
} else {
log("No Recycling Center Nodes set! All dropped items will continue to be discarded. Use /recyclingcenter to define them.",1);
} }
} }
} }