PrettyPipes/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalContainer.java

83 lines
3.3 KiB
Java
Raw Normal View History

2020-05-08 22:58:16 +02:00
package de.ellpeck.prettypipes.terminal.containers;
import de.ellpeck.prettypipes.Utility;
2021-12-02 14:44:26 +01:00
import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
2021-12-02 16:55:04 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.*;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.item.ItemStack;
2021-12-02 16:55:04 +01:00
import net.minecraft.world.item.crafting.RecipeType;
import org.apache.commons.lang3.tuple.Pair;
2020-05-08 22:58:16 +02:00
import javax.annotation.Nullable;
public class CraftingTerminalContainer extends ItemTerminalContainer {
2021-12-02 16:55:04 +01:00
public CraftingContainer craftInventory;
public ResultContainer craftResult;
private final Player player;
2021-12-02 16:55:04 +01:00
public CraftingTerminalContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos) {
2020-05-08 22:58:16 +02:00
super(type, id, player, pos);
this.player = player;
2021-12-02 16:55:04 +01:00
this.slotsChanged(this.craftInventory);
}
@Override
protected void addOwnSlots(Player player) {
this.craftInventory = new WrappedCraftingInventory(this.getTile().craftItems, this, 3, 3);
this.craftResult = new ResultContainer() {
@Override
public void setChanged() {
for (var player : CraftingTerminalContainer.this.getTile().getLookingPlayers())
player.containerMenu.slotsChanged(this);
}
};
this.addSlot(new ResultSlot(player, this.craftInventory, this.craftResult, 0, 25, 77));
2021-12-02 17:46:56 +01:00
for (var i = 0; i < 3; i++)
for (var j = 0; j < 3; j++)
this.addSlot(new Slot(this.craftInventory, j + i * 3, 7 + j * 18, 18 + i * 18));
super.addOwnSlots(player);
}
@Override
2021-12-02 16:55:04 +01:00
public void slotsChanged(Container inventoryIn) {
super.slotsChanged(inventoryIn);
2023-07-07 19:54:52 +02:00
if (!this.player.level().isClientSide) {
2021-12-02 17:46:56 +01:00
var ret = ItemStack.EMPTY;
2023-07-07 19:54:52 +02:00
var optional = this.player.level().getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, this.craftInventory, this.player.level());
if (optional.isPresent())
2023-07-07 19:54:52 +02:00
ret = optional.get().assemble(this.craftInventory, this.player.level().registryAccess());
2021-12-02 16:55:04 +01:00
this.craftResult.setItem(0, ret);
((ServerPlayer) this.player).connection.send(new ClientboundContainerSetSlotPacket(this.containerId, 0, 0, ret));
}
}
@Override
2021-12-02 16:55:04 +01:00
public ItemStack quickMoveStack(Player player, int slotIndex) {
return Utility.transferStackInSlot(this, this::moveItemStackTo, player, slotIndex, stack -> Pair.of(6 + 10, 12 + 10));
2020-05-08 22:58:16 +02:00
}
@Override
protected int getSlotXOffset() {
return 65;
}
2020-05-09 14:56:58 +02:00
@Override
2021-12-02 16:55:04 +01:00
public void clicked(int slotId, int dragType, ClickType clickTypeIn, Player player) {
if (slotId > 0 && clickTypeIn == ClickType.PICKUP) {
2021-12-02 17:46:56 +01:00
var slot = this.slots.get(slotId);
2021-12-02 16:55:04 +01:00
if (slot.container == this.craftInventory && !slot.hasItem())
2020-05-09 14:56:58 +02:00
this.getTile().ghostItems.setStackInSlot(slot.getSlotIndex(), ItemStack.EMPTY);
}
2021-12-02 16:55:04 +01:00
super.clicked(slotId, dragType, clickTypeIn, player);
2020-05-09 14:56:58 +02:00
}
2021-12-02 14:44:26 +01:00
public CraftingTerminalBlockEntity getTile() {
return (CraftingTerminalBlockEntity) this.tile;
}
2020-05-08 22:58:16 +02:00
}