diff --git a/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/BlockEntityAccess.java b/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/BlockEntityAccess.java index 7ad66d35..e12bee4b 100644 --- a/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/BlockEntityAccess.java +++ b/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/BlockEntityAccess.java @@ -1,5 +1,10 @@ package band.kessoku.lib.api.blockentity; -public class BlockEntityAccess { +import band.kessoku.lib.api.data.DataAccess; +import band.kessoku.lib.api.data.DataStructure; +import net.minecraft.block.entity.BlockEntity; + +@FunctionalInterface +public interface BlockEntityAccess extends DataAccess { } diff --git a/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/BlockEntityCoordinator.java b/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/BlockEntityCoordinator.java new file mode 100644 index 00000000..6916a789 --- /dev/null +++ b/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/BlockEntityCoordinator.java @@ -0,0 +1,11 @@ +package band.kessoku.lib.api.blockentity; + +import band.kessoku.lib.api.data.DataStructure; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; + +public class BlockEntityCoordinator { + public static void add(BlockEntityType type, BlockEntityAccess access) { + + } +} diff --git a/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/KessokuBlockEntity.java b/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/KessokuBlockEntity.java index 76b553bc..36dfc0fc 100644 --- a/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/KessokuBlockEntity.java +++ b/blockentity/common/src/main/java/band/kessoku/lib/api/blockentity/KessokuBlockEntity.java @@ -1,5 +1,8 @@ package band.kessoku.lib.api.blockentity; +import band.kessoku.lib.api.data.Storage; +import band.kessoku.lib.impl.ItemSidinator; +import net.minecraft.block.entity.BlockEntityType; import org.slf4j.Marker; import org.slf4j.MarkerFactory; @@ -7,4 +10,13 @@ public class KessokuBlockEntity { public static final String MOD_ID = "kessoku_blockentity"; public static final String NAME = "Kessoku Block Entity API"; public static final Marker MARKER = MarkerFactory.getMarker("[" + NAME +"]"); + + public void init() { + BlockEntityCoordinator.add(BlockEntityType.FURNACE, Storage::inventory); + BlockEntityCoordinator.add(BlockEntityType.CHEST, Storage::inventory); + BlockEntityCoordinator.add( + BlockEntityType.FURNACE, + (blockentity) -> ItemSidinator.builder().side(1).bottom(2, 1).top(0).build() + ); + } } diff --git a/data/common/src/main/java/band/kessoku/lib/api/data/Sidinator.java b/data/common/src/main/java/band/kessoku/lib/api/data/Sidinator.java new file mode 100644 index 00000000..5acea2ed --- /dev/null +++ b/data/common/src/main/java/band/kessoku/lib/api/data/Sidinator.java @@ -0,0 +1,9 @@ +package band.kessoku.lib.api.data; + +import net.minecraft.util.math.Direction; + +import java.util.List; + +public interface Sidinator extends DataStructure { + List get(Direction direction); +} diff --git a/data/common/src/main/java/band/kessoku/lib/api/data/Storage.java b/data/common/src/main/java/band/kessoku/lib/api/data/Storage.java new file mode 100644 index 00000000..5f5df6a0 --- /dev/null +++ b/data/common/src/main/java/band/kessoku/lib/api/data/Storage.java @@ -0,0 +1,24 @@ +package band.kessoku.lib.api.data; + +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; + +public interface Storage extends DataStructure { + T get(int id); + + int size(); + + static Storage inventory(Inventory inventory) { + return new Storage<>() { + @Override + public ItemStack get(int id) { + return inventory.getStack(id); + } + + @Override + public int size() { + return inventory.size(); + } + }; + } +} diff --git a/data/common/src/main/java/band/kessoku/lib/api/data/Timer.java b/data/common/src/main/java/band/kessoku/lib/api/data/Timer.java new file mode 100644 index 00000000..36bac670 --- /dev/null +++ b/data/common/src/main/java/band/kessoku/lib/api/data/Timer.java @@ -0,0 +1,7 @@ +package band.kessoku.lib.api.data; + +public interface Timer extends DataStructure { + int time(); + + int totalTime(); +} diff --git a/data/common/src/main/java/band/kessoku/lib/impl/ItemSidinator.java b/data/common/src/main/java/band/kessoku/lib/impl/ItemSidinator.java new file mode 100644 index 00000000..b9f9de7d --- /dev/null +++ b/data/common/src/main/java/band/kessoku/lib/impl/ItemSidinator.java @@ -0,0 +1,145 @@ +package band.kessoku.lib.impl; + +import band.kessoku.lib.api.data.Sidinator; +import band.kessoku.lib.api.data.Storage; +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.item.ItemStack; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.util.math.Direction; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.BiConsumer; + +public class ItemSidinator implements Sidinator> { + public static Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.INT.listOf().fieldOf("down").forGetter((sidinator -> sidinator.get(Direction.DOWN))), + Codec.INT.listOf().fieldOf("up").forGetter((sidinator -> sidinator.get(Direction.UP))), + Codec.INT.listOf().fieldOf("north").forGetter((sidinator -> sidinator.get(Direction.NORTH))), + Codec.INT.listOf().fieldOf("south").forGetter((sidinator -> sidinator.get(Direction.SOUTH))), + Codec.INT.listOf().fieldOf("west").forGetter((sidinator -> sidinator.get(Direction.WEST))), + Codec.INT.listOf().fieldOf("east").forGetter((sidinator -> sidinator.get(Direction.EAST))) + ).apply(instance, ItemSidinator::new)); + public static final PacketCodec PACKET_CODEC = new PacketCodec<>() { + @Override + public ItemSidinator decode(RegistryByteBuf buf) { + ItemSidinator.Builder builder = ItemSidinator.builder(); + for (Direction value : Direction.values()) { + int size = buf.readInt(); + int[] ids = new int[size]; + for (int i = 0; i < size; i++) { + ids[i] = buf.readInt(); + } + builder.add(value, ids); + } + return builder.build(); + } + + @Override + public void encode(RegistryByteBuf buf, ItemSidinator value) { + value.forEach((direction, ids) -> { + buf.writeInt(ids.size()); + ids.forEach(buf::writeInt); + }); + } + }; + + + private final List down; + private final List up; + private final List north; + private final List south; + private final List west; + private final List east; + + private ItemSidinator( + List down, + List up, + List north, + List south, + List west, + List east + ) { + this.down = down; + this.up = up; + this.north = north; + this.south = south; + this.west = west; + this.east = east; + } + + public static Builder builder() { + return new Builder(); + } + + @Override + public List get(Direction direction) { + return switch (direction) { + case DOWN -> down; + case UP -> up; + case NORTH -> north; + case SOUTH -> south; + case WEST -> west; + case EAST -> east; + }; + } + + public void forEach(BiConsumer> biConsumer) { + for (Direction value : Direction.values()) { + biConsumer.accept(value, get(value)); + } + } + + public static class Builder { + private Builder() {} + + private final List down = new ArrayList<>(); + private final List up = new ArrayList<>(); + private final List north = new ArrayList<>(); + private final List south = new ArrayList<>(); + private final List west = new ArrayList<>(); + private final List east = new ArrayList<>(); + + public Builder side(int... values) { + return add(Direction.EAST, values).add(Direction.WEST, values).add(Direction.SOUTH, values).add(Direction.NORTH, values); + } + + public Builder top(int... values) { + return add(Direction.UP, values); + } + + public Builder bottom(int... values) { + return add(Direction.DOWN, values); + } + + public Builder add(Direction direction, int... values) { + get(direction).addAll(Arrays.stream(values).boxed().toList()); + return this; + } + + private List get(Direction direction) { + return switch (direction) { + case DOWN -> down; + case UP -> up; + case NORTH -> north; + case SOUTH -> south; + case WEST -> west; + case EAST -> east; + }; + } + + public ItemSidinator build() { + return new ItemSidinator( + down.stream().distinct().toList(), + up.stream().distinct().toList(), + north.stream().distinct().toList(), + south.stream().distinct().toList(), + west.stream().distinct().toList(), + east.stream().distinct().toList() + ); + } + } +} diff --git a/data/common/src/main/java/band/kessoku/lib/impl/Progress.java b/data/common/src/main/java/band/kessoku/lib/impl/Progress.java index feb32e57..2246464f 100644 --- a/data/common/src/main/java/band/kessoku/lib/impl/Progress.java +++ b/data/common/src/main/java/band/kessoku/lib/impl/Progress.java @@ -1,12 +1,12 @@ package band.kessoku.lib.impl; -import band.kessoku.lib.api.data.DataStructure; +import band.kessoku.lib.api.data.Timer; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; -public class Progress implements DataStructure { +public class Progress implements Timer { public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( Codec.INT.fieldOf("time").forGetter(Progress::time), Codec.INT.fieldOf("totalTime").forGetter(Progress::totalTime) @@ -30,11 +30,10 @@ public void encode(RegistryByteBuf buf, Progress value) { private Progress(int time, int totalTime) { this.time = time; this.totalTime = totalTime; - } - public Progress(int totalTime) { - this.totalTime = totalTime; + public static Progress create(int totalTime) { + return new Progress(0, totalTime); } public void process() {