Skip to content

Commit 4d56aaa

Browse files
committed
add illegal firework check
1 parent 6bc1de0 commit 4d56aaa

3 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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:
56+
itemStack.setAmount(0);
57+
break;
58+
case ILLEGAL:
59+
FireworkMeta fireworkMeta = (FireworkMeta) itemStack.getItemMeta();
60+
if (fireworkMeta.getPower() > maxPower)
61+
fireworkMeta.setPower(maxPower);
62+
if (fireworkMeta.getPower() < minPower)
63+
fireworkMeta.setPower(minPower);
64+
if (fireworkMeta.getEffectsSize() > maxEffectsAmount)
65+
fireworkMeta.clearEffects();
66+
itemStack.setItemMeta(fireworkMeta);
67+
break;
68+
}
69+
}
70+
}

shared/src/main/java/me/xginko/aef/utils/permissions/AEFPermission.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public enum AEFPermission {
1111
BYPASS_CMD_WHITELIST("bypass.commandwhitelist", "Bypass command whitelist if enabled", PermissionDefault.OP),
1212
BYPASS_CHAT("bypass.chat", "Bypass for any kind of other chat restrictions", PermissionDefault.OP),
1313
BYPASS_PREVENTION_COMMANDSIGN("bypass.prevention.commandsign", "Bypass commandsign prevention", PermissionDefault.FALSE),
14+
BYPASS_ILLEGAL_FIREWORKS("bypass.illegal.firework", "Bypass illegal firework checks", PermissionDefault.FALSE),
1415
BYPASS_ILLEGAL_POTIONS("bypass.illegal.potions", "Bypass illegal potion checks", PermissionDefault.FALSE),
1516
BYPASS_ILLEGAL_ATTRIBUTES("bypass.illegal.attributes", "Bypass attribute and itemflag checks", PermissionDefault.FALSE),
1617
BYPASS_ILLEGAL_OVERSTACKED("bypass.illegal.overstacked", "Bypass overstacked item checks", PermissionDefault.FALSE),

0 commit comments

Comments
 (0)