Skip to content

Commit 0750329

Browse files
committed
Merge remote-tracking branch 'origin/1.20.1.dev'
2 parents 9a34901 + 00ed0a0 commit 0750329

File tree

23 files changed

+765
-77
lines changed

23 files changed

+765
-77
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package net.adeptstack.Blocks.Behaviour.DoorBlock;
2+
3+
import com.simibubi.create.content.contraptions.Contraption;
4+
import com.simibubi.create.content.contraptions.behaviour.SimpleBlockMovingInteraction;
5+
import net.adeptstack.Blocks.Doors.SlidingDoor.TrainSlidingDoorBlock;
6+
import net.minecraft.core.BlockPos;
7+
import net.minecraft.core.Direction;
8+
import net.minecraft.sounds.SoundEvent;
9+
import net.minecraft.sounds.SoundEvents;
10+
import net.minecraft.world.InteractionHand;
11+
import net.minecraft.world.entity.player.Player;
12+
import net.minecraft.world.level.block.DoorBlock;
13+
import net.minecraft.world.level.block.state.BlockState;
14+
import net.minecraft.world.level.block.state.properties.DoorHingeSide;
15+
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
16+
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
17+
18+
public class DoorBlockMovingInteraction extends SimpleBlockMovingInteraction {
19+
20+
@Override
21+
protected BlockState handle(Player player, Contraption contraption, BlockPos pos, BlockState currentState) {
22+
if (!(currentState.getBlock() instanceof DoorBlock))
23+
return currentState;
24+
25+
boolean trainDoor = currentState.getBlock() instanceof TrainSlidingDoorBlock;
26+
SoundEvent sound = currentState.getValue(DoorBlock.OPEN) ? trainDoor ? null : SoundEvents.WOODEN_DOOR_CLOSE
27+
: trainDoor ? SoundEvents.IRON_DOOR_OPEN : SoundEvents.WOODEN_DOOR_OPEN;
28+
29+
BlockPos otherPos = currentState.getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER ? pos.above() : pos.below();
30+
StructureTemplate.StructureBlockInfo info = contraption.getBlocks()
31+
.get(otherPos);
32+
if (info.state().hasProperty(DoorBlock.OPEN)) {
33+
BlockState newState = info.state().cycle(DoorBlock.OPEN);
34+
setContraptionBlockData(contraption.entity, otherPos, new StructureTemplate.StructureBlockInfo(info.pos(), newState, info.nbt()));
35+
}
36+
37+
currentState = currentState.cycle(DoorBlock.OPEN);
38+
39+
if (player != null) {
40+
41+
if (trainDoor) {
42+
DoorHingeSide hinge = currentState.getValue(TrainSlidingDoorBlock.HINGE);
43+
Direction facing = currentState.getValue(TrainSlidingDoorBlock.FACING);
44+
BlockPos doublePos =
45+
pos.relative(hinge == DoorHingeSide.LEFT ? facing.getClockWise() : facing.getCounterClockWise());
46+
StructureTemplate.StructureBlockInfo doubleInfo = contraption.getBlocks()
47+
.get(doublePos);
48+
if (doubleInfo != null && TrainSlidingDoorBlock.isDoubleDoor(currentState, hinge, facing, doubleInfo.state())) {
49+
handlePlayerInteraction(null, InteractionHand.MAIN_HAND, doublePos, contraption.entity);
50+
}
51+
}
52+
else {
53+
float pitch = player.level().random.nextFloat() * 0.1F + 0.9F;
54+
if (sound != null)
55+
56+
playSound(player, sound, pitch);
57+
}
58+
}
59+
60+
return currentState;
61+
}
62+
63+
@Override
64+
protected boolean updateColliders() {
65+
return true;
66+
}
67+
}

common/src/main/java/net/adeptstack/Core/Client/ClientWrapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import net.minecraft.resources.ResourceLocation;
1818
import net.minecraft.world.level.block.state.BlockState;
1919

20+
import java.awt.desktop.SystemSleepEvent;
21+
2022
import static net.adeptstack.Main.MOD_ID;
2123

2224
public class ClientWrapper {

common/src/main/java/net/adeptstack/Core/Network/Packages/ChangeDoorSoundPackage.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,18 @@ public void apply(Supplier<NetworkManager.PacketContext> contextSupplier) {
8282
}
8383

8484
state2 = state2.setValue(TrainSlidingDoorBlock.DOOR_SOUND, door_sound);
85-
state3 = state3.setValue(TrainSlidingDoorBlock.DOOR_SOUND, door_sound);
86-
state4 = state4.setValue(TrainSlidingDoorBlock.DOOR_SOUND, door_sound);
85+
if (pos3 != null && pos4 != null && state3 != null && state4 != null) {
86+
state3 = state3.setValue(TrainSlidingDoorBlock.DOOR_SOUND, door_sound);
87+
state4 = state4.setValue(TrainSlidingDoorBlock.DOOR_SOUND, door_sound);
88+
}
8789
}
8890
}
8991
contextSupplier.get().getPlayer().level().setBlockAndUpdate(pos, state);
9092
if (pos2 != null && state2 != null) {
9193
contextSupplier.get().getPlayer().level().setBlockAndUpdate(pos2, state2);
9294
}
9395

