Skip to content

Commit

Permalink
Mixin cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Nov 18, 2023
1 parent 45ec7be commit 7db87c2
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 72 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ versionForge=47.1.3
versionForgeAutoRenamingTool=1.0.8
versionFabricLoader=2.5.2+0.14.21+1.20.1
versionAccessWidener=2.1.0
versionFabricApi=0.90.7+1.9.32+1.20.1
versionFabricApi=0.90.7+1.9.33+1.20.1
versionMixin=0.12.5+mixin.0.8.5
versionMixinTransmog=0.4.1+1.20.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.logging.LogUtils;
import dev.su5ed.sinytra.connector.loader.ConnectorEarlyLoader;
import dev.su5ed.sinytra.connector.mod.compat.LazyEntityAttributes;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.api.ModInitializer;
Expand Down Expand Up @@ -45,6 +46,7 @@ public static void load() {
ProgressMeter progress = StartupNotificationManager.addProgressBar("[Connector] Loading mods", 0);
try {
loading = true;
LazyEntityAttributes.inject();

// Invoke entry points
EntrypointUtils.invoke("main", ModInitializer.class, ModInitializer::onInitialize);
Expand All @@ -55,6 +57,7 @@ public static void load() {
EntrypointUtils.invoke("server", DedicatedServerModInitializer.class, DedicatedServerModInitializer::onInitializeServer);
}

LazyEntityAttributes.release();
loading = false;
finishedLoading = true;
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
package dev.su5ed.sinytra.connector.mod.compat;

import dev.su5ed.sinytra.connector.mod.mixin.AttributeSupplierAccessor;
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.DefaultAttributes;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.ForgeMod;
import net.minecraftforge.coremod.api.ASMAPI;
import net.minecraftforge.event.entity.EntityAttributeModificationEvent;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
import net.minecraftforge.registries.RegistryObject;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

public class LazyEntityAttributes {
private static final MethodHandle REGISTRY_OBJECT_SET_VALUE = LamdbaExceptionUtils.uncheck(() -> MethodHandles.privateLookupIn(RegistryObject.class, MethodHandles.lookup()).findSetter(RegistryObject.class, "value", Object.class));
private static final List<RegistryObject<Attribute>> ATTRIBUTES = List.of(ForgeMod.SWIM_SPEED, ForgeMod.NAMETAG_DISTANCE, ForgeMod.ENTITY_GRAVITY, ForgeMod.STEP_HEIGHT_ADDITION);
private static final Map<Supplier<? extends Attribute>, PlaceholderAttribute> PLACEHOLDERS = new HashMap<>();

public static Attribute getLazyAttribute(Supplier<? extends Attribute> original) {
return PLACEHOLDERS.computeIfAbsent(original, s -> new PlaceholderAttribute());
public static void inject() {
for (RegistryObject<Attribute> registryObject : ATTRIBUTES) {
Attribute lazyAttribute = PLACEHOLDERS.computeIfAbsent(registryObject, s -> new PlaceholderAttribute());
try {
REGISTRY_OBJECT_SET_VALUE.invoke(registryObject, lazyAttribute);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}

public static void release() {
for (RegistryObject<Attribute> registryObject : ATTRIBUTES) {
try {
REGISTRY_OBJECT_SET_VALUE.invoke(registryObject, null);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}

private static class PlaceholderAttribute extends Attribute {
Expand All @@ -43,7 +67,7 @@ private static void updateAttributeSuppliers(Map<EntityType<? extends LivingEnti
Map<Attribute, AttributeInstance> instances = ObfuscationReflectionHelper.getPrivateValue(AttributeSupplier.class, value, ASMAPI.mapField("f_22241_"));
if (!madeMutable.get()) {
instances = new HashMap<>(instances);
((AttributeSupplierAccessor) value).setInstances(instances);
value.instances = instances;
madeMutable.set(true);
}
Attribute original = originalSupplier.get();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.su5ed.sinytra.connector.mod.mixin.item;
package dev.su5ed.sinytra.connector.mod.mixin;

import net.minecraft.world.entity.monster.piglin.PiglinAi;
import net.minecraft.world.item.ItemStack;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.su5ed.sinytra.connector.mod.mixin.tags;
package dev.su5ed.sinytra.connector.mod.mixin;

import dev.su5ed.sinytra.connector.mod.compat.TagConverter;
import net.minecraft.resources.ResourceLocation;
Expand Down
1 change: 1 addition & 0 deletions src/mod/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public net.minecraft.world.item.MobBucketItem getFishType()Lnet.minecraft.world.
public net.minecraft.world.item.MobBucketItem getEmptySound()Lnet.minecraft.sounds.SoundEvent; # getFishType

public-f net.minecraft.core.IdMapper m_7942_(I)Ljava/lang/Object; # byId
public-f net.minecraft.world.entity.ai.attributes.AttributeSupplier f_22241_ # instances

# Items
public net.minecraft.world.entity.monster.piglin.PiglinAi m_149967_(Lnet/minecraft/world/item/ItemStack;)Z # isBarterCurrency
Expand Down
7 changes: 2 additions & 5 deletions src/mod/resources/connectormod.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
"compatibilityLevel": "JAVA_17",
"plugin": "dev.su5ed.sinytra.connector.mod.ConnectorBootstrap",
"mixins": [
"AttributeSupplierAccessor",
"ForgeHooksMixin",
"LivingEntityMixin",
"PoiTypesMixin",
"PlayerListMixin",
"item.IForgeItemMixin",
"IForgeItemMixin",
"lang.PathPackResourcesAnonMixin",
"lang.PathPackResourcesMixin",
"network.ConnectionTypeMixin",
Expand All @@ -21,10 +19,9 @@
"registries.BuiltInRegistriesMixin",
"registries.ForgeRegistryMixin",
"registries.RegistryDataLoaderMixin",
"tags.TagLoaderMixin"
"TagLoaderMixin"
],
"client": [
"CreativeModeInventoryScreenMixin",
"ForgeHooksClientMixin",
"HumanoidArmorLayerMixin",
"ItemBlockRenderTypesMixin",
Expand Down

0 comments on commit 7db87c2

Please sign in to comment.