Skip to content

Commit 5c68686

Browse files
committed
restructure utils, fixing #284
1 parent 188e3e3 commit 5c68686

File tree

5 files changed

+105
-33
lines changed

5 files changed

+105
-33
lines changed

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/AnarchyExploitFixes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import me.xginko.aef.modules.AEFModule;
99
import me.xginko.aef.utils.KyoriUtil;
1010
import me.xginko.aef.utils.LocaleUtil;
11-
import me.xginko.aef.utils.VersionChecker;
11+
import me.xginko.aef.utils.models.VersionChecker;
1212
import me.xginko.aef.utils.enums.Platform;
1313
import me.xginko.aef.utils.permissions.AEFPermission;
1414
import me.xginko.aef.utils.permissions.PermissionHandler;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package me.xginko.aef.utils;
2+
3+
import com.cryptomorin.xseries.XMaterial;
4+
import me.xginko.aef.utils.reflection.ReflectionUtil;
5+
import org.bukkit.inventory.ItemStack;
6+
import org.bukkit.inventory.meta.BundleMeta;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
import org.jetbrains.annotations.Unmodifiable;
10+
11+
import java.util.List;
12+
13+
public class BundleUtil {
14+
15+
public static final boolean BUNDLES_SUPPPORTED;
16+
17+
static {
18+
BUNDLES_SUPPPORTED
19+
= XMaterial.BUNDLE.isSupported()
20+
&& ReflectionUtil.hasClass("org.bukkit.inventory.meta.BundleMeta");
21+
}
22+
23+
@Nullable
24+
@Unmodifiable
25+
public static List<ItemStack> getBundleContent(@NotNull ItemStack itemStack) {
26+
if (itemStack.getType() != XMaterial.BUNDLE.get()) {
27+
return null;
28+
}
29+
30+
if (DataComponentUtil.COMPONENTS_SUPPORTED) {
31+
return DataComponentUtil.getBundleContent(itemStack);
32+
}
33+
34+
if (itemStack.hasItemMeta()) {
35+
return ((BundleMeta) itemStack.getItemMeta()).getItems();
36+
}
37+
38+
return null;
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package me.xginko.aef.utils;
2+
3+
import io.papermc.paper.datacomponent.DataComponentTypes;
4+
import me.xginko.aef.utils.reflection.ReflectionUtil;
5+
import org.bukkit.inventory.ItemStack;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
import org.jetbrains.annotations.Unmodifiable;
9+
10+
import java.util.List;
11+
12+
public class DataComponentUtil {
13+
14+
public final static boolean COMPONENTS_SUPPORTED;
15+
16+
static {
17+
COMPONENTS_SUPPORTED
18+
= ReflectionUtil.hasClass("io.papermc.paper.datacomponent.DataComponentTypes")
19+
&& ReflectionUtil.hasMethod(ItemStack.class, "getData");
20+
}
21+
22+
@Nullable
23+
@Unmodifiable
24+
@SuppressWarnings("UnstableApiUsage")
25+
public static List<ItemStack> getBundleContent(@NotNull ItemStack itemStack) {
26+
if (itemStack.hasData(DataComponentTypes.BUNDLE_CONTENTS)) {
27+
return itemStack.getData(DataComponentTypes.BUNDLE_CONTENTS).contents();
28+
}
29+
return null;
30+
}
31+
32+
@Nullable
33+
@Unmodifiable
34+
@SuppressWarnings("UnstableApiUsage")
35+
public static List<ItemStack> getContainerContent(@NotNull ItemStack itemStack) {
36+
if (itemStack.hasData(DataComponentTypes.CONTAINER)) {
37+
return itemStack.getData(DataComponentTypes.CONTAINER).contents();
38+
}
39+
return null;
40+
}
41+
42+
}

shared/src/main/java/me/xginko/aef/utils/ItemUtil.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package me.xginko.aef.utils;
22

3-
import com.cryptomorin.xseries.XMaterial;
43
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
54
import de.tr7zw.changeme.nbtapi.NBT;
6-
import io.papermc.paper.datacomponent.DataComponentTypes;
75
import me.xginko.aef.utils.reflection.ReflectionUtil;
86
import net.kyori.adventure.text.Component;
97
import net.kyori.adventure.text.minimessage.MiniMessage;
108
import org.bukkit.Bukkit;
119
import org.bukkit.inventory.EquipmentSlot;
10+
import org.bukkit.inventory.Inventory;
1211
import org.bukkit.inventory.InventoryHolder;
1312
import org.bukkit.inventory.ItemStack;
1413
import org.bukkit.inventory.meta.BlockStateMeta;
1514
import org.bukkit.inventory.meta.BookMeta;
16-
import org.bukkit.inventory.meta.BundleMeta;
1715
import org.bukkit.map.MapView;
1816
import org.jetbrains.annotations.NotNull;
1917
import org.jetbrains.annotations.Nullable;
@@ -26,7 +24,7 @@
2624
public final class ItemUtil {
2725

2826
public static final boolean MAP_SET_TRACKING_POS_AVAILABLE;
29-
private static final boolean BUNDLES_SUPPPORTED, USE_MINIMSG_BOOKMETA, COMPONENTS_SUPPORTED;
27+
private static final boolean USE_MINIMSG_BOOKMETA;
3028
private static final Map<PlayerArmorChangeEvent.SlotType, EquipmentSlot> EQUIPMENT_SLOT_MAP;
3129

3230
static {
@@ -38,14 +36,6 @@ public final class ItemUtil {
3836
&& ReflectionUtil.hasMethod(MiniMessage.class, "miniMessage")
3937
&& ReflectionUtil.hasMethod(BookMeta.class, "pages");
4038

41-
BUNDLES_SUPPPORTED
42-
= XMaterial.BUNDLE.isSupported()
43-
&& ReflectionUtil.hasClass("org.bukkit.inventory.meta.BundleMeta");
44-
45-
COMPONENTS_SUPPORTED
46-
= ReflectionUtil.hasClass("io.papermc.paper.datacomponent.DataComponentTypes")
47-
&& ReflectionUtil.hasMethod(ItemStack.class, "getData");
48-
4939
EQUIPMENT_SLOT_MAP = new EnumMap<>(PlayerArmorChangeEvent.SlotType.class);
5040
EQUIPMENT_SLOT_MAP.put(PlayerArmorChangeEvent.SlotType.HEAD, EquipmentSlot.HEAD);
5141
EQUIPMENT_SLOT_MAP.put(PlayerArmorChangeEvent.SlotType.CHEST, EquipmentSlot.CHEST);
@@ -65,31 +55,32 @@ public static EquipmentSlot getEquipmentSlot(PlayerArmorChangeEvent.SlotType slo
6555
*/
6656
@Nullable
6757
@UnmodifiableView
68-
@SuppressWarnings("UnstableApiUsage")
6958
public static Iterable<ItemStack> getStoredItems(@NotNull ItemStack itemStack) {
70-
if (itemStack.hasItemMeta()) {
71-
if (MaterialUtil.INVENTORY_HOLDERS.get().contains(itemStack.getType())) {
72-
BlockStateMeta blockStateMeta = (BlockStateMeta) itemStack.getItemMeta();
73-
if (blockStateMeta.hasBlockState()) {
74-
return ((InventoryHolder) blockStateMeta.getBlockState()).getInventory();
75-
}
76-
}
59+
Iterable<ItemStack> content;
7760

78-
if (BUNDLES_SUPPPORTED && itemStack.getType() == XMaterial.BUNDLE.get()) {
79-
return ((BundleMeta) itemStack.getItemMeta()).getItems();
80-
}
61+
content = ItemUtil.getShulkerContent(itemStack);
62+
if (content != null) return content;
63+
64+
if (BundleUtil.BUNDLES_SUPPPORTED) {
65+
content = BundleUtil.getBundleContent(itemStack);
66+
if (content != null) return content;
8167
}
8268

83-
if (COMPONENTS_SUPPORTED) {
84-
if (itemStack.hasData(DataComponentTypes.CONTAINER)) {
85-
return itemStack.getData(DataComponentTypes.CONTAINER).contents();
86-
}
69+
if (DataComponentUtil.COMPONENTS_SUPPORTED) {
70+
content = DataComponentUtil.getContainerContent(itemStack);
71+
}
72+
73+
return content;
74+
}
8775

88-
if (itemStack.hasData(DataComponentTypes.BUNDLE_CONTENTS)) {
89-
return itemStack.getData(DataComponentTypes.BUNDLE_CONTENTS).contents();
76+
@Nullable
77+
public static Inventory getShulkerContent(@NotNull ItemStack itemStack) {
78+
if (MaterialUtil.INVENTORY_HOLDERS.get().contains(itemStack.getType()) && itemStack.hasItemMeta()) {
79+
BlockStateMeta blockStateMeta = (BlockStateMeta) itemStack.getItemMeta();
80+
if (blockStateMeta.hasBlockState()) {
81+
return ((InventoryHolder) blockStateMeta.getBlockState()).getInventory();
9082
}
9183
}
92-
9384
return null;
9485
}
9586

shared/src/main/java/me/xginko/aef/utils/VersionChecker.java renamed to shared/src/main/java/me/xginko/aef/utils/models/VersionChecker.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package me.xginko.aef.utils;
1+
package me.xginko.aef.utils.models;
22

33
import com.google.gson.JsonArray;
44
import com.google.gson.JsonParser;
5-
import me.xginko.aef.utils.models.Disableable;
65
import org.bukkit.plugin.java.JavaPlugin;
76

87
import java.io.IOException;

0 commit comments

Comments
 (0)