|
| 1 | +package me.xginko.aef.modules.illegals.items; |
| 2 | + |
| 3 | +import com.cryptomorin.xseries.XMaterial; |
| 4 | +import me.xginko.aef.utils.ItemUtil; |
| 5 | +import me.xginko.aef.utils.enums.IllegalHandling; |
| 6 | +import me.xginko.aef.utils.enums.ItemLegality; |
| 7 | +import me.xginko.aef.utils.permissions.AEFPermission; |
| 8 | +import org.bukkit.inventory.ItemStack; |
| 9 | +import org.bukkit.inventory.meta.FireworkMeta; |
| 10 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | + |
| 13 | +public class IllegalFirework extends IllegalItemModule { |
| 14 | + |
| 15 | + private final int maxPower, minPower, maxEffectsAmount; |
| 16 | + private final boolean checkStored; |
| 17 | + |
| 18 | + public IllegalFirework() { |
| 19 | + super("illegals.firework", false, AEFPermission.BYPASS_ILLEGAL_FIREWORKS, |
| 20 | + "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_FIREWORKS.node() + "\n" + |
| 21 | + "Prevents usage of or reverts firework items with illegal attributes."); |
| 22 | + this.checkStored = config.getBoolean(configPath + ".check-stored-items", false); |
| 23 | + this.maxPower = config.getInt(configPath + ".max-power", 3); |
| 24 | + this.minPower = config.getInt(configPath + ".min-power", 1); |
| 25 | + this.maxEffectsAmount = config.getInt(configPath + ".max-effect-count", 7); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public @NotNull ItemLegality legalityOf(@Nullable ItemStack itemStack) { |
| 30 | + if (itemStack == null || itemStack.getType() != XMaterial.FIREWORK_ROCKET.get() || !itemStack.hasItemMeta()) { |
| 31 | + return ItemLegality.LEGAL; |
| 32 | + } |
| 33 | + |
| 34 | + FireworkMeta fireworkMeta = (FireworkMeta) itemStack.getItemMeta(); |
| 35 | + if (fireworkMeta.getPower() > maxPower || fireworkMeta.getPower() < minPower) { |
| 36 | + return ItemLegality.ILLEGAL; |
| 37 | + } |
| 38 | + |
| 39 | + if (fireworkMeta.getEffectsSize() > maxEffectsAmount) { |
| 40 | + return ItemLegality.ILLEGAL; |
| 41 | + } |
| 42 | + |
| 43 | + if (checkStored) { |
| 44 | + return legalityOf(ItemUtil.getStoredItems(itemStack)); |
| 45 | + } |
| 46 | + |
| 47 | + return ItemLegality.LEGAL; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void handleItem(ItemStack itemStack, ItemLegality legality) { |
| 52 | + if (illegalHandling == IllegalHandling.PREVENT_USE_ONLY) return; // We are cancelling the action in the super class |
| 53 | + |
| 54 | + switch (legality) { |
| 55 | + case CONTAINS_ILLEGAL -> itemStack.setAmount(0); |
| 56 | + case ILLEGAL -> { |
| 57 | + FireworkMeta fireworkMeta = (FireworkMeta) itemStack.getItemMeta(); |
| 58 | + if (fireworkMeta.getPower() > maxPower) |
| 59 | + fireworkMeta.setPower(maxPower); |
| 60 | + if (fireworkMeta.getPower() < minPower) |
| 61 | + fireworkMeta.setPower(minPower); |
| 62 | + if (fireworkMeta.getEffectsSize() > maxEffectsAmount) |
| 63 | + fireworkMeta.clearEffects(); |
| 64 | + itemStack.setItemMeta(fireworkMeta); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments