This commit is contained in:
filoghost 2016-06-13 19:59:43 +02:00
parent fa382186c8
commit 01b51d62e0
3 changed files with 53 additions and 67 deletions

View File

@ -36,7 +36,8 @@ public class Icon {
protected boolean closeOnClick;
private ClickHandler clickHandler;
private Map<Integer, Set<Variable>> variables;
private Set<Variable> nameVariables;
private Map<Integer, Set<Variable>> loreVariables;
private ItemStack cachedItem; // When there are no variables, we don't recreate the item.
public Icon() {
@ -45,7 +46,7 @@ public class Icon {
}
public boolean hasVariables() {
return variables != null;
return nameVariables != null || loreVariables != null;
}
public void setMaterial(Material material) {
@ -80,26 +81,18 @@ public class Icon {
public void setName(String name) {
this.name = name;
this.nameVariables = null; // Reset the variables
if (name == null) {
return;
}
if (name != null) {
for (Variable variable : Variable.values()) {
if (name.contains(variable.getText())) {
if (nameVariables == null) {
nameVariables = new HashSet<Variable>();
}
for (Variable variable : Variable.values()) {
if (name.contains(variable.getText())) {
if (variables == null) {
variables = new HashMap<Integer, Set<Variable>>();
nameVariables.add(variable);
}
Set<Variable> nameVariables = variables.get(-1);
if (nameVariables == null) {
nameVariables = new HashSet<Variable>();
variables.put(-1, nameVariables);
}
nameVariables.add(variable);
}
}
}
@ -116,27 +109,26 @@ public class Icon {
public void setLore(List<String> lore) {
this.lore = lore;
this.loreVariables = null; // Reset the variables
if (lore == null) {
return;
}
for (int i = 0; i < lore.size(); i++) {
for (Variable variable : Variable.values()) {
if (lore.get(i).contains(variable.getText())) {
if (variables == null) {
variables = new HashMap<Integer, Set<Variable>>();
if (lore != null) {
for (int i = 0; i < lore.size(); i++) {
for (Variable variable : Variable.values()) {
if (lore.get(i).contains(variable.getText())) {
if (loreVariables == null) {
loreVariables = new HashMap<Integer, Set<Variable>>();
}
Set<Variable> lineVariables = loreVariables.get(i);
if (lineVariables == null) {
lineVariables = new HashSet<Variable>();
loreVariables.put(i, lineVariables);
}
lineVariables.add(variable);
}
Set<Variable> lineVariables = variables.get(i);
if (lineVariables == null) {
lineVariables = new HashSet<Variable>();
variables.put(i, lineVariables);
}
lineVariables.add(variable);
}
}
}
@ -152,7 +144,7 @@ public class Icon {
public void setEnchantments(Map<Enchantment, Integer> enchantments) {
if (enchantments == null) {
clearEnchantments();
this.enchantments.clear();
return;
}
this.enchantments = enchantments;
@ -211,13 +203,9 @@ public class Icon {
String name = this.name;
if (pov != null && variables != null) {
Set<Variable> nameVariables = variables.get(-1); // Name variables have index -1.
if (nameVariables != null) {
for (Variable nameVariable : nameVariables) {
name = name.replace(nameVariable.getText(), nameVariable.getReplacement(pov));
}
if (pov != null && nameVariables != null) {
for (Variable nameVariable : nameVariables) {
name = name.replace(nameVariable.getText(), nameVariable.getReplacement(pov));
}
}
@ -240,12 +228,12 @@ public class Icon {
output = Utils.newArrayList();
if (pov != null && variables != null) {
if (pov != null && loreVariables != null) {
for (int i = 0; i < lore.size(); i++) {
String line = lore.get(i);
Set<Variable> lineVariables = variables.get(i);
Set<Variable> lineVariables = loreVariables.get(i);
if (lineVariables != null) {
for (Variable lineVariable : lineVariables) {
line = line.replace(lineVariable.getText(), lineVariable.getReplacement(pov));
@ -275,7 +263,7 @@ public class Icon {
public ItemStack createItemstack(Player pov) {
if (variables == null && cachedItem != null) {
if (!this.hasVariables() && cachedItem != null) {
// Performance.
return cachedItem;
}
@ -306,7 +294,7 @@ public class Icon {
}
}
if (variables == null) {
if (!this.hasVariables()) {
// If there are no variables, cache the item.
cachedItem = itemStack;
}

View File

@ -85,29 +85,27 @@ public class ExtendedIconMenu extends IconMenu {
}
public void refresh(Player player, Inventory inventory) {
try {
for (int i = 0; i < icons.length; i++) {
if (icons[i] != null && icons[i] instanceof ExtendedIcon) {
ExtendedIcon extIcon = (ExtendedIcon) icons[i];
if (extIcon.hasViewPermission() || extIcon.hasVariables()) {
// Then we have to refresh it
if (extIcon.canViewIcon(player)) {
if (inventory.getItem(i) == null) {
ItemStack updatedIcon = extIcon.createItemstack(player);
inventory.setItem(i, updatedIcon);
ItemStack newItem = AttributeRemover.hideAttributes(extIcon.createItemstack(player));
inventory.setItem(i, newItem);
} else {
// Performance, only update name and lore.
ItemStack oldItem = AttributeRemover.hideAttributes(inventory.getItem(i));
ItemMeta meta = oldItem.getItemMeta();
meta.setDisplayName(extIcon.calculateName(player));
meta.setLore(extIcon.calculateLore(player));
oldItem.setItemMeta(meta);
}
// Performance, only update name and lore.
ItemStack inventoryItem = AttributeRemover.hideAttributes(inventory.getItem(i));
ItemMeta meta = inventoryItem.getItemMeta();
meta.setDisplayName(extIcon.calculateName(player));
meta.setLore(extIcon.calculateLore(player));
inventoryItem.setItemMeta(meta);
} else {
inventory.setItem(i, null);
}

View File

@ -120,11 +120,11 @@ public class MenuSerializer {
}
if (config.isSet(Nodes.AUTO_REFRESH)) {
double autoRefresh = config.getDouble(Nodes.AUTO_REFRESH);
int tenthsToRefresh = autoRefresh <= 0.1 ? 1 : (int) (autoRefresh * 10.0);
int tenthsToRefresh = (int) (config.getDouble(Nodes.AUTO_REFRESH) * 10.0);
if (tenthsToRefresh < 1) {
tenthsToRefresh = 1;
}
menuData.setRefreshTenths(tenthsToRefresh);
}
return menuData;