Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Apr 23, 2017
1 parent 6f88359 commit 2c07463
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
import java.util.ArrayList;

/**
* todo: wip
* @author Dries007
*/
public class OredictElement extends SlotElement
{
public OredictElement(GuiElementCallback callback, boolean optional, Slot.Type type, boolean wildcard, boolean metawildcard, boolean oredict, boolean stacksize)
protected ItemStack prevSet = null;

public OredictElement(GuiElementCallback callback, boolean optional, boolean stacksize)
{
super(callback, optional, type, wildcard, metawildcard, oredict, stacksize);
super(callback, optional, Slot.Type.INGREDIENT, false, false, true, stacksize, true);
}

@Override
Expand All @@ -57,7 +60,36 @@ protected void focusStatusChanged()
@Override
protected void setItemStack(ItemStack input)
{
super.setItemStack(input);
// if (input == null)
// {
// super.setItemStack(null);
// return;
// }
// int[] ids = OreDictionary.getOreIDs(input);
// if (ids.length == 0)// is not oredict
// {
// super.setItemStack(null);
// return;
// }
// if (oredict == null || ids.length == 1) // was previously not set, start with id 0
// {
// super.setOredict(OreDictionary.getOreName(ids[0]));
// }
// if (input == prevSet) // Rotate through the list of oredict entries this itemstack has
// {
//
// else // find current id, and then set the next, or loop back to 0
// {
// int i = 0;
// while (i < ids.length && !OreDictionary.getOreName(ids[i]).equalsIgnoreCase(oredict)) i++;
// if (++i >= ids.length) i = 0; // Increase and if required loop back to 0
// super.setOredict(OreDictionary.getOreName(ids[i]));
// }
// }
// else // is new stack
// {
//
// }
}

@Override
Expand Down
118 changes: 86 additions & 32 deletions src/main/java/net/doubledoordev/mtrm/client/elements/SlotElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.client.config.GuiUtils;
import net.minecraftforge.oredict.OreDictionary;

import java.util.ArrayList;
import java.util.List;

import static net.doubledoordev.mtrm.client.GuiBase.BASE;

Expand All @@ -47,21 +49,32 @@
public class SlotElement extends GuiElement
{
protected final Slot.Type type;
protected final boolean wildcard;
protected final boolean metawildcard;
protected final boolean oredict;
protected final boolean stacksize;
protected final boolean wildcardAllowed;
protected final boolean metaWildcardAllowed;
protected final boolean oredictAllowed;
protected final boolean stacksizeAllowed;
protected final boolean oredictRequired;

protected ItemStack stack;

public SlotElement(GuiElementCallback callback, boolean optional, Slot.Type type, boolean wildcard, boolean metawildcard, boolean oredict, boolean stacksize)
protected int tickCounter;
protected String oredict;
protected int oredictCounter;
protected List<ItemStack> oredictList;
// For looping trough the available list when the same stack is clicked twice.
protected ItemStack oredictPrevStack;
protected int[] oredictIds;
protected int oredictIdCounter;

public SlotElement(GuiElementCallback callback, boolean optional, Slot.Type type, boolean wildcardAllowed, boolean metaWildcardAllowed, boolean oredictAllowed, boolean stacksizeAllowed, boolean oredictRequired)
{
super(callback, optional);
this.type = type;
this.wildcard = wildcard;
this.metawildcard = metawildcard;
this.oredict = oredict;
this.stacksize = stacksize;
this.wildcardAllowed = wildcardAllowed;
this.metaWildcardAllowed = metaWildcardAllowed;
this.oredictAllowed = oredictAllowed;
this.stacksizeAllowed = stacksizeAllowed;
this.oredictRequired = oredictRequired;
}

@Override
Expand All @@ -88,24 +101,58 @@ protected void focusStatusChanged()
resizeCallback();
}

protected void setItemStack(ItemStack input)
protected void setItemStackOrOredict(ItemStack input)
{
if (input == null)
if (input == null) reset();
else if (!oredictAllowed) setItemStack(input);
else if (input == oredictPrevStack)
{
stack = null;
updateButtonsCallback();
return;
oredictIdCounter++;
oredictIdCounter %= oredictIds.length;
setOredict(OreDictionary.getOreName(oredictIds[oredictIdCounter]));
}
else
{
oredictPrevStack = input;
oredictIds = OreDictionary.getOreIDs(input);
}
}

/**
* Reset
*/
protected void reset()
{
tickCounter = 0;
oredict = null;
oredictCounter = 0;
oredictList = null;
stack = null;
updateButtonsCallback();
}

protected void setItemStack(ItemStack input)
{
if (oredictRequired) return; // Shouldn't happen, but you never know
reset();
stack = input.copy();
stack.setTagCompound(null);
if (!stacksize) stack.stackSize = 1;
if (!stacksizeAllowed) stack.stackSize = 1;
updateButtonsCallback();
}

protected void setOredict(String value)
{
if (!oredictAllowed) return; // Shouldn't happen, but you never know
reset();
oredictList = OreDictionary.getOres(value, false);
updateButtonsCallback();
}

@Override
public String save()
{
return Helper.itemstackToString(stack);
return (oredict != null) ? String.valueOf(oredict) : Helper.itemstackToString(stack);
}

@Override
Expand All @@ -121,12 +168,13 @@ public void draw(int mouseX, int mouseY, float partialTicks)

protected void drawItemStack(ItemStack stack, int x, int y, String altText)
{
if (stack == null) return;
GlStateManager.translate(0.0F, 0.0F, 32.0F);
zLevel = 200.0F;
RenderItem itemRender = mc.getRenderItem();
itemRender.zLevel = 200.0F;
FontRenderer font = null;
if (stack != null) font = stack.getItem().getFontRenderer(stack);
FontRenderer font = stack.getItem().getFontRenderer(stack);
//noinspection ConstantConditions
if (font == null) font = mc.fontRendererObj;
itemRender.renderItemAndEffectIntoGUI(stack, x, y);
itemRender.renderItemOverlayIntoGUI(font, stack, x, y, altText);
Expand All @@ -138,6 +186,17 @@ protected void drawItemStack(ItemStack stack, int x, int y, String altText)
public void update()
{
super.update();
if (oredict != null)
{
if (tickCounter / 6 % 2 == 0)
{
int stacksize = 0;
if (stack != null) stacksize = stack.stackSize;
stack = oredictList.get(oredictCounter++ % oredictList.size()).copy();
stack.stackSize = stacksize;
}
tickCounter ++;
}
}

protected ArrayList<String> getHoverLines()
Expand All @@ -146,12 +205,13 @@ protected ArrayList<String> getHoverLines()
list.add(ChatFormatting.AQUA + "Options:");
list.add("- Type: " + type);
list.add("- Optional: " + optional);
list.add("- Wildcard: " + (wildcard ? "Allowed" : "Not Allowed"));
list.add("- Meta Wildcard: " + (metawildcard ? "Allowed" : "Not Allowed"));
list.add("- Ore Dictionary: " + (oredict ? "Allowed" : "Not Allowed"));
list.add("- Stack size: " + (stacksize ? "Allowed" : "Not Allowed"));
list.add("- Wildcard: " + (wildcardAllowed ? "Allowed" : "Not Allowed"));
list.add("- Meta Wildcard: " + (metaWildcardAllowed ? "Allowed" : "Not Allowed"));
list.add("- Ore Dictionary: " + (oredictAllowed ? "Allowed" : "Not Allowed"));
list.add("- Stack size: " + (stacksizeAllowed ? "Allowed" : "Not Allowed"));
list.add(ChatFormatting.AQUA + "Current value:");
if (stack != null) list.addAll(stack.getTooltip(mc.thePlayer, true));
if (oredict != null) list.add(oredict);
else if (stack != null) list.addAll(stack.getTooltip(mc.thePlayer, true));
else list.add("null");
return list;
}
Expand All @@ -168,14 +228,8 @@ protected void onClickOn(int mouseX, int mouseY, int mouseButton)
{
super.onClickOn(mouseX, mouseY, mouseButton);
ItemStack heldStack = mc.thePlayer.inventory.getItemStack();
if (heldStack != null) setItemStack(heldStack);
else if (stack != null)
{
if (mouseButton == 1)
{
setItemStack(null);
}
}
if (heldStack != null) setItemStackOrOredict(heldStack);
else if (stack != null && mouseButton == 1) setItemStackOrOredict(null);
}

@Override
Expand All @@ -195,7 +249,7 @@ public boolean keyTyped(char typedChar, int keyCode)
@Override
public boolean isValid()
{
return stack != null || optional;
return oredict != null || stack != null || optional;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/doubledoordev/mtrm/xml/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void fatalError(SAXParseException exception) throws SAXException
registerType("nbt", new Nbt.InstanceCreator());
registerType("string", new ManualString.InstanceCreator());
registerType("number", new Number.InstanceCreator());
registerType("oredict", new Oredict.InstanceCreator());
registerType("oredictAllowed", new Oredict.InstanceCreator());
registerType("slot", new Slot.InstanceCreator());
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/doubledoordev/mtrm/xml/elements/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public Array(Element node) throws Exception
this.component = component;
}

@Override
public String toString()
{
return "Array{" +
"component=" + component +
", min=" + min +
", max=" + max +
", optional=" + optional +
'}';
}

@Override
public String toHumanText()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public ManualString(Element node)
@Override
public String toString()
{
return "ManualString{}";
return "ManualString{" +
"optional=" + optional +
'}';
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/doubledoordev/mtrm/xml/elements/Nbt.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public Nbt(Element node)
@Override
public String toString()
{
return "Nbt{}";
return "Nbt{" +
"optional=" + optional +
'}';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public Number(Element node)
@Override
public String toString()
{
return "Number{min=" + min + ", max=" + max + ", stepsize=" + stepsize + '}';
return "Number{" +
"min=" + min +
", max=" + max +
", stepsize=" + stepsize +
", optional=" + optional +
'}';
}

@Override
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/net/doubledoordev/mtrm/xml/elements/Oredict.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package net.doubledoordev.mtrm.xml.elements;

import net.doubledoordev.mtrm.client.elements.GuiElement;
import net.doubledoordev.mtrm.client.elements.SlotElement;
import net.doubledoordev.mtrm.xml.XmlParser;
import org.w3c.dom.Element;

Expand All @@ -38,28 +39,33 @@
public class Oredict implements XmlParser.IStringObject
{
public final boolean optional;
public final boolean stacksize;

public Oredict(Element node)
{
this.optional = node.hasAttribute("optional") && Boolean.parseBoolean(node.getAttribute("optional"));
this.stacksize = node.hasAttribute("stacksizeAllowed") && Boolean.parseBoolean(node.getAttribute("stacksizeAllowed"));
}

@Override
public String toString()
{
return "Oredict{optional=" + optional + '}';
return "Oredict{" +
"optional=" + optional +
", stacksizeAllowed=" + stacksize +
'}';
}

@Override
public String toHumanText()
{
return "<oredict>";
return "<oredictAllowed>";
}

@Override
public GuiElement toGuiElement(GuiElement.GuiElementCallback callback)
{
return null; // todo
return new SlotElement(callback, optional, Slot.Type.INGREDIENT, false, false, true, stacksize, true);
}

public static class InstanceCreator implements XmlParser.IInstanceCreator<Oredict>
Expand Down
Loading

0 comments on commit 2c07463

Please sign in to comment.