Move the worktable into a separate module

This commit is contained in:
Nedelosk committed 2018-01-07 02:18:11 +01:00
1 parent a058c3f8c9
commit 42865951a9
44 files changed
+915 -646

No files matched your search

@@ -0,0 +1,58 @@
package forestry.worktable;
import com.google.common.base.Preconditions;
import javax.annotation.Nullable;
import net.minecraft.init.Items;
import forestry.api.modules.ForestryModule;
import forestry.core.config.Constants;
import forestry.core.network.IPacketRegistry;
import forestry.core.recipes.RecipeUtil;
import forestry.modules.BlankForestryModule;
import forestry.modules.ForestryModuleUids;
import forestry.worktable.blocks.BlockRegistryWorktable;
import forestry.worktable.network.PacketRegistryWorktable;
@ForestryModule(containerID = Constants.MOD_ID, moduleID = ForestryModuleUids.WORKTABLE, name = "Worktable", author = "Nedelosk", url = Constants.URL, unlocalizedDescription = "for.module.worktable.description")
public class ModuleWorktable extends BlankForestryModule {
@Nullable
private static BlockRegistryWorktable blocks;
public static BlockRegistryWorktable getBlocks() {
Preconditions.checkState(blocks != null);
return blocks;
}
@Override
public void registerItemsAndBlocks() {
blocks = new BlockRegistryWorktable();
}
@Override
public IPacketRegistry getPacketRegistry() {
return new PacketRegistryWorktable();
}
@Override
public void doInit() {
BlockRegistryWorktable blocks = getBlocks();
blocks.worktable.init();
}
@Override
public void registerRecipes() {
BlockRegistryWorktable blocks = getBlocks();
RecipeUtil.addRecipe("worktable", blocks.worktable,
"B",
"W",
"C",
'B', Items.BOOK,
'W', "craftingTableWood",
'C', "chestWood");
}
}
@@ -0,0 +1,13 @@
package forestry.worktable.blocks;
import forestry.core.blocks.BlockRegistry;
import forestry.core.items.ItemBlockNBT;
public class BlockRegistryWorktable extends BlockRegistry {
public final BlockWorktable worktable;
public BlockRegistryWorktable() {
worktable = new BlockWorktable(BlockTypeWorktable.WORKTABLE);
registerBlock(worktable, new ItemBlockNBT(worktable), "worktable");
}
}
@@ -0,0 +1,29 @@
package forestry.worktable.blocks;
import forestry.core.blocks.IBlockType;
import forestry.core.blocks.IMachineProperties;
import forestry.core.blocks.MachineProperties;
import forestry.core.tiles.TileForestry;
import forestry.worktable.tiles.TileWorktable;
public enum BlockTypeWorktable implements IBlockType {
WORKTABLE(TileWorktable.class, "worktable");
public static final BlockTypeWorktable[] VALUES = values();
private final IMachineProperties machineProperties;
<T extends TileForestry> BlockTypeWorktable(Class<T> teClass, String name) {
this.machineProperties = new MachineProperties<>(teClass, name);
}
@Override
public IMachineProperties getMachineProperties() {
return machineProperties;
}
@Override
public String getName() {
return getMachineProperties().getName();
}
}
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.blocks;
import forestry.core.blocks.BlockBase;
public class BlockWorktable extends BlockBase<BlockTypeWorktable> {
public BlockWorktable(BlockTypeWorktable worktable) {
super(worktable);
setHarvestLevel("pickaxe", 0);
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.blocks;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,42 @@
package forestry.worktable.compat;
import com.google.common.base.Preconditions;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.core.ForestryAPI;
import forestry.core.config.Constants;
import forestry.core.utils.JeiUtil;
import forestry.modules.ForestryModuleUids;
import forestry.worktable.ModuleWorktable;
import forestry.worktable.blocks.BlockRegistryWorktable;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.JEIPlugin;
import mezz.jei.api.recipe.VanillaRecipeCategoryUid;
import mezz.jei.api.recipe.transfer.IRecipeTransferRegistry;
@JEIPlugin
@SideOnly(Side.CLIENT)
public class WorktableJeiPlugin implements IModPlugin {
@Override
public void register(IModRegistry registry) {
if(!ForestryAPI.enabledModules.contains(new ResourceLocation(Constants.MOD_ID, ForestryModuleUids.WORKTABLE))){
return;
}
BlockRegistryWorktable blocks = ModuleWorktable.getBlocks();
Preconditions.checkNotNull(blocks);
registry.addRecipeCatalyst(new ItemStack(blocks.worktable), VanillaRecipeCategoryUid.CRAFTING);
IRecipeTransferRegistry transferRegistry = registry.getRecipeTransferRegistry();
transferRegistry.addRecipeTransferHandler(new WorktableRecipeTransferHandler(), VanillaRecipeCategoryUid.CRAFTING);
JeiUtil.addDescription(registry, blocks.worktable);
}
}
@@ -0,0 +1,58 @@
package forestry.worktable.compat;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.core.recipes.RecipeUtil;
import forestry.worktable.gui.ContainerWorktable;
import forestry.worktable.inventory.InventoryCraftingForestry;
import forestry.worktable.recipes.MemorizedRecipe;
import mezz.jei.api.gui.IGuiIngredient;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.recipe.transfer.IRecipeTransferError;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandler;
@SideOnly(Side.CLIENT)
class WorktableRecipeTransferHandler implements IRecipeTransferHandler<ContainerWorktable> {
@Override
public Class<ContainerWorktable> getContainerClass() {
return ContainerWorktable.class;
}
@Nullable
@Override
public IRecipeTransferError transferRecipe(ContainerWorktable container, IRecipeLayout recipeLayout, EntityPlayer player, boolean maxTransfer, boolean doTransfer) {
if (doTransfer) {
Map<Integer, ? extends IGuiIngredient<ItemStack>> guiIngredients = recipeLayout.getItemStacks().getGuiIngredients();
InventoryCraftingForestry inventory = new InventoryCraftingForestry(container);
for (Map.Entry<Integer, ? extends IGuiIngredient<ItemStack>> entry : guiIngredients.entrySet()) {
int recipeSlot = entry.getKey();
List<ItemStack> allIngredients = entry.getValue().getAllIngredients();
if (!allIngredients.isEmpty()) {
if (recipeSlot != 0) { // skip the output slot
ItemStack firstIngredient = allIngredients.get(0);
inventory.setInventorySlotContents(recipeSlot - 1, firstIngredient);
}
}
}
List<IRecipe> matchingRecipes = RecipeUtil.findMatchingRecipes(inventory, player.world);
if (!matchingRecipes.isEmpty()) {
MemorizedRecipe recipe = new MemorizedRecipe(inventory, matchingRecipes);
container.sendWorktableRecipeRequest(recipe);
}
}
return null;
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.compat;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,167 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.gui;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.core.gui.ContainerTile;
import forestry.core.gui.IContainerCrafting;
import forestry.core.gui.IGuiSelectable;
import forestry.core.gui.slots.SlotCraftMatrix;
import forestry.core.gui.slots.SlotCrafter;
import forestry.core.inventory.InventoryGhostCrafting;
import forestry.core.network.packets.PacketGuiSelectRequest;
import forestry.core.utils.ItemStackUtil;
import forestry.core.utils.NetworkUtil;
import forestry.worktable.inventory.InventoryCraftingForestry;
import forestry.worktable.inventory.InventoryWorktable;
import forestry.worktable.network.packets.PacketWorktableMemoryUpdate;
import forestry.worktable.network.packets.PacketWorktableRecipeRequest;
import forestry.worktable.network.packets.PacketWorktableRecipeUpdate;
import forestry.worktable.recipes.MemorizedRecipe;
import forestry.worktable.recipes.RecipeMemory;
import forestry.worktable.tiles.TileWorktable;
public class ContainerWorktable extends ContainerTile<TileWorktable> implements IContainerCrafting, IGuiSelectable {
private final InventoryCraftingForestry craftMatrix = new InventoryCraftingForestry(this);
private long lastMemoryUpdate;
private boolean craftMatrixChanged = false;
public ContainerWorktable(EntityPlayer player, TileWorktable tile) {
super(tile, player.inventory, 8, 136);
IInventory craftingDisplay = tile.getCraftingDisplay();
IInventory internalInventory = tile.getInternalInventory();
// Internal inventory
for (int i = 0; i < 2; i++) {
for (int k = 0; k < 9; k++) {
addSlotToContainer(new Slot(internalInventory, InventoryWorktable.SLOT_INVENTORY_1 + k + i * 9, 8 + k * 18, 90 + i * 18));
}
}
// Crafting matrix
for (int l = 0; l < 3; l++) {
for (int k1 = 0; k1 < 3; k1++) {
addSlotToContainer(new SlotCraftMatrix(this, craftingDisplay, k1 + l * 3, 11 + k1 * 18, 20 + l * 18));
}
}
// CraftResult display
addSlotToContainer(new SlotCrafter(player, craftMatrix, craftingDisplay, tile, InventoryGhostCrafting.SLOT_CRAFTING_RESULT, 77, 38));
for (int i = 0; i < craftMatrix.getSizeInventory(); i++) {
onCraftMatrixChanged(tile.getCraftingDisplay(), i);
}
}
@Override
public void detectAndSendChanges() {
if (craftMatrixChanged) {
craftMatrixChanged = false;
tile.setCurrentRecipe(craftMatrix);
sendPacketToListeners(new PacketWorktableRecipeUpdate(tile));
}
super.detectAndSendChanges();
if (lastMemoryUpdate != tile.getMemory().getLastUpdate()) {
lastMemoryUpdate = tile.getMemory().getLastUpdate();
sendPacketToListeners(new PacketWorktableMemoryUpdate(tile));
}
}
public void updateCraftMatrix() {
IInventory crafting = tile.getCraftingDisplay();
for (int i = 0; i < crafting.getSizeInventory(); i++) {
onCraftMatrixChanged(crafting, i);
}
}
// Fired when SlotCraftMatrix detects a change.
// Direct changes to the underlying inventory are not detected, only slot changes.
@Override
public void onCraftMatrixChanged(IInventory iinventory, int slot) {
if (slot >= craftMatrix.getSizeInventory()) {
return;
}
ItemStack stack = iinventory.getStackInSlot(slot);
ItemStack currentStack = craftMatrix.getStackInSlot(slot);
if (!ItemStackUtil.isIdenticalItem(stack, currentStack)) {
craftMatrix.setInventorySlotContents(slot, stack);
}
}
// Fired when this container's craftMatrix detects a change
@Override
public void onCraftMatrixChanged(IInventory iinventory) {
craftMatrixChanged = true;
}
/* Gui Selection Handling */
@SideOnly(Side.CLIENT)
public static void clearRecipe() {
sendRecipeClick(-1, 0);
}
@SideOnly(Side.CLIENT)
public static void sendRecipeClick(int mouseButton, int recipeIndex) {
NetworkUtil.sendToServer(new PacketGuiSelectRequest(mouseButton, recipeIndex));
}
@Override
public void handleSelectionRequest(EntityPlayerMP player, int primary, int secondary) {
switch (primary) {
case -1: { // clicked clear button
tile.clearCraftMatrix();
updateCraftMatrix();
sendPacketToListeners(new PacketWorktableRecipeUpdate(tile));
break;
}
case 0: { // clicked a memorized recipe
tile.chooseRecipeMemory(secondary);
updateCraftMatrix();
sendPacketToListeners(new PacketWorktableRecipeUpdate(tile));
break;
}
case 1: { // right clicked a memorized recipe
long time = player.world.getTotalWorldTime();
RecipeMemory memory = tile.getMemory();
memory.toggleLock(time, secondary);
break;
}
case 100: { // clicked previous recipe conflict button
tile.choosePreviousConflictRecipe();
sendPacketToListeners(new PacketWorktableRecipeUpdate(tile));
break;
}
case 101: { // clicked next recipe conflict button
tile.chooseNextConflictRecipe();
sendPacketToListeners(new PacketWorktableRecipeUpdate(tile));
break;
}
}
}
@SideOnly(Side.CLIENT)
public void sendWorktableRecipeRequest(MemorizedRecipe recipe) {
NetworkUtil.sendToServer(new PacketWorktableRecipeRequest(tile, recipe));
}
}
@@ -0,0 +1,86 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.EntityPlayer;
import forestry.core.config.Constants;
import forestry.core.gui.GuiForestryTitled;
import forestry.core.gui.buttons.GuiBetterButton;
import forestry.core.gui.buttons.StandardButtonTextureSets;
import forestry.core.network.packets.PacketGuiSelectRequest;
import forestry.core.utils.NetworkUtil;
import forestry.core.utils.SoundUtil;
import forestry.worktable.gui.widgets.ClearWorktable;
import forestry.worktable.gui.widgets.MemorizedRecipeSlot;
import forestry.worktable.recipes.RecipeMemory;
import forestry.worktable.tiles.TileWorktable;
public class GuiWorktable extends GuiForestryTitled<ContainerWorktable> {
private static final int SPACING = 18;
private final TileWorktable tile;
private boolean hasRecipeConflict = false;
public GuiWorktable(EntityPlayer player, TileWorktable tile) {
super(Constants.TEXTURE_PATH_GUI + "/worktable2.png", new ContainerWorktable(player, tile), tile);
this.tile = tile;
this.ySize = 218;
RecipeMemory recipeMemory = tile.getMemory();
int slot = 0;
for (int y = 0; y < 3; y++) {
int yPos = 20 + y * SPACING;
for (int x = 0; x < 3; x++) {
int xPos = 110 + x * SPACING;
MemorizedRecipeSlot memorizedRecipeSlot = new MemorizedRecipeSlot(widgetManager, xPos, yPos, recipeMemory, slot++);
widgetManager.add(memorizedRecipeSlot);
}
}
widgetManager.add(new ClearWorktable(widgetManager, 66, 19));
}
@Override
public void updateScreen() {
super.updateScreen();
if (hasRecipeConflict != tile.hasRecipeConflict()) {
hasRecipeConflict = tile.hasRecipeConflict();
if (hasRecipeConflict) {
addButtons();
} else {
buttonList.clear();
}
}
}
private void addButtons() {
buttonList.add(new GuiBetterButton(0, guiLeft + 76, guiTop + 56, StandardButtonTextureSets.LEFT_BUTTON_SMALL));
buttonList.add(new GuiBetterButton(1, guiLeft + 85, guiTop + 56, StandardButtonTextureSets.RIGHT_BUTTON_SMALL));
}
@Override
protected void actionPerformed(GuiButton button) {
int id = 100 + button.id;
NetworkUtil.sendToServer(new PacketGuiSelectRequest(id, 0));
SoundUtil.playButtonClick();
}
@Override
protected void addLedgers() {
addErrorLedger(tile);
addHintLedger("worktable");
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.gui;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.gui.widgets;
import forestry.core.gui.widgets.Widget;
import forestry.core.gui.widgets.WidgetManager;
import forestry.core.utils.SoundUtil;
import forestry.worktable.gui.ContainerWorktable;
public class ClearWorktable extends Widget {
public ClearWorktable(WidgetManager manager, int xPos, int yPos) {
super(manager, xPos, yPos);
width = 7;
height = 7;
}
@Override
public void draw(int startX, int startY) {
}
@Override
public void handleMouseClick(int mouseX, int mouseY, int mouseButton) {
ContainerWorktable.clearRecipe();
SoundUtil.playButtonClick();
}
}
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.gui.widgets;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.item.ItemStack;
import forestry.core.gui.widgets.ItemStackWidgetBase;
import forestry.core.gui.widgets.WidgetManager;
import forestry.core.render.TextureManagerForestry;
import forestry.core.utils.SoundUtil;
import forestry.worktable.gui.ContainerWorktable;
import forestry.worktable.recipes.RecipeMemory;
public class MemorizedRecipeSlot extends ItemStackWidgetBase {
private static final TextureAtlasSprite lockIcon = TextureManagerForestry.getInstance().getDefault("slots/locked");
private final RecipeMemory recipeMemory;
private final int slotNumber;
public MemorizedRecipeSlot(WidgetManager manager, int xPos, int yPos, RecipeMemory recipeMemory, int slot) {
super(manager, xPos, yPos);
this.recipeMemory = recipeMemory;
this.slotNumber = slot;
}
@Override
public ItemStack getItemStack() {
return recipeMemory.getRecipeDisplayOutput(slotNumber);
}
@Override
public void draw(int startX, int startY) {
super.draw(startX, startY);
GlStateManager.disableDepth();
if (recipeMemory.isLocked(slotNumber)) {
TextureManagerForestry.getInstance().bindGuiTextureMap();
manager.gui.drawTexturedModalRect(startX + xPos, startY + yPos, lockIcon, 16, 16);
}
GlStateManager.enableDepth();
}
@Override
public void handleMouseClick(int mouseX, int mouseY, int mouseButton) {
if (!getItemStack().isEmpty()) {
ContainerWorktable.sendRecipeClick(mouseButton, slotNumber);
SoundUtil.playButtonClick();
}
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.gui.widgets;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.inventory;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import forestry.core.gui.ContainerDummy;
import forestry.worktable.gui.ContainerWorktable;
public class InventoryCraftingForestry extends InventoryCrafting {
private final Container eventHandlerIn;
public InventoryCraftingForestry(ContainerWorktable containerWorktable) {
this(containerWorktable, 3, 3);
}
public InventoryCraftingForestry() {
this(ContainerDummy.instance, 3, 3);
}
private InventoryCraftingForestry(Container eventHandlerIn, int width, int height) {
super(eventHandlerIn, width, height);
this.eventHandlerIn = eventHandlerIn;
}
public InventoryCraftingForestry copy() {
InventoryCraftingForestry copy = new InventoryCraftingForestry(this.eventHandlerIn, getWidth(), getHeight());
for (int slot = 0; slot < getSizeInventory(); slot++) {
copy.setInventorySlotContents(slot, getStackInSlot(slot).copy());
}
return copy;
}
}
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.inventory;
import net.minecraft.item.ItemStack;
import forestry.core.inventory.InventoryAdapterTile;
import forestry.core.utils.SlotUtil;
import forestry.worktable.tiles.TileWorktable;
public class InventoryWorktable extends InventoryAdapterTile<TileWorktable> {
public final static short SLOT_INVENTORY_1 = 0;
public final static short SLOT_INVENTORY_COUNT = 18;
public InventoryWorktable(TileWorktable worktable) {
super(worktable, 18, "Items");
}
@Override
public boolean canSlotAccept(int slotIndex, ItemStack itemStack) {
return SlotUtil.isSlotInRange(slotIndex, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT);
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.inventory;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.network;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.core.network.IPacketRegistry;
import forestry.core.network.PacketIdClient;
import forestry.core.network.PacketIdServer;
import forestry.worktable.network.packets.PacketWorktableMemoryUpdate;
import forestry.worktable.network.packets.PacketWorktableRecipeRequest;
import forestry.worktable.network.packets.PacketWorktableRecipeUpdate;
public class PacketRegistryWorktable implements IPacketRegistry {
@Override
public void registerPacketsServer() {
PacketIdServer.WORKTABLE_RECIPE_REQUEST.setPacketHandler(new PacketWorktableRecipeRequest.Handler());
}
@Override
@SideOnly(Side.CLIENT)
public void registerPacketsClient() {
PacketIdClient.WORKTABLE_MEMORY_UPDATE.setPacketHandler(new PacketWorktableMemoryUpdate.Handler());
PacketIdClient.WORKTABLE_CRAFTING_UPDATE.setPacketHandler(new PacketWorktableRecipeUpdate.Handler());
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.network;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.network.packets;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.core.network.ForestryPacket;
import forestry.core.network.IForestryPacketClient;
import forestry.core.network.IForestryPacketHandlerClient;
import forestry.core.network.PacketBufferForestry;
import forestry.core.network.PacketIdClient;
import forestry.core.tiles.TileUtil;
import forestry.worktable.recipes.RecipeMemory;
import forestry.worktable.tiles.TileWorktable;
public class PacketWorktableMemoryUpdate extends ForestryPacket implements IForestryPacketClient {
private final BlockPos pos;
private final RecipeMemory recipeMemory;
public PacketWorktableMemoryUpdate(TileWorktable worktable) {
this.pos = worktable.getPos();
this.recipeMemory = worktable.getMemory();
}
@Override
public PacketIdClient getPacketId() {
return PacketIdClient.WORKTABLE_MEMORY_UPDATE;
}
@Override
protected void writeData(PacketBufferForestry data) throws IOException {
data.writeBlockPos(pos);
recipeMemory.writeData(data);
}
@SideOnly(Side.CLIENT)
public static class Handler implements IForestryPacketHandlerClient {
@Override
public void onPacketData(PacketBufferForestry data, EntityPlayer player) throws IOException {
BlockPos pos = data.readBlockPos();
TileWorktable tile = TileUtil.getTile(player.world, pos, TileWorktable.class);
if (tile != null) {
tile.getMemory().readData(data);
}
}
}
}
@@ -0,0 +1,71 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.network.packets;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.math.BlockPos;
import forestry.core.network.ForestryPacket;
import forestry.core.network.IForestryPacketHandlerServer;
import forestry.core.network.IForestryPacketServer;
import forestry.core.network.PacketBufferForestry;
import forestry.core.network.PacketIdServer;
import forestry.core.tiles.TileUtil;
import forestry.core.utils.NetworkUtil;
import forestry.worktable.gui.ContainerWorktable;
import forestry.worktable.recipes.MemorizedRecipe;
import forestry.worktable.tiles.TileWorktable;
/**
* Used to sync the worktable crafting grid from Client to Server.
*/
public class PacketWorktableRecipeRequest extends ForestryPacket implements IForestryPacketServer {
private final BlockPos pos;
private final MemorizedRecipe recipe;
public PacketWorktableRecipeRequest(TileWorktable worktable, MemorizedRecipe recipe) {
this.pos = worktable.getPos();
this.recipe = recipe;
}
@Override
public PacketIdServer getPacketId() {
return PacketIdServer.WORKTABLE_RECIPE_REQUEST;
}
@Override
protected void writeData(PacketBufferForestry data) throws IOException {
data.writeBlockPos(pos);
recipe.writeData(data);
}
public static class Handler implements IForestryPacketHandlerServer {
@Override
public void onPacketData(PacketBufferForestry data, EntityPlayerMP player) throws IOException {
BlockPos pos = data.readBlockPos();
MemorizedRecipe recipe = new MemorizedRecipe(data);
TileUtil.actOnTile(player.world, pos, TileWorktable.class, worktable -> {
worktable.setCurrentRecipe(recipe);
if (player.openContainer instanceof ContainerWorktable) {
ContainerWorktable containerWorktable = (ContainerWorktable) player.openContainer;
containerWorktable.updateCraftMatrix();
}
NetworkUtil.sendNetworkPacket(new PacketWorktableRecipeUpdate(worktable), pos, player.world);
});
}
}
}
@@ -0,0 +1,65 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.network.packets;
import javax.annotation.Nullable;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.core.network.ForestryPacket;
import forestry.core.network.IForestryPacketClient;
import forestry.core.network.IForestryPacketHandlerClient;
import forestry.core.network.PacketBufferForestry;
import forestry.core.network.PacketIdClient;
import forestry.core.tiles.TileUtil;
import forestry.worktable.recipes.MemorizedRecipe;
import forestry.worktable.tiles.TileWorktable;
/**
* Used to sync the worktable crafting result from Server to Client.
*/
public class PacketWorktableRecipeUpdate extends ForestryPacket implements IForestryPacketClient {
private final BlockPos pos;
@Nullable
private final MemorizedRecipe recipe;
public PacketWorktableRecipeUpdate(TileWorktable worktable) {
this.pos = worktable.getPos();
this.recipe = worktable.getCurrentRecipe();
}
@Override
public PacketIdClient getPacketId() {
return PacketIdClient.WORKTABLE_CRAFTING_UPDATE;
}
@Override
protected void writeData(PacketBufferForestry data) {
data.writeBlockPos(pos);
data.writeStreamable(recipe);
}
@SideOnly(Side.CLIENT)
public static class Handler implements IForestryPacketHandlerClient {
@Override
public void onPacketData(PacketBufferForestry data, EntityPlayer player) throws IOException {
BlockPos pos = data.readBlockPos();
MemorizedRecipe recipe = data.readStreamable(MemorizedRecipe::new);
TileUtil.actOnTile(player.world, pos, TileWorktable.class, tile -> tile.setCurrentRecipe(recipe));
}
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.network.packets;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,219 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.recipes;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import forestry.api.core.INbtReadable;
import forestry.api.core.INbtWritable;
import forestry.core.network.IStreamable;
import forestry.core.network.PacketBufferForestry;
import forestry.core.utils.InventoryUtil;
import forestry.core.utils.NBTUtilForestry;
import forestry.worktable.inventory.InventoryCraftingForestry;
public final class MemorizedRecipe implements INbtWritable, INbtReadable, IStreamable {
private InventoryCraftingForestry craftMatrix = new InventoryCraftingForestry();
private List<IRecipe> recipes = new ArrayList<>();
private int selectedRecipe;
private long lastUsed;
private boolean locked;
public MemorizedRecipe(PacketBufferForestry data) throws IOException {
readData(data);
}
public MemorizedRecipe(NBTTagCompound nbt) {
readFromNBT(nbt);
}
public MemorizedRecipe(InventoryCraftingForestry craftMatrix, List<IRecipe> recipes) {
InventoryUtil.deepCopyInventoryContents(craftMatrix, this.craftMatrix);
this.recipes = recipes;
}
public InventoryCraftingForestry getCraftMatrix() {
return craftMatrix;
}
public void setCraftMatrix(InventoryCraftingForestry craftMatrix) {
this.craftMatrix = craftMatrix;
}
public void incrementRecipe() {
selectedRecipe++;
if (selectedRecipe >= recipes.size()) {
selectedRecipe = 0;
}
}
public void decrementRecipe() {
selectedRecipe--;
if (selectedRecipe < 0) {
selectedRecipe = recipes.size() - 1;
}
}
public boolean hasRecipeConflict() {
return recipes.size() > 1;
}
public void removeRecipeConflicts() {
IRecipe recipe = getSelectedRecipe();
recipes.clear();
recipes.add(recipe);
selectedRecipe = 0;
}
public ItemStack getOutputIcon() {
IRecipe selectedRecipe = getSelectedRecipe();
if (selectedRecipe != null) {
ItemStack recipeOutput = selectedRecipe.getCraftingResult(craftMatrix);
if (!recipeOutput.isEmpty()) {
return recipeOutput;
}
}
return ItemStack.EMPTY;
}
public ItemStack getCraftingResult(InventoryCrafting inventoryCrafting, World world) {
IRecipe selectedRecipe = getSelectedRecipe();
if (selectedRecipe != null && selectedRecipe.matches(inventoryCrafting, world)) {
ItemStack recipeOutput = selectedRecipe.getCraftingResult(inventoryCrafting);
if (!recipeOutput.isEmpty()) {
return recipeOutput;
}
}
return ItemStack.EMPTY;
}
@Nullable
public IRecipe getSelectedRecipe() {
if (recipes.isEmpty()) {
return null;
} else {
return recipes.get(selectedRecipe);
}
}
public boolean hasRecipe(@Nullable IRecipe recipe) {
return this.recipes.contains(recipe);
}
public void updateLastUse(long lastUsed) {
this.lastUsed = lastUsed;
}
public long getLastUsed() {
return lastUsed;
}
public void toggleLock() {
locked = !locked;
}
public boolean isLocked() {
return locked;
}
/* INbtWritable */
@Override
public final void readFromNBT(NBTTagCompound nbttagcompound) {
InventoryUtil.readFromNBT(craftMatrix, nbttagcompound);
lastUsed = nbttagcompound.getLong("LastUsed");
locked = nbttagcompound.getBoolean("Locked");
if (nbttagcompound.hasKey("SelectedRecipe")) {
selectedRecipe = nbttagcompound.getInteger("SelectedRecipe");
}
recipes.clear();
NBTTagList recipesNbt = nbttagcompound.getTagList("Recipes", NBTUtilForestry.EnumNBTType.STRING.ordinal());
for (int i = 0; i < recipesNbt.tagCount(); i++) {
String recipeKey = recipesNbt.getStringTagAt(i);
ResourceLocation key = new ResourceLocation(recipeKey);
IRecipe recipe = ForgeRegistries.RECIPES.getValue(key);
if (recipe != null) {
recipes.add(recipe);
}
}
if (selectedRecipe > recipes.size()) {
selectedRecipe = 0;
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
InventoryUtil.writeToNBT(craftMatrix, nbttagcompound);
nbttagcompound.setLong("LastUsed", lastUsed);
nbttagcompound.setBoolean("Locked", locked);
nbttagcompound.setInteger("SelectedRecipe", selectedRecipe);
NBTTagList recipesNbt = new NBTTagList();
for (IRecipe recipe : recipes) {
ResourceLocation recipeKey = ForgeRegistries.RECIPES.getKey(recipe);
if (recipeKey != null) {
recipesNbt.appendTag(new NBTTagString(recipeKey.toString()));
}
}
nbttagcompound.setTag("Recipes", recipesNbt);
return nbttagcompound;
}
/* IStreamable */
@Override
public void writeData(PacketBufferForestry data) {
data.writeInventory(craftMatrix);
data.writeBoolean(locked);
data.writeVarInt(selectedRecipe);
data.writeVarInt(recipes.size());
for (IRecipe recipe: recipes) {
ResourceLocation recipeId = ForgeRegistries.RECIPES.getKey(recipe);
if (recipeId != null) {
data.writeString(recipeId.toString());
}
}
}
@Override
public void readData(PacketBufferForestry data) throws IOException {
data.readInventory(craftMatrix);
locked = data.readBoolean();
selectedRecipe = data.readVarInt();
recipes.clear();
int recipeCount = data.readVarInt();
for (int i = 0; i < recipeCount; i++) {
String recipeId = data.readString();
IRecipe recipe = ForgeRegistries.RECIPES.getValue(new ResourceLocation(recipeId));
if (recipe != null) {
recipes.add(recipe);
}
}
}
}
@@ -0,0 +1,179 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.recipes;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.core.INbtWritable;
import forestry.core.network.IStreamable;
import forestry.core.network.PacketBufferForestry;
public class RecipeMemory implements INbtWritable, IStreamable {
private static final int capacity = 9;
private final LinkedList<MemorizedRecipe> memorizedRecipes;
private long lastUpdate;
public RecipeMemory() {
memorizedRecipes = new LinkedList<>();
}
public RecipeMemory(NBTTagCompound nbt) {
memorizedRecipes = new LinkedList<>();
if (!nbt.hasKey("RecipeMemory")) {
return;
}
NBTTagList nbttaglist = nbt.getTagList("RecipeMemory", 10);
for (int j = 0; j < nbttaglist.tagCount(); ++j) {
NBTTagCompound recipeNbt = nbttaglist.getCompoundTagAt(j);
MemorizedRecipe recipe = new MemorizedRecipe(recipeNbt);
if (recipe.getSelectedRecipe() != null) {
memorizedRecipes.add(recipe);
}
}
}
public long getLastUpdate() {
return lastUpdate;
}
public void memorizeRecipe(long worldTime, MemorizedRecipe recipe) {
if (recipe.getSelectedRecipe() == null) {
return;
}
lastUpdate = worldTime;
recipe.updateLastUse(lastUpdate);
if (recipe.hasRecipeConflict()) {
recipe.removeRecipeConflicts();
}
// update existing matching recipes
MemorizedRecipe memory = getExistingMemorizedRecipe(recipe.getSelectedRecipe());
if (memory != null) {
updateExistingRecipe(memory, recipe);
return;
}
// add a new recipe
if (memorizedRecipes.size() < capacity) {
memorizedRecipes.add(recipe);
} else {
MemorizedRecipe oldest = getOldestUnlockedRecipe();
if (oldest != null) {
memorizedRecipes.remove(oldest);
memorizedRecipes.add(recipe);
}
}
}
private void updateExistingRecipe(MemorizedRecipe existingRecipe, MemorizedRecipe updatedRecipe) {
if (existingRecipe.isLocked() != updatedRecipe.isLocked()) {
updatedRecipe.toggleLock();
}
int index = memorizedRecipes.indexOf(existingRecipe);
memorizedRecipes.set(index, updatedRecipe);
}
@Nullable
private MemorizedRecipe getOldestUnlockedRecipe() {
MemorizedRecipe oldest = null;
for (MemorizedRecipe existing : memorizedRecipes) {
if (oldest != null && oldest.getLastUsed() < existing.getLastUsed()) {
continue;
}
if (!existing.isLocked()) {
oldest = existing;
}
}
return oldest;
}
@Nullable
public MemorizedRecipe getRecipe(int recipeIndex) {
if (recipeIndex < 0 || recipeIndex >= memorizedRecipes.size()) {
return null;
}
return memorizedRecipes.get(recipeIndex);
}
public ItemStack getRecipeDisplayOutput(int recipeIndex) {
MemorizedRecipe recipe = getRecipe(recipeIndex);
if (recipe == null) {
return ItemStack.EMPTY;
}
return recipe.getOutputIcon();
}
public boolean isLocked(int recipeIndex) {
MemorizedRecipe recipe = getRecipe(recipeIndex);
return recipe != null && recipe.isLocked();
}
public void toggleLock(long worldTime, int recipeIndex) {
lastUpdate = worldTime;
if (memorizedRecipes.size() > recipeIndex) {
memorizedRecipes.get(recipeIndex).toggleLock();
}
}
@Nullable
private MemorizedRecipe getExistingMemorizedRecipe(@Nullable IRecipe recipe) {
if (recipe != null) {
for (MemorizedRecipe memorizedRecipe : memorizedRecipes) {
if (memorizedRecipe.hasRecipe(recipe)) {
return memorizedRecipe;
}
}
}
return null;
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
NBTTagList nbttaglist = new NBTTagList();
for (MemorizedRecipe recipe : memorizedRecipes) {
if (recipe != null && recipe.getSelectedRecipe() != null) {
NBTTagCompound recipeNbt = new NBTTagCompound();
recipe.writeToNBT(recipeNbt);
nbttaglist.appendTag(recipeNbt);
}
}
nbttagcompound.setTag("RecipeMemory", nbttaglist);
return nbttagcompound;
}
@Override
public void writeData(PacketBufferForestry data) {
data.writeStreamables(memorizedRecipes);
}
@Override
@SideOnly(Side.CLIENT)
public void readData(PacketBufferForestry data) throws IOException {
data.readStreamables(memorizedRecipes, MemorizedRecipe::new);
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.recipes;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
@@ -0,0 +1,22 @@
package forestry.worktable.tiles;
import javax.annotation.Nullable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
public interface ICrafterWorktable {
ItemStack getResult(InventoryCrafting inventoryCrafting, World world);
boolean canTakeStack(int slotIndex);
boolean onCraftingStart(EntityPlayer player);
void onCraftingComplete(EntityPlayer player);
@Nullable
IRecipe getRecipeUsed();
}
@@ -0,0 +1,281 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.worktable.tiles;
import com.google.common.base.Preconditions;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.core.inventory.InventoryAdapterTile;
import forestry.core.inventory.InventoryGhostCrafting;
import forestry.core.inventory.wrappers.InventoryMapper;
import forestry.core.network.PacketBufferForestry;
import forestry.core.recipes.RecipeUtil;
import forestry.core.tiles.TileBase;
import forestry.core.utils.InventoryUtil;
import forestry.core.utils.ItemStackUtil;
import forestry.worktable.gui.ContainerWorktable;
import forestry.worktable.gui.GuiWorktable;
import forestry.worktable.inventory.InventoryCraftingForestry;
import forestry.worktable.inventory.InventoryWorktable;
import forestry.worktable.recipes.MemorizedRecipe;
import forestry.worktable.recipes.RecipeMemory;
public class TileWorktable extends TileBase implements ICrafterWorktable {
private RecipeMemory recipeMemory;
private final InventoryAdapterTile craftingDisplay;
@Nullable
private MemorizedRecipe currentRecipe;
public TileWorktable() {
setInternalInventory(new InventoryWorktable(this));
craftingDisplay = new InventoryGhostCrafting<>(this, 10);
recipeMemory = new RecipeMemory();
}
/* LOADING & SAVING */
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
nbttagcompound = super.writeToNBT(nbttagcompound);
craftingDisplay.writeToNBT(nbttagcompound);
recipeMemory.writeToNBT(nbttagcompound);
return nbttagcompound;
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
craftingDisplay.readFromNBT(nbttagcompound);
recipeMemory = new RecipeMemory(nbttagcompound);
}
/* NETWORK */
@Override
public void writeData(PacketBufferForestry data) {
super.writeData(data);
craftingDisplay.writeData(data);
recipeMemory.writeData(data);
}
@Override
@SideOnly(Side.CLIENT)
public void readData(PacketBufferForestry data) throws IOException {
super.readData(data);
craftingDisplay.readData(data);
recipeMemory.readData(data);
}
/* Recipe Conflicts */
public boolean hasRecipeConflict() {
return currentRecipe != null && currentRecipe.hasRecipeConflict();
}
public void chooseNextConflictRecipe() {
if (currentRecipe != null) {
currentRecipe.incrementRecipe();
}
}
public void choosePreviousConflictRecipe() {
if (currentRecipe != null) {
currentRecipe.decrementRecipe();
}
}
@Override
public ItemStack getResult(InventoryCrafting inventoryCrafting, World world) {
if (currentRecipe != null) {
return currentRecipe.getCraftingResult(inventoryCrafting, world);
}
return ItemStack.EMPTY;
}
/* ICrafterWorktable */
@Override
public boolean canTakeStack(int craftingSlotIndex) {
return craftingSlotIndex != InventoryGhostCrafting.SLOT_CRAFTING_RESULT ||
canCraftCurrentRecipe();
}
private boolean canCraftCurrentRecipe() {
return craftRecipe(true);
}
@Override
public boolean onCraftingStart(EntityPlayer player) {
return craftRecipe(false);
}
private boolean craftRecipe(boolean simulate) {
if (currentRecipe == null) {
return false;
}
IRecipe selectedRecipe = currentRecipe.getSelectedRecipe();
if (selectedRecipe == null) {
return false;
}
NonNullList<ItemStack> inventoryStacks = InventoryUtil.getStacks(this);
InventoryCraftingForestry crafting = RecipeUtil.getCraftRecipe(currentRecipe.getCraftMatrix(), inventoryStacks, world, selectedRecipe);
if (crafting == null) {
return false;
}
NonNullList<ItemStack> recipeItems = InventoryUtil.getStacks(crafting);
IInventory inventory;
if (simulate) {
inventory = new InventoryBasic("copy", false, this.getSizeInventory());
InventoryUtil.deepCopyInventoryContents(this, inventory);
} else {
inventory = this;
}
if (!InventoryUtil.deleteExactSet(inventory, recipeItems)) {
return false;
}
if (!simulate) {
// update crafting display to match the ingredients that were actually used
currentRecipe.setCraftMatrix(crafting);
setCurrentRecipe(currentRecipe);
}
return true;
}
@Override
public void onCraftingComplete(EntityPlayer player) {
Preconditions.checkState(currentRecipe != null);
IRecipe selectedRecipe = currentRecipe.getSelectedRecipe();
Preconditions.checkState(selectedRecipe != null);
ForgeHooks.setCraftingPlayer(player);
InventoryCraftingForestry craftMatrix = currentRecipe.getCraftMatrix();
NonNullList<ItemStack> remainingItems = selectedRecipe.getRemainingItems(craftMatrix.copy());
ForgeHooks.setCraftingPlayer(null);
for (ItemStack remainingItem : remainingItems) {
if (remainingItem != null && !remainingItem.isEmpty()) {
if (!InventoryUtil.tryAddStack(this, remainingItem, true)) {
player.dropItem(remainingItem, false);
}
}
}
if (!world.isRemote) {
recipeMemory.memorizeRecipe(world.getTotalWorldTime(), currentRecipe);
}
}
@Nullable
@Override
public IRecipe getRecipeUsed() {
if (currentRecipe == null) {
return null;
}
return currentRecipe.getSelectedRecipe();
}
/* Crafting Container methods */
public RecipeMemory getMemory() {
return recipeMemory;
}
public void chooseRecipeMemory(int recipeIndex) {
MemorizedRecipe recipe = recipeMemory.getRecipe(recipeIndex);
setCurrentRecipe(recipe);
}
private void setCraftingDisplay(IInventory craftMatrix) {
for (int slot = 0; slot < craftMatrix.getSizeInventory(); slot++) {
craftingDisplay.setInventorySlotContents(slot, craftMatrix.getStackInSlot(slot));
}
}
public IInventory getCraftingDisplay() {
return new InventoryMapper(craftingDisplay, InventoryGhostCrafting.SLOT_CRAFTING_1, InventoryGhostCrafting.SLOT_CRAFTING_COUNT);
}
public void clearCraftMatrix() {
for (int slot = 0; slot < craftingDisplay.getSizeInventory(); slot++) {
craftingDisplay.setInventorySlotContents(slot, ItemStack.EMPTY);
}
}
public void setCurrentRecipe(InventoryCraftingForestry crafting) {
List<IRecipe> recipes = RecipeUtil.findMatchingRecipes(crafting, world);
MemorizedRecipe recipe = recipes.isEmpty() ? null : new MemorizedRecipe(crafting, recipes);
if (currentRecipe != null && recipe != null) {
if (recipe.hasRecipe(currentRecipe.getSelectedRecipe())) {
NonNullList<ItemStack> stacks = InventoryUtil.getStacks(crafting);
NonNullList<ItemStack> currentStacks = InventoryUtil.getStacks(currentRecipe.getCraftMatrix());
if (ItemStackUtil.equalSets(stacks, currentStacks)) {
return;
}
}
}
setCurrentRecipe(recipe);
}
/* Network Sync with PacketWorktableRecipeUpdate */
@Nullable
public MemorizedRecipe getCurrentRecipe() {
return currentRecipe;
}
public void setCurrentRecipe(@Nullable MemorizedRecipe recipe) {
this.currentRecipe = recipe;
if (currentRecipe != null) {
setCraftingDisplay(currentRecipe.getCraftMatrix());
}
}
@Override
@SideOnly(Side.CLIENT)
public GuiContainer getGui(EntityPlayer player, int data) {
return new GuiWorktable(player, this);
}
@Override
public Container getContainer(EntityPlayer player, int data) {
return new ContainerWorktable(player, this);
}
}
@@ -0,0 +1,10 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.worktable.tiles;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;