diff --git a/src/main/java/io/github/alloffabric/artis/Artis.java b/src/main/java/io/github/alloffabric/artis/Artis.java index f96de1d..5f5d6d1 100644 --- a/src/main/java/io/github/alloffabric/artis/Artis.java +++ b/src/main/java/io/github/alloffabric/artis/Artis.java @@ -13,15 +13,21 @@ import io.github.alloffabric.artis.util.ArtisRegistry; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; +import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; +import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry; import net.fabricmc.fabric.impl.screenhandler.ExtendedScreenHandlerType; import net.minecraft.block.Block; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.client.MinecraftClient; +import net.minecraft.inventory.CraftingInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.recipe.Recipe; +import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenHandlerContext; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; @@ -37,6 +43,7 @@ public class Artis implements ModInitializer { public static final String MODID = "artis"; public static final Identifier recipe_sync = new Identifier(MODID,"sync_recipe"); + public static final Identifier request_sync = new Identifier(MODID,"request_sync"); public static final Identifier dummy = new Identifier("null","null"); public static final Logger logger = LogManager.getLogger(); @@ -83,6 +90,17 @@ public void onInitialize() { ArtisEvents.init(); isLoaded = true; ARTIS_BLOCK_ENTITY = registerBlockEntity("artis_table", ArtisTableBlockEntity::new, Arrays.copyOf(ARTIS_TABLE_BLOCKS.toArray(), ARTIS_TABLE_BLOCKS.size(), ArtisTableBlock[].class)); + + //seems to be required to not have the recipe vanish when initially opened + ServerSidePacketRegistry.INSTANCE.register(Artis.request_sync, + (packetContext, attachedData) -> { + packetContext.getTaskQueue().execute(() -> { + ScreenHandler container = packetContext.getPlayer().currentScreenHandler; + if (container instanceof ArtisCraftingController) { + container.onContentChanged(null); + } + }); + }); } } } diff --git a/src/main/java/io/github/alloffabric/artis/ArtisClient.java b/src/main/java/io/github/alloffabric/artis/ArtisClient.java index 3d067d8..4a32995 100644 --- a/src/main/java/io/github/alloffabric/artis/ArtisClient.java +++ b/src/main/java/io/github/alloffabric/artis/ArtisClient.java @@ -96,7 +96,6 @@ public void onInitializeClient() { } }); }); - } public static void updateLastRecipe(ArtisCraftingController container, Recipe rec) { diff --git a/src/main/java/io/github/alloffabric/artis/inventory/ArtisCraftingController.java b/src/main/java/io/github/alloffabric/artis/inventory/ArtisCraftingController.java index db2210e..74c71f9 100644 --- a/src/main/java/io/github/alloffabric/artis/inventory/ArtisCraftingController.java +++ b/src/main/java/io/github/alloffabric/artis/inventory/ArtisCraftingController.java @@ -15,6 +15,7 @@ import io.netty.buffer.Unpooled; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.CraftingInventory; @@ -37,6 +38,7 @@ import net.minecraft.util.Identifier; import net.minecraft.world.World; +import java.util.List; import java.util.Optional; public class ArtisCraftingController extends SyncedGuiDescription implements RecipeProvider { @@ -68,7 +70,6 @@ public ArtisCraftingController(ScreenHandlerType type, ArtisTableType tableType, craftInv.setStack(i, blockInventory.getStack(i)); } } - ContainerLayout layout = new ContainerLayout(tableType.getWidth(), tableType.getHeight()); panel = new WPlainPanel(); @@ -98,6 +99,11 @@ public ArtisCraftingController(ScreenHandlerType type, ArtisTableType tableType, panel.add(arrow, layout.getArrowX(), layout.getArrowY() + 4, 22, 15); panel.validate(this); + craftInv.setCheckMatrixChanges(true); + if (player.world.isClient) { + PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); + ClientSidePacketRegistry.INSTANCE.sendToServer(Artis.request_sync, buf); + } } private static BackgroundPainter slotColor(int color) { @@ -195,6 +201,16 @@ public int getCraftingSlotCount() { return getTableType().getWidth() * getTableType().getHeight(); } + // update crafting + //clientside only + @Override + public void updateSlotStacks(List stacks) { + craftInv.setCheckMatrixChanges(false); + super.updateSlotStacks(stacks); + craftInv.setCheckMatrixChanges(true); + onContentChanged(null); + } + //leaving here in case it's needed public void updateResult(int syncId, World world, PlayerEntity player, CraftingInventory craftInv, CraftingResultInventory resultInv) { if (!world.isClient) { diff --git a/src/main/java/io/github/alloffabric/artis/inventory/ArtisCraftingInventory.java b/src/main/java/io/github/alloffabric/artis/inventory/ArtisCraftingInventory.java index 397ee8a..627e1b5 100644 --- a/src/main/java/io/github/alloffabric/artis/inventory/ArtisCraftingInventory.java +++ b/src/main/java/io/github/alloffabric/artis/inventory/ArtisCraftingInventory.java @@ -14,7 +14,7 @@ public class ArtisCraftingInventory extends CraftingInventory { private final DefaultedList stacks; private final ArtisCraftingController container; - private boolean checkMatrixChanges = true; + private boolean checkMatrixChanges = false; public ArtisCraftingInventory(ArtisCraftingController container, int width, int height) { super(container, width, height); @@ -98,4 +98,8 @@ public PlayerEntity getPlayer() { public void setCheckMatrixChanges(boolean b) { this.checkMatrixChanges = b; } + + public DefaultedList getStacks() { + return stacks; + } } diff --git a/src/main/java/io/github/alloffabric/artis/inventory/slot/ValidatedArtisResultSlot.java b/src/main/java/io/github/alloffabric/artis/inventory/slot/ValidatedArtisResultSlot.java index 86b8b32..6acdefd 100644 --- a/src/main/java/io/github/alloffabric/artis/inventory/slot/ValidatedArtisResultSlot.java +++ b/src/main/java/io/github/alloffabric/artis/inventory/slot/ValidatedArtisResultSlot.java @@ -6,6 +6,8 @@ import io.github.cottonmc.cotton.gui.ValidatedSlot; import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.CraftingInventory; +import net.minecraft.inventory.CraftingResultInventory; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket; @@ -27,6 +29,11 @@ public ValidatedArtisResultSlot(PlayerEntity player, ArtisCraftingInventory inve this.syncId = syncId; } + @Override + public void setStack(ItemStack stack) { + //super.setStack(stack); + } + @Override public boolean canInsert(ItemStack stack) { return false; @@ -37,7 +44,6 @@ public ItemStack takeStack(int amount) { if (this.hasStack()) { this.amount += Math.min(amount, this.getStack().getCount()); } - return super.takeStack(amount); } @@ -57,19 +63,13 @@ protected void onCrafted(ItemStack stack) { if (this.amount > 0) { stack.onCraft(this.player.world, this.player, this.amount); } - - if (this.inventory instanceof RecipeUnlocker) { - ((RecipeUnlocker) this.inventory).unlockLastRecipe(this.player); - } - this.amount = 0; } @Override public ItemStack onTakeItem(PlayerEntity player, ItemStack stack) { this.onCrafted(stack); - DefaultedList remainders = player.world.getRecipeManager().getRemainingStacks(craftingInv.getType(), this.craftingInv, player.world); - + DefaultedList remainders = getRemainders(); //= player.world.getRecipeManager().getRemainingStacks(craftingInv.getType(), this.craftingInv, player.world); for (int i = 0; i < remainders.size() - 1; ++i) { ItemStack input = this.craftingInv.getStack(i); ItemStack remainder = remainders.get(i); @@ -116,4 +116,13 @@ public ItemStack onTakeItem(PlayerEntity player, ItemStack stack) { return stack; } + + //note: inventory is actually CraftingResultInventory so it's a safe cast + public DefaultedList getRemainders() { + Recipe lastRecipe = (Recipe) ((CraftingResultInventory)this.inventory).getLastRecipe(); + if (lastRecipe != null && + lastRecipe.matches(craftingInv, player.world)) + return lastRecipe.getRemainingStacks(craftingInv); + else return craftingInv.getStacks(); + } } \ No newline at end of file