From 4402274c0579dd395bdb9a1d81df72325fa7b533 Mon Sep 17 00:00:00 2001 From: Sofie <63377159+SofieBrink@users.noreply.github.com> Date: Fri, 2 May 2025 14:24:54 +0200 Subject: [PATCH 1/2] Deployer Train Renaming --- .../railways/mixin/MixinStationBlock.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/common/src/main/java/com/railwayteam/railways/mixin/MixinStationBlock.java b/common/src/main/java/com/railwayteam/railways/mixin/MixinStationBlock.java index 241e4f552..6546101c6 100644 --- a/common/src/main/java/com/railwayteam/railways/mixin/MixinStationBlock.java +++ b/common/src/main/java/com/railwayteam/railways/mixin/MixinStationBlock.java @@ -18,12 +18,15 @@ package com.railwayteam.railways.mixin; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.railwayteam.railways.Railways; import com.railwayteam.railways.config.CRConfigs; import com.railwayteam.railways.content.conductor.ConductorEntity; import com.railwayteam.railways.mixin_interfaces.ICarriageConductors; import com.railwayteam.railways.registry.CRBlocks; import com.railwayteam.railways.registry.CREntities; +import com.simibubi.create.AllPackets; import com.simibubi.create.Create; import com.simibubi.create.content.kinetics.deployer.DeployerFakePlayer; import com.simibubi.create.content.trains.entity.Carriage; @@ -35,6 +38,8 @@ import com.simibubi.create.content.trains.station.GlobalStation; import com.simibubi.create.content.trains.station.StationBlock; import com.simibubi.create.content.trains.station.StationBlockEntity; +import com.simibubi.create.content.trains.station.TrainEditPacket; +import com.simibubi.create.foundation.utility.Components; import com.simibubi.create.foundation.utility.VecHelper; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; @@ -260,4 +265,57 @@ private void dropSchedule(ServerPlayer sender, StationBlockEntity te, ItemStack te.getLevel() .addFreshEntity(itemEntity); } + +// @Inject(method = "use" +// , at = @At(value = "INVOKE" +// , target = "Lcom/simibubi/create/content/trains/station/StationBlock;onBlockEntityUse(Lnet/minecraft/world/level/BlockGetter;Lnet/minecraft/core/BlockPos;Ljava/util/function/Function;)Lnet/minecraft/world/InteractionResult;" +// , ordinal = 1) +// , cancellable = true +// , remap = false) + @Inject(method = "use", at = @At("HEAD"), cancellable = true, remap = true) + private void deployersNameTag(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit, CallbackInfoReturnable cir) { + ItemStack itemInHand = pPlayer.getItemInHand(pHand); + if (!pLevel.isClientSide + && pPlayer instanceof DeployerFakePlayer deployerFakePlayer + && pLevel.getBlockEntity(pPos) instanceof StationBlockEntity stationBe + && itemInHand.getItem() instanceof net.minecraft.world.item.NameTagItem) + { + cir.setReturnValue(InteractionResult.CONSUME); + GlobalStation station = stationBe.getStation(); + if (station != null && station.getPresentTrain() != null) { + Train train = station.getPresentTrain(); + if (itemInHand.hasTag() && itemInHand.getTag().contains("display")) { + CompoundTag displayTag = itemInHand.getTag().getCompound("display"); + if (displayTag.contains("Name")) { + // Set the train name + JsonObject json = JsonParser.parseString(displayTag.getString("Name")).getAsJsonObject(); + String newName = json.get("text").getAsString(); + if (pLevel.getServer().isDedicatedServer()) { + train.name = Components.literal(newName); + AllPackets.getChannel() + .sendToClientsInServer(new TrainEditPacket + .TrainEditReturnPacket(train.id, newName, train.icon.getId()) + , pLevel.getServer()); + } + else + AllPackets.getChannel() + .sendToServer(new TrainEditPacket(train.id, newName, train.icon.getId())); + } + } + else { + // Get the trains name and put it on the nametag + JsonObject displayNameJson = new JsonObject(); + displayNameJson.addProperty("text", train.name.getString()); + + CompoundTag tag = itemInHand.getOrCreateTag(); + CompoundTag displayTag = tag.contains("display", 10) + ? tag.getCompound("display") + : new CompoundTag(); + displayTag.putString("Name", displayNameJson.toString()); + tag.put("display", displayTag); + itemInHand.setTag(tag); + } + } + } + } } From 7a5a88bb6856eb10377e0b1a9308d9a33984fcad Mon Sep 17 00:00:00 2001 From: Sofie <63377159+SofieBrink@users.noreply.github.com> Date: Sat, 3 May 2025 20:24:46 +0200 Subject: [PATCH 2/2] Add premature return to deployersNameTag Don't send train edit (response) packets if the name hasn't changed. --- .../java/com/railwayteam/railways/mixin/MixinStationBlock.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/common/src/main/java/com/railwayteam/railways/mixin/MixinStationBlock.java b/common/src/main/java/com/railwayteam/railways/mixin/MixinStationBlock.java index 6546101c6..464795d23 100644 --- a/common/src/main/java/com/railwayteam/railways/mixin/MixinStationBlock.java +++ b/common/src/main/java/com/railwayteam/railways/mixin/MixinStationBlock.java @@ -290,6 +290,9 @@ private void deployersNameTag(BlockState pState, Level pLevel, BlockPos pPos, Pl // Set the train name JsonObject json = JsonParser.parseString(displayTag.getString("Name")).getAsJsonObject(); String newName = json.get("text").getAsString(); + + if (train.name.getString().equals(newName)) return; + if (pLevel.getServer().isDedicatedServer()) { train.name = Components.literal(newName); AllPackets.getChannel()