Large amount of work toward being able to place the recipe into the gui.
This commit is contained in:
parent
bee8d4aab0
commit
412d66593b
4 changed files with 102 additions and 13 deletions
|
@ -24,7 +24,9 @@ import techreborn.packets.PacketSetRecipe;
|
|||
import techreborn.tiles.TileAutoCraftingTable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static net.minecraft.item.ItemStack.EMPTY;
|
||||
|
||||
|
@ -75,6 +77,18 @@ public class GuiAutoCrafting extends GuiBase {
|
|||
this.builder.drawMultiEnergyBar(this, 9, 26, (int) this.tileAutoCraftingTable.getEnergy(), (int) this.tileAutoCraftingTable.getMaxPower(), mouseX, mouseY, 0, layer);
|
||||
this.builder.drawProgressBar(this, tileAutoCraftingTable.getProgress(), tileAutoCraftingTable.getMaxProgress(), 120, 44, mouseX, mouseY, TRBuilder.ProgressDirection.RIGHT, layer);
|
||||
|
||||
int mX = mouseX - getGuiLeft();
|
||||
int mY = mouseY - getGuiTop();
|
||||
|
||||
if(recipe != null && !tileAutoCraftingTable.customRecipe){
|
||||
if (builder.isInRect(91, 66, 23, 23, mX, mY)) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("Click to clear");
|
||||
net.minecraftforge.fml.client.config.GuiUtils.drawHoveringText(list, mX, mY, width, height, -1, mc.fontRenderer);
|
||||
GlStateManager.disableLighting();
|
||||
GlStateManager.color(1, 1, 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Based of vanilla code
|
||||
|
@ -174,6 +188,13 @@ public class GuiAutoCrafting extends GuiBase {
|
|||
if (!this.recipeSlector.func_191862_a(mouseX, mouseY, mouseButton)) {
|
||||
super.mouseClicked(mouseX, mouseY, mouseButton);
|
||||
}
|
||||
int mX = mouseX - getGuiLeft();
|
||||
int mY = mouseY - getGuiTop();
|
||||
|
||||
mc.getTextureManager().bindTexture(TRBuilder.GUI_SHEET);
|
||||
if (builder.isInRect(91, 66, 23, 23, mX, mY)) {
|
||||
setRecipe(null, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -194,9 +215,9 @@ public class GuiAutoCrafting extends GuiBase {
|
|||
super.onGuiClosed();
|
||||
}
|
||||
|
||||
public void setRecipe(IRecipe recipe) {
|
||||
tileAutoCraftingTable.setCurrentRecipe(recipe.getRegistryName());
|
||||
NetworkManager.sendToServer(new PacketSetRecipe(tileAutoCraftingTable, recipe.getRegistryName()));
|
||||
public void setRecipe(IRecipe recipe, boolean custom) {
|
||||
tileAutoCraftingTable.setCurrentRecipe(recipe, custom);
|
||||
NetworkManager.sendToServer(new PacketSetRecipe(tileAutoCraftingTable, recipe, custom));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ public class GuiAutoCraftingRecipeSlector extends GuiRecipeBook {
|
|||
|
||||
@Override
|
||||
public void setContainerRecipe(IRecipe recipe, RecipeList recipes) {
|
||||
guiAutoCrafting.setRecipe(recipe);
|
||||
guiAutoCrafting.setRecipe(recipe, false);
|
||||
}
|
||||
|
||||
public void setGuiAutoCrafting(GuiAutoCrafting guiAutoCrafting) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package techreborn.packets;
|
||||
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
@ -17,14 +18,21 @@ public class PacketSetRecipe implements INetworkPacket<PacketSetRecipe> {
|
|||
|
||||
BlockPos pos;
|
||||
ResourceLocation recipe;
|
||||
boolean custom;
|
||||
|
||||
public PacketSetRecipe(TileAutoCraftingTable tile, ResourceLocation recipe) {
|
||||
public PacketSetRecipe(TileAutoCraftingTable tile, IRecipe recipe, boolean custom) {
|
||||
this.pos = tile.getPos();
|
||||
this.recipe = recipe;
|
||||
if(recipe == null){
|
||||
this.recipe = new ResourceLocation("");
|
||||
} else {
|
||||
this.recipe = recipe.getRegistryName();
|
||||
}
|
||||
|
||||
if(this.recipe == null){
|
||||
//TODO fix vanilla recipes
|
||||
this.recipe = new ResourceLocation("");
|
||||
}
|
||||
this.custom = custom;
|
||||
}
|
||||
|
||||
public PacketSetRecipe() {
|
||||
|
@ -34,19 +42,21 @@ public class PacketSetRecipe implements INetworkPacket<PacketSetRecipe> {
|
|||
public void writeData(ExtendedPacketBuffer buffer) throws IOException {
|
||||
buffer.writeBlockPos(pos);
|
||||
buffer.writeResourceLocation(recipe);
|
||||
buffer.writeBoolean(custom);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readData(ExtendedPacketBuffer buffer) throws IOException {
|
||||
pos = buffer.readBlockPos();
|
||||
recipe = buffer.readResourceLocation();
|
||||
custom = buffer.readBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processData(PacketSetRecipe message, MessageContext context) {
|
||||
TileEntity tileEntity = context.getServerHandler().player.world.getTileEntity(pos);;
|
||||
TileEntity tileEntity = context.getServerHandler().player.world.getTileEntity(message.pos);;
|
||||
if(tileEntity instanceof TileAutoCraftingTable){
|
||||
((TileAutoCraftingTable) tileEntity).setCurrentRecipe(recipe);
|
||||
((TileAutoCraftingTable) tileEntity).setCurrentRecipe(message.recipe, message.custom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package techreborn.tiles;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
@ -35,13 +38,37 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
public int maxProgress = 120;
|
||||
public int euTick = 10;
|
||||
public Pair<ResourceLocation, IRecipe> cachedRecipe;
|
||||
public boolean customRecipe = true;
|
||||
InventoryCrafting inventoryCrafting = null;
|
||||
IRecipe lastCustomRecipe = null;
|
||||
|
||||
public void setCurrentRecipe(ResourceLocation recipe) {
|
||||
public void setCurrentRecipe(IRecipe recipe, boolean customRecipe) {
|
||||
if(recipe != null){
|
||||
currentRecipe = recipe.getRegistryName();
|
||||
} else {
|
||||
currentRecipe = null;
|
||||
}
|
||||
|
||||
this.customRecipe = customRecipe;
|
||||
cachedRecipe = null;
|
||||
}
|
||||
|
||||
public void setCurrentRecipe(ResourceLocation recipe, boolean customRecipe) {
|
||||
currentRecipe = recipe;
|
||||
this.customRecipe = customRecipe;
|
||||
cachedRecipe = null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public IRecipe getIRecipe() {
|
||||
if(customRecipe){
|
||||
InventoryCrafting crafting = getCraftingInventory();
|
||||
for(IRecipe testRecipe : CraftingManager.REGISTRY){
|
||||
if(testRecipe.matches(crafting, world)){
|
||||
return testRecipe;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (currentRecipe == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -57,6 +84,21 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
return cachedRecipe.getRight();
|
||||
}
|
||||
|
||||
public InventoryCrafting getCraftingInventory(){
|
||||
if(inventoryCrafting == null){
|
||||
inventoryCrafting = new InventoryCrafting(new Container() {
|
||||
@Override
|
||||
public boolean canInteractWith(EntityPlayer playerIn) {
|
||||
return false;
|
||||
}
|
||||
}, 3, 3);
|
||||
}
|
||||
for (int i = 0; i < 8; i++) {
|
||||
inventoryCrafting.setInventorySlotContents(i, inventory.getStackInSlot(i));
|
||||
}
|
||||
return inventoryCrafting;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
super.update();
|
||||
|
@ -64,7 +106,7 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
return;
|
||||
}
|
||||
IRecipe recipe = getIRecipe();
|
||||
if (recipe != null) {
|
||||
if (recipe != null || customRecipe) {
|
||||
if (progress >= maxProgress) {
|
||||
if (make(recipe)) {
|
||||
progress = 0;
|
||||
|
@ -81,7 +123,10 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
}
|
||||
|
||||
public boolean canMake(IRecipe recipe) {
|
||||
if (recipe.canFit(3, 3)) {
|
||||
if(customRecipe){
|
||||
recipe = getIRecipe();
|
||||
}
|
||||
if (recipe != null && recipe.canFit(3, 3)) {
|
||||
boolean missingOutput = false;
|
||||
int[] stacksInSlots = new int[9];
|
||||
for (int i = 0; i < 9; i++) {
|
||||
|
@ -92,7 +137,8 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
boolean foundIngredient = false;
|
||||
for (int i = 0; i < 9; i++) {
|
||||
ItemStack stack = inventory.getStackInSlot(i);
|
||||
if (stacksInSlots[i] > 0) {
|
||||
int requiredSize = customRecipe ? 1 : 0;
|
||||
if (stacksInSlots[i] > requiredSize) {
|
||||
if (ingredient.apply(stack)) {
|
||||
foundIngredient = true;
|
||||
stacksInSlots[i]--;
|
||||
|
@ -130,6 +176,12 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
|
||||
public boolean make(IRecipe recipe) {
|
||||
if (canMake(recipe)) {
|
||||
if(recipe == null && customRecipe){
|
||||
if(lastCustomRecipe == null){
|
||||
return false;
|
||||
}//Should be uptodate as we just set it in canMake
|
||||
recipe = lastCustomRecipe;
|
||||
}
|
||||
for (int i = 0; i < recipe.getIngredients().size(); i++) {
|
||||
Ingredient ingredient = recipe.getIngredients().get(i);
|
||||
//Looks for the best slot to take it from
|
||||
|
@ -141,15 +193,19 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
ItemStack stack = inventory.getStackInSlot(j);
|
||||
if (ingredient.apply(stack)) {
|
||||
stack.shrink(1); //TODO is this right? or do I need to use it as an actull crafting grid
|
||||
//TOOD check items to be retuned
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ItemStack output = inventory.getStackInSlot(9);
|
||||
//TODO fire forge recipe event
|
||||
ItemStack ouputStack = recipe.getCraftingResult(getCraftingInventory());
|
||||
if (output.isEmpty()) {
|
||||
inventory.setInventorySlotContents(9, recipe.getRecipeOutput().copy());
|
||||
inventory.setInventorySlotContents(9, ouputStack.copy());
|
||||
} else {
|
||||
//TODO use ouputStack in someway?
|
||||
output.grow(recipe.getRecipeOutput().getCount());
|
||||
}
|
||||
return true;
|
||||
|
@ -243,6 +299,7 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
if (currentRecipe != null) {
|
||||
tag.setString("currentRecipe", currentRecipe.toString());
|
||||
}
|
||||
tag.setBoolean("customRecipe", customRecipe);
|
||||
return super.writeToNBT(tag);
|
||||
}
|
||||
|
||||
|
@ -251,6 +308,7 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain
|
|||
if (tag.hasKey("currentRecipe")) {
|
||||
currentRecipe = new ResourceLocation(tag.getString("currentRecipe"));
|
||||
}
|
||||
customRecipe = tag.getBoolean("customRecipe");
|
||||
super.readFromNBT(tag);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue