Skip to content

Commit

Permalink
physics entity locking
Browse files Browse the repository at this point in the history
a locked entity cannot be grabbed
lock by clicking with a hammer
  • Loading branch information
TropheusJ committed Jul 26, 2023
1 parent 78f81a3 commit b92c5f3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -141,12 +169,14 @@ public void recreateFromPacket(ClientboundAddEntityPacket packet) {

private static final EntityDataAccessor<Optional<UUID>> HOLDER_UUID = SynchedEntityData.defineId(CorePhysicsEntity.class, EntityDataSerializers.OPTIONAL_UUID);
private static final EntityDataAccessor<Boolean> ON_BUTTON = SynchedEntityData.defineId(CorePhysicsEntity.class, EntityDataSerializers.BOOLEAN);
public static final EntityDataAccessor<Boolean> 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<UUID> getHolderUUID() {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/portalcubed/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b92c5f3

Please sign in to comment.