Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Moving stuff to interfaces and cleaning up color/blockentity code
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Apr 7, 2024
1 parent 454de71 commit 8311e04
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.compactmods.machines.api.machine.block;

public interface ICompactMachineBlockEntity {
String NBT_COLOR = "machine_color";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.compactmods.machines.api.machine.block;

public interface IUnboundCompactMachineBlockEntity extends ICompactMachineBlockEntity {
String NBT_TEMPLATE_ID = "template_id";
}
Original file line number Diff line number Diff line change
@@ -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<String> getRoom(ItemStack stack) {
if (!stack.hasTag())
Expand All @@ -22,6 +28,11 @@ default Optional<String> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,6 +29,11 @@ default Optional<String> 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;
}

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 8311e04

Please sign in to comment.