abstracted out side buttons (thanks to this they also work on ctrls now)

This commit is contained in:
Raoul Van den Berge
2015-12-20 16:43:56 +01:00
parent d57618f990
commit 23a9b358bf
18 changed files with 322 additions and 158 deletions

View File

@@ -1,7 +1,9 @@
package storagecraft.gui; package storagecraft.gui;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
@@ -12,8 +14,17 @@ import net.minecraft.util.StatCollector;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12; import org.lwjgl.opengl.GL12;
import storagecraft.StorageCraft; import storagecraft.StorageCraft;
import storagecraft.gui.sidebutton.SideButton;
public abstract class GuiBase extends GuiContainer { public abstract class GuiBase extends GuiContainer {
public static final int SIDE_BUTTON_WIDTH = 20;
public static final int SIDE_BUTTON_HEIGHT = 20;
private List<SideButton> sideButtons = new ArrayList<SideButton>();
private int lastButtonId = 0;
private int lastSideButtonY = 6;
public GuiBase(Container container, int w, int h) { public GuiBase(Container container, int w, int h) {
super(container); super(container);
@@ -46,26 +57,70 @@ public abstract class GuiBase extends GuiContainer {
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
drawForeground(mouseX - guiLeft, mouseY - guiTop); mouseX -= guiLeft;
mouseY -= guiTop;
for (SideButton sideButton : sideButtons) {
sideButton.draw(this, sideButton.getX() + 2, sideButton.getY() + 1);
if (inBounds(sideButton.getX(), sideButton.getY(), SIDE_BUTTON_WIDTH, SIDE_BUTTON_HEIGHT, mouseX, mouseY)) {
drawTooltip(mouseX, mouseY, sideButton.getTooltip(this));
}
}
drawForeground(mouseX, mouseY);
} }
protected boolean inBounds(int x, int y, int w, int h, int ox, int oy) { @Override
protected void actionPerformed(GuiButton button) {
super.actionPerformed(button);
for (SideButton sideButton : sideButtons) {
if (sideButton.getId() == button.id) {
sideButton.actionPerformed();
}
}
}
public GuiButton addButton(int x, int y, int w, int h) {
return addButton(x, y, w, h, "");
}
public GuiButton addButton(int x, int y, int w, int h, String text) {
GuiButton button = new GuiButton(lastButtonId++, x, y, w, h, text);
buttonList.add(button);
return button;
}
public void addSideButton(SideButton button) {
button.setX(xSize - 1);
button.setY(lastSideButtonY);
button.setId(addButton(guiLeft + button.getX(), guiTop + button.getY(), SIDE_BUTTON_WIDTH, SIDE_BUTTON_HEIGHT).id);
lastSideButtonY += SIDE_BUTTON_HEIGHT + 4;
sideButtons.add(button);
}
public boolean inBounds(int x, int y, int w, int h, int ox, int oy) {
return ox >= x && ox <= x + w && oy >= y && oy <= y + h; return ox >= x && ox <= x + w && oy >= y && oy <= y + h;
} }
protected void bindTexture(String file) { public void bindTexture(String file) {
bindTexture(StorageCraft.ID, file); bindTexture(StorageCraft.ID, file);
} }
protected void bindTexture(String base, String file) { public void bindTexture(String base, String file) {
mc.getTextureManager().bindTexture(new ResourceLocation(base, "textures/" + file)); mc.getTextureManager().bindTexture(new ResourceLocation(base, "textures/" + file));
} }
protected void drawItem(int x, int y, ItemStack stack) { public void drawItem(int x, int y, ItemStack stack) {
drawItem(x, y, stack, false); drawItem(x, y, stack, false);
} }
protected void drawItem(int x, int y, ItemStack stack, boolean withOverlay) { public void drawItem(int x, int y, ItemStack stack, boolean withOverlay) {
zLevel = 100; zLevel = 100;
itemRender.zLevel = 100; itemRender.zLevel = 100;
@@ -89,16 +144,16 @@ public abstract class GuiBase extends GuiContainer {
zLevel = 0; zLevel = 0;
} }
protected void drawString(int x, int y, String message) { public void drawString(int x, int y, String message) {
drawString(x, y, message, 4210752); drawString(x, y, message, 4210752);
} }
protected void drawString(int x, int y, String message, int color) { public void drawString(int x, int y, String message, int color) {
fontRendererObj.drawString(message, x, y, color); fontRendererObj.drawString(message, x, y, color);
} }
// https://github.com/AppliedEnergistics/Applied-Energistics-2/blob/master/src/main/java/appeng/client/gui/AEBaseGui.java // https://github.com/AppliedEnergistics/Applied-Energistics-2/blob/master/src/main/java/appeng/client/gui/AEBaseGui.java
protected void drawTooltip(int x, int y, String message) { public void drawTooltip(int x, int y, String message) {
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glDisable(GL12.GL_RESCALE_NORMAL); GL11.glDisable(GL12.GL_RESCALE_NORMAL);
@@ -179,7 +234,7 @@ public abstract class GuiBase extends GuiContainer {
GL11.glPopAttrib(); GL11.glPopAttrib();
} }
protected void drawTooltip(int x, int y, ItemStack stack) { public void drawTooltip(int x, int y, ItemStack stack) {
List list = stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips); List list = stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips);
for (int i = 0; i < list.size(); ++i) { for (int i = 0; i < list.size(); ++i) {
@@ -193,7 +248,7 @@ public abstract class GuiBase extends GuiContainer {
drawTooltip(x, y, Joiner.on("\n").join(list)); drawTooltip(x, y, Joiner.on("\n").join(list));
} }
protected String t(String name, Object... format) { public String t(String name, Object... format) {
return StatCollector.translateToLocalFormatted(name, format); return StatCollector.translateToLocalFormatted(name, format);
} }

View File

@@ -1,6 +1,7 @@
package storagecraft.gui; package storagecraft.gui;
import storagecraft.container.ContainerController; import storagecraft.container.ContainerController;
import storagecraft.gui.sidebutton.SideButtonRedstoneMode;
import storagecraft.tile.TileController; import storagecraft.tile.TileController;
public class GuiController extends GuiBase { public class GuiController extends GuiBase {
@@ -14,6 +15,7 @@ public class GuiController extends GuiBase {
@Override @Override
public void init(int x, int y) { public void init(int x, int y) {
addSideButton(new SideButtonRedstoneMode(controller));
} }
@Override @Override

View File

@@ -1,11 +1,25 @@
package storagecraft.gui; package storagecraft.gui;
import storagecraft.container.ContainerDrive; import storagecraft.container.ContainerDrive;
import storagecraft.gui.sidebutton.SideButtonRedstoneMode;
import storagecraft.tile.TileDrive; import storagecraft.tile.TileDrive;
public class GuiDrive extends GuiMachine { public class GuiDrive extends GuiBase {
private TileDrive drive;
public GuiDrive(ContainerDrive container, TileDrive drive) { public GuiDrive(ContainerDrive container, TileDrive drive) {
super(container, 176, 190, drive); super(container, 176, 190);
this.drive = drive;
}
@Override
public void init(int x, int y) {
addSideButton(new SideButtonRedstoneMode(drive));
}
@Override
public void update(int x, int y) {
} }
@Override @Override
@@ -17,8 +31,6 @@ public class GuiDrive extends GuiMachine {
@Override @Override
public void drawForeground(int mouseX, int mouseY) { public void drawForeground(int mouseX, int mouseY) {
super.drawForeground(mouseX, mouseY);
drawString(7, 7, t("gui.storagecraft:drive")); drawString(7, 7, t("gui.storagecraft:drive"));
drawString(7, 96, t("container.inventory")); drawString(7, 96, t("container.inventory"));
} }

View File

@@ -3,34 +3,33 @@ package storagecraft.gui;
import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiButton;
import storagecraft.StorageCraft; import storagecraft.StorageCraft;
import storagecraft.container.ContainerExporter; import storagecraft.container.ContainerExporter;
import storagecraft.gui.sidebutton.SideButtonRedstoneMode;
import storagecraft.network.MessageExporterUpdate; import storagecraft.network.MessageExporterUpdate;
import storagecraft.tile.TileExporter; import storagecraft.tile.TileExporter;
import storagecraft.util.InventoryUtils; import storagecraft.util.InventoryUtils;
public class GuiExporter extends GuiMachine { public class GuiExporter extends GuiBase {
private TileExporter exporter; private TileExporter exporter;
private GuiButton compareNBTButton; private GuiButton compareNBTButton;
private GuiButton compareDamageButton; private GuiButton compareDamageButton;
public GuiExporter(ContainerExporter container, TileExporter exporter) { public GuiExporter(ContainerExporter container, TileExporter exporter) {
super(container, 176, 186, exporter); super(container, 176, 186);
this.exporter = exporter; this.exporter = exporter;
} }
@Override @Override
public void init(int x, int y) { public void init(int x, int y) {
super.init(x, y); addSideButton(new SideButtonRedstoneMode(exporter));
buttonList.add(compareNBTButton = new GuiButton(1, x + 7, y + 41, 100, 20, "")); compareNBTButton = addButton(x + 7, y + 41, 100, 20);
buttonList.add(compareDamageButton = new GuiButton(2, x + 7, y + 63, 120, 20, "")); compareDamageButton = addButton(x + 7, y + 63, 120, 20);
} }
@Override @Override
public void update(int x, int y) { public void update(int x, int y) {
super.update(x, y);
compareNBTButton.displayString = t("misc.storagecraft:compareNBT") + ": "; compareNBTButton.displayString = t("misc.storagecraft:compareNBT") + ": ";
compareNBTButton.displayString += t("misc.storagecraft:" + ((exporter.getCompareFlags() & InventoryUtils.COMPARE_NBT) == InventoryUtils.COMPARE_NBT ? "on" : "off")); compareNBTButton.displayString += t("misc.storagecraft:" + ((exporter.getCompareFlags() & InventoryUtils.COMPARE_NBT) == InventoryUtils.COMPARE_NBT ? "on" : "off"));
@@ -47,8 +46,6 @@ public class GuiExporter extends GuiMachine {
@Override @Override
public void drawForeground(int mouseX, int mouseY) { public void drawForeground(int mouseX, int mouseY) {
super.drawForeground(mouseX, mouseY);
drawString(7, 7, t("gui.storagecraft:exporter")); drawString(7, 7, t("gui.storagecraft:exporter"));
drawString(7, 93, t("container.inventory")); drawString(7, 93, t("container.inventory"));
} }
@@ -59,9 +56,9 @@ public class GuiExporter extends GuiMachine {
int flags = exporter.getCompareFlags(); int flags = exporter.getCompareFlags();
if (button.id == compareNBTButton.id) { if (button == compareNBTButton) {
flags ^= InventoryUtils.COMPARE_NBT; flags ^= InventoryUtils.COMPARE_NBT;
} else if (button.id == compareDamageButton.id) { } else if (button == compareDamageButton) {
flags ^= InventoryUtils.COMPARE_DAMAGE; flags ^= InventoryUtils.COMPARE_DAMAGE;
} }

View File

@@ -9,13 +9,14 @@ import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse; import org.lwjgl.input.Mouse;
import storagecraft.StorageCraft; import storagecraft.StorageCraft;
import storagecraft.container.ContainerGrid; import storagecraft.container.ContainerGrid;
import storagecraft.gui.sidebutton.SideButtonRedstoneMode;
import storagecraft.network.MessageStoragePull; import storagecraft.network.MessageStoragePull;
import storagecraft.network.MessageStoragePush; import storagecraft.network.MessageStoragePush;
import storagecraft.storage.StorageItem; import storagecraft.storage.StorageItem;
import storagecraft.tile.TileController; import storagecraft.tile.TileController;
import storagecraft.tile.TileGrid; import storagecraft.tile.TileGrid;
public class GuiGrid extends GuiMachine { public class GuiGrid extends GuiBase {
private ContainerGrid container; private ContainerGrid container;
private TileGrid grid; private TileGrid grid;
@@ -27,7 +28,7 @@ public class GuiGrid extends GuiMachine {
private int offset; private int offset;
public GuiGrid(ContainerGrid container, TileGrid grid) { public GuiGrid(ContainerGrid container, TileGrid grid) {
super(container, 176, 190, grid); super(container, 176, 190);
this.container = container; this.container = container;
this.grid = grid; this.grid = grid;
@@ -35,9 +36,9 @@ public class GuiGrid extends GuiMachine {
@Override @Override
public void init(int x, int y) { public void init(int x, int y) {
super.init(x, y); addSideButton(new SideButtonRedstoneMode(grid));
searchField = new GuiTextField(fontRendererObj, x + 80 + 2, y + 6 + 1, 88 - 6, fontRendererObj.FONT_HEIGHT); searchField = new GuiTextField(fontRendererObj, x + 80 + 1, y + 6 + 1, 88 - 6, fontRendererObj.FONT_HEIGHT);
searchField.setEnableBackgroundDrawing(false); searchField.setEnableBackgroundDrawing(false);
searchField.setVisible(true); searchField.setVisible(true);
searchField.setTextColor(16777215); searchField.setTextColor(16777215);
@@ -47,8 +48,6 @@ public class GuiGrid extends GuiMachine {
@Override @Override
public void update(int x, int y) { public void update(int x, int y) {
super.update(x, y);
int wheel = Mouse.getDWheel(); int wheel = Mouse.getDWheel();
wheel = Math.max(Math.min(-wheel, 1), -1); wheel = Math.max(Math.min(-wheel, 1), -1);
@@ -99,8 +98,6 @@ public class GuiGrid extends GuiMachine {
@Override @Override
public void drawForeground(int mouseX, int mouseY) { public void drawForeground(int mouseX, int mouseY) {
super.drawForeground(mouseX, mouseY);
drawString(7, 7, t("gui.storagecraft:grid")); drawString(7, 7, t("gui.storagecraft:grid"));
drawString(7, 96, t("container.inventory")); drawString(7, 96, t("container.inventory"));
@@ -149,6 +146,30 @@ public class GuiGrid extends GuiMachine {
} }
} }
public List<StorageItem> getItems() {
List<StorageItem> items = new ArrayList<StorageItem>();
if (!grid.isConnected()) {
return items;
}
items.addAll(grid.getController().getItems());
if (!searchField.getText().trim().isEmpty()) {
Iterator<StorageItem> t = items.iterator();
while (t.hasNext()) {
StorageItem item = t.next();
if (!item.toItemStack().getDisplayName().toLowerCase().contains(searchField.getText().toLowerCase())) {
t.remove();
}
}
}
return items;
}
@Override @Override
public void mouseClicked(int mouseX, int mouseY, int clickedButton) { public void mouseClicked(int mouseX, int mouseY, int clickedButton) {
super.mouseClicked(mouseX, mouseY, clickedButton); super.mouseClicked(mouseX, mouseY, clickedButton);
@@ -181,28 +202,4 @@ public class GuiGrid extends GuiMachine {
super.keyTyped(character, keyCode); super.keyTyped(character, keyCode);
} }
} }
public List<StorageItem> getItems() {
List<StorageItem> items = new ArrayList<StorageItem>();
if (!grid.isConnected()) {
return items;
}
items.addAll(grid.getController().getItems());
if (!searchField.getText().trim().isEmpty()) {
Iterator<StorageItem> t = items.iterator();
while (t.hasNext()) {
StorageItem item = t.next();
if (!item.toItemStack().getDisplayName().toLowerCase().contains(searchField.getText().toLowerCase())) {
t.remove();
}
}
}
return items;
}
} }

View File

@@ -3,11 +3,12 @@ package storagecraft.gui;
import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiButton;
import storagecraft.StorageCraft; import storagecraft.StorageCraft;
import storagecraft.container.ContainerImporter; import storagecraft.container.ContainerImporter;
import storagecraft.gui.sidebutton.SideButtonRedstoneMode;
import storagecraft.network.MessageImporterUpdate; import storagecraft.network.MessageImporterUpdate;
import storagecraft.tile.TileImporter; import storagecraft.tile.TileImporter;
import storagecraft.util.InventoryUtils; import storagecraft.util.InventoryUtils;
public class GuiImporter extends GuiMachine { public class GuiImporter extends GuiBase {
private TileImporter importer; private TileImporter importer;
private GuiButton compareNBTButton; private GuiButton compareNBTButton;
@@ -15,24 +16,22 @@ public class GuiImporter extends GuiMachine {
private GuiButton modeButton; private GuiButton modeButton;
public GuiImporter(ContainerImporter container, TileImporter importer) { public GuiImporter(ContainerImporter container, TileImporter importer) {
super(container, 176, 201, importer); super(container, 176, 201);
this.importer = importer; this.importer = importer;
} }
@Override @Override
public void init(int x, int y) { public void init(int x, int y) {
super.init(x, y); addSideButton(new SideButtonRedstoneMode(importer));
buttonList.add(compareNBTButton = new GuiButton(1, x + 7, y + 41, 100, 20, "")); compareNBTButton = addButton(x + 7, y + 41, 100, 20);
buttonList.add(compareDamageButton = new GuiButton(2, x + 7, y + 63, 120, 20, "")); compareDamageButton = addButton(x + 7, y + 63, 120, 20);
buttonList.add(modeButton = new GuiButton(3, x + 7, y + 85, 80, 20, "")); modeButton = addButton(x + 7, y + 85, 80, 20);
} }
@Override @Override
public void update(int x, int y) { public void update(int x, int y) {
super.update(x, y);
compareNBTButton.displayString = t("misc.storagecraft:compareNBT") + ": "; compareNBTButton.displayString = t("misc.storagecraft:compareNBT") + ": ";
compareNBTButton.displayString += t("misc.storagecraft:" + ((importer.getCompareFlags() & InventoryUtils.COMPARE_NBT) == InventoryUtils.COMPARE_NBT ? "on" : "off")); compareNBTButton.displayString += t("misc.storagecraft:" + ((importer.getCompareFlags() & InventoryUtils.COMPARE_NBT) == InventoryUtils.COMPARE_NBT ? "on" : "off"));
@@ -51,8 +50,6 @@ public class GuiImporter extends GuiMachine {
@Override @Override
public void drawForeground(int mouseX, int mouseY) { public void drawForeground(int mouseX, int mouseY) {
super.drawForeground(mouseX, mouseY);
drawString(7, 7, t("gui.storagecraft:importer")); drawString(7, 7, t("gui.storagecraft:importer"));
drawString(7, 108, t("container.inventory")); drawString(7, 108, t("container.inventory"));
} }
@@ -63,9 +60,9 @@ public class GuiImporter extends GuiMachine {
int flags = importer.getCompareFlags(); int flags = importer.getCompareFlags();
if (button.id == compareNBTButton.id) { if (button == compareNBTButton) {
flags ^= InventoryUtils.COMPARE_NBT; flags ^= InventoryUtils.COMPARE_NBT;
} else if (button.id == compareDamageButton.id) { } else if (button == compareDamageButton) {
flags ^= InventoryUtils.COMPARE_DAMAGE; flags ^= InventoryUtils.COMPARE_DAMAGE;
} }

View File

@@ -1,54 +0,0 @@
package storagecraft.gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import storagecraft.StorageCraft;
import storagecraft.network.MessageRedstoneModeUpdate;
import storagecraft.tile.TileMachine;
public abstract class GuiMachine extends GuiBase {
private TileMachine machine;
public static final ItemStack REDSTONE_MODE_ITEM = new ItemStack(Items.redstone, 1);
private int redstoneModeX;
private int redstoneModeY = 6;
private int redstoneModeWidth = 20;
private int redstoneModeHeight = 20;
public GuiMachine(Container container, int w, int h, TileMachine machine) {
super(container, w, h);
this.redstoneModeX = w - 1;
this.machine = machine;
}
@Override
public void init(int x, int y) {
buttonList.add(new GuiButton(0, x + redstoneModeX, y + redstoneModeY, redstoneModeWidth, redstoneModeHeight, ""));
}
@Override
public void update(int x, int y) {
}
@Override
public void drawForeground(int mouseX, int mouseY) {
drawItem(redstoneModeX + 2, redstoneModeY + 1, REDSTONE_MODE_ITEM);
if (inBounds(redstoneModeX, redstoneModeY, redstoneModeWidth, redstoneModeHeight, mouseX, mouseY)) {
drawTooltip(mouseX, mouseY, t("misc.storagecraft:redstoneMode." + machine.getRedstoneMode().id));
}
}
@Override
protected void actionPerformed(GuiButton button) {
super.actionPerformed(button);
if (button.id == 0) {
StorageCraft.NETWORK.sendToServer(new MessageRedstoneModeUpdate(machine.xCoord, machine.yCoord, machine.zCoord));
}
}
}

View File

@@ -1,11 +1,25 @@
package storagecraft.gui; package storagecraft.gui;
import storagecraft.container.ContainerStorageProxy; import storagecraft.container.ContainerStorageProxy;
import storagecraft.gui.sidebutton.SideButtonRedstoneMode;
import storagecraft.tile.TileStorageProxy; import storagecraft.tile.TileStorageProxy;
public class GuiStorageProxy extends GuiMachine { public class GuiStorageProxy extends GuiBase {
private TileStorageProxy storageProxy;
public GuiStorageProxy(ContainerStorageProxy container, TileStorageProxy storageProxy) { public GuiStorageProxy(ContainerStorageProxy container, TileStorageProxy storageProxy) {
super(container, 176, 131, storageProxy); super(container, 176, 131);
this.storageProxy = storageProxy;
}
@Override
public void init(int x, int y) {
addSideButton(new SideButtonRedstoneMode(storageProxy));
}
@Override
public void update(int x, int y) {
} }
@Override @Override
@@ -17,8 +31,6 @@ public class GuiStorageProxy extends GuiMachine {
@Override @Override
public void drawForeground(int mouseX, int mouseY) { public void drawForeground(int mouseX, int mouseY) {
super.drawForeground(mouseX, mouseY);
drawString(7, 7, t("gui.storagecraft:storageProxy")); drawString(7, 7, t("gui.storagecraft:storageProxy"));
drawString(7, 39, t("container.inventory")); drawString(7, 39, t("container.inventory"));
} }

View File

@@ -0,0 +1,39 @@
package storagecraft.gui.sidebutton;
import storagecraft.gui.GuiBase;
public abstract class SideButton {
private int id;
private int x;
private int y;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public abstract String getTooltip(GuiBase gui);
public abstract void draw(GuiBase gui, int x, int y);
public abstract void actionPerformed();
}

View File

@@ -0,0 +1,31 @@
package storagecraft.gui.sidebutton;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import storagecraft.StorageCraft;
import storagecraft.gui.GuiBase;
import storagecraft.network.MessageRedstoneModeUpdate;
import storagecraft.tile.IRedstoneControllable;
public class SideButtonRedstoneMode extends SideButton {
private IRedstoneControllable control;
public SideButtonRedstoneMode(IRedstoneControllable control) {
this.control = control;
}
@Override
public String getTooltip(GuiBase gui) {
return gui.t("misc.storagecraft:redstoneMode." + control.getRedstoneMode().id);
}
@Override
public void draw(GuiBase gui, int x, int y) {
gui.drawItem(x, y, new ItemStack(Items.redstone, 1));
}
@Override
public void actionPerformed() {
StorageCraft.NETWORK.sendToServer(new MessageRedstoneModeUpdate(control));
}
}

View File

@@ -6,7 +6,7 @@ import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import storagecraft.tile.TileMachine; import storagecraft.tile.IRedstoneControllable;
public class MessageRedstoneModeUpdate implements IMessage, IMessageHandler<MessageRedstoneModeUpdate, IMessage> { public class MessageRedstoneModeUpdate implements IMessage, IMessageHandler<MessageRedstoneModeUpdate, IMessage> {
private int x; private int x;
@@ -16,10 +16,10 @@ public class MessageRedstoneModeUpdate implements IMessage, IMessageHandler<Mess
public MessageRedstoneModeUpdate() { public MessageRedstoneModeUpdate() {
} }
public MessageRedstoneModeUpdate(int x, int y, int z) { public MessageRedstoneModeUpdate(IRedstoneControllable control) {
this.x = x; this.x = control.getX();
this.y = y; this.y = control.getY();
this.z = z; this.z = control.getZ();
} }
@Override @Override
@@ -42,10 +42,10 @@ public class MessageRedstoneModeUpdate implements IMessage, IMessageHandler<Mess
TileEntity tile = player.worldObj.getTileEntity(message.x, message.y, message.z); TileEntity tile = player.worldObj.getTileEntity(message.x, message.y, message.z);
if (tile instanceof TileMachine) { if (tile instanceof IRedstoneControllable) {
TileMachine machine = (TileMachine) tile; IRedstoneControllable control = (IRedstoneControllable) tile;
machine.setRedstoneMode(machine.getRedstoneMode().next()); control.setRedstoneMode(control.getRedstoneMode().next());
} }
return null; return null;

View File

@@ -6,4 +6,10 @@ public interface INetworkTile {
public void fromBytes(ByteBuf buf); public void fromBytes(ByteBuf buf);
public void toBytes(ByteBuf buf); public void toBytes(ByteBuf buf);
public int getX();
public int getY();
public int getZ();
} }

View File

@@ -0,0 +1,13 @@
package storagecraft.tile;
public interface IRedstoneControllable {
public RedstoneMode getRedstoneMode();
public void setRedstoneMode(RedstoneMode mode);
public int getX();
public int getY();
public int getZ();
}

View File

@@ -1,10 +1,14 @@
package storagecraft.tile; package storagecraft.tile;
import net.minecraft.world.World;
public enum RedstoneMode { public enum RedstoneMode {
IGNORE(0), IGNORE(0),
HIGH(1), HIGH(1),
LOW(2); LOW(2);
public static final String NBT = "RedstoneMode";
public final int id; public final int id;
RedstoneMode(int id) { RedstoneMode(int id) {
@@ -21,6 +25,19 @@ public enum RedstoneMode {
return next; return next;
} }
public boolean isEnabled(World world, int x, int y, int z) {
switch (this) {
case IGNORE:
return true;
case HIGH:
return world.isBlockIndirectlyGettingPowered(x, y, z);
case LOW:
return !world.isBlockIndirectlyGettingPowered(x, y, z);
}
return false;
}
public static RedstoneMode getById(int id) { public static RedstoneMode getById(int id) {
for (RedstoneMode control : values()) { for (RedstoneMode control : values()) {
if (control.id == id) { if (control.id == id) {

View File

@@ -69,7 +69,7 @@ public class TileCable extends TileBase {
TileEntity tile = worldObj.getTileEntity(x, y, z); TileEntity tile = worldObj.getTileEntity(x, y, z);
if (tile instanceof TileMachine && ((TileMachine) tile).isEnabled()) { if (tile instanceof TileMachine && ((TileMachine) tile).getRedstoneMode().isEnabled(worldObj, x, y, z)) {
machines.add((TileMachine) tile); machines.add((TileMachine) tile);
visited.add(Vec3.createVectorHelper(x, y, z)); visited.add(Vec3.createVectorHelper(x, y, z));

View File

@@ -17,10 +17,12 @@ import storagecraft.storage.IStorageProvider;
import storagecraft.storage.StorageItem; import storagecraft.storage.StorageItem;
import storagecraft.util.InventoryUtils; import storagecraft.util.InventoryUtils;
public class TileController extends TileBase implements IEnergyReceiver, INetworkTile { public class TileController extends TileBase implements IEnergyReceiver, INetworkTile, IRedstoneControllable {
private List<StorageItem> items = new ArrayList<StorageItem>(); private List<StorageItem> items = new ArrayList<StorageItem>();
private List<IStorage> storages = new ArrayList<IStorage>(); private List<IStorage> storages = new ArrayList<IStorage>();
private RedstoneMode redstoneMode = RedstoneMode.IGNORE;
private List<TileMachine> machines = new ArrayList<TileMachine>(); private List<TileMachine> machines = new ArrayList<TileMachine>();
private List<Vec3> visitedCables = new ArrayList<Vec3>(); private List<Vec3> visitedCables = new ArrayList<Vec3>();
@@ -223,6 +225,10 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
super.readFromNBT(nbt); super.readFromNBT(nbt);
energy.readFromNBT(nbt); energy.readFromNBT(nbt);
if (nbt.hasKey(RedstoneMode.NBT)) {
redstoneMode = RedstoneMode.getById(nbt.getInteger(RedstoneMode.NBT));
}
} }
@Override @Override
@@ -230,6 +236,8 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
super.writeToNBT(nbt); super.writeToNBT(nbt);
energy.writeToNBT(nbt); energy.writeToNBT(nbt);
nbt.setInteger(RedstoneMode.NBT, redstoneMode.id);
} }
@Override @Override
@@ -257,14 +265,42 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
} }
public boolean isActive() { public boolean isActive() {
return energy.getEnergyStored() >= getEnergyUsage(); return energy.getEnergyStored() >= getEnergyUsage() && redstoneMode.isEnabled(worldObj, xCoord, yCoord, zCoord);
} }
@Override
public RedstoneMode getRedstoneMode() {
return redstoneMode;
}
@Override
public void setRedstoneMode(RedstoneMode mode) {
this.redstoneMode = mode;
}
@Override
public int getX() {
return xCoord;
}
@Override
public int getY() {
return yCoord;
}
@Override
public int getZ() {
return zCoord;
}
// @TODO: add helpers for sending redstone control + item storages over net
@Override @Override
public void fromBytes(ByteBuf buf) { public void fromBytes(ByteBuf buf) {
energy.setEnergyStored(buf.readInt()); energy.setEnergyStored(buf.readInt());
energyUsage = buf.readInt(); energyUsage = buf.readInt();
redstoneMode = RedstoneMode.getById(buf.readInt());
items.clear(); items.clear();
int size = buf.readInt(); int size = buf.readInt();
@@ -285,6 +321,8 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
buf.writeInt(energy.getEnergyStored()); buf.writeInt(energy.getEnergyStored());
buf.writeInt(energyUsage); buf.writeInt(energyUsage);
buf.writeInt(redstoneMode.id);
buf.writeInt(items.size()); buf.writeInt(items.size());
for (StorageItem item : items) { for (StorageItem item : items) {

View File

@@ -3,9 +3,7 @@ package storagecraft.tile;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
public abstract class TileMachine extends TileBase implements INetworkTile { public abstract class TileMachine extends TileBase implements INetworkTile, IRedstoneControllable {
public static final String NBT_REDSTONE_MODE = "RedstoneMode";
protected boolean connected = false; protected boolean connected = false;
private RedstoneMode redstoneMode = RedstoneMode.LOW; private RedstoneMode redstoneMode = RedstoneMode.LOW;
@@ -43,27 +41,31 @@ public abstract class TileMachine extends TileBase implements INetworkTile {
return connected; return connected;
} }
public boolean isEnabled() { @Override
switch (redstoneMode) {
case IGNORE:
return true;
case HIGH:
return worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
case LOW:
return !worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
}
return false;
}
public RedstoneMode getRedstoneMode() { public RedstoneMode getRedstoneMode() {
return redstoneMode; return redstoneMode;
} }
@Override
public void setRedstoneMode(RedstoneMode mode) { public void setRedstoneMode(RedstoneMode mode) {
this.redstoneMode = mode; this.redstoneMode = mode;
} }
@Override
public int getX() {
return xCoord;
}
@Override
public int getY() {
return yCoord;
}
@Override
public int getZ() {
return zCoord;
}
public TileController getController() { public TileController getController() {
return (TileController) worldObj.getTileEntity(xController, yController, zController); return (TileController) worldObj.getTileEntity(xController, yController, zController);
} }
@@ -98,8 +100,8 @@ public abstract class TileMachine extends TileBase implements INetworkTile {
public void readFromNBT(NBTTagCompound nbt) { public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt); super.readFromNBT(nbt);
if (nbt.hasKey(NBT_REDSTONE_MODE)) { if (nbt.hasKey(RedstoneMode.NBT)) {
redstoneMode = RedstoneMode.getById(nbt.getInteger(NBT_REDSTONE_MODE)); redstoneMode = RedstoneMode.getById(nbt.getInteger(RedstoneMode.NBT));
} }
} }
@@ -107,7 +109,7 @@ public abstract class TileMachine extends TileBase implements INetworkTile {
public void writeToNBT(NBTTagCompound nbt) { public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt); super.writeToNBT(nbt);
nbt.setInteger(NBT_REDSTONE_MODE, redstoneMode.id); nbt.setInteger(RedstoneMode.NBT, redstoneMode.id);
} }
public abstract int getEnergyUsage(); public abstract int getEnergyUsage();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB