Skip to content

Commit

Permalink
Changed it to make 1 file per handler, so that removing happens befor…
Browse files Browse the repository at this point in the history
…e adding.
  • Loading branch information
dries007 committed Dec 22, 2014
1 parent f5ccee8 commit 1f60613
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 44 deletions.
26 changes: 15 additions & 11 deletions src/main/java/net/doubledoordev/mtrm/gui/MTRMContainer.java
Expand Up @@ -4,28 +4,27 @@
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

/**
* @author Dries007
*/
public class MTRMContainer extends Container
{
private final World world;
public static final int RETURN_SLOT_ID = 1 + (3 * 3) + (3 * 9) + 9;
/**
* The crafting matrix inventory (3x3).
*/
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
public IInventory craftResult = new InventoryCraftResult();
public IInventory returnSlot = new InventoryCraftResult();

public MTRMContainer(InventoryPlayer p_i1808_1_, World world)
public MTRMContainer(InventoryPlayer p_i1808_1_)
{
this.world = world;
this.addSlotToContainer(new Slot(this.craftResult, 0, 138, 48));

for (int y = 0; y < 3; ++y) for (int x = 0; x < 3; ++x) this.addSlotToContainer(new Slot(this.craftMatrix, x + y * 3, 22 + x * 26, 21 + y * 26));
for (int y = 0; y < 3; ++y) for (int x = 0; x < 9; ++x) this.addSlotToContainer(new Slot(p_i1808_1_, x + y * 9 + 9, 8 + x * 18, 99 + y * 18));
for (int x = 0; x < 9; ++x) this.addSlotToContainer(new Slot(p_i1808_1_, x, 8 + x * 18, 157));
this.addSlotToContainer(new Slot(returnSlot, 0, -39, 115));

this.onCraftMatrixChanged(this.craftMatrix);
}
Expand All @@ -35,7 +34,7 @@ public MTRMContainer(InventoryPlayer p_i1808_1_, World world)
*/
public void onCraftMatrixChanged(IInventory p_75130_1_)
{

super.onCraftMatrixChanged(p_75130_1_);
}

public boolean canInteractWith(EntityPlayer p_75145_1_)
Expand All @@ -53,7 +52,7 @@ protected void retrySlotClick(int p_75133_1_, int p_75133_2_, boolean p_75133_3_
public ItemStack slotClick(int i, int mousebtn, int modifier, EntityPlayer player)
{
ItemStack stack = null;
if (i >= 0 && i <= 9) // Fake slots
if ((i >= 0 && i <= 9) || i == RETURN_SLOT_ID) // Fake slots
{
if (mousebtn == 2)
{
Expand All @@ -68,7 +67,12 @@ else if (mousebtn == 0)

if (stackSlot != null) stack = stackSlot.copy();

if (stackHeld != null) getSlot(i).putStack(stackHeld.copy());
if (stackHeld != null)
{
ItemStack newStack = stackHeld.copy();
if (!(i == 0 || i == RETURN_SLOT_ID)) newStack.stackSize = 1;
getSlot(i).putStack(newStack);
}
else getSlot(i).putStack(null);
}
else if (mousebtn == 1)
Expand All @@ -83,7 +87,7 @@ else if (mousebtn == 1)
if (stackHeld != null)
{
stackHeld = stackHeld.copy();
if (stackSlot != null && stackHeld.isItemEqual(stackSlot) && i == 0)
if (stackSlot != null && stackHeld.isItemEqual(stackSlot) && (i == 0 || i == RETURN_SLOT_ID))
{
int max = stackSlot.getMaxStackSize();
if (++stackSlot.stackSize > max) stackSlot.stackSize = max;
Expand Down Expand Up @@ -117,12 +121,12 @@ else if (mousebtn == 1)
*/
public ItemStack transferStackInSlot(EntityPlayer player, int slots)
{
if (slots < 10) ((Slot) this.inventorySlots.get(slots)).putStack(null);
if (slots < 10 || slots == RETURN_SLOT_ID) ((Slot) inventorySlots.get(slots)).putStack(null);
return null;
}

public boolean func_94530_a(ItemStack p_94530_1_, Slot p_94530_2_)
{
return p_94530_2_.inventory != this.craftResult && super.func_94530_a(p_94530_1_, p_94530_2_);
return p_94530_2_.inventory != craftResult && p_94530_2_.inventory != returnSlot && super.func_94530_a(p_94530_1_, p_94530_2_);
}
}
53 changes: 44 additions & 9 deletions src/main/java/net/doubledoordev/mtrm/gui/MTRMGui.java
Expand Up @@ -4,6 +4,7 @@
import cpw.mods.fml.client.config.GuiCheckBox;
import cpw.mods.fml.client.config.GuiSlider;
import cpw.mods.fml.common.registry.GameData;
import cpw.mods.fml.common.registry.GameRegistry;
import net.doubledoordev.mtrm.MineTweakerRecipeMaker;
import net.doubledoordev.mtrm.network.MessageSend;
import net.minecraft.client.gui.GuiButton;
Expand All @@ -21,6 +22,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static net.doubledoordev.mtrm.gui.MTRMContainer.RETURN_SLOT_ID;

/**
* @author Dries007
*/
Expand All @@ -39,6 +42,7 @@ public class MTRMGui extends GuiContainer
private static final Pattern ONLY_DAMAGE_BETWEEN = Pattern.compile("\\.onlyDamageBetween\\((\\d+), ?(\\d+)\\)");
private static final Pattern WITH_DAMAGE = Pattern.compile("\\.withDamage\\((\\d+)\\)");
private static final Pattern TRANSFORM_DAMAGE = Pattern.compile("\\.transformDamage\\((\\d+)\\)");
private static final Pattern GIVE_BACK = Pattern.compile("\\.giveBack\\(<(.*):(.*)(:\\d+)?>(?: ?\\* ?(\\d+))\\)");

private static final int ID_SEND = 10;
private static final int ID_REMOVE = 11;
Expand All @@ -59,6 +63,7 @@ public class MTRMGui extends GuiContainer
private static final int ID_OPTION_REUSE = 26;
private static final int ID_OPTION_ID_OPTION_TRANSFORM_DAMAGE = 27;
private static final int ID_OPTION_NO_RETURN = 28;
private static final int ID_OPTION_RETURN_OK = 29;

private final GuiSlider.ISlider iSlider = new GuiSlider.ISlider()
{
Expand Down Expand Up @@ -88,6 +93,7 @@ public void onChangeSliderValue(GuiSlider slider)
private GuiCheckBox reuse;
private GuiCheckBox noReturn;
private GuiCheckBox transformDamage;
private GuiButtonExt returnOk;
private Map<GuiCheckBox, GuiSlider[]> sliders = new HashMap<>();
private Map<GuiCheckBox, Pattern> patterns = new HashMap<>();
private Map<GuiCheckBox, GuiCustomLabel[]> labels = new HashMap<>();
Expand Down Expand Up @@ -129,15 +135,24 @@ public String getStackToken(boolean nextOreDict, ItemStack stack)
builder.append(stackName);
if (!oreDict && (metaWildcard || stack.getItemDamage() != 0)) builder.append(':').append(metaWildcard || stack.getItemDamage() == OreDictionary.WILDCARD_VALUE ? "*" : stack.getItemDamage());
builder.append('>');
if (stack.stackSize > 1) builder.append(" * ").append(stack.stackSize);
if (anyDamage.isChecked()) builder.append(".anyDamage()");
if (onlyDamaged.isChecked()) builder.append(".onlyDamaged()");
if (withDamage.isChecked()) builder.append(".withDamage(").append(sliders.get(withDamage)[0].getValueInt()).append(')');
if (onlyDamageAtLeast.isChecked()) builder.append(".onlyDamageAtLeast(").append(sliders.get(onlyDamageAtLeast)[0].getValueInt()).append(')');
if (onlyDamageAtMost.isChecked()) builder.append(".onlyDamageAtMost(").append(sliders.get(onlyDamageAtMost)[0].getValueInt()).append(')');
if (onlyDamageBetween.isChecked()) builder.append(".onlyDamageBetween(").append(sliders.get(onlyDamageBetween)[0].getValueInt()).append(", ").append(sliders.get(onlyDamageBetween)[1].getValueInt()).append(')');
if (reuse.isChecked()) builder.append(".reuse()");
if (noReturn.isChecked()) builder.append(".noReturn()");
if (transformDamage.isChecked()) builder.append(".transformDamage(").append(sliders.get(transformDamage)[0].getValueInt()).append(')');
if (stack.stackSize > 1) builder.append(" * ").append(stack.stackSize);
if (container.getSlot(RETURN_SLOT_ID).getHasStack())
{
ItemStack returnStack = container.getSlot(RETURN_SLOT_ID).getStack();
builder.append(".giveBack(<").append(GameData.getItemRegistry().getNameForObject(returnStack.getItem()));
if (returnStack.getItemDamage() != 0) builder.append(':').append(returnStack.getItemDamage());
if (returnStack.stackSize > 1) builder.append(" * ").append(returnStack.stackSize);
builder.append(">)");
}
return builder.toString();
}

Expand All @@ -157,7 +172,7 @@ public void initGui()
this.buttonList.add(new GuiButtonExt(ID_CLOSE, this.width / 2 + 90, this.height / 2 - 25, 110, 20, "Close"));

int wOffset = this.width / 2 - 200;
int hOffset = this.height / 2 - 90;
int hOffset = this.height / 2 - 110;

tokenTxt = new GuiTextField(this.fontRendererObj, wOffset, hOffset - 25, 220 + this.xSize, 20);
tokenTxt.setMaxStringLength(Integer.MAX_VALUE);
Expand All @@ -175,8 +190,10 @@ public void initGui()
this.buttonList.add(reuse = new GuiCheckBox(ID_OPTION_REUSE, wOffset, hOffset += 10, "Reuse", false));
this.buttonList.add(noReturn = new GuiCheckBox(ID_OPTION_NO_RETURN, wOffset, hOffset += 10, "No Return", false));

this.buttonList.add(nextOreDict = new GuiButtonExt(ID_OPTION_NEXT_OREDICT, wOffset, hOffset += 20, 110, 20, "Next oredict value"));
this.buttonList.add(optionsOk = new GuiButtonExt(ID_OPTION_OK, wOffset, hOffset += 20, 110, 20, "Ok!"));
this.buttonList.add(returnOk = new GuiButtonExt(ID_OPTION_RETURN_OK, wOffset + 90, hOffset + 14, 20, 18, "OK"));

this.buttonList.add(nextOreDict = new GuiButtonExt(ID_OPTION_NEXT_OREDICT, wOffset, hOffset += 40, 110, 20, "Next oredict value"));
this.buttonList.add(optionsOk = new GuiButtonExt(ID_OPTION_OK, wOffset, hOffset += 20, 110, 20, "Save changes!"));

// Buttongroups

Expand All @@ -191,7 +208,7 @@ public void initGui()

id = 100;
hOffset += 30;
int hOffsetText = 181;
int hOffsetText = 182;

addSliders(withDamage, new GuiSlider(id++, wOffset, hOffset, 220 + this.xSize, 20, "X = ", "", 0, 0, 0, false, true, iSlider));
addLabels(withDamage, new GuiCustomLabel("With Damage X", -110, hOffsetText));
Expand Down Expand Up @@ -244,6 +261,10 @@ protected void handleMouseClick(Slot slot, int slotNumber, int mouseBtn, int mod
messageSend.data[slotNumber] = getStackToken(false, slot.getStack());
showOptionsFor(slotNumber);
}
if (slotNumber == RETURN_SLOT_ID)
{
tokenTxt.setText(getStackToken(true, inventorySlots.getSlot(editing).getStack()));
}
}

protected void actionPerformed(GuiButton btn)
Expand Down Expand Up @@ -294,6 +315,7 @@ protected void actionPerformed(GuiButton btn)
case ID_OPTION_OREDICT:
nextOreDict.enabled = oreDict.isChecked();
case ID_OPTION_NEXT_OREDICT:
case ID_OPTION_RETURN_OK:
tokenTxt.setText(getStackToken(true, inventorySlots.getSlot(editing).getStack()));
break;
default:
Expand Down Expand Up @@ -351,7 +373,7 @@ private void showOptionsFor(int id)
if (labels.containsKey(checkBox)) for (GuiCustomLabel l : labels.get(checkBox)) l.draw = false;
}
}

container.getSlot(RETURN_SLOT_ID).putStack(null);
}
else
{
Expand All @@ -375,6 +397,14 @@ private void showOptionsFor(int id)
}
}
}

