From 1a38596757e715dba4556e7fc0e58f8a9dd25a3f Mon Sep 17 00:00:00 2001 From: James Green Date: Mon, 19 May 2025 16:40:43 -0700 Subject: [PATCH 1/3] use UUIDs for linked controllers --- .../drivebywire/DriveByWireMod.java | 5 +++ .../drivebywire/WireBlockEntities.java | 6 +++ .../blocks/ControllerHubBlock.java | 30 +++++++++++-- .../blocks/ControllerHubBlockEntity.java | 44 +++++++++++++++++++ .../blocks/TweakedControllerHubBlock.java | 26 +++++++++-- .../stratodonut/drivebywire/util/HubItem.java | 15 +++++-- 6 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlockEntity.java diff --git a/src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java b/src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java index 976707b..1e51f63 100644 --- a/src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java +++ b/src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java @@ -4,6 +4,7 @@ import com.simibubi.create.foundation.data.CreateRegistrate; import com.simibubi.create.foundation.item.ItemDescription; import com.simibubi.create.foundation.item.TooltipHelper; +import net.minecraft.core.BlockPos; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.eventbus.api.IEventBus; @@ -14,6 +15,8 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import org.slf4j.Logger; +import java.util.concurrent.ConcurrentHashMap; + // The value here should match an entry in the META-INF/mods.toml file @Mod("drivebywire") public class DriveByWireMod @@ -24,6 +27,8 @@ public class DriveByWireMod public static final String MOD_ID = "drivebywire"; public static final CreateRegistrate REGISTRATE = CreateRegistrate.create(MOD_ID); + public static final ConcurrentHashMap hubs = new ConcurrentHashMap<>(); + static { REGISTRATE.setTooltipModifierFactory(item -> new ItemDescription.Modifier(item, TooltipHelper.Palette.STANDARD_CREATE)); diff --git a/src/main/java/edn/stratodonut/drivebywire/WireBlockEntities.java b/src/main/java/edn/stratodonut/drivebywire/WireBlockEntities.java index c56961a..8db8406 100644 --- a/src/main/java/edn/stratodonut/drivebywire/WireBlockEntities.java +++ b/src/main/java/edn/stratodonut/drivebywire/WireBlockEntities.java @@ -1,6 +1,7 @@ package edn.stratodonut.drivebywire; import com.tterrag.registrate.util.entry.BlockEntityEntry; +import edn.stratodonut.drivebywire.blocks.ControllerHubBlockEntity; import edn.stratodonut.drivebywire.blocks.WireNetworkBackupBlockEntity; import static edn.stratodonut.drivebywire.DriveByWireMod.REGISTRATE; @@ -11,5 +12,10 @@ public class WireBlockEntities { .validBlocks(WireBlocks.BACKUP_BLOCK) .register(); + public static final BlockEntityEntry CONTROLLER_HUB_BLOCK_ENTITY = REGISTRATE + .blockEntity("controller_hub", ControllerHubBlockEntity::new) + .validBlocks(WireBlocks.CONTROLLER_HUB, WireBlocks.TWEAKED_HUB) + .register(); + public static void register() {} } diff --git a/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlock.java b/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlock.java index 24198d2..7ea8f1e 100644 --- a/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlock.java +++ b/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlock.java @@ -3,7 +3,9 @@ import com.getitemfromblock.create_tweaked_controllers.item.ModItems; import com.simibubi.create.AllItems; import com.simibubi.create.AllShapes; +import com.simibubi.create.foundation.block.IBE; import com.simibubi.create.foundation.utility.VoxelShaper; +import edn.stratodonut.drivebywire.WireBlockEntities; import edn.stratodonut.drivebywire.WireSounds; import edn.stratodonut.drivebywire.util.HubItem; import edn.stratodonut.drivebywire.wire.MultiChannelWireSource; @@ -19,8 +21,9 @@ import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.block.state.properties.Half; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; @@ -31,7 +34,7 @@ import static edn.stratodonut.drivebywire.compat.LinkedControllerWireServerHandler.KEY_TO_CHANNEL; -public class ControllerHubBlock extends Block implements MultiChannelWireSource { +public class ControllerHubBlock extends Block implements MultiChannelWireSource, IBE { public static final VoxelShape BOTTOM_AABB = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D); private static final List channels = Arrays.stream(KEY_TO_CHANNEL).toList(); @@ -44,8 +47,12 @@ public ControllerHubBlock(Properties p_49795_) { public @NotNull InteractionResult use(@NotNull BlockState p_60503_, @NotNull Level level, @NotNull BlockPos blockPos, @NotNull Player player, @NotNull InteractionHand p_60507_, @NotNull BlockHitResult p_60508_) { ItemStack itemStack = player.getItemInHand(p_60507_); - if (AllItems.LINKED_CONTROLLER.is(itemStack.getItem())) { - HubItem.putHub(itemStack, blockPos); + ControllerHubBlockEntity blockEntity = getBlockEntity(level, blockPos); + + System.out.println("Clicked on the block, blockEntity is null? " + (blockEntity == null)); + + if (AllItems.LINKED_CONTROLLER.is(itemStack.getItem()) && blockEntity != null) { + HubItem.putHub(itemStack, blockEntity.getUUID_VALUE()); if (!level.isClientSide) { level.playSound(null, blockPos, WireSounds.PLUG_IN.get(), SoundSource.BLOCKS, 1, 1); player.displayClientMessage(Component.literal("Controller connected!"), true); @@ -77,4 +84,19 @@ public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, return channels.get(Math.floorMod(curIndex + (forward ? 1 : -1), channels.size())); } } + + @Override + public Class getBlockEntityClass() { + return ControllerHubBlockEntity.class; + } + + @Override + public BlockEntityType getBlockEntityType() { + return WireBlockEntities.CONTROLLER_HUB_BLOCK_ENTITY.get(); + } + + @Override + public BlockEntity newBlockEntity(BlockPos p_153215_, BlockState p_153216_) { + return IBE.super.newBlockEntity(p_153215_, p_153216_); + } } diff --git a/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlockEntity.java b/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlockEntity.java new file mode 100644 index 0000000..2ab3123 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlockEntity.java @@ -0,0 +1,44 @@ +package edn.stratodonut.drivebywire.blocks; + +import edn.stratodonut.drivebywire.DriveByWireMod; +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; +import net.minecraft.world.level.block.state.BlockState; + +import java.util.UUID; + +public class ControllerHubBlockEntity extends BlockEntity { + private String UUID_VALUE = UUID.randomUUID().toString(); + public static final String UUID_KEY = DriveByWireMod.MOD_ID + "$ControllerHubUUID"; + + public String getUUID_VALUE() { + return UUID_VALUE; + } + + public ControllerHubBlockEntity(BlockEntityType p_155228_, BlockPos p_155229_, BlockState p_155230_) { + super(p_155228_, p_155229_, p_155230_); + + DriveByWireMod.hubs.put(UUID_VALUE, this.getBlockPos()); + } + + @Override + protected void saveAdditional(CompoundTag p_187471_) { + super.saveAdditional(p_187471_); + if (level != null) { + p_187471_.putString(UUID_KEY, UUID_VALUE); + } + } + + @Override + public void load(CompoundTag p_155245_) { + super.load(p_155245_); + if (p_155245_.contains(UUID_KEY, Tag.TAG_STRING)) { + UUID_VALUE = p_155245_.getString(UUID_KEY); + } + + DriveByWireMod.hubs.put(UUID_VALUE, this.getBlockPos()); + } +} diff --git a/src/main/java/edn/stratodonut/drivebywire/blocks/TweakedControllerHubBlock.java b/src/main/java/edn/stratodonut/drivebywire/blocks/TweakedControllerHubBlock.java index 0fd3e81..8e45af7 100644 --- a/src/main/java/edn/stratodonut/drivebywire/blocks/TweakedControllerHubBlock.java +++ b/src/main/java/edn/stratodonut/drivebywire/blocks/TweakedControllerHubBlock.java @@ -1,5 +1,7 @@ package edn.stratodonut.drivebywire.blocks; +import com.simibubi.create.foundation.block.IBE; +import edn.stratodonut.drivebywire.WireBlockEntities; import edn.stratodonut.drivebywire.WireSounds; import edn.stratodonut.drivebywire.mixinducks.TweakedControllerDuck; import edn.stratodonut.drivebywire.util.HubItem; @@ -14,6 +16,8 @@ import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; @@ -29,7 +33,7 @@ import static edn.stratodonut.drivebywire.compat.TweakedControllerWireServerHandler.AXIS_TO_CHANNEL; import static edn.stratodonut.drivebywire.compat.TweakedControllerWireServerHandler.BUTTON_TO_CHANNEL; -public class TweakedControllerHubBlock extends Block implements MultiChannelWireSource { +public class TweakedControllerHubBlock extends Block implements MultiChannelWireSource, IBE { @Unique private static final List channels = Stream.concat(Arrays.stream(AXIS_TO_CHANNEL), Arrays.stream(BUTTON_TO_CHANNEL)).toList(); @@ -42,8 +46,9 @@ public TweakedControllerHubBlock(Properties p_49795_) { public @NotNull InteractionResult use(@NotNull BlockState p_60503_, @NotNull Level level, @NotNull BlockPos blockPos, @NotNull Player player, @NotNull InteractionHand p_60507_, @NotNull BlockHitResult p_60508_) { ItemStack itemStack = player.getItemInHand(p_60507_); - if (itemStack.getItem() instanceof TweakedControllerDuck) { - HubItem.putHub(itemStack, blockPos); + ControllerHubBlockEntity blockEntity = getBlockEntity(level, blockPos); + if (itemStack.getItem() instanceof TweakedControllerDuck && blockEntity != null) { + HubItem.putHub(itemStack, blockEntity.getUUID_VALUE()); if (!level.isClientSide) { level.playSound(null, blockPos, WireSounds.PLUG_IN.get(), SoundSource.BLOCKS, 1, 1); player.displayClientMessage(Component.literal("Controller connected!"), true); @@ -76,4 +81,19 @@ public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, return channels.get(Math.floorMod(curIndex + (forward ? 1 : -1), channels.size())); } } + + @Override + public Class getBlockEntityClass() { + return ControllerHubBlockEntity.class; + } + + @Override + public BlockEntityType getBlockEntityType() { + return WireBlockEntities.CONTROLLER_HUB_BLOCK_ENTITY.get(); + } + + @Override + public BlockEntity newBlockEntity(BlockPos p_153215_, BlockState p_153216_) { + return IBE.super.newBlockEntity(p_153215_, p_153216_); + } } diff --git a/src/main/java/edn/stratodonut/drivebywire/util/HubItem.java b/src/main/java/edn/stratodonut/drivebywire/util/HubItem.java index 22690f6..83ea650 100644 --- a/src/main/java/edn/stratodonut/drivebywire/util/HubItem.java +++ b/src/main/java/edn/stratodonut/drivebywire/util/HubItem.java @@ -1,5 +1,6 @@ package edn.stratodonut.drivebywire.util; +import edn.stratodonut.drivebywire.DriveByWireMod; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; @@ -8,15 +9,21 @@ import java.util.function.Consumer; public class HubItem { - public static void putHub(ItemStack itemStack, BlockPos pos) { + private static final String HUB_KEY = DriveByWireMod.MOD_ID + "$Hub"; + + public static void putHub(ItemStack itemStack, String uuid) { CompoundTag nbt = itemStack.getOrCreateTag(); - nbt.putLong("Hub", pos.asLong()); + nbt.putString(HUB_KEY, uuid); itemStack.setTag(nbt); } public static void ifHubPresent(ItemStack itemStack, Consumer runnable) { - if (itemStack.hasTag() && itemStack.getTag().contains("Hub", Tag.TAG_LONG)) { - runnable.accept(BlockPos.of(itemStack.getTag().getLong("Hub"))); + CompoundTag tag = itemStack.getTag(); + if (itemStack.hasTag() && tag != null && tag.contains(HUB_KEY, Tag.TAG_STRING)) { + BlockPos hubPos = DriveByWireMod.hubs.get(tag.getString(HUB_KEY)); + if (hubPos != null) { + runnable.accept(hubPos); + } } } } From 82e8a3e3e14cfef2a969ed7321d26d576f2a650a Mon Sep 17 00:00:00 2001 From: James Green Date: Mon, 19 May 2025 18:06:48 -0700 Subject: [PATCH 2/3] single uuid key --- .../java/edn/stratodonut/drivebywire/DriveByWireMod.java | 1 + .../drivebywire/blocks/ControllerHubBlockEntity.java | 8 ++++---- .../java/edn/stratodonut/drivebywire/util/HubItem.java | 7 +++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java b/src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java index 1e51f63..37a2dbe 100644 --- a/src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java +++ b/src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java @@ -27,6 +27,7 @@ public class DriveByWireMod public static final String MOD_ID = "drivebywire"; public static final CreateRegistrate REGISTRATE = CreateRegistrate.create(MOD_ID); + public static final String UUID_KEY = DriveByWireMod.MOD_ID + "$ControllerHubUUID"; public static final ConcurrentHashMap hubs = new ConcurrentHashMap<>(); static { diff --git a/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlockEntity.java b/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlockEntity.java index 2ab3123..1e71750 100644 --- a/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlockEntity.java +++ b/src/main/java/edn/stratodonut/drivebywire/blocks/ControllerHubBlockEntity.java @@ -12,7 +12,7 @@ public class ControllerHubBlockEntity extends BlockEntity { private String UUID_VALUE = UUID.randomUUID().toString(); - public static final String UUID_KEY = DriveByWireMod.MOD_ID + "$ControllerHubUUID"; + public String getUUID_VALUE() { return UUID_VALUE; @@ -28,15 +28,15 @@ public ControllerHubBlockEntity(BlockEntityType p_155228_, BlockPos p_155229_ protected void saveAdditional(CompoundTag p_187471_) { super.saveAdditional(p_187471_); if (level != null) { - p_187471_.putString(UUID_KEY, UUID_VALUE); + p_187471_.putString(DriveByWireMod.UUID_KEY, UUID_VALUE); } } @Override public void load(CompoundTag p_155245_) { super.load(p_155245_); - if (p_155245_.contains(UUID_KEY, Tag.TAG_STRING)) { - UUID_VALUE = p_155245_.getString(UUID_KEY); + if (p_155245_.contains(DriveByWireMod.UUID_KEY, Tag.TAG_STRING)) { + UUID_VALUE = p_155245_.getString(DriveByWireMod.UUID_KEY); } DriveByWireMod.hubs.put(UUID_VALUE, this.getBlockPos()); diff --git a/src/main/java/edn/stratodonut/drivebywire/util/HubItem.java b/src/main/java/edn/stratodonut/drivebywire/util/HubItem.java index 83ea650..0317e14 100644 --- a/src/main/java/edn/stratodonut/drivebywire/util/HubItem.java +++ b/src/main/java/edn/stratodonut/drivebywire/util/HubItem.java @@ -9,18 +9,17 @@ import java.util.function.Consumer; public class HubItem { - private static final String HUB_KEY = DriveByWireMod.MOD_ID + "$Hub"; public static void putHub(ItemStack itemStack, String uuid) { CompoundTag nbt = itemStack.getOrCreateTag(); - nbt.putString(HUB_KEY, uuid); + nbt.putString(DriveByWireMod.UUID_KEY, uuid); itemStack.setTag(nbt); } public static void ifHubPresent(ItemStack itemStack, Consumer runnable) { CompoundTag tag = itemStack.getTag(); - if (itemStack.hasTag() && tag != null && tag.contains(HUB_KEY, Tag.TAG_STRING)) { - BlockPos hubPos = DriveByWireMod.hubs.get(tag.getString(HUB_KEY)); + if (itemStack.hasTag() && tag != null && tag.contains(DriveByWireMod.UUID_KEY, Tag.TAG_STRING)) { + BlockPos hubPos = DriveByWireMod.hubs.get(tag.getString(DriveByWireMod.UUID_KEY)); if (hubPos != null) { runnable.accept(hubPos); } From a9a53a24b43abe91daac0b82271cbf74fd1af991 Mon Sep 17 00:00:00 2001 From: James Green Date: Mon, 19 May 2025 18:51:22 -0700 Subject: [PATCH 3/3] works with VLib --- .../drivebywire/mixin/PaletteMixin.java | 59 +++++++++++++++++++ src/main/resources/drivebywire.mixins.json | 1 + 2 files changed, 60 insertions(+) create mode 100644 src/main/java/edn/stratodonut/drivebywire/mixin/PaletteMixin.java diff --git a/src/main/java/edn/stratodonut/drivebywire/mixin/PaletteMixin.java b/src/main/java/edn/stratodonut/drivebywire/mixin/PaletteMixin.java new file mode 100644 index 0000000..ca88b40 --- /dev/null +++ b/src/main/java/edn/stratodonut/drivebywire/mixin/PaletteMixin.java @@ -0,0 +1,59 @@ +package edn.stratodonut.drivebywire.mixin; + +import edn.stratodonut.drivebywire.DriveByWireMod; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate; +import org.spongepowered.asm.mixin.*; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +@Mixin(StructureTemplate.Palette.class) +public class PaletteMixin { + + @Mutable + @Shadow + @Final + private List blocks; + + @Inject(method = "", at = {@At("RETURN")}) + private void constructorMixin(List blockInfoList, CallbackInfo ci) { + + Map uuidMap = new HashMap<>(); + + for (StructureTemplate.StructureBlockInfo block : blockInfoList) { + if (block.nbt() == null) continue; + System.out.println(block.nbt()); + + drivebywire$updateIfPresent(block.nbt(), uuidMap); + + if (block.nbt().contains("Controller", Tag.TAG_COMPOUND)) { + CompoundTag controller = block.nbt().getCompound("Controller"); + if (controller.contains("tag", Tag.TAG_COMPOUND)) { + CompoundTag tag = controller.getCompound("tag"); + drivebywire$updateIfPresent(tag, uuidMap); + } + } + } + this.blocks = blockInfoList; + } + + @Unique + private static void drivebywire$updateIfPresent(CompoundTag tag, Map uuids) { + if (tag.contains(DriveByWireMod.UUID_KEY, Tag.TAG_STRING)) { + String oldUuid = tag.getString(DriveByWireMod.UUID_KEY); + + if (!uuids.containsKey(oldUuid)) { + uuids.put(oldUuid, UUID.randomUUID().toString()); + } + tag.remove(DriveByWireMod.UUID_KEY); + tag.putString(DriveByWireMod.UUID_KEY, uuids.get(oldUuid)); + } + } +} diff --git a/src/main/resources/drivebywire.mixins.json b/src/main/resources/drivebywire.mixins.json index 2d27cce..0e70ce2 100644 --- a/src/main/resources/drivebywire.mixins.json +++ b/src/main/resources/drivebywire.mixins.json @@ -4,6 +4,7 @@ "compatibilityLevel": "JAVA_17", "mixins": [ "MixinServerLevel", + "PaletteMixin", "compat.MixinLecternControllerBlock", "compat.MixinLinkedControllerInputPacket", "compat.MixinLinkedControllerStopLecternPacket",