Skip to content

Commit

Permalink
fixed crafting table desyncing when shiftcrafting out of a just opene…
Browse files Browse the repository at this point in the history
…d table
  • Loading branch information
Tfarcenim committed Jul 24, 2020
1 parent b1d9cf5 commit 98b3ef1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
18 changes: 18 additions & 0 deletions src/main/java/io/github/alloffabric/artis/Artis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
});
});
}
}
}
1 change: 0 additions & 1 deletion src/main/java/io/github/alloffabric/artis/ArtisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public void onInitializeClient() {
}
});
});

}

public static void updateLastRecipe(ArtisCraftingController container, Recipe<CraftingInventory> rec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -195,6 +201,16 @@ public int getCraftingSlotCount() {
return getTableType().getWidth() * getTableType().getHeight();
}

// update crafting
//clientside only
@Override
public void updateSlotStacks(List<ItemStack> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class ArtisCraftingInventory extends CraftingInventory {
private final DefaultedList<ItemStack> 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);
Expand Down Expand Up @@ -98,4 +98,8 @@ public PlayerEntity getPlayer() {
public void setCheckMatrixChanges(boolean b) {
this.checkMatrixChanges = b;
}

public DefaultedList<ItemStack> getStacks() {
return stacks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -37,7 +44,6 @@ public ItemStack takeStack(int amount) {
if (this.hasStack()) {
this.amount += Math.min(amount, this.getStack().getCount());
}

return super.takeStack(amount);
}

Expand All @@ -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<ItemStack> remainders = player.world.getRecipeManager().getRemainingStacks(craftingInv.getType(), this.craftingInv, player.world);

DefaultedList<ItemStack> 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);
Expand Down Expand Up @@ -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<ItemStack> getRemainders() {
Recipe<CraftingInventory> lastRecipe = (Recipe<CraftingInventory>) ((CraftingResultInventory)this.inventory).getLastRecipe();
if (lastRecipe != null &&
lastRecipe.matches(craftingInv, player.world))
return lastRecipe.getRemainingStacks(craftingInv);
else return craftingInv.getStacks();
}
}

0 comments on commit 98b3ef1

Please sign in to comment.