From 111e69d189d15b31fa21193598a20f947728be54 Mon Sep 17 00:00:00 2001 From: LordGrimmauld Date: Sun, 9 Aug 2020 12:40:33 +0200 Subject: [PATCH] bell movement behaviour --- .../create/AllMovementBehaviours.java | 6 +++- .../actors/BellMovementBehaviour.java | 32 +++++++++++++++++++ .../structureMovement/Contraption.java | 7 ++-- .../structureMovement/ContraptionEntity.java | 7 ++-- .../ContraptionRenderer.java | 11 ++++--- .../structureMovement/MovementBehaviour.java | 7 ++++ 6 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/components/actors/BellMovementBehaviour.java diff --git a/src/main/java/com/simibubi/create/AllMovementBehaviours.java b/src/main/java/com/simibubi/create/AllMovementBehaviours.java index 924f1c5dd..296ae2f6d 100644 --- a/src/main/java/com/simibubi/create/AllMovementBehaviours.java +++ b/src/main/java/com/simibubi/create/AllMovementBehaviours.java @@ -1,8 +1,10 @@ package com.simibubi.create; +import com.simibubi.create.content.contraptions.components.actors.BellMovementBehaviour; import com.simibubi.create.content.contraptions.components.structureMovement.MovementBehaviour; import com.tterrag.registrate.util.nullness.NonNullConsumer; import net.minecraft.block.Block; +import net.minecraft.block.Blocks; import net.minecraft.util.ResourceLocation; import javax.annotation.Nullable; @@ -34,5 +36,7 @@ public class AllMovementBehaviours { return b -> addMovementBehaviour(b.getRegistryName(), movementBehaviour); } - static void register() {} + static void register() { + addMovementBehaviour(Blocks.BELL.getRegistryName(), new BellMovementBehaviour()); + } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/BellMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/BellMovementBehaviour.java new file mode 100644 index 000000000..6b93996d4 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/BellMovementBehaviour.java @@ -0,0 +1,32 @@ +package com.simibubi.create.content.contraptions.components.actors; + +import com.simibubi.create.content.contraptions.components.structureMovement.MovementBehaviour; +import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; + +import net.minecraft.util.SoundCategory; +import net.minecraft.util.SoundEvents; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; + +public class BellMovementBehaviour extends MovementBehaviour { + @Override + public boolean hasSpecialMovementRenderer() { + return false; + } + + @Override + public void onSpeedChanged(MovementContext context, Vec3d oldMotion, Vec3d motion) { + double dotProduct = oldMotion.dotProduct(motion); + + if (dotProduct <= 0 && (context.relativeMotion.length() != 0 || context.rotation.length() == 0) + || context.firstMovement) + context.world.playSound(null, new BlockPos(context.position), SoundEvents.BLOCK_BELL_USE, + SoundCategory.BLOCKS, 2.0F, 1.0F); + } + + @Override + public void stopMoving(MovementContext context) { + context.world.playSound(null, new BlockPos(context.position), SoundEvents.BLOCK_BELL_USE, SoundCategory.BLOCKS, + 2.0F, 1.0F); + } +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java index 42caef70d..236c6d5c3 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java @@ -464,7 +464,8 @@ public abstract class Contraption { else renderOrder.add(0, info.pos); CompoundNBT tag = info.nbt; - if (tag == null || AllMovementBehaviours.hasMovementBehaviour(block)) + MovementBehaviour movementBehaviour = AllMovementBehaviours.getMovementBehaviour(block); + if (tag == null || movementBehaviour == null || movementBehaviour.hasSpecialMovementRenderer()) return; tag.putInt("x", info.pos.getX()); @@ -472,6 +473,8 @@ public abstract class Contraption { tag.putInt("z", info.pos.getZ()); TileEntity te = TileEntity.create(tag); + if (te == null) + return; te.setLocation(new WrappedWorld(world) { @Override @@ -816,7 +819,7 @@ public abstract class Contraption { public Map getSeatMapping() { return seatMapping; } - + public void setSeatMapping(Map seatMapping) { this.seatMapping = seatMapping; } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntity.java index f8b8680b5..f0d47fc0f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntity.java @@ -459,6 +459,7 @@ public class ContraptionEntity extends Entity implements IEntityAdditionalSpawnD boolean newPosVisited = false; BlockPos gridPosition = new BlockPos(actorPosition); + Vec3d oldMotion = context.motion; if (!context.stall) { Vec3d previousPosition = context.position; @@ -500,6 +501,8 @@ public class ContraptionEntity extends Entity implements IEntityAdditionalSpawnD actor.visitNewPosition(context, gridPosition); context.firstMovement = false; } + if (!oldMotion.equals(context.motion)) + actor.onSpeedChanged(context, oldMotion, context.motion); actor.tick(context); contraption.stalled |= context.stall; } @@ -615,7 +618,7 @@ public class ContraptionEntity extends Entity implements IEntityAdditionalSpawnD } if (compound.contains("Controller")) controllerPos = NBTUtil.readBlockPos(compound.getCompound("Controller")); - + if (compound.contains("OnCoupling")) { setCouplingId(NBTUtil.readUniqueId(compound.getCompound("OnCoupling"))); setCoupledCart(NBTUtil.readUniqueId(compound.getCompound("CoupledCart"))); @@ -661,7 +664,7 @@ public class ContraptionEntity extends Entity implements IEntityAdditionalSpawnD compound.putFloat("InitialAngle", initialAngle); compound.putBoolean("Stalled", isStalled()); compound.putBoolean("Initialized", initialized); - + if (getCouplingId() != null) { compound.put("OnCoupling", NBTUtil.writeUniqueId(getCouplingId())); compound.put("CoupledCart", NBTUtil.writeUniqueId(getCoupledCart())); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionRenderer.java index ead238993..92c2ba6c5 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionRenderer.java @@ -106,12 +106,13 @@ public class ContraptionRenderer { for (MatrixStack m : matrixStacks) { m.push(); MatrixStacker.of(m) - .translate(blockInfo.pos); + .translate(blockInfo.pos); } - - Contraption.getMovement(blockInfo.state) - .renderInContraption(context, ms, msLocal, buffer); - + + MovementBehaviour movementBehaviour = Contraption.getMovement(blockInfo.state); + if (movementBehaviour != null) + movementBehaviour.renderInContraption(context, ms, msLocal, buffer); + for (MatrixStack m : matrixStacks) m.pop(); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java index 1be0ada51..bf25055fe 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java @@ -47,8 +47,15 @@ public abstract class MovementBehaviour { } + public boolean hasSpecialMovementRenderer() { + return true; + } + @OnlyIn(Dist.CLIENT) public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {} + public void onSpeedChanged(MovementContext context, Vec3d oldMotion, Vec3d motion) { + + } }