Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible fix for odd behaviour #21

Open
wants to merge 3 commits into
base: 1.16.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package carpet_autocraftingtable;

import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.screen.CraftingScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeFinder;
import net.minecraft.recipe.RecipeUnlocker;
import net.minecraft.screen.CraftingScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;

public class AutoCraftingTableContainer extends CraftingScreenHandler {
private final CraftingTableBlockEntity blockEntity;
private final PlayerEntity player;
private CraftingInventory crafting_inv;

AutoCraftingTableContainer(int id, PlayerInventory playerInventory, CraftingTableBlockEntity blockEntity) {
super(id, playerInventory);
this.blockEntity = blockEntity;
this.player = playerInventory.player;

this.crafting_inv = blockEntity.boundCraftingInventory(this);

slots.clear();
this.addSlot(new OutputSlot(this.blockEntity, this.player));

Expand All @@ -43,6 +51,7 @@ public class AutoCraftingTableContainer extends CraftingScreenHandler {
public void onContentChanged(Inventory inv) {
if (this.player instanceof ServerPlayerEntity) {
ServerPlayNetworkHandler netHandler = ((ServerPlayerEntity) this.player).networkHandler;
netHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(this.syncId, 0, this.blockEntity.getStack(1)));
netHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(this.syncId, 0, this.blockEntity.getStack(0)));
}
}
Expand Down Expand Up @@ -72,6 +81,7 @@ public ItemStack transferSlot(PlayerEntity player, int slot) {
}

public void close(PlayerEntity player) {
this.crafting_inv = blockEntity.unbindCraftingInventory();
PlayerInventory playerInventory = player.inventory;
if (!playerInventory.getCursorStack().isEmpty()) {
player.dropItem(playerInventory.getCursorStack(), false);
Expand Down Expand Up @@ -119,8 +129,33 @@ public ItemStack onTakeItem(PlayerEntity player, ItemStack stack)
}
}

@Override
public boolean canUse(PlayerEntity player) {
return this.blockEntity.canPlayerUse(player);
}
@Override
public void populateRecipeFinder(RecipeFinder finder) {
this.crafting_inv.provideRecipeInputs(finder);
}

@Override
public void clearCraftingSlots() {
this.crafting_inv.clear();
}

@Override
public boolean matches(Recipe<? super CraftingInventory> recipe) {
return recipe.matches(this.crafting_inv, this.player.world);
}

@Override
public int getCraftingWidth() {
return this.crafting_inv.getWidth();
}

@Override
public int getCraftingHeight() {
return this.crafting_inv.getHeight();
}

@Override
public boolean canUse(PlayerEntity player) {
return this.blockEntity.canPlayerUse(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ public class CraftingTableBlockEntity extends LockableContainerBlockEntity imple
public ItemStack output = ItemStack.EMPTY;
private Recipe<?> lastRecipe;
private List<AutoCraftingTableContainer> openContainers = new ArrayList<>();
private CraftingInventory craftingInventory = new CraftingInventory(null, 3, 3);

public CraftingTableBlockEntity() { //this(BlockEntityType.BARREL);
this(TYPE);
}

private CraftingInventory craftingInventory = new CraftingInventory(null, 3, 3);

private CraftingTableBlockEntity(BlockEntityType<?> type) {
super(type);
this.inventory = DefaultedList.ofSize(9, ItemStack.EMPTY);
Expand All @@ -58,6 +57,22 @@ private CraftingTableBlockEntity(BlockEntityType<?> type) {

public static void init() { } // registers BE type

public CraftingInventory boundCraftingInventory(ScreenHandler handler) {
/**
* Binds screen handler to crafting infterface and returns
* the crafting interface
*/
((CraftingInventoryMixin) craftingInventory).setHandler(handler);
return craftingInventory;
}

public void unbindCraftingInventory() {
/**
* Unbinds the crafting inventory from curret screen handler
*/
((CraftingInventoryMixin) craftingInventory).setHandler(null);
}

@Override
public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.screen.ScreenHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.jetbrains.annotations.Nullable;

@Mixin(CraftingInventory.class)
public interface CraftingInventoryMixin
{
@Accessor("stacks")
void setInventory(DefaultedList<ItemStack> inventory);

@Accessor("handler")
void setHandler(@Nullable ScreenHandler handler);
}