Skip to content

Instantly share code, notes, and snippets.

@YanisBft
Created July 19, 2019 14:03
Show Gist options
  • Save YanisBft/f537ff6b5663a7d744282a3065aa7548 to your computer and use it in GitHub Desktop.
Save YanisBft/f537ff6b5663a7d744282a3065aa7548 to your computer and use it in GitHub Desktop.
package com.yanis48.minalchemy.container;
import java.util.Optional;
import com.yanis48.minalchemy.init.MinalchemyBlocks;
import com.yanis48.minalchemy.init.MinalchemyRecipes;
import com.yanis48.minalchemy.init.MinalchemyTags;
import com.yanis48.minalchemy.recipe.AlchemyRecipe;
import net.minecraft.block.Blocks;
import net.minecraft.container.BlockContext;
import net.minecraft.container.CraftingContainer;
import net.minecraft.container.Slot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.BasicInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeFinder;
import net.minecraft.recipe.RecipeInputProvider;
import net.minecraft.world.World;
public class AlchemyTableContainer extends CraftingContainer<Inventory> {
private final Inventory alchemyInv;
private final BlockContext context;
private final World world;
private final PlayerEntity player;
public AlchemyTableContainer(int syncId, PlayerEntity player) {
this(syncId, player.inventory, BlockContext.EMPTY);
}
public AlchemyTableContainer(int syncId, PlayerInventory playerInv, BlockContext blockContext) {
super(null, syncId);
this.alchemyInv = new BasicInventory(3) {
public void markDirty() {
super.markDirty();
AlchemyTableContainer.this.onContentChanged(this);
}
};
this.context = blockContext;
this.player = playerInv.player;
this.world = player.world;
// Input Slot 1
this.addSlot(new Slot(this.alchemyInv, 0, 49, 23) {
public boolean canInsert(ItemStack stack) {
return stack.getItem().isIn(MinalchemyTags.ELEMENTS);
}
});
// Input Slot 2
this.addSlot(new Slot(this.alchemyInv, 1, 49, 44) {
public boolean canInsert(ItemStack stack) {
return stack.getItem().isIn(MinalchemyTags.ELEMENTS);
}
});
// Output Slot
this.addSlot(new Slot(this.alchemyInv, 2, 129, 34) {
public boolean canInsert(ItemStack stack) {
return false;
}
});
// Player Inventory
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
// Player Hotbar
for(int i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerInv, i, 8 + i * 18, 142));
}
}
@Override
public boolean canUse(PlayerEntity player) {
return canUse(this.context, player, MinalchemyBlocks.ALCHEMY_TABLE);
}
@Override
public void onContentChanged(Inventory inv) {
world.setBlockState(this.player.getBlockPos(), Blocks.GOLD_BLOCK.getDefaultState());
/*this.context.run((world, pos) -> {
syncCraft(this.syncId, this.world, this.player, this.alchemyInv);
});*/
if (inv == this.alchemyInv) {
syncCraft(this.syncId, this.world, this.player, this.alchemyInv);
}
}
private void syncCraft(int syncId, World world, PlayerEntity player, Inventory alchemyInv) {
if (!world.isClient) {
ItemStack stack = ItemStack.EMPTY;
Optional<AlchemyRecipe> optional = world.getRecipeManager().getFirstMatch(MinalchemyRecipes.ALCHEMY, alchemyInv, world);
if (optional.isPresent()) {
AlchemyRecipe recipe = optional.get();
stack = recipe.craft(alchemyInv);
stack.setCount(1);
world.setBlockState(this.player.getBlockPos(), Blocks.EMERALD_BLOCK.getDefaultState());
} else {
world.setBlockState(this.player.getBlockPos(), Blocks.REDSTONE_BLOCK.getDefaultState());
}
/*if (stackInSlot1.getItem() == MinalchemyItems.WATER_ELEMENT && stackInSlot2.getItem() == MinalchemyItems.WATER_ELEMENT) {
this.resultInv.setInvStack(2, MinalchemyItems.PUDDLE_ELEMENT.getStackForRender());
}*/
}
}
@Override
public void close(PlayerEntity player) {
super.close(player);
player.inventory.insertStack(this.alchemyInv.getInvStack(0));
player.inventory.insertStack(this.alchemyInv.getInvStack(1));
/*this.context.run((world, pos) -> {
this.dropInventory(player, world, this.alchemyInv);
});*/
}
public ItemStack transferSlot(PlayerEntity player, int slotNum) {
ItemStack stackToTransfer = ItemStack.EMPTY;
Slot slot = this.slotList.get(slotNum);
if (slot != null && slot.hasStack()) {
ItemStack stackInSlot = slot.getStack();
stackToTransfer = stackInSlot.copy();
ItemStack stackInSlot1 = this.alchemyInv.getInvStack(0);
ItemStack stackInSlot2 = this.alchemyInv.getInvStack(1);
if (slotNum == 2) {
if (!this.insertItem(stackInSlot, 3, 39, true)) {
return ItemStack.EMPTY;
}
slot.onStackChanged(stackInSlot, stackToTransfer);
} else if (slotNum != 0 && slotNum != 1) {
if (!stackInSlot1.isEmpty() && !stackInSlot2.isEmpty()) {
if (slotNum >= 3 && slotNum < 30) {
if (!this.insertItem(stackInSlot, 30, 39, false)) {
return ItemStack.EMPTY;
}
} else if (slotNum >= 30 && slotNum < 39 && !this.insertItem(stackInSlot, 3, 30, false)) {
return ItemStack.EMPTY;
}
} else if (!this.insertItem(stackInSlot, 0, 2, false)) {
return ItemStack.EMPTY;
}
} else if (!this.insertItem(stackInSlot, 3, 39, false)) {
return ItemStack.EMPTY;
}
if (stackInSlot.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
} else {
slot.markDirty();
}
if (stackInSlot.getCount() == stackToTransfer.getCount()) {
return ItemStack.EMPTY;
}
slot.onTakeItem(player, stackInSlot);
}
return stackToTransfer;
}
@Override
public void populateRecipeFinder(RecipeFinder finder) {
if (this.alchemyInv instanceof RecipeInputProvider) {
((RecipeInputProvider)this.alchemyInv).provideRecipeInputs(finder);
}
}
@Override
public void clearCraftingSlots() {
this.alchemyInv.clear();
}
@Override
public boolean matches(Recipe<? super Inventory> recipe) {
return recipe.matches(this.alchemyInv, this.world);
}
@Override
public int getCraftingResultSlotIndex() {
return 2;
}
@Override
public int getCraftingWidth() {
return 1;
}
@Override
public int getCraftingHeight() {
return 1;
}
@Override
public int getCraftingSlotCount() {
return 3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment