diff --git a/arclight-api/src/main/java/io/izzel/arclight/api/Unsafe.java b/arclight-api/src/main/java/io/izzel/arclight/api/Unsafe.java index b2c0c90b6..6d9db882c 100644 --- a/arclight-api/src/main/java/io/izzel/arclight/api/Unsafe.java +++ b/arclight-api/src/main/java/io/izzel/arclight/api/Unsafe.java @@ -27,6 +27,18 @@ public class Unsafe { } } + public static T getStatic(Class cl, String name) { + try { + Unsafe.ensureClassInitialized(cl); + Field field = cl.getDeclaredField(name); + Object materialByNameBase = Unsafe.staticFieldBase(field); + long materialByNameOffset = Unsafe.staticFieldOffset(field); + return (T) Unsafe.getObject(materialByNameBase, materialByNameOffset); + } catch (Exception e) { + return null; + } + } + public static MethodHandles.Lookup lookup() { return lookup; } diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/server/TicketTypeMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/server/TicketTypeMixin.java index 27aca7b2c..ed7e2d399 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/server/TicketTypeMixin.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/server/TicketTypeMixin.java @@ -6,13 +6,14 @@ import org.bukkit.plugin.Plugin; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; -import io.izzel.arclight.common.mod.ArclightConstants; + +import java.util.Comparator; @Mixin(TicketType.class) public abstract class TicketTypeMixin implements TicketTypeBridge { - private static final TicketType PLUGIN = ArclightConstants.PLUGIN; - private static final TicketType PLUGIN_TICKET = ArclightConstants.PLUGIN_TICKET; + private static final TicketType PLUGIN = TicketType.create("plugin", (a, b) -> 0); + private static final TicketType PLUGIN_TICKET = TicketType.create("plugin_ticket", Comparator.comparing(it -> it.getClass().getName())); @Override @Accessor(value = "lifespan") public abstract void bridge$setLifespan(long lifespan); diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/storage/loot/LootParametersMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/storage/loot/LootParametersMixin.java index b37b6973b..337b1e2a2 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/storage/loot/LootParametersMixin.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/storage/loot/LootParametersMixin.java @@ -1,6 +1,6 @@ package io.izzel.arclight.common.mixin.core.world.storage.loot; -import io.izzel.arclight.common.mod.ArclightConstants; +import net.minecraft.util.ResourceLocation; import net.minecraft.world.storage.loot.LootParameter; import net.minecraft.world.storage.loot.LootParameters; import org.spongepowered.asm.mixin.Mixin; @@ -8,5 +8,5 @@ @Mixin(LootParameters.class) public class LootParametersMixin { - private static final LootParameter LOOTING_MOD = ArclightConstants.LOOTING_MOD; + private static final LootParameter LOOTING_MOD = new LootParameter<>(new ResourceLocation("bukkit:looting_mod"));; } diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mod/ArclightConstants.java b/arclight-common/src/main/java/io/izzel/arclight/common/mod/ArclightConstants.java index 64cb06b3b..c2b6de066 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mod/ArclightConstants.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mod/ArclightConstants.java @@ -2,23 +2,22 @@ import com.google.common.collect.ImmutableList; import io.izzel.arclight.api.EnumHelper; -import net.minecraft.util.ResourceLocation; +import io.izzel.arclight.api.Unsafe; import net.minecraft.util.Unit; import net.minecraft.world.server.TicketType; import net.minecraft.world.storage.loot.LootParameter; +import net.minecraft.world.storage.loot.LootParameters; import org.bukkit.TreeType; import org.bukkit.plugin.Plugin; -import java.util.Comparator; - public class ArclightConstants { - public static final TicketType PLUGIN = TicketType.create("plugin", (a, b) -> 0); - public static final TicketType PLUGIN_TICKET = TicketType.create("plugin_ticket", Comparator.comparing(it -> it.getClass().getName())); + public static final TicketType PLUGIN = Unsafe.getStatic(TicketType.class, "PLUGIN"); + public static final TicketType PLUGIN_TICKET = Unsafe.getStatic(TicketType.class, "PLUGIN_TICKET"); public static final TreeType MOD = EnumHelper.addEnum(TreeType.class, "MOD", ImmutableList.of(), ImmutableList.of()); - public static final LootParameter LOOTING_MOD = new LootParameter<>(new ResourceLocation("bukkit:looting_mod")); + public static final LootParameter LOOTING_MOD = Unsafe.getStatic(LootParameters.class, "LOOTING_MOD"); /** * Arclight marker magic value for non-used custom dimension diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java b/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java index e1516d89e..4155b6e52 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java @@ -45,19 +45,19 @@ import java.util.Map; import java.util.Set; -@SuppressWarnings({"unchecked", "ConstantConditions"}) +@SuppressWarnings({"ConstantConditions"}) public class BukkitRegistry { private static final List> MAT_CTOR = ImmutableList.of(int.class); private static final List> ENTITY_CTOR = ImmutableList.of(String.class, Class.class, int.class); private static final List> ENV_CTOR = ImmutableList.of(int.class); - private static final Map BY_NAME = getStatic(Material.class, "BY_NAME"); - private static final Map BLOCK_MATERIAL = getStatic(CraftMagicNumbers.class, "BLOCK_MATERIAL"); - private static final Map ITEM_MATERIAL = getStatic(CraftMagicNumbers.class, "ITEM_MATERIAL"); - private static final Map MATERIAL_ITEM = getStatic(CraftMagicNumbers.class, "MATERIAL_ITEM"); - private static final Map MATERIAL_BLOCK = getStatic(CraftMagicNumbers.class, "MATERIAL_BLOCK"); - private static final Map ENTITY_NAME_MAP = getStatic(EntityType.class, "NAME_MAP"); - private static final Map ENVIRONMENT_MAP = getStatic(World.Environment.class, "lookup"); + private static final Map BY_NAME = Unsafe.getStatic(Material.class, "BY_NAME"); + private static final Map BLOCK_MATERIAL = Unsafe.getStatic(CraftMagicNumbers.class, "BLOCK_MATERIAL"); + private static final Map ITEM_MATERIAL = Unsafe.getStatic(CraftMagicNumbers.class, "ITEM_MATERIAL"); + private static final Map MATERIAL_ITEM = Unsafe.getStatic(CraftMagicNumbers.class, "MATERIAL_ITEM"); + private static final Map MATERIAL_BLOCK = Unsafe.getStatic(CraftMagicNumbers.class, "MATERIAL_BLOCK"); + private static final Map ENTITY_NAME_MAP = Unsafe.getStatic(EntityType.class, "NAME_MAP"); + private static final Map ENVIRONMENT_MAP = Unsafe.getStatic(World.Environment.class, "lookup"); public static void registerAll() { CrashReportExtender.registerCrashCallable("Arclight", () -> new CraftCrashReport().call().toString()); @@ -255,18 +255,6 @@ private static EntityPropertySpec entitySpec(ResourceLocation location) { return ArclightConfig.spec().getCompat().getEntity(location.toString()).orElse(EntityPropertySpec.EMPTY); } - private static T getStatic(Class cl, String name) { - try { - Unsafe.ensureClassInitialized(cl); - Field field = cl.getDeclaredField(name); - Object materialByNameBase = Unsafe.staticFieldBase(field); - long materialByNameOffset = Unsafe.staticFieldOffset(field); - return (T) Unsafe.getObject(materialByNameBase, materialByNameOffset); - } catch (Exception e) { - return null; - } - } - private static void putStatic(Class cl, String name, Object o) { try { Unsafe.ensureClassInitialized(cl);