Skip to content

Commit

Permalink
Fix bug with RedCommands, ItemUtils now mutates
Browse files Browse the repository at this point in the history
- Fix bug with command manager where messages would display as their name rather than their value
- Commands now implement PluginIdentifiableCommand, which should make them more reload-friendly
- ItemUtils now mutates items rather than returning clones
- ItemBuilder now no longer returns a new ItemBuilder object with each call, instead mutating and returning itself
  • Loading branch information
boxbeam committed Sep 8, 2021
1 parent 5ac2429 commit 481034e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
}
dependencies {
compileOnly "org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT"
implementation 'com.github.Redempt:RedCommands:1.0.5'
implementation 'com.github.Redempt:RedCommands:1.1'
}
sourceSets {
main {
Expand Down
46 changes: 29 additions & 17 deletions src/redempt/redlib/itemutils/ItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ public ItemBuilder(ItemStack item) {
* @return The ItemBuilder with the new stack size
*/
public ItemBuilder setCount(int amount) {
ItemBuilder clone = new ItemBuilder(this.clone());
clone.setAmount(amount);
return clone;
setAmount(amount);
return this;
}

/**
Expand All @@ -61,7 +60,8 @@ public ItemBuilder setCount(int amount) {
* @return The enchanted ItemBuilder
*/
public ItemBuilder addEnchant(Enchantment enchant, int level) {
return new ItemBuilder(ItemUtils.addEnchant(this, enchant, level));
ItemUtils.addEnchant(this, enchant, level);
return this;
}

/**
Expand All @@ -70,7 +70,8 @@ public ItemBuilder addEnchant(Enchantment enchant, int level) {
* @return The ItemBuilder with lore added
*/
public ItemBuilder setLore(String... lore) {
return new ItemBuilder(ItemUtils.setLore(this, lore));
ItemUtils.setLore(this, lore);
return this;
}

/**
Expand All @@ -79,7 +80,8 @@ public ItemBuilder setLore(String... lore) {
* @return The ItemBuilder with lore added
*/
public ItemBuilder addLore(String line) {
return new ItemBuilder(ItemUtils.addLore(this, line));
ItemUtils.addLore(this, line);
return this;
}

/**
Expand All @@ -88,7 +90,8 @@ public ItemBuilder addLore(String line) {
* @return The ItemBuilder with lore added
*/
public ItemBuilder addLore(Iterable<String> lines) {
return new ItemBuilder(ItemUtils.addLore(this, lines));
ItemUtils.addLore(this, lines);
return this;
}

/**
Expand All @@ -97,7 +100,8 @@ public ItemBuilder addLore(Iterable<String> lines) {
* @return The renamed ItemBuilder
*/
public ItemBuilder setName(String name) {
return new ItemBuilder(ItemUtils.rename(this, name));
ItemUtils.rename(this, name);
return this;
}

/**
Expand All @@ -108,7 +112,7 @@ public ItemBuilder setName(String name) {
@SuppressWarnings("deprecation")
public ItemBuilder setDurability(int durability) {
this.setDurability((short) durability);
return new ItemBuilder(this);
return this;
}

/**
Expand All @@ -118,7 +122,8 @@ public ItemBuilder setDurability(int durability) {
* @return The ItemBuilder with the attribute added
*/
public ItemBuilder addAttribute(Attribute attribute, AttributeModifier modifier) {
return new ItemBuilder(ItemUtils.addAttribute(this, attribute, modifier));
ItemUtils.addAttribute(this, attribute, modifier);
return this;
}

/**
Expand All @@ -129,7 +134,8 @@ public ItemBuilder addAttribute(Attribute attribute, AttributeModifier modifier)
* @return The ItemBuilder with the attribute added
*/
public ItemBuilder addAttribute(Attribute attribute, double amount, Operation operation) {
return new ItemBuilder(ItemUtils.addAttribute(this, attribute, amount, operation));
ItemUtils.addAttribute(this, attribute, amount, operation);
return this;
}

/**
Expand All @@ -141,7 +147,8 @@ public ItemBuilder addAttribute(Attribute attribute, double amount, Operation op
* @return The ItemBuilder with the attribute added
*/
public ItemBuilder addAttribute(Attribute attribute, double amount, Operation operation, EquipmentSlot slot) {
return new ItemBuilder(ItemUtils.addAttribute(this, attribute, amount, operation, slot));
ItemUtils.addAttribute(this, attribute, amount, operation, slot);
return this;
}

/**
Expand All @@ -150,7 +157,8 @@ public ItemBuilder addAttribute(Attribute attribute, double amount, Operation op
* @return The ItemBuilder with the flags added
*/
public ItemBuilder addItemFlags(ItemFlag... flags) {
return new ItemBuilder(ItemUtils.addItemFlags(this, flags));
ItemUtils.addItemFlags(this, flags);
return this;
}

/**
Expand All @@ -159,7 +167,8 @@ public ItemBuilder addItemFlags(ItemFlag... flags) {
* @return The ItemBuilder with the damage applied
*/
public ItemBuilder addDamage(int damage) {
return new ItemBuilder(ItemUtils.damage(this, damage));
ItemUtils.damage(this, damage);
return this;
}

/**
Expand All @@ -168,7 +177,8 @@ public ItemBuilder addDamage(int damage) {
* @return The ItemBuilder with the custom model data set
*/
public ItemBuilder setCustomModelData(int customModelData) {
return new ItemBuilder(ItemUtils.setCustomModelData(this, customModelData));
ItemUtils.setCustomModelData(this, customModelData);
return this;
}

/**
Expand All @@ -181,15 +191,17 @@ public ItemBuilder setCustomModelData(int customModelData) {
* @return The ItemBuilder with the persistent data added
*/
public <T, Z> ItemBuilder addPersistentTag(NamespacedKey key, PersistentDataType<T, Z> type, Z data) {
return new ItemBuilder(ItemUtils.addPersistentTag(this, key, type, data));
ItemUtils.addPersistentTag(this, key, type, data);
return this;
}

/**
* Sets this ItemBuilder to be unbreakable
* @return The ItemBuilder with the unbreakable tag added
*/
public ItemBuilder unbreakable() {
return new ItemBuilder(ItemUtils.setUnbreakable(this));
ItemUtils.setUnbreakable(this);
return this;
}

}
61 changes: 24 additions & 37 deletions src/redempt/redlib/itemutils/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public class ItemUtils {
public static ItemStack rename(ItemStack item, String name) {
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -68,9 +67,8 @@ public static ItemStack setLore(ItemStack item, String line) {
List<String> lore = new ArrayList<>();
lore.add(line);
meta.setLore(lore);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -82,9 +80,8 @@ public static ItemStack setLore(ItemStack item, String line) {
public static ItemStack setLore(ItemStack item, List<String> lore) {
ItemMeta meta = item.getItemMeta();
meta.setLore(lore);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -99,9 +96,8 @@ public static ItemStack addLore(ItemStack item, String line) {
lore = lore == null ? new ArrayList<>() : lore;
lore.add(line);
meta.setLore(lore);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -116,9 +112,8 @@ public static ItemStack addLore(ItemStack item, Iterable<String> lines) {
lore = lore == null ? new ArrayList<>() : lore;
lines.forEach(lore::add);
meta.setLore(lore);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand Down Expand Up @@ -157,9 +152,8 @@ public static ItemStack addEnchant(ItemStack item, Enchantment enchant, int leve
if (level == 0) {
meta.removeEnchant(enchant);
}
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -172,9 +166,8 @@ public static ItemStack addEnchant(ItemStack item, Enchantment enchant, int leve
public static ItemStack addAttribute(ItemStack item, Attribute attribute, AttributeModifier modifier) {
ItemMeta meta = item.getItemMeta();
meta.addAttributeModifier(attribute, modifier);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -189,9 +182,8 @@ public static ItemStack addAttribute(ItemStack item, Attribute attribute, double
ItemMeta meta = item.getItemMeta();
AttributeModifier modifier = new AttributeModifier(attribute.toString(), amount, operation);
meta.addAttributeModifier(attribute, modifier);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -203,9 +195,8 @@ public static ItemStack addAttribute(ItemStack item, Attribute attribute, double
public static ItemStack addItemFlags(ItemStack item, ItemFlag... flags) {
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(flags);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -217,9 +208,8 @@ public static ItemStack addItemFlags(ItemStack item, ItemFlag... flags) {
public static ItemStack setCustomModelData(ItemStack item, int customModelData) {
ItemMeta meta = item.getItemMeta();
meta.setCustomModelData(customModelData);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -235,9 +225,8 @@ public static ItemStack setCustomModelData(ItemStack item, int customModelData)
public static <T, Z> ItemStack addPersistentTag(ItemStack item, NamespacedKey key, PersistentDataType<T, Z> type, Z data) {
ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(key, type, data);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -253,9 +242,8 @@ public static ItemStack addAttribute(ItemStack item, Attribute attribute, double
ItemMeta meta = item.getItemMeta();
AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), attribute.toString(), amount, operation, slot);
meta.addAttributeModifier(attribute, modifier);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
item.setItemMeta(meta);
return item;
}

/**
Expand All @@ -266,7 +254,6 @@ public static ItemStack addAttribute(ItemStack item, Attribute attribute, double
* @throws IllegalArgumentException if the item is not damageable
*/
public static ItemStack damage(ItemStack item, int amount) {
item = item.clone();
if (RedLib.MID_VERSION >= 13) {
ItemMeta meta = item.getItemMeta();
if (!(meta instanceof Damageable)) {
Expand Down

0 comments on commit 481034e

Please sign in to comment.