|
| 1 | +package net.adeptstack.Blocks.Behaviour.DoorBlock; |
| 2 | + |
| 3 | +import com.simibubi.create.content.contraptions.Contraption; |
| 4 | +import com.simibubi.create.content.contraptions.behaviour.MovementBehaviour; |
| 5 | +import com.simibubi.create.content.contraptions.behaviour.MovementContext; |
| 6 | +import com.simibubi.create.content.contraptions.elevator.ElevatorColumn; |
| 7 | +import com.simibubi.create.content.contraptions.elevator.ElevatorContraption; |
| 8 | +import com.simibubi.create.content.decoration.slidingDoor.DoorControl; |
| 9 | +import com.simibubi.create.content.decoration.slidingDoor.DoorControlBehaviour; |
| 10 | +import com.simibubi.create.content.trains.entity.Carriage; |
| 11 | +import com.simibubi.create.content.trains.entity.CarriageContraptionEntity; |
| 12 | +import com.simibubi.create.content.trains.station.GlobalStation; |
| 13 | +import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour; |
| 14 | +import com.simibubi.create.foundation.utility.animation.LerpedFloat; |
| 15 | +import net.adeptstack.Blocks.Doors.SlidingDoor.TrainSlidingDoorBlock; |
| 16 | +import net.adeptstack.Blocks.Doors.SlidingDoor.TrainSlidingDoorBlockEntity; |
| 17 | +import net.adeptstack.registry.TrainUtilitiesBuilderTransformers; |
| 18 | +import net.minecraft.core.BlockPos; |
| 19 | +import net.minecraft.core.Direction; |
| 20 | +import net.minecraft.resources.ResourceKey; |
| 21 | +import net.minecraft.server.MinecraftServer; |
| 22 | +import net.minecraft.server.level.ServerLevel; |
| 23 | +import net.minecraft.sounds.SoundEvents; |
| 24 | +import net.minecraft.sounds.SoundSource; |
| 25 | +import net.minecraft.world.level.Level; |
| 26 | +import net.minecraft.world.level.block.DoorBlock; |
| 27 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 28 | +import net.minecraft.world.level.block.state.BlockState; |
| 29 | +import net.minecraft.world.level.block.state.properties.DoorHingeSide; |
| 30 | +import net.minecraft.world.level.block.state.properties.DoubleBlockHalf; |
| 31 | +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate; |
| 32 | +import net.minecraft.world.phys.Vec3; |
| 33 | + |
| 34 | +import java.lang.ref.WeakReference; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.TimerTask; |
| 37 | + |
| 38 | +public class DoorBlockMovementBehaviour implements MovementBehaviour { |
| 39 | + |
| 40 | + @Override |
| 41 | + public void tick(MovementContext context) { |
| 42 | + StructureTemplate.StructureBlockInfo structureBlockInfo = context.contraption.getBlocks() |
| 43 | + .get(context.localPos); |
| 44 | + if (structureBlockInfo == null) |
| 45 | + return; |
| 46 | + boolean open = structureBlockInfo.state().getValue(DoorBlock.OPEN); |
| 47 | + |
| 48 | + if (!context.world.isClientSide()) |
| 49 | + tickOpen(context, open); |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + protected void tickOpen(MovementContext context, boolean currentlyOpen) { |
| 54 | + boolean shouldOpen = shouldOpen(context); |
| 55 | + if (!shouldUpdate(context, shouldOpen)) |
| 56 | + return; |
| 57 | + if (currentlyOpen == shouldOpen) |
| 58 | + return; |
| 59 | + |
| 60 | + BlockPos pos = context.localPos; |
| 61 | + Contraption contraption = context.contraption; |
| 62 | + |
| 63 | + StructureTemplate.StructureBlockInfo info = contraption.getBlocks() |
| 64 | + .get(pos); |
| 65 | + if (info == null || !info.state().hasProperty(DoorBlock.OPEN)) |
| 66 | + return; |
| 67 | + |
| 68 | + toggleDoor(pos, contraption, info); |
| 69 | + } |
| 70 | + |
| 71 | + private void toggleDoor(BlockPos pos, Contraption contraption, StructureTemplate.StructureBlockInfo info) { |
| 72 | + BlockState newState = info.state().cycle(DoorBlock.OPEN); |
| 73 | + contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(info.pos(), newState, info.nbt())); |
| 74 | + |
| 75 | + BlockPos otherPos = newState.getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER ? pos.above() : pos.below(); |
| 76 | + info = contraption.getBlocks() |
| 77 | + .get(otherPos); |
| 78 | + if (info != null && info.state().hasProperty(DoorBlock.OPEN)) { |
| 79 | + newState = info.state().cycle(DoorBlock.OPEN); |
| 80 | + contraption.entity.setBlock(otherPos, new StructureTemplate.StructureBlockInfo(info.pos(), newState, info.nbt())); |
| 81 | + contraption.invalidateColliders(); |
| 82 | + |
| 83 | + boolean open = newState.getValue(DoorBlock.OPEN); |
| 84 | + |
| 85 | + if (!open) |
| 86 | + contraption.getContraptionWorld().playLocalSound(pos.getX(), pos.getY(), pos.getZ(), |
| 87 | + SoundEvents.IRON_DOOR_CLOSE, SoundSource.BLOCKS, .125f, 1, false); |
| 88 | + else { |
| 89 | + contraption.getContraptionWorld().playLocalSound(pos.getX(), pos.getY(), pos.getZ(), |
| 90 | + SoundEvents.IRON_DOOR_OPEN, SoundSource.BLOCKS, .125f, 1, false); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + protected boolean shouldUpdate(MovementContext context, boolean shouldOpen) { |
| 96 | + if (context.firstMovement && shouldOpen) |
| 97 | + return false; |
| 98 | + if (!context.data.contains("Open")) { |
| 99 | + context.data.putBoolean("Open", shouldOpen); |
| 100 | + return true; |
| 101 | + } |
| 102 | + boolean wasOpen = context.data.getBoolean("Open"); |
| 103 | + context.data.putBoolean("Open", shouldOpen); |
| 104 | + return wasOpen != shouldOpen; |
| 105 | + } |
| 106 | + |
| 107 | + protected boolean shouldOpen(MovementContext context) { |
| 108 | + if (context.disabled) |
| 109 | + return false; |
| 110 | + Contraption contraption = context.contraption; |
| 111 | + boolean canOpen = context.motion.length() < 1 / 128f && !contraption.entity.isStalled() |
| 112 | + || contraption instanceof ElevatorContraption ec && ec.arrived; |
| 113 | + |
| 114 | + if (!canOpen) { |
| 115 | + context.temporaryData = null; |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + if (context.temporaryData instanceof WeakReference<?> wr && wr.get()instanceof DoorControlBehaviour dcb) |
| 120 | + if (dcb.blockEntity != null && !dcb.blockEntity.isRemoved()) |
| 121 | + return shouldOpenAt(dcb, context); |
| 122 | + |
| 123 | + context.temporaryData = null; |
| 124 | + DoorControlBehaviour doorControls = null; |
| 125 | + |
| 126 | + if (contraption instanceof ElevatorContraption ec) |
| 127 | + doorControls = getElevatorDoorControl(ec, context); |
| 128 | + if (context.contraption.entity instanceof CarriageContraptionEntity cce) |
| 129 | + doorControls = getTrainStationDoorControl(cce, context); |
| 130 | + |
| 131 | + if (doorControls == null) |
| 132 | + return false; |
| 133 | + |
| 134 | + context.temporaryData = new WeakReference<>(doorControls); |
| 135 | + return shouldOpenAt(doorControls, context); |
| 136 | + } |
| 137 | + |
| 138 | + protected boolean shouldOpenAt(DoorControlBehaviour controller, MovementContext context) { |
| 139 | + if (controller.mode == DoorControl.ALL) |
| 140 | + return true; |
| 141 | + if (controller.mode == DoorControl.NONE) |
| 142 | + return false; |
| 143 | + return controller.mode.matches(getDoorFacing(context)); |
| 144 | + } |
| 145 | + |
| 146 | + protected DoorControlBehaviour getElevatorDoorControl(ElevatorContraption ec, MovementContext context) { |
| 147 | + Integer currentTargetY = ec.getCurrentTargetY(context.world); |
| 148 | + if (currentTargetY == null) |
| 149 | + return null; |
| 150 | + ElevatorColumn.ColumnCoords columnCoords = ec.getGlobalColumn(); |
| 151 | + if (columnCoords == null) |
| 152 | + return null; |
| 153 | + ElevatorColumn elevatorColumn = ElevatorColumn.get(context.world, columnCoords); |
| 154 | + if (elevatorColumn == null) |
| 155 | + return null; |
| 156 | + return BlockEntityBehaviour.get(context.world, elevatorColumn.contactAt(currentTargetY), |
| 157 | + DoorControlBehaviour.TYPE); |
| 158 | + } |
| 159 | + |
| 160 | + protected DoorControlBehaviour getTrainStationDoorControl(CarriageContraptionEntity cce, MovementContext context) { |
| 161 | + Carriage carriage = cce.getCarriage(); |
| 162 | + if (carriage == null || carriage.train == null) |
| 163 | + return null; |
| 164 | + GlobalStation currentStation = carriage.train.getCurrentStation(); |
| 165 | + if (currentStation == null) |
| 166 | + return null; |
| 167 | + |
| 168 | + BlockPos stationPos = currentStation.getBlockEntityPos(); |
| 169 | + ResourceKey<Level> stationDim = currentStation.getBlockEntityDimension(); |
| 170 | + MinecraftServer server = context.world.getServer(); |
| 171 | + if (server == null) |
| 172 | + return null; |
| 173 | + ServerLevel stationLevel = server.getLevel(stationDim); |
| 174 | + if (stationLevel == null || !stationLevel.isLoaded(stationPos)) |
| 175 | + return null; |
| 176 | + return BlockEntityBehaviour.get(stationLevel, stationPos, DoorControlBehaviour.TYPE); |
| 177 | + } |
| 178 | + |
| 179 | + protected Direction getDoorFacing(MovementContext context) { |
| 180 | + Direction stateFacing = context.state.getValue(DoorBlock.FACING); |
| 181 | + Direction originalFacing = Direction.get(Direction.AxisDirection.POSITIVE, stateFacing.getAxis()); |
| 182 | + Vec3 centerOfContraption = context.contraption.bounds.getCenter(); |
| 183 | + Vec3 diff = Vec3.atCenterOf(context.localPos) |
| 184 | + .add(Vec3.atLowerCornerOf(stateFacing.getNormal()) |
| 185 | + .scale(-.45f)) |
| 186 | + .subtract(centerOfContraption); |
| 187 | + if (originalFacing.getAxis() |
| 188 | + .choose(diff.x, diff.y, diff.z) < 0) |
| 189 | + originalFacing = originalFacing.getOpposite(); |
| 190 | + |
| 191 | + Vec3 directionVec = Vec3.atLowerCornerOf(originalFacing.getNormal()); |
| 192 | + directionVec = context.rotation.apply(directionVec); |
| 193 | + return Direction.getNearest(directionVec.x, directionVec.y, directionVec.z); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public boolean renderAsNormalBlockEntity() { |
| 198 | + return true; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public boolean mustTickWhileDisabled() { |
| 203 | + return true; |
| 204 | + } |
| 205 | +} |
0 commit comments