-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement enchant item action (#1420)
Signed-off-by: Pablo Herrera <pabloherrerapalacio@gmail.com>
- Loading branch information
1 parent
2c79609
commit ea6d91f
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
core/src/main/java/tc/oc/pgm/action/actions/EnchantItemAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package tc.oc.pgm.action.actions; | ||
|
||
import java.util.Objects; | ||
import org.bukkit.enchantments.Enchantment; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.PlayerInventory; | ||
import tc.oc.pgm.api.player.MatchPlayer; | ||
import tc.oc.pgm.util.inventory.ItemMatcher; | ||
import tc.oc.pgm.util.math.Formula; | ||
|
||
public class EnchantItemAction extends AbstractAction<MatchPlayer> { | ||
|
||
private final ItemMatcher matcher; | ||
private final Enchantment enchant; | ||
private final Formula<MatchPlayer> level; | ||
|
||
public EnchantItemAction(ItemMatcher matcher, Enchantment enchant, Formula<MatchPlayer> level) { | ||
super(MatchPlayer.class); | ||
this.matcher = matcher; | ||
this.enchant = enchant; | ||
this.level = level; | ||
} | ||
|
||
@Override | ||
public void trigger(MatchPlayer player) { | ||
PlayerInventory inv = Objects.requireNonNull(player.getInventory()); | ||
|
||
int level = Math.max(0, (int) this.level.applyAsDouble(player)); | ||
|
||
for (ItemStack current : inv.getArmorContents()) { | ||
if (current != null && matcher.matches(current)) enchant(current, level); | ||
} | ||
for (int i = 0; i < inv.getSize(); i++) { | ||
ItemStack current = inv.getItem(i); | ||
if (current != null && matcher.matches(current)) enchant(current, level); | ||
} | ||
} | ||
|
||
private void enchant(ItemStack current, int level) { | ||
if (current.getEnchantmentLevel(enchant) != level) { | ||
if (level == 0) current.removeEnchantment(enchant); | ||
else current.addUnsafeEnchantment(enchant, level); | ||
} | ||
} | ||
} |