Skip to content

Commit

Permalink
Transfer Base
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemiofitor4096 committed Nov 30, 2024
1 parent 41e6be6 commit 3bed7bf
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<T extends BlockEntity, S extends DataStructure> extends DataAccess<T, S> {

}
Original file line number Diff line number Diff line change
@@ -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 <T extends BlockEntity, S extends DataStructure> void add(BlockEntityType<T> type, BlockEntityAccess<T, S> access) {

}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
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;

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()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package band.kessoku.lib.api.data;

import net.minecraft.util.math.Direction;

import java.util.List;

public interface Sidinator<S extends DataStructure> extends DataStructure {
List<Integer> get(Direction direction);
}
24 changes: 24 additions & 0 deletions data/common/src/main/java/band/kessoku/lib/api/data/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package band.kessoku.lib.api.data;

import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;

public interface Storage<T> extends DataStructure {
T get(int id);

int size();

static Storage<ItemStack> inventory(Inventory inventory) {
return new Storage<>() {
@Override
public ItemStack get(int id) {
return inventory.getStack(id);
}

@Override
public int size() {
return inventory.size();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package band.kessoku.lib.api.data;

public interface Timer extends DataStructure {
int time();

int totalTime();
}
145 changes: 145 additions & 0 deletions data/common/src/main/java/band/kessoku/lib/impl/ItemSidinator.java
Original file line number Diff line number Diff line change
@@ -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<Storage<ItemStack>> {
public static Codec<ItemSidinator> 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<RegistryByteBuf, ItemSidinator> 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<Integer> down;
private final List<Integer> up;
private final List<Integer> north;
private final List<Integer> south;
private final List<Integer> west;
private final List<Integer> east;

private ItemSidinator(
List<Integer> down,
List<Integer> up,
List<Integer> north,
List<Integer> south,
List<Integer> west,
List<Integer> 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<Integer> 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<Direction, List<Integer>> biConsumer) {
for (Direction value : Direction.values()) {
biConsumer.accept(value, get(value));
}
}

public static class Builder {
private Builder() {}

private final List<Integer> down = new ArrayList<>();
private final List<Integer> up = new ArrayList<>();
private final List<Integer> north = new ArrayList<>();
private final List<Integer> south = new ArrayList<>();
private final List<Integer> west = new ArrayList<>();
private final List<Integer> 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<Integer> 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()
);
}
}
}
9 changes: 4 additions & 5 deletions data/common/src/main/java/band/kessoku/lib/impl/Progress.java
Original file line number Diff line number Diff line change
@@ -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<Progress> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("time").forGetter(Progress::time),
Codec.INT.fieldOf("totalTime").forGetter(Progress::totalTime)
Expand All @@ -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() {
Expand Down

0 comments on commit 3bed7bf

Please sign in to comment.