Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
better
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyMialee committed Oct 7, 2021
1 parent b420741 commit ebb5401
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 58 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +19,11 @@ public IntangibilityChangerItem(Settings settings) {
public TypedActionResult<ItemStack> 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);
Expand Down
58 changes: 16 additions & 42 deletions src/main/java/amymialee/collisionslib/mixin/LivingEntityMixin.java
Original file line number Diff line number Diff line change
@@ -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<Boolean> INTANGIBLE;

public LivingEntityMixin(EntityType<?> type, World world) {
super(type, world);
public abstract class LivingEntityMixin extends Entity {
protected LivingEntityMixin(EntityType<? extends LivingEntity> 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<DefaultAttributeContainer.Builder> info) {
info.getReturnValue().add(CollisionsLib.TANGIBLE);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"environment": "*",
"entrypoints": {
"client": [
"amymialee.collisionslib.client.CollisionsLibClient"
"amymialee.collisionslib.CollisionsLibClient"
],
"main": [
"amymialee.collisionslib.Collisionslib"
"amymialee.collisionslib.CollisionsLib"
]
},
"mixins": [
Expand Down

0 comments on commit ebb5401

Please sign in to comment.