94-
if (pos3 != null & pos4 != null && state3 != null && state4 != null) {
96+
if (pos3 != null && pos4 != null && state3 != null && state4 != null) {
9597
contextSupplier.get().getPlayer().level().setBlockAndUpdate(pos3, state3);
9698
contextSupplier.get().getPlayer().level().setBlockAndUpdate(pos4, state4);
9799
}

common/src/main/java/net/adeptstack/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import com.simibubi.create.foundation.item.TooltipHelper;
77
import com.simibubi.create.foundation.item.TooltipModifier;
88
import com.tterrag.registrate.providers.ProviderType;
9+
import dev.architectury.platform.Platform;
910
import net.adeptstack.Blocks.Doors.SlidingDoor.TrainSlidingDoorBlock;
1011
import net.adeptstack.Core.Network.ModNetwork;
1112
import net.adeptstack.registry.*;
13+
import net.fabricmc.api.EnvType;
1214
import net.minecraft.data.DataGenerator;
1315
import net.minecraft.data.DataProvider;
1416
import net.minecraft.resources.ResourceLocation;
@@ -28,7 +30,9 @@ public static void init() {
2830
ModTabs.CREATIVE_MODE_TABS.register();
2931
ModSounds.SOUND_EVENTS.register();
3032
ModNetwork.init();
31-
ModPartialModels.init();
33+
if (Platform.getEnv() == EnvType.CLIENT) {
34+
ModPartialModels.init();
35+
}
3236
ModTags.register();
3337
}
3438

common/src/main/java/net/adeptstack/registry/ModBlocks.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.adeptstack.registry;
22

3+
import com.simibubi.create.content.decoration.palettes.GlassPaneBlock;
34
import com.tterrag.registrate.util.entry.BlockEntry;
45
import net.adeptstack.Blocks.Doors.SlidingDoor.TrainSlidingDoorBlock;
56
import net.adeptstack.Blocks.PanelBlocks.IsoWallBlock;
@@ -33,6 +34,9 @@ public class ModBlocks {
3334
public static final BlockEntry<GlassBlock> FRAMELESS_GLASS =
3435
TrainUtilitiesBuilderTransformers.GlassBlock("frameless_glass", MapColor.NONE);
3536

37+
public static final BlockEntry<GlassPaneBlock> FRAMELESS_GLASS_PANE =
38+
TrainUtilitiesBuilderTransformers.GlassPaneBlock("frameless_glass_pane", MapColor.NONE);
39+
3640
//ic(e) line blocks
3741
public static final BlockEntry<LineBlock> TOP_REDLINE_BLOCK =
3842
TrainUtilitiesBuilderTransformers.LineBlock("top_redline_block", MapColor.TERRACOTTA_WHITE);

common/src/main/java/net/adeptstack/registry/TrainUtilitiesBuilderTransformers.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.simibubi.create.AllBlocks;
44
import com.simibubi.create.AllTags;
5+
import com.simibubi.create.content.decoration.palettes.GlassPaneBlock;
56
import com.simibubi.create.content.decoration.slidingDoor.SlidingDoorBlock;
67
import com.simibubi.create.foundation.data.AssetLookup;
78
import com.tterrag.registrate.builders.BlockBuilder;
89
import com.tterrag.registrate.util.entry.BlockEntry;
910
import com.tterrag.registrate.util.nullness.NonNullUnaryOperator;
1011
import io.github.fabricators_of_create.porting_lib.models.generators.ModelFile;
12+
import net.adeptstack.Blocks.Behaviour.DoorBlock.DoorBlockMovementBehaviour;
13+
import net.adeptstack.Blocks.Behaviour.DoorBlock.DoorBlockMovingInteraction;
1114
import net.adeptstack.Blocks.Behaviour.SlidingDoor.TrainSlidingDoorMovementBehaviour;
1215
import net.adeptstack.Blocks.Behaviour.SlidingDoor.TrainSlidingDoorMovingInteraction;
1316
import net.adeptstack.Blocks.PanelBlocks.IsoWallBlock;
@@ -99,6 +102,21 @@ public static BlockEntry<GlassBlock> GlassBlock(String id, MapColor color) {
99102
.register();
100103
}
101104

105+
public static BlockEntry<GlassPaneBlock> GlassPaneBlock(String id, MapColor color) {
106+
return REGISTRATE
107+
.block(id, GlassPaneBlock::new)
108+
.initialProperties(() -> Blocks.GLASS_PANE)
109+
.properties(p -> p.sound(SoundType.GLASS).mapColor(color))
110+
.addLayer(() -> RenderType::translucent)
111+
.transform(pickaxeOnly())
112+
.tag(ModTags.AllBlockTags.FRAMEABLE.tag)
113+
.loot((lr, block) -> lr.add(block, lr.createSingleItemTable(block)))
114+
.item()
115+
.tab(TRAINUTILS_TAB.getKey())
116+
.build()
117+
.register();
118+
}
119+
102120
public static BlockEntry<Block> DefaultBlock(String id, MapColor color) {
103121
return REGISTRATE
104122
.block(id, Block::new)
@@ -148,7 +166,8 @@ public static <B extends DoorBlock, P> NonNullUnaryOperator<BlockBuilder<B, P>>
148166
.properties(p -> p.strength(3.0F, 6.0F))
149167
.addLayer(() -> RenderType::cutout)
150168
.transform(pickaxeOnly())
151-
.onRegister(interactionBehaviour(new TrainSlidingDoorMovingInteraction()))
169+
.onRegister(interactionBehaviour(new DoorBlockMovingInteraction()))
170+
.onRegister(movementBehaviour(new DoorBlockMovementBehaviour()))
152171
.tag(BlockTags.DOORS)
153172
.tag(ModTags.AllBlockTags.DOORS.tag)
154173
.tag(AllTags.AllBlockTags.NON_DOUBLE_DOOR.tag)

0 commit comments

Comments
 (0)