diff --git a/src/main/java/modfest/lacrimis/Lacrimis.java b/src/main/java/modfest/lacrimis/Lacrimis.java index f348c586..c7e333d0 100644 --- a/src/main/java/modfest/lacrimis/Lacrimis.java +++ b/src/main/java/modfest/lacrimis/Lacrimis.java @@ -31,7 +31,6 @@ public void onInitialize() { ModEntities.register(); ModItems.register(); ModCrafting.register(); - ModNetworking.register(); ModSounds.register(); ModParticles.register(); ModStatusEffects.register(); diff --git a/src/main/java/modfest/lacrimis/block/CombinerBlock.java b/src/main/java/modfest/lacrimis/block/CombinerBlock.java index 70f95fd4..dc26fac7 100644 --- a/src/main/java/modfest/lacrimis/block/CombinerBlock.java +++ b/src/main/java/modfest/lacrimis/block/CombinerBlock.java @@ -29,19 +29,17 @@ public BlockEntity createBlockEntity(BlockView world) { return new CombinerEntity(); } - @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { - ActionResult parentResult = super.onUse(state, world, pos, player, hand, hit); - if(parentResult == ActionResult.PASS && player.getStackInHand(hand).getItem() != ModItems.diviningRod) { - if(world.isClient) { - return ActionResult.SUCCESS; - } else { - ContainerProviderRegistry.INSTANCE.openContainer(ModCrafting.COMBINER_SCREEN_ID, player, buf -> buf.writeBlockPos(pos)); - - return ActionResult.CONSUME; + if (world.isClient) { + return ActionResult.SUCCESS; + } else { + BlockEntity blockEntity = world.getBlockEntity(pos); + if (blockEntity instanceof CombinerEntity) { + player.openHandledScreen((CombinerEntity)blockEntity); } + + return ActionResult.CONSUME; } - return parentResult; } @Override diff --git a/src/main/java/modfest/lacrimis/block/InfusionTableBlock.java b/src/main/java/modfest/lacrimis/block/InfusionTableBlock.java index 80a45e3a..c0582416 100644 --- a/src/main/java/modfest/lacrimis/block/InfusionTableBlock.java +++ b/src/main/java/modfest/lacrimis/block/InfusionTableBlock.java @@ -54,19 +54,17 @@ public BlockRenderType getRenderType(BlockState state) { return BlockRenderType.MODEL; } - @Override - public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { - ActionResult parentResult = super.onUse(state, world, pos, player, hand, hit); - if(parentResult == ActionResult.PASS && player.getStackInHand(hand).getItem() != ModItems.diviningRod) { - if(world.isClient) { - return ActionResult.SUCCESS; - } else { - ContainerProviderRegistry.INSTANCE.openContainer(ModCrafting.INFUSION_SCREEN_ID, player, buf -> buf.writeBlockPos(pos)); - - return ActionResult.CONSUME; + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + if (world.isClient) { + return ActionResult.SUCCESS; + } else { + BlockEntity blockEntity = world.getBlockEntity(pos); + if (blockEntity instanceof InfusionTableEntity) { + player.openHandledScreen((InfusionTableEntity)blockEntity); } + + return ActionResult.CONSUME; } - return parentResult; } @Override @@ -74,7 +72,7 @@ public void neighborUpdate(BlockState state, World world, BlockPos pos, Block bl if(state.get(POWERED) != world.isReceivingRedstonePower(pos)) { BlockEntity entity = world.getBlockEntity(pos); if(!state.get(POWERED) && entity instanceof InfusionTableEntity) - ((InfusionTableEntity) entity).startCrafting = true; + ((InfusionTableEntity) entity).inventory.properties.setSignal(true); world.setBlockState(pos, state.with(POWERED, world.isReceivingRedstonePower(pos))); } } diff --git a/src/main/java/modfest/lacrimis/block/entity/CombinerEntity.java b/src/main/java/modfest/lacrimis/block/entity/CombinerEntity.java index 3252d84c..3213aeff 100644 --- a/src/main/java/modfest/lacrimis/block/entity/CombinerEntity.java +++ b/src/main/java/modfest/lacrimis/block/entity/CombinerEntity.java @@ -1,20 +1,51 @@ package modfest.lacrimis.block.entity; +import modfest.lacrimis.crafting.CombinerScreenHandler; import modfest.lacrimis.init.ModEntities; +import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; import net.minecraft.block.BlockState; import net.minecraft.entity.EntityType; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.nbt.CompoundTag; +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.Identifier; import net.minecraft.util.registry.Registry; +import org.jetbrains.annotations.Nullable; -public class CombinerEntity extends SoulTankEntity { +public class CombinerEntity extends SoulTankEntity implements ExtendedScreenHandlerFactory { + public static final int SIZE = 2; public EntityType type; public CombinerEntity() { - super(ModEntities.combiner, 0, 2); + super(ModEntities.combiner, 0, SIZE); type = null; } + @Override + public Text getDisplayName() { + return new TranslatableText("lacrimis.gui.combiner"); + } + + @Nullable + @Override + public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { + return new CombinerScreenHandler(syncId, inv, inventory, type, pos); + } + + @Override + public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf) { + String s = "null"; + if(type != null) + s = Registry.ENTITY_TYPE.getId(type).toString(); + buf.writeBlockPos(pos); + buf.writeString(s); + } + @Override public void fromTag(BlockState state, CompoundTag tag) { super.fromTag(state, tag); diff --git a/src/main/java/modfest/lacrimis/block/entity/InfusionTableEntity.java b/src/main/java/modfest/lacrimis/block/entity/InfusionTableEntity.java index a4fed98f..f878ddb0 100644 --- a/src/main/java/modfest/lacrimis/block/entity/InfusionTableEntity.java +++ b/src/main/java/modfest/lacrimis/block/entity/InfusionTableEntity.java @@ -4,33 +4,52 @@ import java.util.stream.IntStream; import modfest.lacrimis.crafting.InfusionRecipe; +import modfest.lacrimis.crafting.InfusionScreenHandler; import modfest.lacrimis.init.ModCrafting; import modfest.lacrimis.init.ModEntities; import modfest.lacrimis.init.ModParticles; import modfest.lacrimis.util.DuctUtil; import modfest.lacrimis.util.SoulTank; import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.recipe.CraftingRecipe; import net.minecraft.recipe.RecipeType; +import net.minecraft.screen.NamedScreenHandlerFactory; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; import net.minecraft.util.Tickable; import net.minecraft.util.math.Direction; import org.jetbrains.annotations.Nullable; -public class InfusionTableEntity extends SoulTankEntity implements Tickable { +public class InfusionTableEntity extends SoulTankEntity implements Tickable, NamedScreenHandlerFactory { + public static final int CAPACITY = 1000; + public static final int SIZE = 10; public static final int OUTPUT_STACK = 9; public static final int[] INPUT_STACKS = IntStream.rangeClosed(0, 8).toArray(); private final Random random = new Random(); public ItemStack holding = ItemStack.EMPTY; - public boolean startCrafting = false; public InfusionTableEntity() { super(ModEntities.infusionTable, 1000, 10); getTank().setLimit(0); } + @Override + public Text getDisplayName() { + return new TranslatableText("lacrimis.gui.infusion"); + } + + @Nullable + @Override + public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { + return new InfusionScreenHandler(syncId, inv, inventory); + } + @Override public void tick() { if(world == null) @@ -68,7 +87,7 @@ else if (holding.getItem() == output.getItem()) } //Check for new recipe - if(holding.isEmpty() && (startCrafting || !inventory.getStack(OUTPUT_STACK).isEmpty())) { + if(holding.isEmpty() && (inventory.properties.hasSignal() || !inventory.getStack(OUTPUT_STACK).isEmpty())) { InfusionRecipe recipe = this.world.getRecipeManager().getFirstMatch(ModCrafting.INFUSION_RECIPE, inventory, this.world).orElse(null); if(recipe == null) recipe = this.world.getRecipeManager().getFirstMatch(ModCrafting.CRUCIBLE_RECIPE, inventory, this.world).orElse(null); @@ -82,17 +101,17 @@ else if (holding.getItem() == output.getItem()) takeIngredients(); tank.setLimit(recipe.getTears()); holding = recipe.getOutput().copy(); - startCrafting = false; + inventory.properties.setSignal(false); } else if(vanillaRecipe != null && canAcceptOutput(vanillaRecipe.getOutput())) { takeIngredients(); tank.setLimit(5); holding = vanillaRecipe.getOutput().copy(); - startCrafting = false; + inventory.properties.setSignal(false); } } - if(startCrafting) - startCrafting = false; + if(inventory.properties.hasSignal()) + inventory.properties.setSignal(false); //Collect tears if(tank.getSpace() > 0) diff --git a/src/main/java/modfest/lacrimis/block/entity/SoulTankEntity.java b/src/main/java/modfest/lacrimis/block/entity/SoulTankEntity.java index e0f51e4a..714fabd6 100644 --- a/src/main/java/modfest/lacrimis/block/entity/SoulTankEntity.java +++ b/src/main/java/modfest/lacrimis/block/entity/SoulTankEntity.java @@ -24,7 +24,7 @@ public SoulTankEntity(BlockEntityType type, int capacity, int inventory) { super(type); tank = new SoulTank(capacity); tank.addListener(this::mark); - this.inventory = new InfusionInventory(this, inventory); + this.inventory = new InfusionInventory(tank, inventory); } public float getRelativeLevel() { diff --git a/src/main/java/modfest/lacrimis/client/init/ClientModCrafting.java b/src/main/java/modfest/lacrimis/client/init/ClientModCrafting.java index 88b87def..72a2b73e 100644 --- a/src/main/java/modfest/lacrimis/client/init/ClientModCrafting.java +++ b/src/main/java/modfest/lacrimis/client/init/ClientModCrafting.java @@ -1,28 +1,20 @@ package modfest.lacrimis.client.init; import modfest.lacrimis.Lacrimis; +import modfest.lacrimis.client.render.screen.CombinerScreen; +import modfest.lacrimis.client.render.screen.InfusionScreen; import modfest.lacrimis.compat.patchiouli.PageCrucible; import modfest.lacrimis.compat.patchiouli.PageInfusion; -import modfest.lacrimis.crafting.CombinerScreen; -import modfest.lacrimis.crafting.CombinerScreenHandler; -import modfest.lacrimis.crafting.InfusionScreen; -import modfest.lacrimis.crafting.InfusionScreenHandler; import modfest.lacrimis.init.ModCrafting; -import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry; -import net.minecraft.client.MinecraftClient; -import net.minecraft.text.TranslatableText; +import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry; import net.minecraft.util.Identifier; import vazkii.patchouli.client.book.ClientBookRegistry; public class ClientModCrafting { public static void registerClient() { - if(MinecraftClient.getInstance().player != null) { - // Infusion GUI - ScreenProviderRegistry.INSTANCE.registerFactory(ModCrafting.INFUSION_SCREEN_ID, - container -> new InfusionScreen(container, MinecraftClient.getInstance().player.inventory, new TranslatableText(Lacrimis.MODID + ".gui.infusion"))); - ScreenProviderRegistry.INSTANCE.registerFactory(ModCrafting.COMBINER_SCREEN_ID, - container -> new CombinerScreen(container, MinecraftClient.getInstance().player.inventory, new TranslatableText(Lacrimis.MODID + ".gui.combiner"))); - } + ScreenRegistry.register(ModCrafting.INFUSION_SCREEN_HANDLER, InfusionScreen::new); + ScreenRegistry.register(ModCrafting.COMBINER_SCREEN_HANDLER, CombinerScreen::new); + // Patchouli pages ClientBookRegistry.INSTANCE.pageTypes.put(new Identifier(Lacrimis.MODID, "crucible"), PageCrucible.class); ClientBookRegistry.INSTANCE.pageTypes.put(new Identifier(Lacrimis.MODID, "infusion"), PageInfusion.class); diff --git a/src/main/java/modfest/lacrimis/client/init/ClientModNetworking.java b/src/main/java/modfest/lacrimis/client/init/ClientModNetworking.java index 209f5f8c..1a9de616 100644 --- a/src/main/java/modfest/lacrimis/client/init/ClientModNetworking.java +++ b/src/main/java/modfest/lacrimis/client/init/ClientModNetworking.java @@ -40,16 +40,4 @@ private static void handleCrucibleParticlesPacket(MinecraftClient client, Client } }); } - - public static void sendCombinerNullPacket(BlockPos pos) { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - buf.writeBlockPos(pos); - ClientPlayNetworking.send(COMBINER_NULL_ID, buf); - } - - public static void sendInfusionStartPacket(BlockPos pos) { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - buf.writeBlockPos(pos); - ClientPlayNetworking.send(INFUSION_START_ID, buf); - } } diff --git a/src/main/java/modfest/lacrimis/crafting/CombinerScreen.java b/src/main/java/modfest/lacrimis/client/render/screen/CombinerScreen.java similarity index 92% rename from src/main/java/modfest/lacrimis/crafting/CombinerScreen.java rename to src/main/java/modfest/lacrimis/client/render/screen/CombinerScreen.java index ea22ca3d..3af5830e 100644 --- a/src/main/java/modfest/lacrimis/crafting/CombinerScreen.java +++ b/src/main/java/modfest/lacrimis/client/render/screen/CombinerScreen.java @@ -1,8 +1,10 @@ -package modfest.lacrimis.crafting; +package modfest.lacrimis.client.render.screen; import com.mojang.blaze3d.systems.RenderSystem; import modfest.lacrimis.Lacrimis; +import modfest.lacrimis.crafting.CombinerScreenHandler; 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; diff --git a/src/main/java/modfest/lacrimis/crafting/InfusionScreen.java b/src/main/java/modfest/lacrimis/client/render/screen/InfusionScreen.java similarity index 83% rename from src/main/java/modfest/lacrimis/crafting/InfusionScreen.java rename to src/main/java/modfest/lacrimis/client/render/screen/InfusionScreen.java index 0e2a6a45..b19cc68d 100644 --- a/src/main/java/modfest/lacrimis/crafting/InfusionScreen.java +++ b/src/main/java/modfest/lacrimis/client/render/screen/InfusionScreen.java @@ -1,18 +1,17 @@ -package modfest.lacrimis.crafting; +package modfest.lacrimis.client.render.screen; import com.mojang.blaze3d.systems.RenderSystem; import modfest.lacrimis.Lacrimis; +import modfest.lacrimis.crafting.InfusionScreenHandler; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.screen.ingame.HandledScreen; -import net.minecraft.client.gui.screen.recipebook.RecipeBookProvider; -import net.minecraft.client.gui.screen.recipebook.RecipeBookWidget; import net.minecraft.client.gui.widget.TexturedButtonWidget; +import net.minecraft.client.render.GameRenderer; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.screen.slot.Slot; -import net.minecraft.screen.slot.SlotActionType; +import net.minecraft.screen.EnchantmentScreenHandler; import net.minecraft.text.Text; import net.minecraft.util.Identifier; @@ -21,7 +20,6 @@ public class InfusionScreen extends HandledScreen { private static final Identifier TEXTURE = new Identifier("textures/gui/container/crafting_table.png"); private static final Identifier START_BUTTON_TEXTURE = new Identifier(Lacrimis.MODID, "textures/gui/start_button.png"); - private TexturedButtonWidget startButton; private boolean isNarrow; public InfusionScreen(InfusionScreenHandler handler, PlayerInventory inventory, Text title) { @@ -32,8 +30,8 @@ public InfusionScreen(InfusionScreenHandler handler, PlayerInventory inventory, protected void init() { super.init(); isNarrow = width < 379; - startButton = new TexturedButtonWidget(x + backgroundWidth - 54, y + titleY + 3, 20, 18, 0, 0, 19, START_BUTTON_TEXTURE, (buttonWidget) -> { - handler.startCrafting(); + TexturedButtonWidget startButton = new TexturedButtonWidget(x + backgroundWidth - 54, y + titleY + 3, 20, 18, 0, 0, 19, START_BUTTON_TEXTURE, (buttonWidget) -> { + this.client.interactionManager.clickButton(this.handler.syncId, 0); }); addButton(startButton); titleX = 24; diff --git a/src/main/java/modfest/lacrimis/compat/patchiouli/PageInfusion.java b/src/main/java/modfest/lacrimis/compat/patchiouli/PageInfusion.java index bfd731b1..f1887f8b 100644 --- a/src/main/java/modfest/lacrimis/compat/patchiouli/PageInfusion.java +++ b/src/main/java/modfest/lacrimis/compat/patchiouli/PageInfusion.java @@ -1,10 +1,12 @@ package modfest.lacrimis.compat.patchiouli; +import com.google.gson.annotations.SerializedName; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.item.ItemStack; import net.minecraft.recipe.Ingredient; import net.minecraft.text.TranslatableText; +import net.minecraft.util.Identifier; import net.minecraft.util.collection.DefaultedList; import com.mojang.blaze3d.systems.RenderSystem; @@ -15,13 +17,16 @@ import modfest.lacrimis.init.ModCrafting; public class PageInfusion extends PageDoubleRecipeRegistry { + @SerializedName("crafting_texture") + public static final Identifier TEXTURE = new Identifier("lacrimis", "textures/gui/crafting.png"); + public PageInfusion() { super(ModCrafting.INFUSION_RECIPE); } @Override protected void drawRecipe(MatrixStack ms, InfusionRecipe recipe, int recipeX, int recipeY, int mouseX, int mouseY, boolean second) { - this.mc.getTextureManager().bindTexture(ModCrafting.craftingTexture); + this.mc.getTextureManager().bindTexture(TEXTURE); RenderSystem.enableBlend(); DrawableHelper.drawTexture(ms, recipeX - 2, recipeY - 2, 0.0F, 0.0F, 100, 62, 128, 128); boolean shaped = recipe instanceof ShapedInfusionRecipe; diff --git a/src/main/java/modfest/lacrimis/crafting/CombinerScreenHandler.java b/src/main/java/modfest/lacrimis/crafting/CombinerScreenHandler.java index e1248509..3bbccb33 100644 --- a/src/main/java/modfest/lacrimis/crafting/CombinerScreenHandler.java +++ b/src/main/java/modfest/lacrimis/crafting/CombinerScreenHandler.java @@ -1,105 +1,124 @@ package modfest.lacrimis.crafting; -import modfest.lacrimis.client.init.ClientModNetworking; +import modfest.lacrimis.block.entity.CombinerEntity; +import modfest.lacrimis.init.ModCrafting; +import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.EntityType; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.CraftingResultInventory; import net.minecraft.inventory.Inventory; import net.minecraft.inventory.InventoryChangedListener; +import net.minecraft.inventory.SimpleInventory; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; +import net.minecraft.network.PacketByteBuf; import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.slot.FurnaceOutputSlot; import net.minecraft.screen.slot.Slot; +import net.minecraft.screen.slot.SlotActionType; import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import modfest.lacrimis.Lacrimis; -import modfest.lacrimis.block.entity.CombinerEntity; import modfest.lacrimis.init.ModItems; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.registry.Registry; public class CombinerScreenHandler extends ScreenHandler implements InventoryChangedListener { - private final CombinerEntity entity; + private final SimpleInventory input; protected final Inventory output = new CraftingResultInventory(); + private EntityType entityType; + private BlockPos pos; + + public CombinerScreenHandler(int syncId, PlayerInventory player, PacketByteBuf buf) { + this(syncId, player, new SimpleInventory(CombinerEntity.SIZE), null, buf.readBlockPos()); + String s = buf.readString(); + if(!s.equals("null")) + entityType = Registry.ENTITY_TYPE.get(Identifier.tryParse(s)); + } - public CombinerScreenHandler(int syncId, PlayerEntity player, CombinerEntity entity) { - super(null, syncId); - this.entity = entity; - - if (entity.getWorld() != null && !entity.getWorld().isClient) - entity.inventory.addListener(this); + public CombinerScreenHandler(int syncId, PlayerInventory player, SimpleInventory inventory, EntityType type, BlockPos pos) { + super(ModCrafting.COMBINER_SCREEN_HANDLER, syncId); + this.input = inventory; + this.input.addListener(this); + this.entityType = type; + this.pos = pos; - this.addSlot(new Slot(entity.inventory, 0, 27, 47)); - this.addSlot(new Slot(entity.inventory, 1, 76, 47)); - this.addSlot(new FurnaceOutputSlot(player, output, 0, 134, 47) { + this.addSlot(new Slot(inventory, 0, 27, 47)); + this.addSlot(new Slot(inventory, 1, 76, 47)); + this.addSlot(new Slot(output, 2, 134, 47) { @Override public ItemStack onTakeItem(PlayerEntity player, ItemStack stack) { - return CombinerScreenHandler.this.onTakeOutput(stack); + CombinerScreenHandler.this.onTakeOutput(player); + return stack; } }); int k; for(k = 0; k < 3; ++k) { for(int j = 0; j < 9; ++j) { - this.addSlot(new Slot(player.inventory, j + k * 9 + 9, 8 + j * 18, 84 + k * 18)); + this.addSlot(new Slot(player, j + k * 9 + 9, 8 + j * 18, 84 + k * 18)); } } for(k = 0; k < 9; ++k) { - this.addSlot(new Slot(player.inventory, k, 8 + k * 18, 142)); + this.addSlot(new Slot(player, k, 8 + k * 18, 142)); } - onContentChanged(entity.inventory); + updateResult(); } - protected ItemStack onTakeOutput(ItemStack stack) { + protected void onTakeOutput(PlayerEntity player) { decrement(0); decrement(1); - entity.type = null; + entityType = null; output.setStack(0, ItemStack.EMPTY); - if(entity.getWorld() != null && entity.getWorld().isClient) - ClientModNetworking.sendCombinerNullPacket(entity.getPos()); - return stack; + + BlockEntity blockEntity = player.world.getBlockEntity(pos); + if(blockEntity instanceof CombinerEntity) + ((CombinerEntity) blockEntity).type = null; } private void decrement(int i) { - ItemStack itemStack = entity.inventory.getStack(i); + ItemStack itemStack = input.getStack(i); itemStack.decrement(1); - entity.inventory.setStack(i, itemStack); + input.setStack(i, itemStack); } public void updateResult() { - if(entity.type != null && entity.inventory.getStack(1).getItem() == ModItems.taintedSludge) { - if(entity.inventory.getStack(0).getItem() == ModItems.brokenSpawner) - if(output.getStack(0).isEmpty()) { - String id = EntityType.getId(entity.type).toString(); - CompoundTag[] tags = new CompoundTag[5]; - for(int i = 0; i < tags.length; i++) - tags[i] = new CompoundTag(); - - //Build tags - tags[4].putString("id", id); - tags[3].put("Entity", tags[4]); - tags[3].putInt("Weight", 1); - ListTag list = new ListTag(); - list.add(tags[3]); - tags[1].put("SpawnPotentials", list); - tags[2].putString("id", id); - tags[1].put("SpawnData", tags[2]); - tags[0].put("BlockEntityTag", tags[1]); - - //Set output - ItemStack stack = new ItemStack(Items.SPAWNER); - MutableText text = new TranslatableText(entity.type.getTranslationKey()); - text.append(new TranslatableText(Lacrimis.MODID + ".tooltip.spawner")); - stack.setTag(tags[0]); - stack.setCustomName(text); - //{BlockEntityTag:{SpawnData:{id:"#ID"},SpawnPotentials:[{Entity:{id:"#ID"}, Weight:1}]}} - output.setStack(0, stack); - } - return; + if(entityType != null && input.getStack(1).getItem() == ModItems.taintedSludge && + input.getStack(0).getItem() == ModItems.brokenSpawner) { + if(output.getStack(0).isEmpty()) { + String id = EntityType.getId(entityType).toString(); + CompoundTag[] tags = new CompoundTag[5]; + for(int i = 0; i < tags.length; i++) + tags[i] = new CompoundTag(); + + //Build tags + tags[4].putString("id", id); + tags[3].put("Entity", tags[4]); + tags[3].putInt("Weight", 1); + ListTag list = new ListTag(); + list.add(tags[3]); + tags[1].put("SpawnPotentials", list); + tags[2].putString("id", id); + tags[1].put("SpawnData", tags[2]); + tags[0].put("BlockEntityTag", tags[1]); + + //Set output + ItemStack stack = new ItemStack(Items.SPAWNER); + MutableText text = new TranslatableText(entityType.getTranslationKey()); + text.append(new TranslatableText(Lacrimis.MODID + ".tooltip.spawner")); + stack.setTag(tags[0]); + stack.setCustomName(text); + //{BlockEntityTag:{SpawnData:{id:"#ID"},SpawnPotentials:[{Entity:{id:"#ID"}, Weight:1}]}} + output.setStack(0, stack); + } + return; } if(!output.getStack(0).isEmpty()) output.setStack(0, ItemStack.EMPTY); @@ -113,20 +132,25 @@ public void onInventoryChanged(Inventory sender) { @Override public void onContentChanged(Inventory inventory) { super.onContentChanged(inventory); - if(entity.inventory == inventory) + if(inventory == output && output.getStack(0).isEmpty()) { + entityType = null; + this.updateResult(); + } + + if(inventory == input) this.updateResult(); } @Override public boolean canUse(PlayerEntity player) { - return this.entity.inventory.canPlayerUse(player); + return input.canPlayerUse(player); } @Override public ItemStack transferSlot(PlayerEntity player, int index) { ItemStack itemStack = ItemStack.EMPTY; Slot slot = slots.get(index); - if (slot != null && slot.hasStack()) { + if (slot.hasStack()) { ItemStack itemStack2 = slot.getStack(); itemStack = itemStack2.copy(); if (index == 2) { @@ -162,8 +186,8 @@ public ItemStack transferSlot(PlayerEntity player, int index) { } public Text getEntity() { - if(entity.type != null) { - Text t = new TranslatableText(entity.type.getTranslationKey()); + if(entityType != null) { + Text t = new TranslatableText(entityType.getTranslationKey()); return new TranslatableText(Lacrimis.MODID + ".gui.combiner.entity").append(t); } return new TranslatableText(Lacrimis.MODID + ".gui.combiner.none"); diff --git a/src/main/java/modfest/lacrimis/crafting/InfusionInventory.java b/src/main/java/modfest/lacrimis/crafting/InfusionInventory.java index c4429e6c..159f6a66 100644 --- a/src/main/java/modfest/lacrimis/crafting/InfusionInventory.java +++ b/src/main/java/modfest/lacrimis/crafting/InfusionInventory.java @@ -1,43 +1,35 @@ package modfest.lacrimis.crafting; +import modfest.lacrimis.util.SoulTank; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.CraftingInventory; import net.minecraft.inventory.Inventory; import net.minecraft.inventory.SimpleInventory; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; import net.minecraft.screen.PropertyDelegate; -import modfest.lacrimis.block.entity.SoulTankEntity; import net.minecraft.screen.ScreenHandler; -import net.minecraft.screen.ScreenHandlerType; -import org.jetbrains.annotations.Nullable; public class InfusionInventory extends SimpleInventory { - public final CustomDelegate properties; - public final SoulTankEntity entity; + public final static int TEARS = 0; + public final static int CAPACITY = 1; + public final static int SIGNAL = 2; + public final SoulTankDelegate properties; + public final SoulTank tank; + + public InfusionInventory(int capacity, int size) { + this(new SoulTank(capacity), size); + } - public InfusionInventory(SoulTankEntity entity, int size) { + public InfusionInventory(SoulTank tank, int size) { super(size); - this.entity = entity; - this.properties = new CustomDelegate(entity); + this.tank = tank; + this.properties = new SoulTankDelegate(tank); - entity.getTank().addListener(this::markDirty); - } - - @Override - public void markDirty() { - super.markDirty(); - this.entity.markDirty(); + tank.addListener(this::markDirty); } public int getTears() { - return properties.get(0); - } - - public void removeTears(int tears) { - entity.getTank().removeTears(tears); + return properties.get(TEARS); } public CraftingInventory setupCrafting() { @@ -49,69 +41,46 @@ public CraftingInventory setupCrafting() { return crafting; } - @Override - public void readTags(ListTag tags) { - int j; - for(j = 0; j < this.size(); ++j) { - this.setStack(j, ItemStack.EMPTY); - } - - for(j = 0; j < tags.size(); ++j) { - CompoundTag compoundTag = tags.getCompound(j); - int k = compoundTag.getByte("Slot") & 255; - if (k < this.size()) { - this.setStack(k, ItemStack.fromTag(compoundTag)); - } - } - - } - - @Override - public ListTag getTags() { - ListTag listTag = new ListTag(); - - for(int i = 0; i < this.size(); ++i) { - ItemStack itemStack = this.getStack(i); - if (!itemStack.isEmpty()) { - CompoundTag compoundTag = new CompoundTag(); - compoundTag.putByte("Slot", (byte)i); - itemStack.toTag(compoundTag); - listTag.add(compoundTag); - } - } + public static class SoulTankDelegate implements PropertyDelegate { + private final SoulTank tank; + private boolean signal = false; - return listTag; - } - - private static class CustomDelegate implements PropertyDelegate { - private final SoulTankEntity entity; - - CustomDelegate(SoulTankEntity entity) { - this.entity = entity; + SoulTankDelegate(SoulTank tank) { + this.tank = tank; } @Override public int size() { - return 1; + return 2; } @Override public int get(int index) { - switch (index) { - case 0: - return entity.getTank().getTears(); - default: - return 0; - } + if(index == TEARS) + return tank.getTears(); + else if(index == CAPACITY) + return tank.getCapacity(); + else if(index == SIGNAL) + return signal ? 1 : 0; + return 0; } @Override public void set(int index, int value) { - switch (index) { - case 0: - entity.getTank().setTears(value); - break; - } + if(index == TEARS) + tank.setTears(value); + else if(index == CAPACITY) + tank.setLimit(value); + else if(index == SIGNAL) + signal = value > 0; + } + + public boolean hasSignal() { + return get(SIGNAL) > 0; + } + + public void setSignal(boolean value) { + set(SIGNAL, value ? 1 : 0); } } diff --git a/src/main/java/modfest/lacrimis/crafting/InfusionScreenHandler.java b/src/main/java/modfest/lacrimis/crafting/InfusionScreenHandler.java index 9dbf8ca3..3f905277 100644 --- a/src/main/java/modfest/lacrimis/crafting/InfusionScreenHandler.java +++ b/src/main/java/modfest/lacrimis/crafting/InfusionScreenHandler.java @@ -1,10 +1,11 @@ package modfest.lacrimis.crafting; import modfest.lacrimis.block.entity.InfusionTableEntity; -import modfest.lacrimis.client.init.ClientModNetworking; +import modfest.lacrimis.init.ModCrafting; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.Inventory; import net.minecraft.inventory.InventoryChangedListener; import net.minecraft.item.ItemStack; @@ -14,24 +15,25 @@ import net.minecraft.screen.AbstractRecipeScreenHandler; import net.minecraft.screen.slot.FurnaceOutputSlot; import net.minecraft.screen.slot.Slot; +import net.minecraft.world.World; public class InfusionScreenHandler extends AbstractRecipeScreenHandler implements InventoryChangedListener { private static final int OUTPUT_SLOT = 0; private final InfusionInventory input; - private final InfusionTableEntity entity; - private final PlayerEntity player; - - public InfusionScreenHandler(int syncId, PlayerEntity player, InfusionTableEntity entity) { - super(null, syncId); - this.input = entity.inventory; - this.entity = entity; - this.player = player; - this.addProperties(input.properties); + private final World world; - if (entity.getWorld() != null && !entity.getWorld().isClient) - this.input.addListener(this); + public InfusionScreenHandler(int syncId, PlayerInventory player) { + this(syncId, player, new InfusionInventory(InfusionTableEntity.CAPACITY, InfusionTableEntity.SIZE)); + } - this.addSlot(new FurnaceOutputSlot(player, this.input, InfusionTableEntity.OUTPUT_STACK, 124, 35)); + public InfusionScreenHandler(int syncId, PlayerInventory player, InfusionInventory inventory) { + super(ModCrafting.INFUSION_SCREEN_HANDLER, syncId); + this.world = player.player.world; + this.input = inventory; + this.input.addListener(this); + this.addProperties(input.properties); + + this.addSlot(new FurnaceOutputSlot(player.player, this.input, InfusionTableEntity.OUTPUT_STACK, 124, 35)); for (int y = 0; y < 3; ++y) { for (int x = 0; x < 3; ++x) { this.addSlot(new Slot(this.input, x + y * 3, 30 + x * 18, 17 + y * 18)); @@ -39,19 +41,21 @@ public InfusionScreenHandler(int syncId, PlayerEntity player, InfusionTableEntit } for (int y = 0; y < 3; ++y) { for (int x = 0; x < 9; ++x) { - this.addSlot(new Slot(player.inventory, x + y * 9 + 9, 8 + x * 18, 84 + y * 18)); + this.addSlot(new Slot(player, x + y * 9 + 9, 8 + x * 18, 84 + y * 18)); } } for (int x = 0; x < 9; ++x) { - this.addSlot(new Slot(player.inventory, x, 8 + x * 18, 142)); + this.addSlot(new Slot(player, x, 8 + x * 18, 142)); } onContentChanged(input); } - public void startCrafting() { - entity.startCrafting = true; - ClientModNetworking.sendInfusionStartPacket(entity.getPos()); + @Override + public boolean onButtonClick(PlayerEntity player, int id) { + if(id == 0) + input.properties.setSignal(true); + return super.onButtonClick(player, id); } @Override @@ -65,30 +69,29 @@ public void clearCraftingSlots() { @Override public boolean matches(Recipe recipe) { - return recipe.matches(this.input, this.player.world); + return recipe.matches(this.input, this.world); } @Override public void close(PlayerEntity player) { super.close(player); - if(entity.getWorld() != null && !entity.getWorld().isClient) - this.input.removeListener(this); + this.input.removeListener(this); } @Override public boolean canUse(PlayerEntity player) { - return this.entity.inventory.canPlayerUse(player); + return this.input.canPlayerUse(player); } @Override public ItemStack transferSlot(PlayerEntity player, int index) { ItemStack leftInHand = ItemStack.EMPTY; Slot slot = this.slots.get(index); - if (slot != null && slot.hasStack()) { + if (slot.hasStack()) { ItemStack transfered = slot.getStack(); leftInHand = transfered.copy(); if (index == OUTPUT_SLOT) { - transfered.getItem().onCraft(transfered, this.entity.getWorld(), player); + transfered.getItem().onCraft(transfered, player.world, player); if (!this.insertItem(transfered, input.size(), 46, true)) { return ItemStack.EMPTY; } @@ -118,10 +121,10 @@ public ItemStack transferSlot(PlayerEntity player, int index) { return ItemStack.EMPTY; } - ItemStack itemStack3 = slot.onTakeItem(player, transfered); - if (index == OUTPUT_SLOT) { + slot.onTakeItem(player, transfered); + /*if (index == OUTPUT_SLOT) { player.dropItem(itemStack3, false); - } + }*/ } return leftInHand; @@ -158,11 +161,16 @@ public RecipeBookCategory getCategory() { return null; } + @Override + public boolean canInsertIntoSlot(Slot slot) { + return slot.id > OUTPUT_SLOT; + } + public int getRequiredTears() { - return this.entity.getTank().getCapacity(); + return input.properties.get(InfusionInventory.CAPACITY); } public int getCurrentTears() { - return this.entity.getTank().getTears(); + return input.properties.get(InfusionInventory.TEARS); } } diff --git a/src/main/java/modfest/lacrimis/init/ModCrafting.java b/src/main/java/modfest/lacrimis/init/ModCrafting.java index 9eeff8fa..51ee0267 100644 --- a/src/main/java/modfest/lacrimis/init/ModCrafting.java +++ b/src/main/java/modfest/lacrimis/init/ModCrafting.java @@ -1,11 +1,13 @@ package modfest.lacrimis.init; import modfest.lacrimis.crafting.*; +import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; import net.minecraft.recipe.RecipeSerializer; import net.minecraft.recipe.RecipeType; import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.util.registry.Registry; @@ -22,6 +24,9 @@ public class ModCrafting { public static final Identifier INFUSION_SCREEN_ID = new Identifier(Lacrimis.MODID, "infusion"); public static final Identifier COMBINER_SCREEN_ID = new Identifier(Lacrimis.MODID, "combiner"); + public static ScreenHandlerType INFUSION_SCREEN_HANDLER; + public static ScreenHandlerType COMBINER_SCREEN_HANDLER; + public static final RecipeType INFUSION_RECIPE = new RecipeType() { @Override public String toString() { @@ -39,29 +44,9 @@ public String toString() { public static final RecipeSerializer SHAPELESS_INFUSION_SERIALIZER = new ShapelessInfusionRecipe.Serializer(); public static final RecipeSerializer CRUCIBLE_SERIALIZER = new CrucibleRecipe.Serializer(); - @SerializedName("crafting_texture") - public static final Identifier craftingTexture = new Identifier("lacrimis", "textures/gui/crafting.png"); - - // Don't convert the container factories to lambdas, it will cause the mod - // to crash on the server because of class loading - @SuppressWarnings("Convert2Lambda") public static void register() { - ContainerProviderRegistry.INSTANCE.registerFactory(INFUSION_SCREEN_ID, new ContainerFactory() { - @Override - public ScreenHandler create(int syncId, Identifier identifier, PlayerEntity player, PacketByteBuf buf) { - BlockPos pos = buf.readBlockPos(); - InfusionTableEntity entity = (InfusionTableEntity) player.getEntityWorld().getBlockEntity(pos); - return new InfusionScreenHandler(syncId, player, entity); - } - }); - ContainerProviderRegistry.INSTANCE.registerFactory(COMBINER_SCREEN_ID, new ContainerFactory() { - @Override - public ScreenHandler create(int syncId, Identifier identifier, PlayerEntity player, PacketByteBuf buf) { - BlockPos pos = buf.readBlockPos(); - CombinerEntity entity = (CombinerEntity) player.getEntityWorld().getBlockEntity(pos); - return new CombinerScreenHandler(syncId, player, entity); - } - }); + INFUSION_SCREEN_HANDLER = ScreenHandlerRegistry.registerSimple(INFUSION_SCREEN_ID, InfusionScreenHandler::new); + COMBINER_SCREEN_HANDLER = ScreenHandlerRegistry.registerExtended(COMBINER_SCREEN_ID, CombinerScreenHandler::new); Registry.register(Registry.RECIPE_TYPE, new Identifier(Lacrimis.MODID, "infusion"), INFUSION_RECIPE); Registry.register(Registry.RECIPE_TYPE, new Identifier(Lacrimis.MODID, "crucible"), CRUCIBLE_RECIPE); diff --git a/src/main/java/modfest/lacrimis/init/ModItems.java b/src/main/java/modfest/lacrimis/init/ModItems.java index 4690bd7b..d17d2e2f 100644 --- a/src/main/java/modfest/lacrimis/init/ModItems.java +++ b/src/main/java/modfest/lacrimis/init/ModItems.java @@ -21,6 +21,7 @@ public class ModItems { private static final Item.Settings SETTINGS = new Item.Settings().group(Lacrimis.ITEM_GROUP); + private static final Item.Settings SMALL_SETTINGS = new Item.Settings().group(Lacrimis.ITEM_GROUP).maxCount(16); private static final Item.Settings TOOL_SETTINGS = new Item.Settings().group(Lacrimis.ITEM_GROUP).maxCount(1); private static final Item.Settings RUNE_SETTINGS = new Item.Settings().group(Lacrimis.RUNE_ITEM_GROUP); @@ -103,8 +104,8 @@ public static void register() { bottleOfTears = register("bottle_of_tears", new BottleOfTearsItem(SETTINGS)); diviningRod = register("divining_rod", new DiviningRodItem(TOOL_SETTINGS)); goldDiviningRod = register("gold_divining_rod", new DiviningRodItem(TOOL_SETTINGS)); - taintedPearl = register("tainted_pearl", new TaintedPearlItem(SETTINGS.maxCount(16))); - soulShell = register("soul_shell", new SoulShellItem(SETTINGS.maxCount(16))); + taintedPearl = register("tainted_pearl", new TaintedPearlItem(SMALL_SETTINGS)); + soulShell = register("soul_shell", new SoulShellItem(SMALL_SETTINGS)); soulTotem = register("soul_totem", new SoulTotemItem(TOOL_SETTINGS)); tearIngot = register("tear_ingot", new Item(SETTINGS)); taintedSludge = register("tainted_sludge", new Item(SETTINGS)); diff --git a/src/main/java/modfest/lacrimis/init/ModNetworking.java b/src/main/java/modfest/lacrimis/init/ModNetworking.java index ad8c1c0c..4fb15167 100644 --- a/src/main/java/modfest/lacrimis/init/ModNetworking.java +++ b/src/main/java/modfest/lacrimis/init/ModNetworking.java @@ -1,54 +1,19 @@ package modfest.lacrimis.init; -import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.api.networking.v1.PlayerLookup; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.block.entity.BlockEntity; import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; -import net.minecraft.util.math.BlockPos; import io.netty.buffer.Unpooled; - import modfest.lacrimis.Lacrimis; -import modfest.lacrimis.block.entity.CombinerEntity; -import modfest.lacrimis.block.entity.InfusionTableEntity; public class ModNetworking { - public static final Identifier INFUSION_START_ID = new Identifier(Lacrimis.MODID, "infusion_start"); - public static final Identifier COMBINER_NULL_ID = new Identifier(Lacrimis.MODID, "combiner_null"); public static final Identifier CRUCIBLE_PARTICLES_ID = new Identifier(Lacrimis.MODID, "crucible_particles"); - public static void register() { - ServerPlayNetworking.registerGlobalReceiver(INFUSION_START_ID, ModNetworking::handleInfusionStartPacket); - ServerPlayNetworking.registerGlobalReceiver(COMBINER_NULL_ID, ModNetworking::handleCombinerNullPacket); - } - - //Server Infusion Start - private static void handleInfusionStartPacket(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buffer, PacketSender sender) { - BlockPos pos = buffer.readBlockPos(); - - server.execute(() -> { - BlockEntity entity = player.world.getBlockEntity(pos); - if(entity instanceof InfusionTableEntity) - ((InfusionTableEntity) entity).startCrafting = true; - }); - } - - //Server Combiner Null - private static void handleCombinerNullPacket(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buffer, PacketSender sender) { - BlockPos pos = buffer.readBlockPos(); - server.execute(() -> { - BlockEntity entity = player.world.getBlockEntity(pos); - if(entity instanceof CombinerEntity) - ((CombinerEntity) entity).type = null; - }); - } - public static void sendCrucibleParticlesPacket(ServerWorld world, BlockEntity entity) { PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); buf.writeDouble(entity.getPos().getX()); diff --git a/src/main/java/modfest/lacrimis/util/DuctUtil.java b/src/main/java/modfest/lacrimis/util/DuctUtil.java index a9cd2589..934528f5 100644 --- a/src/main/java/modfest/lacrimis/util/DuctUtil.java +++ b/src/main/java/modfest/lacrimis/util/DuctUtil.java @@ -27,7 +27,7 @@ public class DuctUtil { public static List scanDucts(World world, BlockPos pos, boolean extracting, int goal, Tester test) { - if(world == null || world instanceof ClientWorld) + if(world == null || world.isClient) return new ArrayList<>(); //Set up block lists