From fdd9be1b302842b0f0374623f5222cf7048595ba Mon Sep 17 00:00:00 2001 From: chemiofitor <98449295+chemiofitor@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:37:49 +0800 Subject: [PATCH] ok --- build.gradle | 23 +++ src/main/generated/.cache/cache | 6 + src/main/java/ho/artisan/lib/HOLibrary.java | 21 ++- .../artisan/lib/common/block/HODataBlock.java | 31 ++++ .../lib/common/block/HOReserveBlock.java | 41 ++++++ .../lib/common/blockentity/HOBlockEntity.java | 106 ++++++++++++++ .../blockentity/HOScreenBlockEntity.java | 55 ++++++++ .../ho/artisan/lib/common/item/FuelItem.java | 4 - .../screen/handler/HOScreenHandler.java | 19 +++ .../ho/artisan/lib/common/slot/HOSlot.java | 61 ++++++++ .../java/ho/artisan/lib/data/BaseData.java | 16 +++ src/main/java/ho/artisan/lib/data/IData.java | 31 ++++ .../java/ho/artisan/lib/data/IScreen.java | 10 ++ .../java/ho/artisan/lib/data/Observable.java | 11 ++ .../ho/artisan/lib/data/base/BooleanData.java | 45 ++++++ .../ho/artisan/lib/data/base/ByteData.java | 45 ++++++ .../ho/artisan/lib/data/base/CharData.java | 45 ++++++ .../ho/artisan/lib/data/base/DoubleData.java | 46 ++++++ .../ho/artisan/lib/data/base/FloatData.java | 45 ++++++ .../ho/artisan/lib/data/base/IntData.java | 45 ++++++ .../ho/artisan/lib/data/base/LongData.java | 45 ++++++ .../ho/artisan/lib/data/base/ShortData.java | 45 ++++++ .../lib/data/complex/SlotListItemData.java | 36 +++++ .../lib/data/container/ListItemData.java | 133 ++++++++++++++++++ .../lib/data/container/SingleItemData.java | 39 +++++ .../lib/data/container/StackItemData.java | 120 ++++++++++++++++ .../artisan/lib/data/misc/DirectionData.java | 42 ++++++ .../ho/artisan/lib/data/misc/StringData.java | 38 +++++ .../lib/example/HORegistryHandler.java | 60 -------- .../artisan/lib/gen/IdentifierGenerator.java | 23 --- .../java/ho/artisan/lib/gen/TagGenerator.java | 24 ---- .../registrar/BlockEntityTypeRegistrar.java | 13 +- .../artisan/lib/registrar/BlockRegistrar.java | 16 +++ .../{registry => }/registrar/IRegistrar.java | 23 +-- .../artisan/lib/registrar/ItemRegistrar.java | 21 +++ .../MinecraftRegistrar.java} | 20 ++- .../registrar/ScreenHandlerTypeRegistrar.java | 18 +++ .../java/ho/artisan/lib/registry/Casket.java | 52 ------- .../registry/registrar/BlockRegistrar.java | 56 -------- .../lib/registry/registrar/ItemRegistrar.java | 82 ----------- .../ho/artisan/lib/test/TestHOLibrary.java | 59 ++++++++ .../artisan/lib/test/list/TestItemData.java | 37 +++++ .../artisan/lib/test/list/TestListBlock.java | 24 ++++ .../lib/test/list/TestListBlockEntity.java | 43 ++++++ .../ho/artisan/lib/test/list/TestScreen.java | 38 +++++ .../lib/test/list/TestScreenHandler.java | 70 +++++++++ .../ho/artisan/lib/test/stack/TestBlock.java | 38 +++++ .../lib/test/stack/TestBlockEntity.java | 20 +++ .../artisan/lib/test/stack/TestRenderer.java | 46 ++++++ .../resources/assets/ho/blockstates/test.json | 7 + .../assets/ho/blockstates/test_2.json | 7 + .../resources/assets/{holib => ho}/icon.png | Bin .../resources/assets/ho/models/item/test.json | 6 + .../assets/ho/models/item/test_2.json | 6 + .../resources/assets/ho/textures/gui/test.png | Bin 0 -> 857 bytes .../assets/ho/textures/item/coal/iron.png | Bin 0 -> 379 bytes .../assets/ho/textures/item/coal_sword.png | Bin 0 -> 295 bytes src/main/resources/fabric.mod.json | 14 +- src/main/resources/holib.accesswidener | 7 +- .../{holib.mixin.json => holib.mixins.json} | 2 +- 60 files changed, 1682 insertions(+), 354 deletions(-) create mode 100644 src/main/generated/.cache/cache create mode 100644 src/main/java/ho/artisan/lib/common/block/HODataBlock.java create mode 100644 src/main/java/ho/artisan/lib/common/block/HOReserveBlock.java create mode 100644 src/main/java/ho/artisan/lib/common/blockentity/HOBlockEntity.java create mode 100644 src/main/java/ho/artisan/lib/common/blockentity/HOScreenBlockEntity.java create mode 100644 src/main/java/ho/artisan/lib/common/screen/handler/HOScreenHandler.java create mode 100644 src/main/java/ho/artisan/lib/common/slot/HOSlot.java create mode 100644 src/main/java/ho/artisan/lib/data/BaseData.java create mode 100644 src/main/java/ho/artisan/lib/data/IData.java create mode 100644 src/main/java/ho/artisan/lib/data/IScreen.java create mode 100644 src/main/java/ho/artisan/lib/data/Observable.java create mode 100644 src/main/java/ho/artisan/lib/data/base/BooleanData.java create mode 100644 src/main/java/ho/artisan/lib/data/base/ByteData.java create mode 100644 src/main/java/ho/artisan/lib/data/base/CharData.java create mode 100644 src/main/java/ho/artisan/lib/data/base/DoubleData.java create mode 100644 src/main/java/ho/artisan/lib/data/base/FloatData.java create mode 100644 src/main/java/ho/artisan/lib/data/base/IntData.java create mode 100644 src/main/java/ho/artisan/lib/data/base/LongData.java create mode 100644 src/main/java/ho/artisan/lib/data/base/ShortData.java create mode 100644 src/main/java/ho/artisan/lib/data/complex/SlotListItemData.java create mode 100644 src/main/java/ho/artisan/lib/data/container/ListItemData.java create mode 100644 src/main/java/ho/artisan/lib/data/container/SingleItemData.java create mode 100644 src/main/java/ho/artisan/lib/data/container/StackItemData.java create mode 100644 src/main/java/ho/artisan/lib/data/misc/DirectionData.java create mode 100644 src/main/java/ho/artisan/lib/data/misc/StringData.java delete mode 100644 src/main/java/ho/artisan/lib/example/HORegistryHandler.java delete mode 100644 src/main/java/ho/artisan/lib/gen/IdentifierGenerator.java delete mode 100644 src/main/java/ho/artisan/lib/gen/TagGenerator.java rename src/main/java/ho/artisan/lib/{registry => }/registrar/BlockEntityTypeRegistrar.java (61%) create mode 100644 src/main/java/ho/artisan/lib/registrar/BlockRegistrar.java rename src/main/java/ho/artisan/lib/{registry => }/registrar/IRegistrar.java (51%) create mode 100644 src/main/java/ho/artisan/lib/registrar/ItemRegistrar.java rename src/main/java/ho/artisan/lib/{registry/registrar/VanillaRegistrar.java => registrar/MinecraftRegistrar.java} (56%) create mode 100644 src/main/java/ho/artisan/lib/registrar/ScreenHandlerTypeRegistrar.java delete mode 100644 src/main/java/ho/artisan/lib/registry/Casket.java delete mode 100644 src/main/java/ho/artisan/lib/registry/registrar/BlockRegistrar.java delete mode 100644 src/main/java/ho/artisan/lib/registry/registrar/ItemRegistrar.java create mode 100644 src/main/java/ho/artisan/lib/test/TestHOLibrary.java create mode 100644 src/main/java/ho/artisan/lib/test/list/TestItemData.java create mode 100644 src/main/java/ho/artisan/lib/test/list/TestListBlock.java create mode 100644 src/main/java/ho/artisan/lib/test/list/TestListBlockEntity.java create mode 100644 src/main/java/ho/artisan/lib/test/list/TestScreen.java create mode 100644 src/main/java/ho/artisan/lib/test/list/TestScreenHandler.java create mode 100644 src/main/java/ho/artisan/lib/test/stack/TestBlock.java create mode 100644 src/main/java/ho/artisan/lib/test/stack/TestBlockEntity.java create mode 100644 src/main/java/ho/artisan/lib/test/stack/TestRenderer.java create mode 100644 src/main/resources/assets/ho/blockstates/test.json create mode 100644 src/main/resources/assets/ho/blockstates/test_2.json rename src/main/resources/assets/{holib => ho}/icon.png (100%) create mode 100644 src/main/resources/assets/ho/models/item/test.json create mode 100644 src/main/resources/assets/ho/models/item/test_2.json create mode 100644 src/main/resources/assets/ho/textures/gui/test.png create mode 100644 src/main/resources/assets/ho/textures/item/coal/iron.png create mode 100644 src/main/resources/assets/ho/textures/item/coal_sword.png rename src/main/resources/{holib.mixin.json => holib.mixins.json} (90%) diff --git a/build.gradle b/build.gradle index 1a862de..6f9493b 100644 --- a/build.gradle +++ b/build.gradle @@ -10,6 +10,29 @@ loom { accessWidenerPath = file("src/main/resources/holib.accesswidener") } +loom { + runs { + datagen { + inherit client + name "Minecraft DataGeneration" + vmArg "-Dfabric-api.datagen" + vmArg "-Dfabric-api.datagen.output-dir=${file("src/main/generated")}" + + runDir "build/datagen" + } + } +} + +sourceSets { + main { + resources { + srcDirs += [ + 'src/main/generated' + ] + } + } +} + repositories { maven { url "https://maven.terraformersmc.com/releases/" } maven { url "https://api.modrinth.com/maven" } diff --git a/src/main/generated/.cache/cache b/src/main/generated/.cache/cache new file mode 100644 index 0000000..fa9f364 --- /dev/null +++ b/src/main/generated/.cache/cache @@ -0,0 +1,6 @@ +bbb381f96e51e6e0d287f582143af547aa5f19b1 assets\ho\models\item\coal_sword.json +8021ad86c3499788e1ca1453352a9340398451b6 assets\ho\models\item\iron_coal.json +dcff91ebced2ae747965a906aa4a0580e22b4890 assets\holib\lang\en_us.json +bf7ab9938c517f88614ff7ebfde9c93ae70e36bc data\c\tags\items\coal\iron.json +bf7ab9938c517f88614ff7ebfde9c93ae70e36bc data\c\tags\items\coal.json +77ed1d946b345861306eb581756dc9c14ec55168 assets\holib\lang\zh_cn.json diff --git a/src/main/java/ho/artisan/lib/HOLibrary.java b/src/main/java/ho/artisan/lib/HOLibrary.java index e2109c5..35bbe27 100644 --- a/src/main/java/ho/artisan/lib/HOLibrary.java +++ b/src/main/java/ho/artisan/lib/HOLibrary.java @@ -1,21 +1,28 @@ package ho.artisan.lib; -import ho.artisan.lib.example.HORegistryHandler; +import ho.artisan.lib.test.TestHOLibrary; +import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.FabricLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class HOLibrary implements ModInitializer { +public class HOLibrary implements ModInitializer, ClientModInitializer { public static final String MOD_ID = "holib"; - public static final String MOD_NAME = "HOLibrary"; + public static final String MOD_NAME = "HO-Library"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME); @Override public void onInitialize() { - if (FabricLoader.getInstance().isDevelopmentEnvironment()) { - HORegistryHandler.load(); - } - LOGGER.info(MOD_NAME + " has set up!"); + if (FabricLoader.getInstance().isDevelopmentEnvironment()) + TestHOLibrary.onInitialize(); + LOGGER.info("HO-Library has set up!"); + } + + @Override + public void onInitializeClient() { + if (FabricLoader.getInstance().isDevelopmentEnvironment()) + TestHOLibrary.onInitializeClient(); + LOGGER.info("HO-Library has set up in client-side!"); } } diff --git a/src/main/java/ho/artisan/lib/common/block/HODataBlock.java b/src/main/java/ho/artisan/lib/common/block/HODataBlock.java new file mode 100644 index 0000000..7616041 --- /dev/null +++ b/src/main/java/ho/artisan/lib/common/block/HODataBlock.java @@ -0,0 +1,31 @@ +package ho.artisan.lib.common.block; + +import ho.artisan.lib.common.blockentity.HOBlockEntity; +import net.minecraft.block.BlockRenderType; +import net.minecraft.block.BlockState; +import net.minecraft.block.BlockWithEntity; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.util.math.BlockPos; +import org.jetbrains.annotations.Nullable; + +import java.util.function.BiFunction; + +public class HODataBlock extends BlockWithEntity { + private final BiFunction tBuilder; + + public HODataBlock(Settings settings, BiFunction tBuilder) { + super(settings); + this.tBuilder = tBuilder; + } + + @Override + public BlockRenderType getRenderType(BlockState state) { + return BlockRenderType.MODEL; + } + + @Nullable + @Override + public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { + return tBuilder.apply(pos, state); + } +} diff --git a/src/main/java/ho/artisan/lib/common/block/HOReserveBlock.java b/src/main/java/ho/artisan/lib/common/block/HOReserveBlock.java new file mode 100644 index 0000000..7c4d650 --- /dev/null +++ b/src/main/java/ho/artisan/lib/common/block/HOReserveBlock.java @@ -0,0 +1,41 @@ +package ho.artisan.lib.common.block; + +import ho.artisan.lib.common.blockentity.HOBlockEntity; +import net.minecraft.block.BlockState; +import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.BlockView; +import net.minecraft.world.World; + +import java.util.function.BiFunction; + +public class HOReserveBlock extends HODataBlock { + public HOReserveBlock(Settings settings, BiFunction tBuilder) { + super(settings, tBuilder); + } + + @Override + public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) { + ItemStack itemStack = super.getPickStack(world, pos, state); + if (world.getBlockEntity(pos) instanceof HOBlockEntity blockEntity) { + blockEntity.writeReserves(itemStack); + } + return itemStack; + } + + @Override + public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) { + if (world.getBlockEntity(pos) instanceof HOBlockEntity blockEntity) { + if (!world.isClient && player.isCreative() && !blockEntity.getReserves().isEmpty()) { + ItemStack itemStack = new ItemStack(this); + blockEntity.writeReserves(itemStack); + ItemEntity itemEntity = new ItemEntity(world, (double)pos.getX() + 0.5, (double)pos.getY() + 0.5, (double)pos.getZ() + 0.5, itemStack); + itemEntity.setToDefaultPickupDelay(); + world.spawnEntity(itemEntity); + } + } + super.onBreak(world, pos, state, player); + } +} diff --git a/src/main/java/ho/artisan/lib/common/blockentity/HOBlockEntity.java b/src/main/java/ho/artisan/lib/common/blockentity/HOBlockEntity.java new file mode 100644 index 0000000..92a537d --- /dev/null +++ b/src/main/java/ho/artisan/lib/common/blockentity/HOBlockEntity.java @@ -0,0 +1,106 @@ +package ho.artisan.lib.common.blockentity; + +import ho.artisan.lib.data.IData; +import ho.artisan.lib.data.Observable; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.Packet; +import net.minecraft.network.listener.ClientPlayPacketListener; +import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; +import net.minecraft.util.math.BlockPos; +import org.jetbrains.annotations.Nullable; + +import java.util.*; + +public abstract class HOBlockEntity extends BlockEntity { + private final Map> dataMap = new HashMap<>(); + private final Set reserveDataKeys = new TreeSet<>(); + + public HOBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { + super(type, pos, state); + } + + public void load(IData... iData) { + for (IData iDatum : iData) { + this.dataMap.put(iDatum.key(), iDatum); + if (iDatum instanceof Observable observable) + observable.addListener("BlockUpdate", (o -> markDirty())); + } + } + + public void reserve(IData... iData) { + for (IData iDatum : iData) { + reserveDataKeys.add(iDatum.key()); + } + } + + @Override + public void markDirty() { + super.markDirty(); + if (world != null) { + world.updateListeners(pos, this.getCachedState(), this.getCachedState(), Block.NOTIFY_LISTENERS); + } + } + + @Override + protected void writeNbt(NbtCompound nbt) { + super.writeNbt(nbt); + getDataMap().forEach((key, value) -> value.write(nbt)); + } + + @Override + public void readNbt(NbtCompound nbt) { + super.readNbt(nbt); + getDataMap().forEach((key, value) -> value.read(nbt)); + } + + @Nullable + @Override + public Packet toUpdatePacket() { + return BlockEntityUpdateS2CPacket.create(this); + } + + @Override + public NbtCompound toInitialChunkDataNbt() { + return createNbt(); + } + + public void writeReserves(ItemStack stack) { + NbtCompound nbt = new NbtCompound(); + for (IData reserve : getReserves()) { + reserve.write(nbt); + } + BlockItem.setBlockEntityNbt(stack, this.getType(), nbt); + } + + public Collection> getReserves() { + Collection> collection = new ArrayList<>(); + getDataMap().forEach((s, data) -> { + if (reserveDataKeys.contains(s)) + collection.add(data); + }); + return collection; + } + + public Map> getDataMap() { + return this.dataMap; + } + + @Nullable + @SuppressWarnings("unchecked") + public IData getData(String key, Class type) { + IData iData = this.dataMap.get(key); + try { + if (type.isInstance(iData.get())) + return (IData) iData; + return null; + } catch (Exception e) { + return null; + } + } +} diff --git a/src/main/java/ho/artisan/lib/common/blockentity/HOScreenBlockEntity.java b/src/main/java/ho/artisan/lib/common/blockentity/HOScreenBlockEntity.java new file mode 100644 index 0000000..105acb1 --- /dev/null +++ b/src/main/java/ho/artisan/lib/common/blockentity/HOScreenBlockEntity.java @@ -0,0 +1,55 @@ +package ho.artisan.lib.common.blockentity; + +import ho.artisan.lib.data.IData; +import ho.artisan.lib.data.IScreen; +import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.math.BlockPos; +import org.jetbrains.annotations.Nullable; + +import java.util.*; + +public abstract class HOScreenBlockEntity extends HOBlockEntity implements ExtendedScreenHandlerFactory { + private final Set screenDataKeys = new TreeSet<>(); + + public HOScreenBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { + super(type, pos, state); + } + + public void screenLoad(IData... iData) { + for (IData iDatum : iData) { + screenDataKeys.add(iDatum.key()); + } + } + + public Collection> getScreenData() { + Collection> collection = new ArrayList<>(); + getDataMap().forEach((s, data) -> { + if (screenDataKeys.contains(s)) + collection.add(data); + }); + return collection; + } + + @Override + public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf) { + getScreenData().forEach(data -> { + if (data instanceof IScreen iScreen) + iScreen.write(player, buf); + else + data.write(buf); + }); + } + + @Nullable + @Override + public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { + return null; + } +} diff --git a/src/main/java/ho/artisan/lib/common/item/FuelItem.java b/src/main/java/ho/artisan/lib/common/item/FuelItem.java index d6352c3..c2d0907 100644 --- a/src/main/java/ho/artisan/lib/common/item/FuelItem.java +++ b/src/main/java/ho/artisan/lib/common/item/FuelItem.java @@ -3,10 +3,6 @@ import net.fabricmc.fabric.api.registry.FuelRegistry; import net.minecraft.item.Item; - -/** - * 使用FabricAPI注册燃烧时间的燃料类物品 - */ public class FuelItem extends Item { public FuelItem(Settings settings, int burnTime) { super(settings); diff --git a/src/main/java/ho/artisan/lib/common/screen/handler/HOScreenHandler.java b/src/main/java/ho/artisan/lib/common/screen/handler/HOScreenHandler.java new file mode 100644 index 0000000..0a90e88 --- /dev/null +++ b/src/main/java/ho/artisan/lib/common/screen/handler/HOScreenHandler.java @@ -0,0 +1,19 @@ +package ho.artisan.lib.common.screen.handler; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; +import org.jetbrains.annotations.Nullable; + +public abstract class HOScreenHandler extends ScreenHandler { + protected HOScreenHandler(@Nullable ScreenHandlerType type, int syncId, PlayerInventory inventory, PacketByteBuf buf) { + super(type, syncId); + } + + @Override + public boolean canUse(PlayerEntity player) { + return false; + } +} diff --git a/src/main/java/ho/artisan/lib/common/slot/HOSlot.java b/src/main/java/ho/artisan/lib/common/slot/HOSlot.java new file mode 100644 index 0000000..28984a9 --- /dev/null +++ b/src/main/java/ho/artisan/lib/common/slot/HOSlot.java @@ -0,0 +1,61 @@ +package ho.artisan.lib.common.slot; + +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.Direction; + +import java.util.*; +import java.util.function.Predicate; + +public class HOSlot { + private final Set availableDirections; + private final List> predicates; + private final int index; + + private HOSlot(int index, Set availableDirections, List> predicates) { + this.availableDirections = availableDirections; + this.index = index; + this.predicates = predicates; + } + + public boolean testItemstack(ItemStack stack) { + for (Predicate predicate : predicates) + if (!predicate.test(stack)) + return false; + return true; + } + + public boolean isAvailable(Direction direction) { + return availableDirections.contains(direction); + } + + public int getIndex() { + return index; + } + + public static class Builder { + private final Set availableDirections = new HashSet<>(); + private final List> predicates = new ArrayList<>(); + private final int index; + + public Builder(int index) { + this.index = index; + availableDirections.addAll(List.of(Direction.values())); + } + + public void predicate(Predicate predicate) { + predicates.add(predicate); + } + + public void available(Direction... directions) { + availableDirections.addAll(Arrays.asList(directions)); + } + + public void unavailable(Direction... directions) { + Arrays.asList(directions).forEach(availableDirections::remove); + } + + public HOSlot create() { + return new HOSlot(index, availableDirections, predicates); + } + } +} diff --git a/src/main/java/ho/artisan/lib/data/BaseData.java b/src/main/java/ho/artisan/lib/data/BaseData.java new file mode 100644 index 0000000..77427db --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/BaseData.java @@ -0,0 +1,16 @@ +package ho.artisan.lib.data; + +import org.jetbrains.annotations.NotNull; + +public abstract class BaseData implements IData { + private final String key; + + public BaseData(String key) { + this.key = key; + } + + @Override + public @NotNull String key() { + return this.key; + } +} diff --git a/src/main/java/ho/artisan/lib/data/IData.java b/src/main/java/ho/artisan/lib/data/IData.java new file mode 100644 index 0000000..0eb0216 --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/IData.java @@ -0,0 +1,31 @@ +package ho.artisan.lib.data; + +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Supplier; + +public interface IData extends Supplier { + void write(NbtCompound nbt); + + void read(NbtCompound nbt); + + @NotNull + String key(); + + @NotNull + @Override + T get(); + + default void write(PacketByteBuf buf) { + NbtCompound compound = new NbtCompound(); + write(compound); + buf.writeNbt(compound); + } + + default void read(PacketByteBuf buf) { + NbtCompound compound = buf.readNbt(); + read(compound); + } +} diff --git a/src/main/java/ho/artisan/lib/data/IScreen.java b/src/main/java/ho/artisan/lib/data/IScreen.java new file mode 100644 index 0000000..0a5964b --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/IScreen.java @@ -0,0 +1,10 @@ +package ho.artisan.lib.data; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; + +public interface IScreen { + void write(ServerPlayerEntity player, PacketByteBuf buf); + + void read(ServerPlayerEntity player, PacketByteBuf buf); +} diff --git a/src/main/java/ho/artisan/lib/data/Observable.java b/src/main/java/ho/artisan/lib/data/Observable.java new file mode 100644 index 0000000..e1d0d36 --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/Observable.java @@ -0,0 +1,11 @@ +package ho.artisan.lib.data; + +import java.util.function.Consumer; + +public interface Observable { + void addListener(String id, Consumer consumer); + + void removeListener(String id); + + void markDirty(); +} diff --git a/src/main/java/ho/artisan/lib/data/base/BooleanData.java b/src/main/java/ho/artisan/lib/data/base/BooleanData.java new file mode 100644 index 0000000..1114fbe --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/base/BooleanData.java @@ -0,0 +1,45 @@ +package ho.artisan.lib.data.base; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +public class BooleanData extends BaseData { + private boolean value; + + public BooleanData(String key, boolean defaultValue) { + super(key); + this.value = defaultValue; + } + + @Override + public void write(NbtCompound nbt) { + nbt.putBoolean(key(), value); + } + + @Override + public void read(NbtCompound nbt) { + set(nbt.getBoolean(key())); + } + + @Override + public void write(PacketByteBuf buf) { + buf.writeBoolean(get()); + } + + @Override + public void read(PacketByteBuf buf) { + set(buf.readBoolean()); + } + + @Override + public @NotNull Boolean get() { + return this.value; + } + + public boolean set(boolean value) { + this.value = value; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/base/ByteData.java b/src/main/java/ho/artisan/lib/data/base/ByteData.java new file mode 100644 index 0000000..3c6cfdd --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/base/ByteData.java @@ -0,0 +1,45 @@ +package ho.artisan.lib.data.base; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +public class ByteData extends BaseData { + private byte value; + + public ByteData(String key, byte defaultValue) { + super(key); + this.value = defaultValue; + } + + @Override + public void write(NbtCompound nbt) { + nbt.putByte(key(), value); + } + + @Override + public void read(NbtCompound nbt) { + set(nbt.getByte(key())); + } + + @Override + public void write(PacketByteBuf buf) { + buf.writeByte(get()); + } + + @Override + public void read(PacketByteBuf buf) { + set(buf.readByte()); + } + + @Override + public @NotNull Byte get() { + return this.value; + } + + public boolean set(byte value) { + this.value = value; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/base/CharData.java b/src/main/java/ho/artisan/lib/data/base/CharData.java new file mode 100644 index 0000000..58984d5 --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/base/CharData.java @@ -0,0 +1,45 @@ +package ho.artisan.lib.data.base; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +public class CharData extends BaseData { + private char value; + + public CharData(String key, char defaultValue) { + super(key); + this.value = defaultValue; + } + + @Override + public void write(NbtCompound nbt) { + nbt.putByte(key(), (byte) value); + } + + @Override + public void read(NbtCompound nbt) { + set((char) nbt.getByte(key())); + } + + @Override + public void write(PacketByteBuf buf) { + buf.writeChar(get()); + } + + @Override + public void read(PacketByteBuf buf) { + set(buf.readChar()); + } + + @Override + public @NotNull Character get() { + return this.value; + } + + public boolean set(char value) { + this.value = value; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/base/DoubleData.java b/src/main/java/ho/artisan/lib/data/base/DoubleData.java new file mode 100644 index 0000000..5b9c31f --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/base/DoubleData.java @@ -0,0 +1,46 @@ +package ho.artisan.lib.data.base; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +public class DoubleData extends BaseData { + private double value; + + public DoubleData(String key, double defaultValue) { + super(key); + this.value = defaultValue; + } + + @Override + public void write(NbtCompound nbt) { + nbt.putDouble(key(), value); + } + + @Override + public void read(NbtCompound nbt) { + set(nbt.getDouble(key())); + } + + @Override + public void write(PacketByteBuf buf) { + buf.writeDouble(get()); + } + + @Override + public void read(PacketByteBuf buf) { + set(buf.readDouble()); + } + + @Override + public @NotNull Double get() { + return this.value; + } + + + public boolean set(double value) { + this.value = value; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/base/FloatData.java b/src/main/java/ho/artisan/lib/data/base/FloatData.java new file mode 100644 index 0000000..fb283d1 --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/base/FloatData.java @@ -0,0 +1,45 @@ +package ho.artisan.lib.data.base; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +public class FloatData extends BaseData { + private float value; + + public FloatData(String key, float defaultValue) { + super(key); + this.value = defaultValue; + } + + @Override + public void write(NbtCompound nbt) { + nbt.putFloat(key(), value); + } + + @Override + public void read(NbtCompound nbt) { + set(nbt.getFloat(key())); + } + + @Override + public void write(PacketByteBuf buf) { + buf.writeFloat(get()); + } + + @Override + public void read(PacketByteBuf buf) { + set(buf.readFloat()); + } + + @Override + public @NotNull Float get() { + return this.value; + } + + public boolean set(float value) { + this.value = value; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/base/IntData.java b/src/main/java/ho/artisan/lib/data/base/IntData.java new file mode 100644 index 0000000..78b27da --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/base/IntData.java @@ -0,0 +1,45 @@ +package ho.artisan.lib.data.base; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +public class IntData extends BaseData { + private int value; + + public IntData(String key, int defaultValue) { + super(key); + this.value = defaultValue; + } + + @Override + public void write(NbtCompound nbt) { + nbt.putInt(key(), value); + } + + @Override + public void read(NbtCompound nbt) { + set(nbt.getInt(key())); + } + + @Override + public void write(PacketByteBuf buf) { + buf.writeInt(get()); + } + + @Override + public void read(PacketByteBuf buf) { + set(buf.readInt()); + } + + @Override + public @NotNull Integer get() { + return this.value; + } + + public boolean set(int value) { + this.value = value; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/base/LongData.java b/src/main/java/ho/artisan/lib/data/base/LongData.java new file mode 100644 index 0000000..054d348 --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/base/LongData.java @@ -0,0 +1,45 @@ +package ho.artisan.lib.data.base; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +public class LongData extends BaseData { + private long value; + + public LongData(String key, long defaultValue) { + super(key); + this.value = defaultValue; + } + + @Override + public void write(NbtCompound nbt) { + nbt.putLong(key(), value); + } + + @Override + public void read(NbtCompound nbt) { + set(nbt.getLong(key())); + } + + @Override + public void write(PacketByteBuf buf) { + buf.writeLong(get()); + } + + @Override + public void read(PacketByteBuf buf) { + set(buf.readLong()); + } + + @Override + public @NotNull Long get() { + return this.value; + } + + public boolean set(long value) { + this.value = value; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/base/ShortData.java b/src/main/java/ho/artisan/lib/data/base/ShortData.java new file mode 100644 index 0000000..d5024b3 --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/base/ShortData.java @@ -0,0 +1,45 @@ +package ho.artisan.lib.data.base; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import org.jetbrains.annotations.NotNull; + +public class ShortData extends BaseData { + private short value; + + public ShortData(String key, short defaultValue) { + super(key); + this.value = defaultValue; + } + + @Override + public void write(NbtCompound nbt) { + nbt.putShort(key(), value); + } + + @Override + public void read(NbtCompound nbt) { + set(nbt.getShort(key())); + } + + @Override + public void write(PacketByteBuf buf) { + buf.writeShort(get()); + } + + @Override + public void read(PacketByteBuf buf) { + set(buf.readShort()); + } + + @Override + public @NotNull Short get() { + return this.value; + } + + public boolean set(short value) { + this.value = value; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/complex/SlotListItemData.java b/src/main/java/ho/artisan/lib/data/complex/SlotListItemData.java new file mode 100644 index 0000000..a85c568 --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/complex/SlotListItemData.java @@ -0,0 +1,36 @@ +package ho.artisan.lib.data.complex; + +import ho.artisan.lib.common.slot.HOSlot; +import ho.artisan.lib.data.container.ListItemData; +import net.minecraft.inventory.SidedInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.Direction; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class SlotListItemData extends ListItemData implements SidedInventory { + private final List slots; + + public SlotListItemData(String key, HOSlot... slots) { + super(key, slots.length); + this.slots = List.of(slots); + } + + @Override + public int[] getAvailableSlots(Direction side) { + return slots.stream().filter(slot -> slot.isAvailable(side)).mapToInt(HOSlot::getIndex).toArray(); + } + + @Override + public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) { + var optional = slots.stream().filter(hoSlot -> hoSlot.getIndex() == slot).findFirst(); + return optional.filter(hoSlot -> hoSlot.testItemstack(stack) && hoSlot.isAvailable(dir)).isPresent(); + } + + @Override + public boolean canExtract(int slot, ItemStack stack, Direction dir) { + var optional = slots.stream().filter(hoSlot -> hoSlot.getIndex() == slot).findFirst(); + return optional.filter(hoSlot -> hoSlot.testItemstack(stack) && hoSlot.isAvailable(dir)).isPresent(); + } +} diff --git a/src/main/java/ho/artisan/lib/data/container/ListItemData.java b/src/main/java/ho/artisan/lib/data/container/ListItemData.java new file mode 100644 index 0000000..af0d15a --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/container/ListItemData.java @@ -0,0 +1,133 @@ +package ho.artisan.lib.data.container; + +import ho.artisan.lib.data.IData; +import ho.artisan.lib.data.Observable; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventories; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.nbt.NbtList; +import org.jetbrains.annotations.NotNull; + +import java.util.*; +import java.util.function.Consumer; + +public class ListItemData implements IData>, Inventory, Iterable, Observable { + private final List container; + private final int size; + private final String key; + + public ListItemData(String key, int size) { + this.container = new ArrayList<>(size); + this.size = size; + this.key = key; + for (int i = 0; i < size; i++) { + this.container.add(ItemStack.EMPTY); + } + } + + @Override + public void write(NbtCompound nbt) { + NbtList list = new NbtList(); + for (ItemStack itemStack : this.container) { + list.add(itemStack.writeNbt(new NbtCompound())); + } + nbt.put(key(), list); + } + + @Override + public void read(NbtCompound nbt) { + this.clear(); + NbtList list = nbt.getList(key(), NbtElement.COMPOUND_TYPE); + for (NbtElement nbtElement : list) { + if (nbtElement instanceof NbtCompound nbtCompound) + this.container.add(ItemStack.fromNbt(nbtCompound)); + } + } + + @Override + public @NotNull String key() { + return this.key; + } + + @Override + public @NotNull List get() { + return this.container; + } + + @NotNull + @Override + public Iterator iterator() { + return this.container.iterator(); + } + + @Override + public void clear() { + this.container.clear(); + markDirty(); + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + for (ItemStack itemStack : this.container) { + if (itemStack != ItemStack.EMPTY) + return false; + } + return true; + } + + @Override + public ItemStack getStack(int slot) { + return this.container.get(slot); + } + + @Override + public ItemStack removeStack(int slot, int amount) { + var stack = Inventories.splitStack(this.container, slot, amount); + if (!stack.isEmpty()) + markDirty(); + return stack; + } + + @Override + public ItemStack removeStack(int slot) { + return Inventories.removeStack(this.container, slot); + } + + @Override + public void setStack(int slot, ItemStack stack) { + this.container.set(slot, stack); + markDirty(); + } + + @Override + public boolean canPlayerUse(PlayerEntity player) { + return true; + } + + private final Map> listeners = new HashMap<>(); + + @Override + public void addListener(String id, Consumer consumer) { + this.listeners.put(id, consumer); + } + + @Override + public void removeListener(String id) { + this.listeners.remove(id); + } + + @Override + public void markDirty() { + for (Consumer listener : listeners.values()) { + listener.accept(this); + } + } +} diff --git a/src/main/java/ho/artisan/lib/data/container/SingleItemData.java b/src/main/java/ho/artisan/lib/data/container/SingleItemData.java new file mode 100644 index 0000000..6ac9fe3 --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/container/SingleItemData.java @@ -0,0 +1,39 @@ +package ho.artisan.lib.data.container; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import org.jetbrains.annotations.NotNull; + +public class SingleItemData extends BaseData { + private ItemStack itemStack; + + private SingleItemData(String key, ItemStack itemStack) { + super(key); + this.itemStack = itemStack; + } + + public static SingleItemData create(String key, ItemStack itemStack) { + return new SingleItemData(key, itemStack); + } + + @Override + public void write(NbtCompound nbt) { + nbt.put(key(), itemStack.writeNbt(new NbtCompound())); + } + + @Override + public void read(NbtCompound nbt) { + set(ItemStack.fromNbt(nbt.getCompound(key()))); + } + + public boolean set(ItemStack itemStack) { + this.itemStack = itemStack; + return true; + } + + @Override + public @NotNull ItemStack get() { + return this.itemStack; + } +} diff --git a/src/main/java/ho/artisan/lib/data/container/StackItemData.java b/src/main/java/ho/artisan/lib/data/container/StackItemData.java new file mode 100644 index 0000000..629191d --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/container/StackItemData.java @@ -0,0 +1,120 @@ +package ho.artisan.lib.data.container; + +import ho.artisan.lib.data.IData; +import ho.artisan.lib.data.Observable; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.nbt.NbtList; +import net.minecraft.util.Clearable; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Stack; +import java.util.function.Consumer; + +public class StackItemData implements IData>, Clearable, Iterable, Observable { + private final Stack container = new Stack<>(); + private final String key; + private final int size; + + public StackItemData(String key, int size) { + this.key = key; + this.size = size; + } + + public ItemStack pop() { + if (!this.container.isEmpty()) { + ItemStack stack = this.container.pop(); + if (!stack.isEmpty()) + markDirty(); + return stack; + } + return ItemStack.EMPTY; + } + + public boolean add(ItemStack stack) { + if (!stack.isEmpty()) { + container.add(stack); + markDirty(); + return true; + } + return false; + } + + public ItemStack peek() { + if (!this.container.isEmpty()) + return container.peek(); + return ItemStack.EMPTY; + } + + public boolean isEmpty() { + return container.isEmpty(); + } + + public boolean isAddible() { + return container.size() + 1 <= this.size; + } + + @Override + public void write(NbtCompound nbt) { + NbtList list = new NbtList(); + for (ItemStack itemStack : this.container) { + list.add(itemStack.writeNbt(new NbtCompound())); + } + nbt.put(key(), list); + } + + @Override + public void read(NbtCompound nbt) { + this.clear(); + NbtList list = nbt.getList(key(), NbtElement.COMPOUND_TYPE); + for (NbtElement nbtElement : list) { + if (nbtElement instanceof NbtCompound nbtCompound) + this.add(ItemStack.fromNbt(nbtCompound)); + } + } + + @Override + public @NotNull String key() { + return this.key; + } + + @Override + public @NotNull Stack get() { + return this.container; + } + + @Override + public void clear() { + this.container.clear(); + markDirty(); + } + + @NotNull + @Override + public Iterator iterator() { + return this.container.iterator(); + } + + private final Map> listeners = new HashMap<>(); + + @Override + public void addListener(String id, Consumer consumer) { + this.listeners.put(id, consumer); + } + + @Override + public void removeListener(String id) { + this.listeners.remove(id); + } + + @Override + public void markDirty() { + for (Consumer listener : listeners.values()) { + listener.accept(this); + } + } +} diff --git a/src/main/java/ho/artisan/lib/data/misc/DirectionData.java b/src/main/java/ho/artisan/lib/data/misc/DirectionData.java new file mode 100644 index 0000000..1f7b57c --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/misc/DirectionData.java @@ -0,0 +1,42 @@ +package ho.artisan.lib.data.misc; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.util.math.Direction; +import org.jetbrains.annotations.NotNull; + +public class DirectionData extends BaseData { + private Direction direction; + + public DirectionData(String key, Direction direction) { + super(key); + this.direction = direction; + } + + @Override + public void write(NbtCompound root) { + NbtCompound nbt = new NbtCompound(); + nbt.putString("Type", "Direction"); + nbt.putString("Value", get().toString()); + root.put(key(), nbt); + } + + @Override + public void read(NbtCompound root) { + NbtCompound nbt = root.getCompound(key()); + if (nbt.getString("Type").equals("Direction")) { + set(Direction.valueOf(nbt.getString("Value").toUpperCase())); + } + } + + @Override + public @NotNull Direction get() { + return this.direction; + } + + + public boolean set(Direction direction) { + this.direction = direction; + return true; + } +} diff --git a/src/main/java/ho/artisan/lib/data/misc/StringData.java b/src/main/java/ho/artisan/lib/data/misc/StringData.java new file mode 100644 index 0000000..560ad4f --- /dev/null +++ b/src/main/java/ho/artisan/lib/data/misc/StringData.java @@ -0,0 +1,38 @@ +package ho.artisan.lib.data.misc; + +import ho.artisan.lib.data.BaseData; +import net.minecraft.nbt.NbtCompound; +import org.jetbrains.annotations.NotNull; + +public class StringData extends BaseData { + private String value; + + private StringData(String key, String value) { + super(key); + this.value = value; + } + + public static StringData create(String key, String value) { + return new StringData(key, value); + } + + @Override + public void write(NbtCompound nbt) { + nbt.putString(key(), get()); + } + + @Override + public void read(NbtCompound nbt) { + set(nbt.getString(key())); + } + + public boolean set(String value) { + this.value = value; + return true; + } + + @Override + public @NotNull String get() { + return this.value; + } +} diff --git a/src/main/java/ho/artisan/lib/example/HORegistryHandler.java b/src/main/java/ho/artisan/lib/example/HORegistryHandler.java deleted file mode 100644 index c17213f..0000000 --- a/src/main/java/ho/artisan/lib/example/HORegistryHandler.java +++ /dev/null @@ -1,60 +0,0 @@ -package ho.artisan.lib.example; - -import ho.artisan.lib.common.item.FuelItem; -import ho.artisan.lib.registry.registrar.BlockRegistrar; -import ho.artisan.lib.registry.registrar.ItemRegistrar; -import net.minecraft.block.AbstractBlock; -import net.minecraft.block.Block; -import net.minecraft.block.Blocks; -import net.minecraft.item.BlockItem; -import net.minecraft.item.Item; -import net.minecraft.item.SwordItem; -import net.minecraft.item.ToolMaterials; -import net.minecraft.util.Rarity; - -public class HORegistryHandler { - public static final Block IRON_COAL_BLOCK; - - public static final BlockItem IRON_COAL_BLOCK_ITEM; - - public static final FuelItem IRON_COAL; - public static final FuelItem GOLD_COAL; - public static final FuelItem DIAMOND_COAL; - - public static final Item COPPER_SWORD; - public static final Item TIN_SWORD; - public static final Item LEAD_SWORD; - - public static void load() {} - - static { - BlockRegistrar registrar = new BlockRegistrar("ho"); - - registrar.templateSettings(AbstractBlock.Settings.copy(Blocks.IRON_BLOCK)); - IRON_COAL_BLOCK = registrar.factory("iron_coal_block"); - } - - static { - ItemRegistrar registrar = new ItemRegistrar("ho"); - - IRON_COAL = registrar.register("iron_coal", new FuelItem(new Item.Settings().maxCount(16), 400)); - - IRON_COAL_BLOCK_ITEM = registrar.blockItem("iron_coal_block", () -> IRON_COAL_BLOCK); - - registrar.templateSettings(new Item.Settings().maxCount(16)); - GOLD_COAL = registrar.func("gold_coal", - settings -> new FuelItem(settings, 800)); - DIAMOND_COAL = registrar.func("diamond_coal", - settings -> new FuelItem(settings.rarity(Rarity.EPIC), 1600)); - registrar.clearTemplate(); - - registrar.templateSettings(new Item.Settings().rarity(Rarity.UNCOMMON)); - registrar.templateItem(settings -> new SwordItem(ToolMaterials.WOOD, 0, 0, settings)); - COPPER_SWORD = registrar.factory("copper_sword", settings -> settings.maxDamageIfAbsent(200)); - TIN_SWORD = registrar.factory("tin_sword", settings -> settings.maxDamageIfAbsent(150)); - LEAD_SWORD = registrar.factory("lead_sword", settings -> settings.maxDamageIfAbsent(100)); - registrar.clearTemplate(); - - registrar.factory("copper_dust", "tin_dust", "lead_dust"); - } -} diff --git a/src/main/java/ho/artisan/lib/gen/IdentifierGenerator.java b/src/main/java/ho/artisan/lib/gen/IdentifierGenerator.java deleted file mode 100644 index 76c5d94..0000000 --- a/src/main/java/ho/artisan/lib/gen/IdentifierGenerator.java +++ /dev/null @@ -1,23 +0,0 @@ -package ho.artisan.lib.gen; - - -import net.minecraft.util.Identifier; - -import java.util.Arrays; -import java.util.List; - -public class IdentifierGenerator { - private final String namespace; - - public IdentifierGenerator(String namespace) { - this.namespace = namespace; - } - - public Identifier gen(String path) { - return new Identifier(namespace, path); - } - - public List gen(String... paths) { - return Arrays.stream(paths).map(this::gen).toList(); - } -} diff --git a/src/main/java/ho/artisan/lib/gen/TagGenerator.java b/src/main/java/ho/artisan/lib/gen/TagGenerator.java deleted file mode 100644 index 05d03a2..0000000 --- a/src/main/java/ho/artisan/lib/gen/TagGenerator.java +++ /dev/null @@ -1,24 +0,0 @@ -package ho.artisan.lib.gen; - -import net.minecraft.tag.TagKey; -import net.minecraft.util.Identifier; -import net.minecraft.util.registry.Registry; - -import java.util.Arrays; -import java.util.List; - -public class TagGenerator { - private final Registry registry; - - public TagGenerator(Registry registry) { - this.registry = registry; - } - - public TagKey gen(Identifier id) { - return TagKey.of(registry.getKey(), id); - } - - public List> gen(Identifier... id) { - return Arrays.stream(id).map(this::gen).toList(); - } -} diff --git a/src/main/java/ho/artisan/lib/registry/registrar/BlockEntityTypeRegistrar.java b/src/main/java/ho/artisan/lib/registrar/BlockEntityTypeRegistrar.java similarity index 61% rename from src/main/java/ho/artisan/lib/registry/registrar/BlockEntityTypeRegistrar.java rename to src/main/java/ho/artisan/lib/registrar/BlockEntityTypeRegistrar.java index 46b27cf..73aa47c 100644 --- a/src/main/java/ho/artisan/lib/registry/registrar/BlockEntityTypeRegistrar.java +++ b/src/main/java/ho/artisan/lib/registrar/BlockEntityTypeRegistrar.java @@ -1,4 +1,4 @@ -package ho.artisan.lib.registry.registrar; +package ho.artisan.lib.registrar; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.minecraft.block.Block; @@ -6,16 +6,15 @@ import net.minecraft.block.entity.BlockEntityType; import net.minecraft.util.registry.Registry; -public class BlockEntityTypeRegistrar extends VanillaRegistrar> { +public class BlockEntityTypeRegistrar extends MinecraftRegistrar> { public BlockEntityTypeRegistrar(String modid) { super(modid, Registry.BLOCK_ENTITY_TYPE); } - public - BlockEntityType - register(String id, - FabricBlockEntityTypeBuilder.Factory factory, - Block... blocks) { + public BlockEntityType register( + String id, + FabricBlockEntityTypeBuilder.Factory factory, + Block... blocks) { return register(id, FabricBlockEntityTypeBuilder.create(factory, blocks).build()); } } diff --git a/src/main/java/ho/artisan/lib/registrar/BlockRegistrar.java b/src/main/java/ho/artisan/lib/registrar/BlockRegistrar.java new file mode 100644 index 0000000..0af848b --- /dev/null +++ b/src/main/java/ho/artisan/lib/registrar/BlockRegistrar.java @@ -0,0 +1,16 @@ +package ho.artisan.lib.registrar; + +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.Material; +import net.minecraft.util.registry.Registry; +import org.jetbrains.annotations.NotNull; + +public class BlockRegistrar extends MinecraftRegistrar { + @NotNull + public AbstractBlock.Settings settings = AbstractBlock.Settings.of(Material.STONE); + + public BlockRegistrar(String modid) { + super(modid, Registry.BLOCK); + } +} diff --git a/src/main/java/ho/artisan/lib/registry/registrar/IRegistrar.java b/src/main/java/ho/artisan/lib/registrar/IRegistrar.java similarity index 51% rename from src/main/java/ho/artisan/lib/registry/registrar/IRegistrar.java rename to src/main/java/ho/artisan/lib/registrar/IRegistrar.java index 54d6bf7..af5ffb9 100644 --- a/src/main/java/ho/artisan/lib/registry/registrar/IRegistrar.java +++ b/src/main/java/ho/artisan/lib/registrar/IRegistrar.java @@ -1,9 +1,7 @@ -package ho.artisan.lib.registry.registrar; +package ho.artisan.lib.registrar; -import ho.artisan.lib.registry.Casket; import net.minecraft.util.Identifier; - /** * 实现注册器的规范接口 * @@ -17,9 +15,9 @@ public interface IRegistrar { String namespace(); /** - * 简化生成ID + * 简化生成Identifier */ - Identifier id(String id); + Identifier createID(String id); /** * 注册条目 @@ -29,19 +27,4 @@ public interface IRegistrar { */ @SuppressWarnings("UnusedReturnValue") K register(String id, K object); - - - /** - * 注册条目并返回封装 - * - * @param id 注册条目的ID - * @param object 注册条目的实例 - */ - @SuppressWarnings("UnusedReturnValue") - default Casket casket(String id, K object) { - return new Casket<>(register(id, object), id(id)); - } - - - } \ No newline at end of file diff --git a/src/main/java/ho/artisan/lib/registrar/ItemRegistrar.java b/src/main/java/ho/artisan/lib/registrar/ItemRegistrar.java new file mode 100644 index 0000000..75f24ee --- /dev/null +++ b/src/main/java/ho/artisan/lib/registrar/ItemRegistrar.java @@ -0,0 +1,21 @@ +package ho.artisan.lib.registrar; + +import net.minecraft.block.Block; +import net.minecraft.item.BlockItem; +import net.minecraft.item.Item; +import net.minecraft.util.registry.Registry; +import org.jetbrains.annotations.NotNull; + +public class ItemRegistrar extends MinecraftRegistrar { ; + public ItemRegistrar(@NotNull String modid) { + super(modid, Registry.ITEM); + } + + public Item simple(String id, Item.Settings settings) { + return register(id, new Item(settings)); + } + + public BlockItem blockItem(String id, Block block) { + return register(id, new BlockItem(block, new Item.Settings())); + } +} diff --git a/src/main/java/ho/artisan/lib/registry/registrar/VanillaRegistrar.java b/src/main/java/ho/artisan/lib/registrar/MinecraftRegistrar.java similarity index 56% rename from src/main/java/ho/artisan/lib/registry/registrar/VanillaRegistrar.java rename to src/main/java/ho/artisan/lib/registrar/MinecraftRegistrar.java index 63b0445..ef2bca0 100644 --- a/src/main/java/ho/artisan/lib/registry/registrar/VanillaRegistrar.java +++ b/src/main/java/ho/artisan/lib/registrar/MinecraftRegistrar.java @@ -1,28 +1,26 @@ -package ho.artisan.lib.registry.registrar; +package ho.artisan.lib.registrar; -import ho.artisan.lib.gen.IdentifierGenerator; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import org.jetbrains.annotations.NotNull; /** * 实现原版注册的简易注册器 + * Identifier生成托管到IdentifierGenerator * * @param 实现注册的类型 */ -public class VanillaRegistrar implements IRegistrar { - private final String modid; - private final Registry registry; - public final IdentifierGenerator idGenerator; +public class MinecraftRegistrar implements IRegistrar { + protected final String modid; + protected final Registry registry; /** * @param modid 模组ID * @param registry 原版注册表实例 */ - public VanillaRegistrar(String modid, @NotNull Registry registry) { + public MinecraftRegistrar(String modid, @NotNull Registry registry) { this.modid = modid; this.registry = registry; - this.idGenerator = new IdentifierGenerator(modid); } @Override @@ -38,11 +36,11 @@ public String namespace() { */ @Override public K register(String id, K object) { - return Registry.register(registry, id(id), object); + return Registry.register(registry, createID(id), object); } @Override - public Identifier id(String path) { - return idGenerator.gen(path); + public Identifier createID(String path) { + return new Identifier(modid, path); } } diff --git a/src/main/java/ho/artisan/lib/registrar/ScreenHandlerTypeRegistrar.java b/src/main/java/ho/artisan/lib/registrar/ScreenHandlerTypeRegistrar.java new file mode 100644 index 0000000..4774f43 --- /dev/null +++ b/src/main/java/ho/artisan/lib/registrar/ScreenHandlerTypeRegistrar.java @@ -0,0 +1,18 @@ +package ho.artisan.lib.registrar; + +import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.util.registry.Registry; + +public class ScreenHandlerTypeRegistrar extends MinecraftRegistrar> { + public ScreenHandlerTypeRegistrar(String modid) { + super(modid, Registry.SCREEN_HANDLER); + } + + + public ScreenHandlerType extended(String id, ExtendedScreenHandlerType.ExtendedFactory factory) { + return register(id, new ExtendedScreenHandlerType<>(factory)); + } + +} diff --git a/src/main/java/ho/artisan/lib/registry/Casket.java b/src/main/java/ho/artisan/lib/registry/Casket.java deleted file mode 100644 index 90f66ef..0000000 --- a/src/main/java/ho/artisan/lib/registry/Casket.java +++ /dev/null @@ -1,52 +0,0 @@ -package ho.artisan.lib.registry; - -import net.minecraft.util.Identifier; -import org.jetbrains.annotations.NotNull; - -import java.util.Objects; -import java.util.function.Function; -import java.util.function.Supplier; - -public final class Casket implements Supplier { - private final T object; - private final Identifier identifier; - - public Casket(@NotNull T object, @NotNull Identifier id) { - this.object = object; - this.identifier = id; - } - - @Override - public T get() { - return object; - } - - public F func(Function function) { - return function.apply(object); - } - - public Identifier id() { - return identifier; - } - - public String sid() { - return identifier.getPath(); - } - - public String namespace() { - return identifier.getNamespace(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Casket casket = (Casket) o; - return Objects.equals(object, casket.object) && Objects.equals(identifier, casket.identifier); - } - - @Override - public int hashCode() { - return Objects.hash(object, identifier); - } -} diff --git a/src/main/java/ho/artisan/lib/registry/registrar/BlockRegistrar.java b/src/main/java/ho/artisan/lib/registry/registrar/BlockRegistrar.java deleted file mode 100644 index 02a10e4..0000000 --- a/src/main/java/ho/artisan/lib/registry/registrar/BlockRegistrar.java +++ /dev/null @@ -1,56 +0,0 @@ -package ho.artisan.lib.registry.registrar; - -import net.minecraft.block.AbstractBlock; -import net.minecraft.block.Block; -import net.minecraft.block.Material; -import net.minecraft.util.registry.Registry; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; -import java.util.List; -import java.util.function.Consumer; -import java.util.function.Function; - -public class BlockRegistrar extends VanillaRegistrar { - private AbstractBlock.Settings templateSettings = AbstractBlock.Settings.of(Material.AIR); - private Function templateBlock = Block::new; - - public BlockRegistrar(String modid) { - super(modid, Registry.BLOCK); - } - - public K func(String id, @NotNull Function function) { - return register(id, function.apply(templateSettings)); - } - - public Block factory(String id) { - return func(id, templateBlock); - } - - public Block factory(String id, Consumer consumer) { - return func(id, settings -> { - consumer.accept(settings); - return templateBlock.apply(settings); - }); - } - - public List factory(String... id) { - return Arrays.stream(id).map(this::factory).toList(); - } - - public List factory(Consumer consumer, String... id) { - return Arrays.stream(id).map(this::factory).toList(); - } - - @SuppressWarnings("UnusedReturnValue") - public AbstractBlock.Settings templateSettings(AbstractBlock.Settings settings) { - this.templateSettings = settings; - return this.templateSettings; - } - - @SuppressWarnings("UnusedReturnValue") - public Function templateBlock(Function template) { - this.templateBlock = template; - return this.templateBlock; - } -} diff --git a/src/main/java/ho/artisan/lib/registry/registrar/ItemRegistrar.java b/src/main/java/ho/artisan/lib/registry/registrar/ItemRegistrar.java deleted file mode 100644 index 1c8b360..0000000 --- a/src/main/java/ho/artisan/lib/registry/registrar/ItemRegistrar.java +++ /dev/null @@ -1,82 +0,0 @@ -package ho.artisan.lib.registry.registrar; - -import net.minecraft.block.Block; -import net.minecraft.item.BlockItem; -import net.minecraft.item.Item; -import net.minecraft.util.registry.Registry; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; -import java.util.List; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Supplier; - -/** - * 实现物品注册的注册器 - */ -public class ItemRegistrar extends VanillaRegistrar { - private Item.Settings templateSettings = new Item.Settings(); - private Function templateItem = Item::new; - - public ItemRegistrar(String modid) { - super(modid, Registry.ITEM); - } - - /** - * 简化注册简单物品 - */ - public Item factory(String id) { - return func(id, templateItem); - } - - public Item factory(String id, Consumer consumer) { - return func(id, settings -> { - consumer.accept(settings); - return new Item(settings); - }); - } - - public List factory(String... id) { - return Arrays.stream(id).map(this::factory).toList(); - } - - public List factory(Consumer consumer, String... id) { - return Arrays.stream(id).map(this::factory).toList(); - } - - /** - * 简化注册方块物品 - */ - public BlockItem blockItem(String id, Supplier block) { - return func(id, settings -> new BlockItem(block.get(), settings)); - } - - /** - * 通过函数式接口来实现简化注册 - */ - public K func(String id, @NotNull Function function) { - return register(id, function.apply(templateSettings)); - } - - /** - * 切换 {@code func()} 所使用的 {@code Item.Setting} - * @see ho.artisan.lib.registry.registrar.ItemRegistrar#func(String, Function) - */ - @SuppressWarnings("UnusedReturnValue") - public Item.Settings templateSettings(Item.Settings settings) { - this.templateSettings = settings; - return this.templateSettings; - } - - @SuppressWarnings("UnusedReturnValue") - public Function templateItem(Function template) { - this.templateItem = template; - return this.templateItem; - } - - public void clearTemplate() { - templateSettings = new Item.Settings(); - templateItem = Item::new; - } -} diff --git a/src/main/java/ho/artisan/lib/test/TestHOLibrary.java b/src/main/java/ho/artisan/lib/test/TestHOLibrary.java new file mode 100644 index 0000000..2401b78 --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/TestHOLibrary.java @@ -0,0 +1,59 @@ +package ho.artisan.lib.test; + +import ho.artisan.lib.registrar.BlockEntityTypeRegistrar; +import ho.artisan.lib.registrar.BlockRegistrar; +import ho.artisan.lib.registrar.ItemRegistrar; +import ho.artisan.lib.registrar.ScreenHandlerTypeRegistrar; +import ho.artisan.lib.test.list.TestListBlock; +import ho.artisan.lib.test.list.TestListBlockEntity; +import ho.artisan.lib.test.list.TestScreen; +import ho.artisan.lib.test.list.TestScreenHandler; +import ho.artisan.lib.test.stack.TestBlock; +import ho.artisan.lib.test.stack.TestBlockEntity; +import ho.artisan.lib.test.stack.TestRenderer; +import net.fabricmc.fabric.api.client.rendering.v1.BlockEntityRendererRegistry; +import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; +import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage; +import net.minecraft.block.Block; +import net.minecraft.block.Material; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.client.gui.screen.ingame.HandledScreens; +import net.minecraft.item.BlockItem; +import net.minecraft.screen.ScreenHandlerType; + +public class TestHOLibrary { + public static final Block TEST; + public static final Block TEST_2; + public static final BlockItem TEST_ITEM; + public static final BlockItem TEST_2_ITEM; + public static final BlockEntityType TEST_TYPE; + public static final BlockEntityType TEST_TYPE_2; + + public static final ScreenHandlerType TEST_SCREEN; + + public static void onInitialize() { + ItemStorage.SIDED.registerForBlockEntity(((test, direction) -> test.inventoryWrapper), TEST_TYPE_2); + + } + + public static void onInitializeClient() { + BlockEntityRendererRegistry.register(TEST_TYPE, TestRenderer::new); + + HandledScreens.register(TEST_SCREEN, TestScreen::new); + } + + static { + BlockRegistrar blockRegistrar = new BlockRegistrar("ho"); + BlockEntityTypeRegistrar blockEntityTypeRegistrar = new BlockEntityTypeRegistrar("ho"); + ScreenHandlerTypeRegistrar screenHandlerTypeRegistrar = new ScreenHandlerTypeRegistrar("ho"); + ItemRegistrar itemRegistrar = new ItemRegistrar("ho"); + + TEST = blockRegistrar.register("test", new TestBlock(FabricBlockSettings.of(Material.STONE))); + TEST_2 = blockRegistrar.register("test_2", new TestListBlock(FabricBlockSettings.of(Material.STONE))); + TEST_TYPE = blockEntityTypeRegistrar.register("test", TestBlockEntity::new, TEST); + TEST_TYPE_2 = blockEntityTypeRegistrar.register("test_2", TestListBlockEntity::new, TEST_2); + TEST_SCREEN = screenHandlerTypeRegistrar.extended("test", TestScreenHandler::new); + TEST_ITEM = itemRegistrar.blockItem("test", TEST); + TEST_2_ITEM = itemRegistrar.blockItem("test_2", TEST_2); + } +} diff --git a/src/main/java/ho/artisan/lib/test/list/TestItemData.java b/src/main/java/ho/artisan/lib/test/list/TestItemData.java new file mode 100644 index 0000000..987e6e9 --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/list/TestItemData.java @@ -0,0 +1,37 @@ +package ho.artisan.lib.test.list; + +import ho.artisan.lib.data.container.ListItemData; +import net.minecraft.inventory.SidedInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.Direction; +import org.jetbrains.annotations.Nullable; + +public class TestItemData extends ListItemData implements SidedInventory { + public static final int[] INPUT = new int[]{0, 1}; + public static final int[] ADDITION = new int[]{2}; + public static final int[] OUTPUT = new int[]{3}; + + public TestItemData() { + super("Items", 4); + } + + @Override + public int[] getAvailableSlots(Direction side) { + if (side == Direction.DOWN) + return OUTPUT; + else if (side == Direction.UP) + return ADDITION; + else + return INPUT; + } + + @Override + public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) { + return slot != 3; + } + + @Override + public boolean canExtract(int slot, ItemStack stack, Direction dir) { + return slot == 3; + } +} diff --git a/src/main/java/ho/artisan/lib/test/list/TestListBlock.java b/src/main/java/ho/artisan/lib/test/list/TestListBlock.java new file mode 100644 index 0000000..cdf1e52 --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/list/TestListBlock.java @@ -0,0 +1,24 @@ +package ho.artisan.lib.test.list; + +import ho.artisan.lib.common.block.HODataBlock; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public class TestListBlock extends HODataBlock { + public TestListBlock(Settings settings) { + super(settings, TestListBlockEntity::new); + } + + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + if (!world.isClient) { + player.openHandledScreen(createScreenHandlerFactory(state, world, pos)); + } + return ActionResult.SUCCESS; + } +} diff --git a/src/main/java/ho/artisan/lib/test/list/TestListBlockEntity.java b/src/main/java/ho/artisan/lib/test/list/TestListBlockEntity.java new file mode 100644 index 0000000..ae341b9 --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/list/TestListBlockEntity.java @@ -0,0 +1,43 @@ +package ho.artisan.lib.test.list; + +import ho.artisan.lib.common.blockentity.HOBlockEntity; +import ho.artisan.lib.test.TestHOLibrary; +import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; +import net.fabricmc.fabric.api.transfer.v1.item.InventoryStorage; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.math.BlockPos; +import org.jetbrains.annotations.Nullable; + +public class TestListBlockEntity extends HOBlockEntity implements ExtendedScreenHandlerFactory { + public final TestItemData itemData = new TestItemData(); + public final InventoryStorage inventoryWrapper = InventoryStorage.of(itemData, null); + + public TestListBlockEntity(BlockPos pos, BlockState state) { + super(TestHOLibrary.TEST_TYPE_2, pos, state); + load(itemData); + reserve(itemData); + } + + @Override + public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf) { + itemData.write(buf); + } + + @Override + public Text getDisplayName() { + return new TranslatableText(getCachedState().getBlock().getTranslationKey()); + } + + @Nullable + @Override + public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { + return new TestScreenHandler(syncId, inv, itemData); + } +} diff --git a/src/main/java/ho/artisan/lib/test/list/TestScreen.java b/src/main/java/ho/artisan/lib/test/list/TestScreen.java new file mode 100644 index 0000000..24871df --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/list/TestScreen.java @@ -0,0 +1,38 @@ +package ho.artisan.lib.test.list; + +import com.mojang.blaze3d.systems.RenderSystem; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.gui.screen.ingame.HandledScreen; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +@Environment(value= EnvType.CLIENT) +public class TestScreen extends HandledScreen { + private static final Identifier TEXTURE = new Identifier("ho", "textures/gui/test.png"); + + public TestScreen(TestScreenHandler handler, PlayerInventory inventory, Text title) { + super(handler, inventory, title); + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + this.renderBackground(matrices); + super.render(matrices, mouseX, mouseY, delta); + this.drawMouseoverTooltip(matrices, mouseX, mouseY); + } + + @Override + protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) { + int k; + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); + RenderSystem.setShaderTexture(0, TEXTURE); + int i = this.x; + int j = this.y; + this.drawTexture(matrices, i, j, 0, 0, this.backgroundWidth, this.backgroundHeight); + } +} diff --git a/src/main/java/ho/artisan/lib/test/list/TestScreenHandler.java b/src/main/java/ho/artisan/lib/test/list/TestScreenHandler.java new file mode 100644 index 0000000..ad07b4d --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/list/TestScreenHandler.java @@ -0,0 +1,70 @@ +package ho.artisan.lib.test.list; + +import ho.artisan.lib.test.TestHOLibrary; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.slot.Slot; + +public class TestScreenHandler extends ScreenHandler { + private final TestItemData itemData; + + public TestScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) { + this(syncId, playerInventory, new TestItemData()); + itemData.read(buf); + } + + public TestScreenHandler(int syncId, PlayerInventory playerInventory, TestItemData itemData) { + super(TestHOLibrary.TEST_SCREEN, syncId); + this.itemData = itemData; + + this.addSlot(new Slot(itemData, 0, 53, 35)); + this.addSlot(new Slot(itemData, 1, 71, 35)); + this.addSlot(new Slot(itemData, 2, 107, 17)); + this.addSlot(new Slot(itemData, 3, 107, 53)); + + int i; + for (i = 0; i < 3; ++i) { + for (int j = 0; j < 9; ++j) { + this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); + } + } + for (i = 0; i < 9; ++i) { + this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142)); + } + } + + @Override + public ItemStack transferSlot(PlayerEntity player, int index) { + ItemStack itemStack = ItemStack.EMPTY; + Slot slot = this.slots.get(index); + if (slot.hasStack()) { + ItemStack itemStack2 = slot.getStack(); + itemStack = itemStack2.copy(); + if (index < 4 ? !this.insertItem(itemStack2, 4, 40, true) : !this.insertItem(itemStack2, 0, 4, false)) { + return ItemStack.EMPTY; + } + if (itemStack2.isEmpty()) { + slot.setStack(ItemStack.EMPTY); + } else { + slot.markDirty(); + } + if (itemStack2.getCount() == itemStack.getCount()) { + return ItemStack.EMPTY; + } + slot.onTakeItem(player, itemStack2); + } + return itemStack; + } + + public TestItemData getItemData() { + return this.itemData; + } + + @Override + public boolean canUse(PlayerEntity player) { + return getItemData().canPlayerUse(player); + } +} diff --git a/src/main/java/ho/artisan/lib/test/stack/TestBlock.java b/src/main/java/ho/artisan/lib/test/stack/TestBlock.java new file mode 100644 index 0000000..cc19d82 --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/stack/TestBlock.java @@ -0,0 +1,38 @@ +package ho.artisan.lib.test.stack; + +import ho.artisan.lib.common.block.HODataBlock; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public class TestBlock extends HODataBlock { + public TestBlock(Settings settings) { + super(settings, TestBlockEntity::new); + } + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + if (world.getBlockEntity(pos) instanceof TestBlockEntity blockEntity) { + blockEntity.itemDirection.set(player.getMovementDirection()); + if (player.isSneaking()) { + ItemStack stack = blockEntity.itemData.pop().copy(); + if (stack != ItemStack.EMPTY) { + if (!player.getInventory().insertStack(stack)) + player.dropStack(stack); + return ActionResult.success(world.isClient); + } + }else { + if (blockEntity.itemData.isAddible()) { + ItemStack stack = player.getStackInHand(hand).split(64); + if (blockEntity.itemData.add(stack)) + return ActionResult.success(world.isClient); + } + } + } + return ActionResult.CONSUME; + } +} diff --git a/src/main/java/ho/artisan/lib/test/stack/TestBlockEntity.java b/src/main/java/ho/artisan/lib/test/stack/TestBlockEntity.java new file mode 100644 index 0000000..4b204d3 --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/stack/TestBlockEntity.java @@ -0,0 +1,20 @@ +package ho.artisan.lib.test.stack; + +import ho.artisan.lib.common.blockentity.HOBlockEntity; +import ho.artisan.lib.data.container.StackItemData; +import ho.artisan.lib.data.misc.DirectionData; +import ho.artisan.lib.test.TestHOLibrary; +import net.minecraft.block.BlockState; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; + +public class TestBlockEntity extends HOBlockEntity { + public final StackItemData itemData = new StackItemData("Items", 1); + public final DirectionData itemDirection = new DirectionData("ItemDirection", Direction.NORTH); + + public TestBlockEntity(BlockPos pos, BlockState state) { + super(TestHOLibrary.TEST_TYPE, pos, state); + load(itemDirection, itemData); + reserve(itemData); + } +} diff --git a/src/main/java/ho/artisan/lib/test/stack/TestRenderer.java b/src/main/java/ho/artisan/lib/test/stack/TestRenderer.java new file mode 100644 index 0000000..eba30c9 --- /dev/null +++ b/src/main/java/ho/artisan/lib/test/stack/TestRenderer.java @@ -0,0 +1,46 @@ +package ho.artisan.lib.test.stack; + +import ho.artisan.lib.test.stack.TestBlockEntity; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.render.WorldRenderer; +import net.minecraft.client.render.block.entity.BlockEntityRenderer; +import net.minecraft.client.render.block.entity.BlockEntityRendererFactory; +import net.minecraft.client.render.item.ItemRenderer; +import net.minecraft.client.render.model.json.ModelTransformation; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.Direction; + +import java.util.Stack; + +public class TestRenderer implements BlockEntityRenderer { + + public TestRenderer(BlockEntityRendererFactory.Context ctx) {} + + @Override + public void render(TestBlockEntity blockEntity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light0, int overlay) { + Direction direction = blockEntity.itemDirection.get(); + Stack stack = blockEntity.itemData.get(); + ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer(); + float y = 1; + + if (!stack.isEmpty() && blockEntity.getWorld() != null) { + int light = WorldRenderer.getLightmapCoordinates(blockEntity.getWorld(), blockEntity.getPos().up()); + for (int i = 0, stackSize = stack.size(); i < stackSize; i++) { + boolean blockItem = itemRenderer.getModel(stack.elementAt(i), blockEntity.getWorld(), null, (int) blockEntity.getPos().asLong()).hasDepth(); + matrices.push(); + if (blockItem) + y += 7/24f; + else + y += 1/16f; + matrices.translate(0.5f, y, 0.5f); + matrices.multiply(direction.getRotationQuaternion()); + itemRenderer.renderItem(stack.elementAt(i), ModelTransformation.Mode.FIXED, light, overlay, matrices, vertexConsumers, 0); + matrices.pop(); + if (blockItem) + y += 5/24f; + } + } + } +} diff --git a/src/main/resources/assets/ho/blockstates/test.json b/src/main/resources/assets/ho/blockstates/test.json new file mode 100644 index 0000000..650068c --- /dev/null +++ b/src/main/resources/assets/ho/blockstates/test.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "minecraft:block/stone" + } + } +} \ No newline at end of file diff --git a/src/main/resources/assets/ho/blockstates/test_2.json b/src/main/resources/assets/ho/blockstates/test_2.json new file mode 100644 index 0000000..650068c --- /dev/null +++ b/src/main/resources/assets/ho/blockstates/test_2.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "minecraft:block/stone" + } + } +} \ No newline at end of file diff --git a/src/main/resources/assets/holib/icon.png b/src/main/resources/assets/ho/icon.png similarity index 100% rename from src/main/resources/assets/holib/icon.png rename to src/main/resources/assets/ho/icon.png diff --git a/src/main/resources/assets/ho/models/item/test.json b/src/main/resources/assets/ho/models/item/test.json new file mode 100644 index 0000000..1a2f6a7 --- /dev/null +++ b/src/main/resources/assets/ho/models/item/test.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "minecraft:block/stone" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/ho/models/item/test_2.json b/src/main/resources/assets/ho/models/item/test_2.json new file mode 100644 index 0000000..1a2f6a7 --- /dev/null +++ b/src/main/resources/assets/ho/models/item/test_2.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "minecraft:block/stone" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/ho/textures/gui/test.png b/src/main/resources/assets/ho/textures/gui/test.png new file mode 100644 index 0000000000000000000000000000000000000000..f668c9e93b58e7447d56bc0e5a218016da10b3c7 GIT binary patch literal 857 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58911MRQ8&P5D>38$lZxy-8q?;Kn_c~qpu?a z!^VE@KZ&eBzG#3?h%1o(|NsB7W5+^6L(R?2ySuxABB`6FO9LtPk|4iepeztDNO?26 z0fjgVJR*x382Ao@Fyrz36)6l1OuIZ?978JN-rl~LciBP2HE?<6pa1iDGR@^eei>|i z=W8@)Z#-Yogqb?5ig(Ixf6tx!?bo*2-D`rI4{W>id;gxR+%*h))?WG=rB}fI;2pP0 z0|QgRyG<+t4h)Ptelu|@G%(EBy@s7>PyO3t+@E#%3VvTY|386|MWCU0x++jD$AP=M zg&V%tsxauEQHSY$kjSw8ynf+-QT;RDwHRzJO5VM3@%;R6XTBIQ)U@^MvC6-n39{f^ z>@=VO3Jm4*{Tc2(oyfrSp<9{p$J(t>OM@By+uhczxMh5KHN%|lbXVp5?j7RKgnV0Gau}e zt6D1E@PDy@0E zX~_m4LS_H} literal 0 HcmV?d00001 diff --git a/src/main/resources/assets/ho/textures/item/coal/iron.png b/src/main/resources/assets/ho/textures/item/coal/iron.png new file mode 100644 index 0000000000000000000000000000000000000000..d9db6fb536a4297c89661ea8efbb3b7257b66293 GIT binary patch literal 379 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbKJV{wqX6T`Z5GB1G~mUKs7M+SzC z{oH>NS%Li60G|-o;^N{jU%uS9abx4gjV&!L6DCY}_3G8(!-wzRzkm1c-J3UWZr!?d z$BrHQ_wS!PdGdn?5ANN&w|n>Q?c28p1_rKOyY}kUt9S0)*}Qr4)TvWfty*>9z=3`H z_RX0yXUdc*KpQHLEGz?3@+Coj!T<3BhO?4oAlGvictjR6Fz_7)VaDV6D^h@hHJ&bx zAsWG@XHW7SGT>pkaAe)xDO*(?B0vA1A6Xq^pZMSIBdc=U_1N|$yuP)YA>_1yM*cSW2Xm7)+m|rd^-a$An;gxQtSLAoE;dW-lUFkh?dt0-)sb4BzY&>ak(u6{1-oD!MNS%G|m0G|+70RaI=M@M#cb~QCMWo2b%W@ZZu3mF+1Nl8f!4Glv@PCqmLLSIoa zeikt?F`%aP70-%+lt4+4U+{la!0O=~RKvF)gc&VZgH?cnDV{ElAsWG< zeYb@g6gXHXvUmso`=9Up;aX+mT}HME#tS6ZvN|0IcGyvOC}LZKz|`#$cUS}0zsXU0 zspf9BO*ARyjnYTkoBcvIlYO5!8OjO&T-&r_dhC|4to#D5h{JLYZ|cu>)I8nB9o52l Q0~90-p00i_>zopr0A%uAvH$=8 literal 0 HcmV?d00001 diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 997202f..4824da7 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -13,20 +13,32 @@ "homepage": "https://modrinth.com/mod/ho-library" }, "license": "MIT", - "icon": "assets/holib/icon.png", + "icon": "assets/ho/icon.png", "environment": "*", "entrypoints": { "main": [ "ho.artisan.lib.HOLibrary" + ], + "client": [ + "ho.artisan.lib.HOLibrary" ] }, "depends": { "fabric": "*" }, + "mixins": [ + "holib.mixins.json" + ], + "accessWidener" : "holib.accesswidener", "custom": { "modmenu": { "badges": [ "library"], "update_checker": false + }, + "loom:injected_interfaces": { + "net/minecraft/class_4915": [ + "ho/artisan/lib/datagen/inf/HOItemModelGen" + ] } } } diff --git a/src/main/resources/holib.accesswidener b/src/main/resources/holib.accesswidener index 1218bfc..cdc8062 100644 --- a/src/main/resources/holib.accesswidener +++ b/src/main/resources/holib.accesswidener @@ -1,7 +1,2 @@ accessWidener v1 named -accessible field net/minecraft/client/gui/screen/ingame/CreativeInventoryScreen selectedTab Lnet/minecraft/item/ItemGroup; -accessible field net/minecraft/client/gui/screen/ingame/HandledScreen x I -accessible field net/minecraft/client/gui/screen/ingame/HandledScreen y I -accessible method net/minecraft/client/gui/screen/ingame/CreativeInventoryScreen getTabX (Lnet/minecraft/item/ItemGroup;)I -accessible field net/minecraft/client/gui/screen/ingame/HandledScreen backgroundHeight I -accessible field net/minecraft/client/gui/screen/Screen textRenderer Lnet/minecraft/client/font/TextRenderer; \ No newline at end of file +accessible field net/minecraft/data/client/ItemModelGenerator writer Ljava/util/function/BiConsumer; \ No newline at end of file diff --git a/src/main/resources/holib.mixin.json b/src/main/resources/holib.mixins.json similarity index 90% rename from src/main/resources/holib.mixin.json rename to src/main/resources/holib.mixins.json index fcb1079..657a4fb 100644 --- a/src/main/resources/holib.mixin.json +++ b/src/main/resources/holib.mixins.json @@ -4,7 +4,7 @@ "package": "ho.artisan.lib.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ - "ItemSettingMixin" + ], "client": [], "server": [],