Skip to content

Commit

Permalink
Make it possible to specify the custom durability through the level.
Browse files Browse the repository at this point in the history
In other words:
'283':
  other:
	enchantments:
	  NO_DURABILITY: 50
  • Loading branch information
aadnk committed Oct 19, 2013
1 parent 3c8e0f7 commit 39b8991
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
Expand Up @@ -477,7 +477,7 @@ public RenameRule apply(@Nullable RenameRule input) {
};
}
});
return String.format("Added the enchantment '%s' to every item.", LeveledEnchantment.parse(args));
return String.format("Added enchantment to every selected item.");
}

private String addDechantment(CommandSender sender, final Deque<String> args) {
Expand All @@ -499,7 +499,7 @@ public RenameRule apply(@Nullable RenameRule input) {
};
}
});
return String.format("Dechanted '%s' for every item.", LeveledEnchantment.parse(args));
return String.format("Dechanted every selected item.");
}

/**
Expand Down
@@ -1,13 +1,13 @@
package org.shininet.bukkit.itemrenamer.configuration;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import javax.annotation.Nonnull;

import org.shininet.bukkit.itemrenamer.utils.CollectionsUtil;
import org.shininet.bukkit.itemrenamer.wrappers.LeveledEnchantment;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -168,10 +168,14 @@ public Builder enchantments(Collection<LeveledEnchantment> enchantments) {
public Builder mergeEnchantments(@Nonnull Collection<LeveledEnchantment> enchantments) {
Preconditions.checkNotNull(enchantments, "enchantments cannot be NULL.");

if (this.enchantments != null)
if (this.enchantments != null) {
for (LeveledEnchantment enchantment : enchantments)
removeByType(this.enchantments, enchantment);

this.enchantments.addAll(enchantments);
else
} else {
enchantments(enchantments);
}
return this;
}

Expand All @@ -193,13 +197,30 @@ public Builder dechantments(Collection<LeveledEnchantment> dechantments) {
public Builder mergeDechantments(@Nonnull Collection<LeveledEnchantment> dechantments) {
Preconditions.checkNotNull(dechantments, "dechantments cannot be NULL.");

if (this.dechantments != null)
if (this.dechantments != null) {
for (LeveledEnchantment enchantment : enchantments)
removeByType(this.enchantments, enchantment);

this.dechantments.addAll(dechantments);
else
} else {
dechantments(dechantments);
}
return this;
}

/**
* Remove all enchantments of the same type (custom or a specific vanilla enchantments).
* @param source - the source set.
* @param other - the other enchantment.
*/
private void removeByType(Set<LeveledEnchantment> source, LeveledEnchantment other) {
for (Iterator<LeveledEnchantment> it = source.iterator(); it.hasNext(); ) {
if (other.sameType(it.next())) {
it.remove();
}
}
}

/**
* Remove enchantments and dechantments that cancel each other.
*/
Expand Down
Expand Up @@ -3,9 +3,19 @@
import org.bukkit.inventory.ItemStack;

public class HideDurabilityEnchanter implements Enchanter {
private final int newDurability;

/**
* Hide the real durability of an item by replacing it with the given durability,
* @param newDurability - the new durability.
*/
public HideDurabilityEnchanter(int newDurability) {
this.newDurability = newDurability;
}

@Override
public ItemStack enchant(ItemStack stack) {
stack.setDurability((short) 0);
stack.setDurability((short) newDurability);
return stack;
}

Expand Down
Expand Up @@ -152,7 +152,7 @@ else if (custom == CustomEnchantment.GLOW)
else if (custom == CustomEnchantment.NO_ATTRIBUTES)
enchanter = new HideAttributesEnchanter();
else if (custom == CustomEnchantment.NO_DURABILITY)
enchanter = new HideDurabilityEnchanter();
enchanter = new HideDurabilityEnchanter(level);
else
throw new IllegalStateException("Invalid custom enchantment: " + custom);
}
Expand All @@ -167,6 +167,18 @@ public int getLevel() {
return level;
}

/**
* Determine if the given leveled enchantment is of the same type.
* @param other - the other enchantment.
* @return TRUE if it is, FALSE otherwise.
*/
public boolean sameType(LeveledEnchantment other) {
if (hasCustomEnchantment())
return other.hasCustomEnchantment() && other.getCustom() == custom;
else
return !other.hasCustomEnchantment() && other.getEnchantment() == enchantment;
}

/**
* Determine if this leveled enchantment represents a custom type.
* @return TRUE if it does, FALSE otherwise.
Expand Down

0 comments on commit 39b8991

Please sign in to comment.