From b92c5f31f30225ddf06ee4dbaa248f2fe48372d1 Mon Sep 17 00:00:00 2001 From: TropheusJ Date: Wed, 26 Jul 2023 04:35:07 -0400 Subject: [PATCH] physics entity locking a locked entity cannot be grabbed lock by clicking with a hammer --- .../portalcubed/entity/CorePhysicsEntity.java | 46 +++++++++++++++++++ .../portalcubed/util/HolderComponent.java | 2 +- .../assets/portalcubed/lang/en_us.json | 3 ++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fusionflux/portalcubed/entity/CorePhysicsEntity.java b/src/main/java/com/fusionflux/portalcubed/entity/CorePhysicsEntity.java index 62869b8f..89c7f260 100644 --- a/src/main/java/com/fusionflux/portalcubed/entity/CorePhysicsEntity.java +++ b/src/main/java/com/fusionflux/portalcubed/entity/CorePhysicsEntity.java @@ -14,6 +14,7 @@ import com.fusionflux.portalcubed.util.PortalDirectionUtils; import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.Packet; @@ -23,11 +24,13 @@ import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundSource; import net.minecraft.tags.DamageTypeTags; import net.minecraft.util.Mth; import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.*; import net.minecraft.world.entity.player.Player; @@ -108,6 +111,31 @@ public boolean hurt(DamageSource source, float amount) { return false; } + @Override + @NotNull + protected InteractionResult mobInteract(Player player, InteractionHand hand) { + if (!player.isCreative() || level().isClientSide()) + return InteractionResult.PASS; + ItemStack stack = player.getItemInHand(hand); + if (!stack.is(PortalCubedItems.HAMMER)) + return InteractionResult.PASS; + // toggle + Boolean locked = !entityData.get(LOCKED); + entityData.set(LOCKED, locked); + + if (locked) { + getHolderUUID().ifPresent(uuid -> { + Player holder = level().getPlayerByUUID(uuid); + if (holder != null) { + PortalCubedComponents.HOLDER_COMPONENT.get(holder).stopHolding(); + } + }); + } + + player.displayClientMessage(Component.translatable("portalcubed.physics_entity.locked." + locked), true); + return InteractionResult.SUCCESS; + } + @Override public boolean shouldShowName() { return false; @@ -141,12 +169,14 @@ public void recreateFromPacket(ClientboundAddEntityPacket packet) { private static final EntityDataAccessor> HOLDER_UUID = SynchedEntityData.defineId(CorePhysicsEntity.class, EntityDataSerializers.OPTIONAL_UUID); private static final EntityDataAccessor ON_BUTTON = SynchedEntityData.defineId(CorePhysicsEntity.class, EntityDataSerializers.BOOLEAN); + public static final EntityDataAccessor LOCKED = SynchedEntityData.defineId(CorePhysicsEntity.class, EntityDataSerializers.BOOLEAN); @Override protected void defineSynchedData() { super.defineSynchedData(); this.getEntityData().define(HOLDER_UUID, Optional.empty()); this.getEntityData().define(ON_BUTTON, false); + this.getEntityData().define(LOCKED, false); } public Optional getHolderUUID() { @@ -165,6 +195,10 @@ public void setOnButton(boolean on) { getEntityData().set(ON_BUTTON, on); } + public boolean isLocked() { + return entityData.get(LOCKED); + } + public void setRotYaw(float yaw) { this.yBodyRot = yaw; } @@ -362,6 +396,18 @@ public void move(MoverType movementType, Vec3 movement) { } } + @Override + public void readAdditionalSaveData(CompoundTag compound) { + super.readAdditionalSaveData(compound); + entityData.set(LOCKED, compound.getBoolean("Locked")); + } + + @Override + public void addAdditionalSaveData(CompoundTag compound) { + super.addAdditionalSaveData(compound); + compound.putBoolean("Locked", isLocked()); + } + protected SoundEvent getCollisionSound() { return PortalCubedSounds.CUBE_LOW_HIT_EVENT; // TODO: implement for other physics objects (this requires a lot of assets) } diff --git a/src/main/java/com/fusionflux/portalcubed/util/HolderComponent.java b/src/main/java/com/fusionflux/portalcubed/util/HolderComponent.java index 028510e2..4a6acec5 100644 --- a/src/main/java/com/fusionflux/portalcubed/util/HolderComponent.java +++ b/src/main/java/com/fusionflux/portalcubed/util/HolderComponent.java @@ -45,7 +45,7 @@ public final class HolderComponent implements AutoSyncedComponent { public boolean hold(CorePhysicsEntity entityToHold) { Objects.requireNonNull(entityToHold, "The entity to hold can not be null!"); - if (entityBeingHeld() == null && !entityToHold.fizzling()) { + if (entityBeingHeld() == null && !entityToHold.fizzling() && !entityToHold.isLocked()) { entityToHold.setHolderUUID(Optional.of(owner.getUUID())); this.heldEntity = entityToHold; RayonIntegration.INSTANCE.setNoGravity(heldEntity, true); diff --git a/src/main/resources/assets/portalcubed/lang/en_us.json b/src/main/resources/assets/portalcubed/lang/en_us.json index c416ed27..d3e915e2 100644 --- a/src/main/resources/assets/portalcubed/lang/en_us.json +++ b/src/main/resources/assets/portalcubed/lang/en_us.json @@ -465,6 +465,9 @@ "key.portalcubed.remove_portals" : "Remove Portals", "key.portalcubed.toggle_hidden_blocks" : "Toggle Hidden Blocks", + "portalcubed.physics_entity.locked.true": "Physics entity is now locked", + "portalcubed.physics_entity.locked.false": "Physics entity is now unlocked", + "portalcubed.auto_portal.set_portal_type": "Set portal type to %s", "portalcubed.portal_type.primary": "Primary", "portalcubed.portal_type.secondary": "Secondary",