Matcher m = GIVE_BACK.matcher(token);
if (m.find())
{
int meta = m.group(3) != null ? Integer.parseInt(m.group(3)) : 0;
int size = m.group(4) != null ? Integer.parseInt(m.group(4)) : 1;
container.getSlot(RETURN_SLOT_ID).putStack(new ItemStack(GameRegistry.findItem(m.group(1), m.group(2)), size, meta));
}
}

for (GuiCheckBox[] group : allGroups)
Expand Down Expand Up @@ -423,6 +453,7 @@ private void setOptionsVisible(boolean visible)
withDamage.visible = visible;
onlyDamaged.visible = visible;
reuse.visible = visible;
returnOk.visible = visible;

for (GuiCheckBox[] group : allGroups)
{
Expand All @@ -435,6 +466,8 @@ private void setOptionsVisible(boolean visible)
}

tokenTxt.setVisible(visible);

if (!visible) container.putStackInSlot(RETURN_SLOT_ID, null);
}

protected void keyTyped(char p_73869_1_, int p_73869_2_)
Expand All @@ -458,13 +491,14 @@ protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_)
{
if (editing != -1)
{
this.fontRendererObj.drawString("Editing slot " + editing, -110, -35, 0xFFFFFF);
this.fontRendererObj.drawString("Slot Options", -100, 0, 0xFFFFFF);
this.fontRendererObj.drawString("Editing slot " + editing, -110, -55, 0xFFFFFF);
this.fontRendererObj.drawString("Slot Options", -100, -20, 0xFFFFFF);
this.fontRendererObj.drawString("Return Slot", -100, 120, 0xFFFFFF);
}
this.fontRendererObj.drawString("Recipe Options", this.xSize + 15, 0, 0xFFFFFF);
this.fontRendererObj.drawString("MineTweaker Recipe Maker", 28, 4, 4210752);
this.fontRendererObj.drawString(I18n.format("container.inventory"), 8, this.ySize - 96 + 5, 4210752);
if (errorMessage != null) this.fontRendererObj.drawString(errorMessage, 28, -10, 0xFF0000);
if (errorMessage != null) this.drawCenteredString(this.fontRendererObj, errorMessage, this.xSize / 2, -15, 0xFF0000);
this.fontRendererObj.drawString("0", 144, 53, 0xFFFFFF);
for (int y = 0; y < 3; ++y) for (int x = 0; x < 3; ++x) this.fontRendererObj.drawString(String.valueOf(1 + y * 3 + x), 28 + x * 26, 25 + y * 26, 0xFFFFFF);
for (GuiCustomLabel[] labela : labels.values()) for (GuiCustomLabel label : labela) if (label.draw) this.fontRendererObj.drawString(label.text, label.x, label.y, label.color);
Expand All @@ -477,6 +511,7 @@ protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2
int k = (this.width - this.xSize) / 2;
int l = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
if (editing != -1) this.drawTexturedModalRect(k - 40, l + 114, 21, 20, 18, 18);
tokenTxt.drawTextBox();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/doubledoordev/mtrm/gui/MTRMGuiHandler.java
Expand Up @@ -12,12 +12,12 @@ public class MTRMGuiHandler implements IGuiHandler
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
return new MTRMContainer(player.inventory, world);
return new MTRMContainer(player.inventory);
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
return new MTRMGui(new MTRMContainer(player.inventory, world));
return new MTRMGui(new MTRMContainer(player.inventory));
}
}

0 comments on commit 1f60613

Please sign in to comment.