Mekanism-tilera-Edition/common/mekanism/client/gui/GuiMOreDictFilter.java

353 lines
8.7 KiB
Java
Raw Normal View History

package mekanism.client.gui;
2013-10-27 01:04:33 +02:00
import java.util.ArrayList;
2013-10-26 19:25:51 +02:00
import java.util.List;
2013-12-01 06:03:40 +01:00
import mekanism.api.EnumColor;
import mekanism.api.Coord4D;
2013-10-26 19:25:51 +02:00
import mekanism.common.PacketHandler;
import mekanism.common.PacketHandler.Transmission;
import mekanism.common.inventory.container.ContainerFilter;
import mekanism.common.miner.MOreDictFilter;
2013-11-20 00:16:26 +01:00
import mekanism.common.network.PacketDigitalMinerGui;
import mekanism.common.network.PacketDigitalMinerGui.MinerGuiPacket;
2013-10-27 22:47:17 +01:00
import mekanism.common.network.PacketEditFilter;
import mekanism.common.network.PacketNewFilter;
import mekanism.common.tile.TileEntityDigitalMiner;
2013-10-26 19:25:51 +02:00
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MekanismUtils.ResourceType;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.entity.player.EntityPlayer;
2013-11-20 04:13:35 +01:00
import net.minecraft.item.ItemBlock;
2013-10-26 19:25:51 +02:00
import net.minecraft.item.ItemStack;
2013-10-27 01:04:33 +02:00
import net.minecraftforge.oredict.OreDictionary;
2013-10-26 19:25:51 +02:00
2013-10-27 01:04:33 +02:00
import org.lwjgl.input.Keyboard;
2013-10-26 19:25:51 +02:00
import org.lwjgl.opengl.GL11;
public class GuiMOreDictFilter extends GuiMekanism
{
public TileEntityDigitalMiner tileEntity;
public boolean isNew = false;
public MOreDictFilter origFilter;
public MOreDictFilter filter = new MOreDictFilter();
2013-10-26 19:25:51 +02:00
private GuiTextField oreDictText;
2013-10-27 01:04:33 +02:00
public ItemStack renderStack;
2013-10-27 01:04:33 +02:00
public int ticker = 0;
2013-10-26 19:25:51 +02:00
public int stackSwitch = 0;
2013-10-26 19:25:51 +02:00
public int stackIndex = 0;
2013-10-26 19:25:51 +02:00
public List<ItemStack> iterStacks;
2013-10-26 19:25:51 +02:00
public String status = EnumColor.DARK_GREEN + "All OK";
public GuiMOreDictFilter(EntityPlayer player, TileEntityDigitalMiner tentity, int index)
2013-10-27 01:04:33 +02:00
{
super(new ContainerFilter(player.inventory, tentity));
2013-10-27 01:04:33 +02:00
tileEntity = tentity;
origFilter = (MOreDictFilter)tileEntity.filters.get(index);
filter = ((MOreDictFilter)tentity.filters.get(index)).clone();
2013-10-27 23:24:16 +01:00
updateStackList(filter.oreDictName);
2013-10-27 01:04:33 +02:00
}
public GuiMOreDictFilter(EntityPlayer player, TileEntityDigitalMiner tentity)
2013-10-26 19:25:51 +02:00
{
super(new ContainerFilter(player.inventory, tentity));
2013-10-26 19:25:51 +02:00
tileEntity = tentity;
isNew = true;
2013-10-26 19:25:51 +02:00
}
2013-10-26 19:25:51 +02:00
@Override
public void initGui()
{
super.initGui();
int guiWidth = (width - xSize) / 2;
int guiHeight = (height - ySize) / 2;
2013-10-26 19:25:51 +02:00
buttonList.clear();
buttonList.add(new GuiButton(0, guiWidth + 27, guiHeight + 62, 60, 20, "Save"));
buttonList.add(new GuiButton(1, guiWidth + 89, guiHeight + 62, 60, 20, "Delete"));
if(isNew)
{
((GuiButton)buttonList.get(1)).enabled = false;
}
oreDictText = new GuiTextField(fontRenderer, guiWidth + 35, guiHeight + 47, 95, 12);
2013-10-26 19:25:51 +02:00
oreDictText.setMaxStringLength(12);
oreDictText.setFocused(true);
}
2013-10-26 19:25:51 +02:00
@Override
public void keyTyped(char c, int i)
{
2013-10-27 23:24:16 +01:00
if(!oreDictText.isFocused() || i == Keyboard.KEY_ESCAPE)
2013-10-26 19:25:51 +02:00
{
2013-10-27 23:24:16 +01:00
super.keyTyped(c, i);
2013-10-26 19:25:51 +02:00
}
2013-10-27 01:04:33 +02:00
if(oreDictText.isFocused() && i == Keyboard.KEY_RETURN)
{
setOreDictKey();
return;
}
2013-11-08 13:53:32 +01:00
if(Character.isLetter(c) || Character.isDigit(c) || c == '*' || i == Keyboard.KEY_BACK || i == Keyboard.KEY_DELETE || i == Keyboard.KEY_LEFT || i == Keyboard.KEY_RIGHT)
2013-10-26 19:25:51 +02:00
{
oreDictText.textboxKeyTyped(c, i);
}
}
2013-10-26 19:25:51 +02:00
@Override
protected void actionPerformed(GuiButton guibutton)
{
super.actionPerformed(guibutton);
2013-10-26 19:25:51 +02:00
if(guibutton.id == 0)
{
2013-10-27 23:24:16 +01:00
if(!oreDictText.getText().isEmpty())
{
setOreDictKey();
}
if(filter.oreDictName != null && !filter.oreDictName.isEmpty())
{
2013-10-27 22:47:17 +01:00
if(isNew)
{
PacketHandler.sendPacket(Transmission.SERVER, new PacketNewFilter().setParams(Coord4D.get(tileEntity), filter));
2013-10-27 22:47:17 +01:00
}
else {
PacketHandler.sendPacket(Transmission.SERVER, new PacketEditFilter().setParams(Coord4D.get(tileEntity), false, origFilter, filter));
2013-10-27 22:47:17 +01:00
}
PacketHandler.sendPacket(Transmission.SERVER, new PacketDigitalMinerGui().setParams(MinerGuiPacket.SERVER, Coord4D.get(tileEntity), 0));
}
else {
status = EnumColor.DARK_RED + "No key";
ticker = 20;
}
2013-10-26 19:25:51 +02:00
}
2013-10-27 22:47:17 +01:00
else if(guibutton.id == 1)
{
PacketHandler.sendPacket(Transmission.SERVER, new PacketEditFilter().setParams(Coord4D.get(tileEntity), true, origFilter));
PacketHandler.sendPacket(Transmission.SERVER, new PacketDigitalMinerGui().setParams(MinerGuiPacket.SERVER, Coord4D.get(tileEntity), 0));
2013-10-27 22:47:17 +01:00
}
2013-10-26 19:25:51 +02:00
}
2013-10-26 19:25:51 +02:00
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
{
2013-10-26 19:25:51 +02:00
int xAxis = (mouseX - (width - xSize) / 2);
int yAxis = (mouseY - (height - ySize) / 2);
fontRenderer.drawString((isNew ? "New" : "Edit") + " OreDict Filter", 43, 6, 0x404040);
2013-10-26 19:25:51 +02:00
fontRenderer.drawString("Status: " + status, 35, 20, 0x00CD00);
fontRenderer.drawString("Key: " + filter.oreDictName, 35, 32, 0x00CD00);
2013-10-27 01:04:33 +02:00
if(renderStack != null)
2013-10-26 19:25:51 +02:00
{
try {
GL11.glPushMatrix();
GL11.glEnable(GL11.GL_LIGHTING);
itemRenderer.renderItemAndEffectIntoGUI(fontRenderer, mc.getTextureManager(), renderStack, 12, 19);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glPopMatrix();
} catch(Exception e) {}
2013-10-27 01:04:33 +02:00
}
2013-10-26 19:25:51 +02:00
super.drawGuiContainerForegroundLayer(mouseX, mouseY);
}
2013-10-26 19:25:51 +02:00
@Override
protected void drawGuiContainerBackgroundLayer(float partialTick, int mouseX, int mouseY)
{
2013-10-26 19:25:51 +02:00
super.drawGuiContainerBackgroundLayer(partialTick, mouseX, mouseY);
mc.renderEngine.bindTexture(MekanismUtils.getResource(ResourceType.GUI, "GuiMOreDictFilter.png"));
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
int guiWidth = (width - xSize) / 2;
int guiHeight = (height - ySize) / 2;
drawTexturedModalRect(guiWidth, guiHeight, 0, 0, xSize, ySize);
2013-10-26 19:25:51 +02:00
int xAxis = (mouseX - (width - xSize) / 2);
int yAxis = (mouseY - (height - ySize) / 2);
2013-10-26 19:25:51 +02:00
if(xAxis >= 5 && xAxis <= 16 && yAxis >= 5 && yAxis <= 16)
{
drawTexturedModalRect(guiWidth + 5, guiHeight + 5, 176, 0, 11, 11);
}
else {
drawTexturedModalRect(guiWidth + 5, guiHeight + 5, 176, 11, 11, 11);
}
if(xAxis >= 131 && xAxis <= 143 && yAxis >= 47 && yAxis <= 59)
2013-10-26 19:25:51 +02:00
{
drawTexturedModalRect(guiWidth + 131, guiHeight + 47, 176 + 11, 0, 12, 12);
2013-10-26 19:25:51 +02:00
}
else {
drawTexturedModalRect(guiWidth + 131, guiHeight + 47, 176 + 11, 12, 12, 12);
2013-10-26 19:25:51 +02:00
}
oreDictText.drawTextBox();
}
2013-10-26 19:25:51 +02:00
@Override
public void updateScreen()
{
super.updateScreen();
2013-10-27 01:04:33 +02:00
oreDictText.updateCursorCounter();
2013-10-27 01:04:33 +02:00
if(ticker > 0)
{
ticker--;
}
else {
status = EnumColor.DARK_GREEN + "All OK";
}
2013-10-27 01:04:33 +02:00
if(stackSwitch > 0)
{
stackSwitch--;
}
2013-10-27 01:04:33 +02:00
if(stackSwitch == 0 && iterStacks != null && iterStacks.size() > 0)
{
stackSwitch = 20;
2013-10-27 01:04:33 +02:00
if(stackIndex == -1 || stackIndex == iterStacks.size()-1)
{
stackIndex = 0;
}
else if(stackIndex < iterStacks.size()-1)
{
stackIndex++;
}
2013-10-27 01:04:33 +02:00
renderStack = iterStacks.get(stackIndex);
}
else if(iterStacks != null && iterStacks.size() == 0)
{
renderStack = null;
}
2013-10-26 19:25:51 +02:00
}
@Override
protected void mouseClicked(int mouseX, int mouseY, int button)
{
super.mouseClicked(mouseX, mouseY, button);
oreDictText.mouseClicked(mouseX, mouseY, button);
if(button == 0)
2013-10-26 19:25:51 +02:00
{
int xAxis = (mouseX - (width - xSize) / 2);
int yAxis = (mouseY - (height - ySize) / 2);
2013-10-26 19:25:51 +02:00
if(xAxis >= 5 && xAxis <= 16 && yAxis >= 5 && yAxis <= 16)
{
mc.sndManager.playSoundFX("random.click", 1.0F, 1.0F);
PacketHandler.sendPacket(Transmission.SERVER, new PacketDigitalMinerGui().setParams(MinerGuiPacket.SERVER, Coord4D.get(tileEntity), 0));
2013-10-26 19:25:51 +02:00
}
if(xAxis >= 131 && xAxis <= 143 && yAxis >= 47 && yAxis <= 59)
2013-10-27 04:46:39 +01:00
{
mc.sndManager.playSoundFX("random.click", 1.0F, 1.0F);
setOreDictKey();
}
2013-10-26 19:25:51 +02:00
}
}
private void updateStackList(String oreName)
{
if(iterStacks == null)
{
iterStacks = new ArrayList<ItemStack>();
}
else {
iterStacks.clear();
}
List<String> keys = new ArrayList<String>();
for(String s : OreDictionary.getOreNames())
{
if(oreName.equals(s) || oreName.equals("*"))
{
keys.add(s);
}
else if(oreName.endsWith("*") && !oreName.startsWith("*"))
{
if(s.startsWith(oreName.substring(0, oreName.length()-1)))
{
keys.add(s);
}
}
else if(oreName.startsWith("*") && !oreName.endsWith("*"))
{
if(s.endsWith(oreName.substring(1)))
{
keys.add(s);
}
}
else if(oreName.startsWith("*") && oreName.endsWith("*"))
{
if(s.contains(oreName.substring(1, oreName.length()-1)))
{
keys.add(s);
}
}
}
for(String key : keys)
{
for(ItemStack stack : OreDictionary.getOres(key))
{
ItemStack toAdd = stack.copy();
if(!iterStacks.contains(stack) && toAdd.getItem() instanceof ItemBlock)
{
iterStacks.add(stack.copy());
}
}
}
stackSwitch = 0;
stackIndex = -1;
}
private void setOreDictKey()
{
String oreName = oreDictText.getText();
if(oreName == null || oreName.isEmpty())
{
status = EnumColor.DARK_RED + "No key entered";
return;
}
else if(oreName.equals(filter.oreDictName))
{
status = EnumColor.DARK_RED + "Same key";
return;
}
updateStackList(oreName);
filter.oreDictName = oreName;
oreDictText.setText("");
}
}