Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/edn/stratodonut/drivebywire/DriveByWireMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -24,6 +27,9 @@ 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<String, BlockPos> hubs = new ConcurrentHashMap<>();

static {
REGISTRATE.setTooltipModifierFactory(item ->
new ItemDescription.Modifier(item, TooltipHelper.Palette.STANDARD_CREATE));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,5 +12,10 @@ public class WireBlockEntities {
.validBlocks(WireBlocks.BACKUP_BLOCK)
.register();

public static final BlockEntityEntry<ControllerHubBlockEntity> CONTROLLER_HUB_BLOCK_ENTITY = REGISTRATE
.blockEntity("controller_hub", ControllerHubBlockEntity::new)
.validBlocks(WireBlocks.CONTROLLER_HUB, WireBlocks.TWEAKED_HUB)
.register();

public static void register() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<ControllerHubBlockEntity> {
public static final VoxelShape BOTTOM_AABB = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D);

private static final List<String> channels = Arrays.stream(KEY_TO_CHANNEL).toList();
Expand All @@ -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);
Expand Down Expand Up @@ -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<ControllerHubBlockEntity> getBlockEntityClass() {
return ControllerHubBlockEntity.class;
}

@Override
public BlockEntityType<? extends ControllerHubBlockEntity> 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_);
}
}
Original file line number Diff line number Diff line change
@@ -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 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(DriveByWireMod.UUID_KEY, UUID_VALUE);
}
}

@Override
public void load(CompoundTag p_155245_) {
super.load(p_155245_);
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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<ControllerHubBlockEntity> {
@Unique
private static final List<String> channels =
Stream.concat(Arrays.stream(AXIS_TO_CHANNEL), Arrays.stream(BUTTON_TO_CHANNEL)).toList();
Expand All @@ -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);
Expand Down Expand Up @@ -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<ControllerHubBlockEntity> getBlockEntityClass() {
return ControllerHubBlockEntity.class;
}

@Override
public BlockEntityType<? extends ControllerHubBlockEntity> 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_);
}
}
59 changes: 59 additions & 0 deletions src/main/java/edn/stratodonut/drivebywire/mixin/PaletteMixin.java
Original file line number Diff line number Diff line change
@@ -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<StructureTemplate.StructureBlockInfo> blocks;

@Inject(method = "<init>", at = {@At("RETURN")})
private void constructorMixin(List<StructureTemplate.StructureBlockInfo> blockInfoList, CallbackInfo ci) {

Map<String, String> 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<String, String> 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));
}
}
}
14 changes: 10 additions & 4 deletions src/main/java/edn/stratodonut/drivebywire/util/HubItem.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,15 +9,20 @@
import java.util.function.Consumer;

public class HubItem {
public static void putHub(ItemStack itemStack, BlockPos pos) {

public static void putHub(ItemStack itemStack, String uuid) {
CompoundTag nbt = itemStack.getOrCreateTag();
nbt.putLong("Hub", pos.asLong());
nbt.putString(DriveByWireMod.UUID_KEY, uuid);
itemStack.setTag(nbt);
}

public static void ifHubPresent(ItemStack itemStack, Consumer<BlockPos> 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(DriveByWireMod.UUID_KEY, Tag.TAG_STRING)) {
BlockPos hubPos = DriveByWireMod.hubs.get(tag.getString(DriveByWireMod.UUID_KEY));
if (hubPos != null) {
runnable.accept(hubPos);
}
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/drivebywire.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"compatibilityLevel": "JAVA_17",
"mixins": [
"MixinServerLevel",
"PaletteMixin",
"compat.MixinLecternControllerBlock",
"compat.MixinLinkedControllerInputPacket",
"compat.MixinLinkedControllerStopLecternPacket",
Expand Down