From ebb54015e1c1745bbe3979b5b3936f1b4b3dd20a Mon Sep 17 00:00:00 2001 From: AmyMialee <10999535+AmyMialee@users.noreply.github.com> Date: Thu, 7 Oct 2021 20:24:30 +0100 Subject: [PATCH] better --- gradle.properties | 2 +- ...{Collisionslib.java => CollisionsLib.java} | 12 +++- .../{client => }/CollisionsLibClient.java | 6 +- .../client/LivingEntityAccessor.java | 6 -- .../items/IntangibilityChangerItem.java | 10 +++- .../mixin/LivingEntityMixin.java | 58 +++++-------------- src/main/resources/fabric.mod.json | 4 +- 7 files changed, 40 insertions(+), 58 deletions(-) rename src/main/java/amymialee/collisionslib/{Collisionslib.java => CollisionsLib.java} (62%) rename src/main/java/amymialee/collisionslib/{client => }/CollisionsLibClient.java (56%) delete mode 100644 src/main/java/amymialee/collisionslib/client/LivingEntityAccessor.java diff --git a/gradle.properties b/gradle.properties index 995d18e..fe75ce7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.17.1 yarn_mappings=1.17.1+build.61 loader_version=0.11.7 # Mod Properties -mod_version=1.0-SNAPSHOT +mod_version=1.0.3 maven_group=AmyMialee archives_base_name=collisionslib # Dependencies diff --git a/src/main/java/amymialee/collisionslib/Collisionslib.java b/src/main/java/amymialee/collisionslib/CollisionsLib.java similarity index 62% rename from src/main/java/amymialee/collisionslib/Collisionslib.java rename to src/main/java/amymialee/collisionslib/CollisionsLib.java index 2677446..adb827d 100644 --- a/src/main/java/amymialee/collisionslib/Collisionslib.java +++ b/src/main/java/amymialee/collisionslib/CollisionsLib.java @@ -4,19 +4,29 @@ import amymialee.collisionslib.items.SizeChangerItem; import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.attribute.ClampedEntityAttribute; +import net.minecraft.entity.attribute.EntityAttribute; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.util.Identifier; import net.minecraft.util.Rarity; import net.minecraft.util.registry.Registry; -public class Collisionslib implements ModInitializer { +public class CollisionsLib implements ModInitializer { public static final String MODID = "collisionslib"; public static final Item THINIFIER = new SizeChangerItem(new Item.Settings().group(ItemGroup.TOOLS).maxCount(1).rarity(Rarity.RARE).fireproof()); public static final Item PHASIFIER = new IntangibilityChangerItem(new Item.Settings().group(ItemGroup.TOOLS).maxCount(1).rarity(Rarity.RARE).fireproof()); + public static final EntityAttribute TANGIBLE = new ClampedEntityAttribute( + "attribute." + MODID + '.' + "tangibility", 0, 0, 1).setTracked(true); + + public static boolean getTangibility(final LivingEntity entity) { + return entity.getAttributeInstance(TANGIBLE) != null && (entity.getAttributeInstance(TANGIBLE).getValue() > 0); + } @Override public void onInitialize() { + Registry.register(Registry.ATTRIBUTE, new Identifier(MODID, "tangibility"), TANGIBLE); Registry.register(Registry.ITEM, new Identifier(MODID, "phasifier"), PHASIFIER); if (FabricLoader.getInstance().isModLoaded("pehkui")) { Registry.register(Registry.ITEM, new Identifier(MODID, "thinifier"), THINIFIER); diff --git a/src/main/java/amymialee/collisionslib/client/CollisionsLibClient.java b/src/main/java/amymialee/collisionslib/CollisionsLibClient.java similarity index 56% rename from src/main/java/amymialee/collisionslib/client/CollisionsLibClient.java rename to src/main/java/amymialee/collisionslib/CollisionsLibClient.java index f21cc9d..0a7aa0b 100644 --- a/src/main/java/amymialee/collisionslib/client/CollisionsLibClient.java +++ b/src/main/java/amymialee/collisionslib/CollisionsLibClient.java @@ -1,10 +1,8 @@ -package amymialee.collisionslib.client; +package amymialee.collisionslib; import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -@Environment(EnvType.CLIENT) public class CollisionsLibClient implements ClientModInitializer { @Override public void onInitializeClient() { diff --git a/src/main/java/amymialee/collisionslib/client/LivingEntityAccessor.java b/src/main/java/amymialee/collisionslib/client/LivingEntityAccessor.java deleted file mode 100644 index 6569ac4..0000000 --- a/src/main/java/amymialee/collisionslib/client/LivingEntityAccessor.java +++ /dev/null @@ -1,6 +0,0 @@ -package amymialee.collisionslib.client; - -public interface LivingEntityAccessor { - void setIntangible(boolean value); - boolean isIntangible(); -} diff --git a/src/main/java/amymialee/collisionslib/items/IntangibilityChangerItem.java b/src/main/java/amymialee/collisionslib/items/IntangibilityChangerItem.java index 9831989..29907f1 100644 --- a/src/main/java/amymialee/collisionslib/items/IntangibilityChangerItem.java +++ b/src/main/java/amymialee/collisionslib/items/IntangibilityChangerItem.java @@ -1,6 +1,8 @@ package amymialee.collisionslib.items; -import amymialee.collisionslib.client.LivingEntityAccessor; +import amymialee.collisionslib.CollisionsLib; +import net.minecraft.entity.attribute.AttributeContainer; +import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -17,7 +19,11 @@ public IntangibilityChangerItem(Settings settings) { public TypedActionResult use(World world, PlayerEntity user, Hand hand) { ItemStack itemStack = user.getStackInHand(hand); if (!world.isClient) { - ((LivingEntityAccessor) user).setIntangible(!((LivingEntityAccessor) user).isIntangible()); + if (user.getAttributeInstance(CollisionsLib.TANGIBLE).getBaseValue() == 0) { + user.getAttributeInstance(CollisionsLib.TANGIBLE).setBaseValue(1); + } else { + user.getAttributeInstance(CollisionsLib.TANGIBLE).setBaseValue(0); + } return TypedActionResult.success(itemStack, world.isClient()); } return super.use(world, user, hand); diff --git a/src/main/java/amymialee/collisionslib/mixin/LivingEntityMixin.java b/src/main/java/amymialee/collisionslib/mixin/LivingEntityMixin.java index 3c67616..13248e4 100644 --- a/src/main/java/amymialee/collisionslib/mixin/LivingEntityMixin.java +++ b/src/main/java/amymialee/collisionslib/mixin/LivingEntityMixin.java @@ -1,74 +1,48 @@ package amymialee.collisionslib.mixin; -import amymialee.collisionslib.client.LivingEntityAccessor; +import amymialee.collisionslib.CollisionsLib; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; -import net.minecraft.entity.data.DataTracker; -import net.minecraft.entity.data.TrackedData; -import net.minecraft.entity.data.TrackedDataHandlerRegistry; +import net.minecraft.entity.attribute.DefaultAttributeContainer; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.vehicle.BoatEntity; -import net.minecraft.nbt.NbtCompound; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(LivingEntity.class) -public abstract class LivingEntityMixin extends Entity implements LivingEntityAccessor { - @Unique private static final TrackedData INTANGIBLE; - - public LivingEntityMixin(EntityType type, World world) { - super(type, world); +public abstract class LivingEntityMixin extends Entity { + protected LivingEntityMixin(EntityType entityType, World world) { + super(entityType, world); } @Inject(method = "pushAway", at = @At("HEAD"), cancellable = true) protected void pushAway(Entity entity, CallbackInfo ci) { - if (entity instanceof LivingEntity && ((LivingEntityAccessor) entity).isIntangible()) { + if (entity instanceof LivingEntity && (CollisionsLib.getTangibility(((LivingEntity) ((Object) this))) || CollisionsLib.getTangibility((LivingEntity) entity))) { ci.cancel(); } } @Unique public boolean collidesWith(Entity other) { - return !this.isIntangible() && BoatEntity.canCollide(this, other); + if (other instanceof LivingEntity) { + return (this.isCollidable() && other.isCollidable()); + } + return other.isCollidable() && !this.isConnectedThroughVehicle(other); } @Unique public boolean isCollidable() { - return !isIntangible(); - } - - @Inject(method = "initDataTracker", at = @At("TAIL")) - protected void initDataTracker(CallbackInfo ci) { - this.getDataTracker().startTracking(INTANGIBLE, false); - } - - @Inject(method = "writeCustomDataToNbt", at = @At("HEAD")) - public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) { - nbt.putBoolean("intangible", this.getDataTracker().get(INTANGIBLE)); - } - - @Inject(method = "readCustomDataFromNbt", at = @At("TAIL")) - public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) { - this.getDataTracker().set(INTANGIBLE, nbt.getBoolean("intangible")); - } - - @Unique - @Override - public boolean isIntangible() { - return this.getDataTracker().get(INTANGIBLE); - } - - @Unique - @Override - public void setIntangible(boolean intangible) { - this.getDataTracker().set(INTANGIBLE, intangible); + return CollisionsLib.getTangibility(((LivingEntity) ((Object) this))); } - static { - INTANGIBLE = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.BOOLEAN); + @Inject(method = "createLivingAttributes()Lnet/minecraft/entity/attribute/DefaultAttributeContainer$Builder;", at = @At("RETURN")) + private static void addAttributes(final CallbackInfoReturnable info) { + info.getReturnValue().add(CollisionsLib.TANGIBLE); } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1315343..f1f0b76 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -16,10 +16,10 @@ "environment": "*", "entrypoints": { "client": [ - "amymialee.collisionslib.client.CollisionsLibClient" + "amymialee.collisionslib.CollisionsLibClient" ], "main": [ - "amymialee.collisionslib.Collisionslib" + "amymialee.collisionslib.CollisionsLib" ] }, "mixins": [