Skip to content

Instantly share code, notes, and snippets.

@YanisBft
Created July 18, 2019 15:14
Show Gist options
  • Save YanisBft/df907119a0213538966a7f847e1b89a3 to your computer and use it in GitHub Desktop.
Save YanisBft/df907119a0213538966a7f847e1b89a3 to your computer and use it in GitHub Desktop.
Container code
package com.yanis48.minalchemy.container;
import net.minecraft.container.Container;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DefaultedList;
@SuppressWarnings("unused")
public class AlchemyInventory extends CraftingInventory {
public static final int INV_SIZE = 1;
private final DefaultedList<ItemStack> stacks;
public final PlayerEntity accessor;
public AlchemyInventory(Container container) {
this(container, null);
}
public AlchemyInventory(Container container, PlayerEntity player) {
super(container, INV_SIZE, 1);
this.accessor = player;
this.stacks = DefaultedList.ofSize(INV_SIZE, ItemStack.EMPTY);
}
}
package com.yanis48.minalchemy.container;
import com.yanis48.minalchemy.init.MinalchemyRecipes;
import net.minecraft.container.CraftingResultSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DefaultedList;
public class AlchemyResultSlot extends CraftingResultSlot {
private final CraftingInventory craftingInv;
private final PlayerEntity player;
public AlchemyResultSlot(PlayerEntity player, CraftingInventory craftingInv, Inventory inv, int invSlot, int x, int y) {
super(player, craftingInv, inv, invSlot, x, y);
this.craftingInv = craftingInv;
this.player = player;
}
@Override
public ItemStack onTakeItem(PlayerEntity player, ItemStack result) {
this.onCrafted(result);
DefaultedList<ItemStack> remainders = player.world.getRecipeManager().getRemainingStacks(MinalchemyRecipes.ALCHEMY, this.craftingInv, player.world);
for(int i = 0; i < remainders.size(); ++i) {
ItemStack ingredient = this.craftingInv.getInvStack(i);
ItemStack remainder = remainders.get(i);
if (!ingredient.isEmpty()) {
this.craftingInv.takeInvStack(i, 1);
ingredient = this.craftingInv.getInvStack(i);
}
if (!ingredient.getItem().hasRecipeRemainder()) continue;
if (!remainder.isEmpty()) {
if (ingredient.isEmpty()) {
this.craftingInv.setInvStack(i, remainder);
} else if (ItemStack.areItemsEqual(ingredient, remainder) && ItemStack.areTagsEqual(ingredient, remainder)) {
remainder.increment(ingredient.getCount());
this.craftingInv.setInvStack(i, remainder);
} else if (!this.player.inventory.insertStack(remainder)) {
this.player.dropItem(remainder, false);
}
}
}
return result;
}
}
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.client.network.packet.GuiSlotUpdateS2CPacket;
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.CraftingResultInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeFinder;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.World;
public class AlchemyTableContainer extends CraftingContainer<AlchemyInventory> {
private final AlchemyInventory alchemyInv;
private final CraftingResultInventory resultInv;
private final BlockContext context;
private final PlayerEntity player;
public AlchemyTableContainer(int syncId, PlayerEntity player) {
this(syncId, player.inventory, BlockContext.create(player.world, player.getBlockPos()));
}
public AlchemyTableContainer(int syncId, PlayerInventory playerInv, BlockContext blockContext) {
super(null, syncId);
this.alchemyInv = new AlchemyInventory(this, playerInv.player);
this.resultInv = new CraftingResultInventory();
this.context = blockContext;
this.player = playerInv.player;
// Output Slot
this.addSlot(new AlchemyResultSlot(player, this.alchemyInv, this.resultInv, 0, 129, 34));
// Input Slot 1
this.addSlot(new Slot(this.alchemyInv, 1, 49, 23) {
public boolean canInsert(ItemStack stack) {
return stack.getItem().isIn(MinalchemyTags.ELEMENTS);
}
});
// Input Slot 2
this.addSlot(new Slot(this.alchemyInv, 2, 49, 44) {
public boolean canInsert(ItemStack stack) {
return stack.getItem().isIn(MinalchemyTags.ELEMENTS);
}
});
// 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) {
this.context.run((world, pos) -> {
syncCraft(this.syncId, world, this.player, this.alchemyInv, this.resultInv);
});
}
private static void syncCraft(int syncId, World world, PlayerEntity player, Inventory craftingInv2, CraftingResultInventory resultInv) {
if (!world.isClient) {
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
ItemStack stack = ItemStack.EMPTY;
Optional<AlchemyRecipe> optional = world.getRecipeManager().getFirstMatch(MinalchemyRecipes.ALCHEMY, craftingInv2, world);
if (optional.isPresent()) {
AlchemyRecipe recipe = optional.get();
if (resultInv.shouldCraftRecipe(world, serverPlayer, recipe)) {
stack = recipe.craft(craftingInv2);
stack.setCount(1);
}
}
resultInv.setInvStack(0, stack);
serverPlayer.networkHandler.sendPacket(new GuiSlotUpdateS2CPacket(syncId, 0, stack));
}
}
@Override
public void close(PlayerEntity player) {
super.close(player);
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(1);
ItemStack stackInSlot2 = this.alchemyInv.getInvStack(2);
if (slotNum == 0) {
/*this.context.run((world, pos) -> {
stackInSlot.getItem().onCraft(stackInSlot, world, player);
});*/
if (!this.insertItem(stackInSlot, 3, 39, true)) {
return ItemStack.EMPTY;
}
slot.onStackChanged(stackInSlot, stackToTransfer);
} else if (slotNum != 1 && slotNum != 2) {
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);
}
/*else if (slotNum >= 3 && slotNum < 30) {
if (!this.insertItem(stackInSlot, 30, 39, false)) {
return ItemStack.EMPTY;
}
} else if (slotNum >= 30 && slotNum < 39) {
if (!this.insertItem(stackInSlot, 3, 30, 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;
}
ItemStack slotToTake = slot.onTakeItem(player, stackInSlot);
if (slotNum == 0) {
player.dropItem(slotToTake, false);
}
}*/
return stackToTransfer;
}
@Override
public void populateRecipeFinder(RecipeFinder finder) {
this.alchemyInv.provideRecipeInputs(finder);
}
@Override
public void clearCraftingSlots() {
this.alchemyInv.clear();
this.resultInv.clear();
}
@Override
public boolean matches(Recipe<? super AlchemyInventory> recipe) {
return recipe.matches(this.alchemyInv, this.player.world);
}
@Override
public int getCraftingResultSlotIndex() {
return 0;
}
@Override
public int getCraftingWidth() {
return this.alchemyInv.getWidth();
}
@Override
public int getCraftingHeight() {
return this.alchemyInv.getHeight();
}
@Override
public int getCraftingSlotCount() {
return 3;
}
}
package com.yanis48.minalchemy.client;
import com.mojang.blaze3d.platform.GlStateManager;
import com.yanis48.minalchemy.Minalchemy;
import com.yanis48.minalchemy.container.AlchemyTableContainer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
@Environment(EnvType.CLIENT)
public class AlchemyTableScreen extends AbstractContainerScreen<AlchemyTableContainer> {
private static final Identifier TEXTURE = new Identifier(Minalchemy.MOD_ID, "textures/gui/alchemy_table.png");
public AlchemyTableScreen(int syncId, PlayerEntity player) {
super(new AlchemyTableContainer(syncId, player), player.inventory, new TranslatableText("container.minalchemy.alchemy_table", new Object[0]));
}
@Override
protected void drawForeground(int int_1, int int_2) {
this.font.draw(this.title.asFormattedString(), 8.0F, 6.0F, 4210752);
this.font.draw(this.playerInventory.getDisplayName().asFormattedString(), 8.0F, (float)(this.containerHeight - 96 + 2), 4210752);
}
@Override
public void render(int mouseX, int mouseY, float partialTicks) {
this.renderBackground();
this.drawBackground(partialTicks, mouseX, mouseY);
super.render(mouseX, mouseY, partialTicks);
this.drawMouseoverTooltip(mouseX, mouseY);
}
@Override
protected void drawBackground(float float_1, int int_1, int int_2) {
GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
this.minecraft.getTextureManager().bindTexture(TEXTURE);
int int_3 = (this.width - this.containerWidth) / 2;
int int_4 = (this.height - this.containerHeight) / 2;
this.blit(int_3, int_4, 0, 0, this.containerWidth, this.containerHeight);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment