Created
March 10, 2020 13:34
-
-
Save Lanse505/a5fec07d73df0ceec695506b4a980e9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.teamacronymcoders.essence.container; | |
| import com.hrznstudio.titanium.component.inventory.InventoryComponent; | |
| import com.hrznstudio.titanium.container.impl.BasicInventoryContainer; | |
| import com.teamacronymcoders.essence.items.misc.PortableCrafter; | |
| import net.minecraft.entity.player.PlayerEntity; | |
| import net.minecraft.entity.player.PlayerInventory; | |
| import net.minecraft.entity.player.ServerPlayerEntity; | |
| import net.minecraft.inventory.CraftResultInventory; | |
| import net.minecraft.inventory.CraftingInventory; | |
| import net.minecraft.inventory.IInventory; | |
| import net.minecraft.inventory.container.ContainerType; | |
| import net.minecraft.inventory.container.CraftingResultSlot; | |
| import net.minecraft.inventory.container.Slot; | |
| import net.minecraft.item.ItemStack; | |
| import net.minecraft.item.crafting.ICraftingRecipe; | |
| import net.minecraft.item.crafting.IRecipeType; | |
| import net.minecraft.network.PacketBuffer; | |
| import net.minecraft.network.play.server.SSetSlotPacket; | |
| import net.minecraft.util.Hand; | |
| import net.minecraft.util.IWorldPosCallable; | |
| import net.minecraft.world.World; | |
| import net.minecraftforge.items.wrapper.RecipeWrapper; | |
| import net.minecraftforge.registries.ObjectHolder; | |
| import java.util.Optional; | |
| public class PortableCrafterContainer extends BasicInventoryContainer { | |
| @ObjectHolder("essence:portable_crafter") | |
| public static ContainerType<PortableCrafterContainer> type; | |
| private PlayerInventory inventory; | |
| private PortableCrafter portableCrafter; | |
| private IWorldPosCallable callable; | |
| private CraftingInventory craftingInventory; | |
| private CraftResultInventory resultInventory; | |
| public PortableCrafterContainer(int id, PlayerInventory inventory, PacketBuffer buffer) { | |
| this((PortableCrafter) inventory.player.getHeldItem(Hand.valueOf(buffer.readString())).getItem(), inventory, id); | |
| } | |
| public PortableCrafterContainer(PortableCrafter portableCrafter, PlayerInventory inventory, int id) { | |
| super(type, inventory, id); | |
| this.inventory = inventory; | |
| this.portableCrafter = portableCrafter; | |
| this.callable = IWorldPosCallable.of(inventory.player.world, inventory.player.getPosition()); | |
| this.craftingInventory = new CraftingInventory(this, 3, 3); | |
| this.resultInventory = new CraftResultInventory(); | |
| initInventory(); | |
| } | |
| public static void onCraft(int windowId, World world, PlayerEntity crafter, CraftingInventory inventory, CraftResultInventory result) { | |
| if (!world.isRemote) { | |
| ServerPlayerEntity serverPlayer = (ServerPlayerEntity) crafter; | |
| ItemStack stack = ItemStack.EMPTY; | |
| Optional<ICraftingRecipe> optional = world.getServer().getRecipeManager().getRecipe(IRecipeType.CRAFTING, inventory, world); | |
| if (optional.isPresent()) { | |
| ICraftingRecipe recipe = optional.get(); | |
| if (result.canUseRecipe(world, serverPlayer, recipe)) { | |
| stack = recipe.getCraftingResult(inventory); | |
| } | |
| } | |
| result.setInventorySlotContents(0, stack); | |
| serverPlayer.connection.sendPacket(new SSetSlotPacket(windowId, 0, stack)); | |
| } | |
| } | |
| @Override | |
| public void onCraftMatrixChanged(IInventory inventoryIn) { | |
| callable.consume((world, pos) -> onCraft(this.windowId, world, getPlayerInventory().player, this.craftingInventory, this.resultInventory)); | |
| } | |
| public void addGridSlots() { | |
| InventoryComponent<?> grid = this.portableCrafter.getGrid(); | |
| for (int i = 0; i < grid.getSlots(); i++) | |
| addSlot(new Slot( | |
| this.craftingInventory, i, | |
| grid.getXPos() + grid.getSlotPosition().apply(i).getLeft(), | |
| grid.getYPos() + grid.getSlotPosition().apply(i).getRight()) | |
| ); | |
| } | |
| public void addOutputSlot() { | |
| InventoryComponent<?> output = this.portableCrafter.getOutput(); | |
| addSlot(new CraftingResultSlot(inventory.player, this.craftingInventory, this.resultInventory, 0, | |
| output.getXPos() + output.getSlotPosition().apply(0).getLeft(), | |
| output.getYPos() + output.getSlotPosition().apply(0).getRight())); | |
| } | |
| @Override | |
| public boolean canInteractWith(PlayerEntity playerIn) { | |
| return true; | |
| } | |
| @Override | |
| public void addExtraSlots() { | |
| super.addExtraSlots(); | |
| addGridSlots(); | |
| addOutputSlot(); | |
| } | |
| public PortableCrafter getPortableCrafter() { | |
| return portableCrafter; | |
| } | |
| @Override | |
| public void onContainerClosed(PlayerEntity playerIn) { | |
| IWorldPosCallable.of(playerIn.world, playerIn.getPosition()).consume((world, pos) -> this.clearContainer(playerIn, world, new RecipeWrapper(this.portableCrafter.getGrid()))); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment