Skip to content

Commit

Permalink
Move mutability to apply to spawn eggs only
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEpicBlock committed Nov 17, 2024
1 parent 744dfe8 commit 26d3667
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.modfest.fireblanket.mixin.accessor;

import net.minecraft.entity.decoration.ArmorStandEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ArmorStandEntity.class)
public interface ArmorStandEntityAccessor {
@Accessor
void setDisabledSlots(int d);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.modfest.fireblanket.mixin.accessor;

import net.minecraft.entity.decoration.ItemFrameEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ItemFrameEntity.class)
public interface ItemFrameAccessor {
@Accessor
void setFixed(boolean v);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.modfest.fireblanket.mixin.entity_immutability;

import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.entity.decoration.AbstractDecorationEntity;
import net.minecraft.item.DecorationItem;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.util.ActionResult;
import net.modfest.fireblanket.Fireblanket;
import net.modfest.fireblanket.util.ImmutableEntities;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(DecorationItem.class)
public class MixinDecorationItem {
@Inject(method = "useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getOrDefault(Lnet/minecraft/component/ComponentType;Ljava/lang/Object;)Ljava/lang/Object;"))
private void onInitSpawnedEntity(ItemUsageContext context, CallbackInfoReturnable<ActionResult> cir, @Local AbstractDecorationEntity entity) {
if (context.getWorld().getGameRules().getBoolean(Fireblanket.NEW_ENTITIES_IMMUTABLE)) {
ImmutableEntities.makeImmutable(entity);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.modfest.fireblanket.mixin.entity_immutability;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.modfest.fireblanket.Fireblanket;
import net.modfest.fireblanket.util.ImmutableEntities;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.function.Consumer;

@Mixin(EntityType.class)
public class MixinEntityType {
@WrapOperation(method = "create(Lnet/minecraft/server/world/ServerWorld;Ljava/util/function/Consumer;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/entity/SpawnReason;ZZ)Lnet/minecraft/entity/Entity;", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityType;create(Lnet/minecraft/world/World;)Lnet/minecraft/entity/Entity;"))
private Entity afterCreated(EntityType<?> instance, World world, Operation<Entity> original, ServerWorld world2, @Nullable Consumer<Entity> afterConsumer, BlockPos pos, SpawnReason reason, boolean alignPosition, boolean invertY) {
var entity = original.call(instance, world);
if (world.getGameRules().getBoolean(Fireblanket.NEW_ENTITIES_IMMUTABLE) && reason == SpawnReason.SPAWN_EGG) {
if (entity != null) {
ImmutableEntities.makeImmutable(entity);
}
}
return entity;
}
}

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/net/modfest/fireblanket/util/ImmutableEntities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.modfest.fireblanket.util;

import net.minecraft.entity.Entity;
import net.modfest.fireblanket.mixin.accessor.ArmorStandEntityAccessor;
import net.modfest.fireblanket.mixin.accessor.ItemFrameAccessor;
import net.modfest.fireblanket.mixinsupport.ImmmovableLivingEntity;

public class ImmutableEntities {
public static void makeImmutable(Entity entity) {
// Set invulnerability
entity.setInvulnerable(true);

if (entity instanceof ArmorStandEntityAccessor ae) {
// Disable all slots
ae.setDisabledSlots(4144959);
// Disable movement (prevents abuse of fishing rods)
if (entity instanceof ImmmovableLivingEntity im) {
im.setNoMovement(true);
}
}

if (entity instanceof ItemFrameAccessor ie) {
// Make item frames fixed
ie.setFixed(true);
}
}
}
6 changes: 4 additions & 2 deletions src/main/resources/fireblanket.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"compatibilityLevel": "JAVA_17",
"plugin": "net.modfest.fireblanket.FireblanketMixin",
"mixins": [
"accessor.ArmorStandEntityAccessor",
"accessor.BlockEntityTypeAccessor",
"accessor.ClientConnectionAccessor",
"accessor.EntityTypeAccessor",
"accessor.ItemFrameAccessor",
"accessor.ServerChunkManagerAccessor",
"accessor.ServerLoginNetworkHandlerAccessor",
"adventure_fix.MixinItemStack",
Expand All @@ -17,8 +19,8 @@
"be_sync.MixinChunkHolder",
"block.MixinCommandBlock",
"block_format.MixinChunkSection",
"entity_immutability.MixinArmorStand",
"entity_immutability.MixinItemFrame",
"entity_immutability.MixinDecorationItem",
"entity_immutability.MixinEntityType",
"entity_ticking.MixinDebugStickItem",
"entity_ticking.MixinEntity",
"entity_ticking.MixinItemGroups",
Expand Down

0 comments on commit 26d3667

Please sign in to comment.