From 8311e0434506fac6b8e82f38825f045379b66e5b Mon Sep 17 00:00:00 2001 From: Ted Senft Date: Sun, 7 Apr 2024 05:34:04 -0400 Subject: [PATCH] Moving stuff to interfaces and cleaning up color/blockentity code --- .../block/IBoundCompactMachineBlockEntity.java | 10 ++++++++++ .../machine/block/ICompactMachineBlockEntity.java | 5 +++++ .../block/IUnboundCompactMachineBlockEntity.java | 5 +++++ .../machine/item/IBoundCompactMachineItem.java | 13 ++++++++++++- .../api/machine/item/ICompactMachineItem.java | 15 +++++++++++++++ .../machine/item/IUnboundCompactMachineItem.java | 11 +++++++++++ 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 core-api/src/main/java/dev/compactmods/machines/api/machine/block/IBoundCompactMachineBlockEntity.java create mode 100644 core-api/src/main/java/dev/compactmods/machines/api/machine/block/ICompactMachineBlockEntity.java create mode 100644 core-api/src/main/java/dev/compactmods/machines/api/machine/block/IUnboundCompactMachineBlockEntity.java diff --git a/core-api/src/main/java/dev/compactmods/machines/api/machine/block/IBoundCompactMachineBlockEntity.java b/core-api/src/main/java/dev/compactmods/machines/api/machine/block/IBoundCompactMachineBlockEntity.java new file mode 100644 index 0000000..833f689 --- /dev/null +++ b/core-api/src/main/java/dev/compactmods/machines/api/machine/block/IBoundCompactMachineBlockEntity.java @@ -0,0 +1,10 @@ +package dev.compactmods.machines.api.machine.block; + +public interface IBoundCompactMachineBlockEntity extends ICompactMachineBlockEntity { + + String NBT_OWNER = "owner"; + String NBT_COLOR = "machine_color"; + String NBT_ROOM_CODE = "room_code"; + String NBT_ROOM_COLOR = "room_color"; + +} diff --git a/core-api/src/main/java/dev/compactmods/machines/api/machine/block/ICompactMachineBlockEntity.java b/core-api/src/main/java/dev/compactmods/machines/api/machine/block/ICompactMachineBlockEntity.java new file mode 100644 index 0000000..c9fe535 --- /dev/null +++ b/core-api/src/main/java/dev/compactmods/machines/api/machine/block/ICompactMachineBlockEntity.java @@ -0,0 +1,5 @@ +package dev.compactmods.machines.api.machine.block; + +public interface ICompactMachineBlockEntity { + String NBT_COLOR = "machine_color"; +} diff --git a/core-api/src/main/java/dev/compactmods/machines/api/machine/block/IUnboundCompactMachineBlockEntity.java b/core-api/src/main/java/dev/compactmods/machines/api/machine/block/IUnboundCompactMachineBlockEntity.java new file mode 100644 index 0000000..4536954 --- /dev/null +++ b/core-api/src/main/java/dev/compactmods/machines/api/machine/block/IUnboundCompactMachineBlockEntity.java @@ -0,0 +1,5 @@ +package dev.compactmods.machines.api.machine.block; + +public interface IUnboundCompactMachineBlockEntity extends ICompactMachineBlockEntity { + String NBT_TEMPLATE_ID = "template_id"; +} diff --git a/core-api/src/main/java/dev/compactmods/machines/api/machine/item/IBoundCompactMachineItem.java b/core-api/src/main/java/dev/compactmods/machines/api/machine/item/IBoundCompactMachineItem.java index b8ecdd7..d637051 100644 --- a/core-api/src/main/java/dev/compactmods/machines/api/machine/item/IBoundCompactMachineItem.java +++ b/core-api/src/main/java/dev/compactmods/machines/api/machine/item/IBoundCompactMachineItem.java @@ -1,12 +1,18 @@ package dev.compactmods.machines.api.machine.item; +import dev.compactmods.machines.api.machine.MachineConstants; +import dev.compactmods.machines.api.machine.block.IBoundCompactMachineBlockEntity; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; import java.util.Optional; public interface IBoundCompactMachineItem extends ICompactMachineItem { - public static final String NBT_ROOM_CODE = "room_code"; + String NBT_ROOM_CODE = "room_code"; default Optional getRoom(ItemStack stack) { if (!stack.hasTag()) @@ -22,6 +28,11 @@ default Optional getRoom(ItemStack stack) { default void setRoom(ItemStack stack, String room) { var tag = stack.getOrCreateTag(); tag.putString(NBT_ROOM_CODE, room); + + var blockEntityData = new CompoundTag(); + blockEntityData.putString("id", MachineConstants.BOUND_MACHINE_ENTITY.toString()); + blockEntityData.putString(IBoundCompactMachineBlockEntity.NBT_ROOM_CODE, room); + addBlockEntityData(stack, blockEntityData); } } diff --git a/core-api/src/main/java/dev/compactmods/machines/api/machine/item/ICompactMachineItem.java b/core-api/src/main/java/dev/compactmods/machines/api/machine/item/ICompactMachineItem.java index 450cfc8..e87a112 100644 --- a/core-api/src/main/java/dev/compactmods/machines/api/machine/item/ICompactMachineItem.java +++ b/core-api/src/main/java/dev/compactmods/machines/api/machine/item/ICompactMachineItem.java @@ -1,7 +1,12 @@ package dev.compactmods.machines.api.machine.item; +import dev.compactmods.machines.api.machine.MachineConstants; +import dev.compactmods.machines.api.machine.block.IBoundCompactMachineBlockEntity; +import dev.compactmods.machines.api.machine.block.ICompactMachineBlockEntity; +import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.NotNull; import java.util.Optional; @@ -24,6 +29,11 @@ default Optional getMachineName(ItemStack stack) { default ItemStack setColor(ItemStack stack, int color) { var tag = stack.getOrCreateTag(); tag.putInt(NBT_COLOR, color); + + var blockEntityData = new CompoundTag(); + blockEntityData.putInt(ICompactMachineBlockEntity.NBT_COLOR, color); + addBlockEntityData(stack, blockEntityData); + return stack; } @@ -37,4 +47,9 @@ default int getMachineColor(ItemStack stack) { return tag.getInt(NBT_COLOR); } + default void addBlockEntityData(@NotNull ItemStack stack, @NotNull CompoundTag stackTag) { + final var existingTag = stack.getOrCreateTagElement("BlockEntityTag"); + existingTag.merge(stackTag); + } + } diff --git a/core-api/src/main/java/dev/compactmods/machines/api/machine/item/IUnboundCompactMachineItem.java b/core-api/src/main/java/dev/compactmods/machines/api/machine/item/IUnboundCompactMachineItem.java index ce5fdd4..29f0c76 100644 --- a/core-api/src/main/java/dev/compactmods/machines/api/machine/item/IUnboundCompactMachineItem.java +++ b/core-api/src/main/java/dev/compactmods/machines/api/machine/item/IUnboundCompactMachineItem.java @@ -1,6 +1,11 @@ package dev.compactmods.machines.api.machine.item; import dev.compactmods.machines.api.Constants; +import dev.compactmods.machines.api.machine.MachineConstants; +import dev.compactmods.machines.api.machine.block.IBoundCompactMachineBlockEntity; +import dev.compactmods.machines.api.machine.block.ICompactMachineBlockEntity; +import dev.compactmods.machines.api.machine.block.IUnboundCompactMachineBlockEntity; +import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.NotNull; @@ -13,6 +18,12 @@ public interface IUnboundCompactMachineItem extends ICompactMachineItem { default ItemStack setTemplate(ItemStack stack, ResourceLocation templateId) { var tag = stack.getOrCreateTag(); tag.putString(NBT_TEMPLATE_ID, templateId.toString()); + + var blockEntityData = new CompoundTag(); + blockEntityData.putString("id", MachineConstants.UNBOUND_MACHINE_ENTITY.toString()); + blockEntityData.putString(IUnboundCompactMachineBlockEntity.NBT_TEMPLATE_ID, templateId.toString()); + addBlockEntityData(stack, blockEntityData); + return stack; }