Skip to content

Commit

Permalink
Merge pull request #621 from Hunter19823/1.18/#604_ClearCraftingMenu
Browse files Browse the repository at this point in the history
Added a CraftingGrid to PlayerJS
  • Loading branch information
LatvianModder authored Dec 26, 2023
2 parents 58a845f + 1ea3062 commit 6c27b26
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions common/src/main/java/dev/latvian/mods/kubejs/player/PlayerJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import dev.architectury.hooks.level.entity.PlayerHooks;
import dev.latvian.mods.kubejs.core.PlayerKJS;
import dev.latvian.mods.kubejs.entity.LivingEntityJS;
import dev.latvian.mods.kubejs.item.ContainerInventory;
import dev.latvian.mods.kubejs.item.InventoryJS;
import dev.latvian.mods.kubejs.item.ItemHandlerUtils;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.item.RangedWrapper;
import dev.latvian.mods.kubejs.stages.Stages;
import dev.latvian.mods.kubejs.util.AttachedData;
import dev.latvian.mods.kubejs.util.WithAttachedData;
Expand All @@ -28,6 +30,7 @@ public abstract class PlayerJS<P extends Player> extends LivingEntityJS implemen

private final PlayerDataJS playerData;
private InventoryJS inventory;
private InventoryJS craftingGrid;

public PlayerJS(PlayerDataJS data, P player) {
super(player);
Expand Down Expand Up @@ -76,8 +79,27 @@ public void markDirty() {
return inventory;
}

public InventoryJS getCraftingGrid() {
if (craftingGrid == null) {
// Wrap the crafting grid in a ranged wrapper and container inventory to prevent the clear
// method from causing an index out of bounds exception.
// This is a workaround for a bug in the vanilla crafting grid implementation.
// For any mods that increase the size of the crafting grid, this won't work.
craftingGrid = new InventoryJS(new RangedWrapper(new ContainerInventory(minecraftPlayer.inventoryMenu.getCraftSlots()), 0, 4)) {

@Override
public void markDirty() {
sendInventoryUpdate();
}
};
}

return craftingGrid;
}

public void sendInventoryUpdate() {
minecraftPlayer.getInventory().setChanged();
minecraftPlayer.inventoryMenu.getCraftSlots().setChanged();
minecraftPlayer.inventoryMenu.broadcastChanges();
}

Expand Down

0 comments on commit 6c27b26

Please sign in to comment.