Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Match NBT/ Ignore NBT", "Match Meta/Ignore Meta" options in Item Loader and Unloader #59

Merged
merged 9 commits into from
Jul 13, 2024
36 changes: 27 additions & 9 deletions src/main/java/mods/railcraft/client/gui/GuiLoaderItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import net.minecraft.entity.player.InventoryPlayer;

import mods.railcraft.client.gui.buttons.GuiMultiButton;
import mods.railcraft.common.blocks.RailcraftTileEntity;
import mods.railcraft.client.gui.buttons.GuiOverlayMultiButton;
import mods.railcraft.common.blocks.machine.gamma.TileLoaderItemBase;
import mods.railcraft.common.blocks.machine.gamma.TileLoaderItemBase.EnumRedstoneMode;
import mods.railcraft.common.blocks.machine.gamma.TileLoaderItemBase.EnumTransferMode;
import mods.railcraft.common.blocks.machine.gamma.TileLoaderItemBase.MatchMetadataMode;
import mods.railcraft.common.blocks.machine.gamma.TileLoaderItemBase.MatchNBTMode;
import mods.railcraft.common.core.RailcraftConstants;
import mods.railcraft.common.gui.containers.ContainerItemLoader;
import mods.railcraft.common.plugins.forge.LocalizationPlugin;
Expand All @@ -21,16 +25,20 @@ public class GuiLoaderItem extends TileGui {
private final String FILTER_LABEL = LocalizationPlugin.translate("railcraft.gui.filters");
private final String CART_FILTER_LABEL = LocalizationPlugin.translate("railcraft.gui.filters.carts");
private final String BUFFER_LABEL = LocalizationPlugin.translate("railcraft.gui.item.loader.buffer");
private GuiMultiButton transferMode;
private GuiMultiButton redstoneMode;
private GuiMultiButton<EnumTransferMode> transferMode;
private GuiMultiButton<EnumRedstoneMode> redstoneMode;
private GuiMultiButton<MatchMetadataMode> matchMetadataMode;
private GuiMultiButton<MatchNBTMode> matchNBTMode;
private final TileLoaderItemBase tile;

public GuiLoaderItem(InventoryPlayer inv, TileLoaderItemBase tile) {
super(
(RailcraftTileEntity) tile,
tile,
new ContainerItemLoader(inv, tile),
RailcraftConstants.GUI_TEXTURE_FOLDER + "gui_item_loader.png");
RailcraftConstants.GUI_TEXTURE_FOLDER + "new/gui_item_loader.png");
this.tile = tile;

ySize = 182;
}

@Override
Expand All @@ -40,10 +48,18 @@ public void initGui() {
buttonList.clear();
int w = (width - xSize) / 2;
int h = (height - ySize) / 2;
buttonList
.add(transferMode = new GuiMultiButton(0, w + 62, h + 45, 52, tile.getTransferModeController().copy()));
buttonList
.add(redstoneMode = new GuiMultiButton(0, w + 62, h + 62, 52, tile.getRedstoneModeController().copy()));
buttonList.add(
transferMode = new GuiMultiButton<>(0, w + 62, h + 45, 52, tile.getTransferModeController().copy()));
buttonList.add(
redstoneMode = new GuiMultiButton<>(0, w + 62, h + 62, 52, tile.getRedstoneModeController().copy()));
buttonList.add(
matchMetadataMode = new GuiOverlayMultiButton<>(
0,
w + 8,
h + 81,
tile.getMatchMetadataController().copy()));
buttonList.add(
matchNBTMode = new GuiOverlayMultiButton<>(0, w + 8 + 16, h + 81, tile.getMatchNbtController().copy()));
}

@Override
Expand All @@ -59,6 +75,8 @@ public void onGuiClosed() {
if (Game.isNotHost(tile.getWorld())) {
tile.getTransferModeController().setCurrentState(transferMode.getController().getCurrentState());
tile.getRedstoneModeController().setCurrentState(redstoneMode.getController().getCurrentState());
tile.getMatchMetadataController().setCurrentState(matchMetadataMode.getController().getCurrentState());
tile.getMatchNbtController().setCurrentState(matchNBTMode.getController().getCurrentState());
PacketBuilder.instance().sendGuiReturnPacket(tile);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
* @author CovertJaguar <http://railcraft.info/wiki/info:license>
*/
@SideOnly(Side.CLIENT)
public class GuiMultiButton extends GuiBetterButton {
public class GuiMultiButton<T extends IMultiButtonState> extends GuiBetterButton {

private final MultiButtonController control;
private final MultiButtonController<? extends T> control;
public boolean canChange = true;

public GuiMultiButton(int id, int x, int y, int width, MultiButtonController control) {
Expand Down Expand Up @@ -104,7 +104,7 @@ public boolean mousePressed(Minecraft minecraft, int mouseX, int mouseY) {
return pressed;
}

public MultiButtonController getController() {
public MultiButtonController<? extends T> getController() {
return control;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mods.railcraft.client.gui.buttons;

import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import mods.railcraft.common.core.RailcraftConstants;
import mods.railcraft.common.gui.buttons.IButtonTextureSet;
import mods.railcraft.common.gui.buttons.IOverlayMultiButtonState;
import mods.railcraft.common.gui.buttons.MultiButtonController;

public class GuiOverlayMultiButton<T extends IOverlayMultiButtonState> extends GuiMultiButton<T> {

private static final ResourceLocation WIDGETS = new ResourceLocation(
RailcraftConstants.GUI_TEXTURE_FOLDER + "new/widgets.png");

public GuiOverlayMultiButton(int id, int x, int y, MultiButtonController<? extends T> control) {
super(id, x, y, 16, control);
}

@Override
protected void bindButtonTextures(Minecraft minecraft) {
minecraft.renderEngine.bindTexture(WIDGETS);
}

@Override
public void drawButton(Minecraft minecraft, int x, int y) {
super.drawButton(minecraft, x, y);

if (!visible) {
return;
}

bindButtonTextures(minecraft);

GL11.glPushMatrix();

float scaleFactor = 0.5F;
GL11.glTranslatef(xPosition, yPosition, 0);
GL11.glScalef(scaleFactor, scaleFactor, 1);

T state = getController().getButtonState();
IButtonTextureSet overlay = state.getOverlayTexture();

drawTexturedModalRect(0, 0, overlay.getX(), overlay.getY(), overlay.getWidth(), overlay.getHeight());

GL11.glPopMatrix();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public boolean matches(ItemStack stack) {
if (stack == null) return false;
if (InvTools.isItemEqual(stack, original)) return true;
if (stack.getItem() instanceof ItemCartAnchor || stack.getItem() instanceof ItemLocomotive)
return InvTools.isItemEqual(stack, original, false, false);
return InvTools.isItemEqual(stack, original, false, false, false);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import mods.railcraft.common.gui.EnumGui;
import mods.railcraft.common.gui.GuiHandler;
import mods.railcraft.common.util.inventory.AdjacentInventoryCache;
import mods.railcraft.common.util.inventory.InvFilteredHelper;
import mods.railcraft.common.util.inventory.InvTools;
import mods.railcraft.common.util.inventory.InventorySorter;
import mods.railcraft.common.util.inventory.ItemStackMap;
Expand Down Expand Up @@ -124,8 +125,12 @@ public void updateEntity() {
if (numMoved == null) {
numMoved = 0;
}
if (numMoved < InvTools.countItems(getItemFilters(), filter)) {
ItemStack moved = InvTools.moveOneItem(chests, cartInv, filter);

InvFilteredHelper helper = InvFilteredHelper
.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), filter);

if (numMoved < helper.countItems(getItemFilters())) {
ItemStack moved = helper.moveOneItem(chests, cartInv);
if (moved != null) {
movedItemCart = true;
numMoved++;
Expand All @@ -135,7 +140,7 @@ public void updateEntity() {
}
}
if (!hasFilter) {
ItemStack moved = InvTools.moveOneItem(chests, cartInv);
ItemStack moved = InvFilteredHelper.acceptAll().moveOneItem(chests, cartInv);
if (moved != null) {
movedItemCart = true;
break;
Expand All @@ -151,9 +156,13 @@ public void updateEntity() {
if (!checkedItems.add(filter)) {
continue;
}
int stocked = InvTools.countItems(cartInv, filter);
if (stocked < InvTools.countItems(getItemFilters(), filter)) {
ItemStack moved = InvTools.moveOneItem(chests, cartInv, filter);

InvFilteredHelper helper = InvFilteredHelper
.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), filter);

int stocked = helper.countItems(cartInv);
if (stocked < helper.countItems(getItemFilters())) {
ItemStack moved = helper.moveOneItem(chests, cartInv);
if (moved != null) {
movedItemCart = true;
break;
Expand All @@ -170,17 +179,23 @@ public void updateEntity() {
if (!checkedItems.add(filter)) {
continue;
}
int stocked = InvTools.countItems(chests, filter);
if (stocked > InvTools.countItems(getItemFilters(), filter)) {
ItemStack moved = InvTools.moveOneItem(chests, cartInv, filter);

InvFilteredHelper helper = InvFilteredHelper
.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), filter);

int stocked = helper.countItems(chests);
if (stocked > helper.countItems(getItemFilters())) {
ItemStack moved = helper.moveOneItem(chests, cartInv);
if (moved != null) {
movedItemCart = true;
break;
}
}
}
if (!movedItemCart) {
movedItemCart = InvTools.moveOneItemExcept(chests, cartInv, getItemFilters().getContents()) != null;
movedItemCart = InvFilteredHelper
.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), getItemFilters().getContents())
.invert().moveOneItem(chests, cartInv) != null;
}
break;
}
Expand All @@ -194,14 +209,16 @@ public void updateEntity() {
continue;
}
hasFilter = true;
ItemStack moved = InvTools.moveOneItem(chests, cartInv, filter);

ItemStack moved = InvFilteredHelper.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), filter)
.moveOneItem(chests, cartInv);
if (moved != null) {
movedItemCart = true;
break;
}
}
if (!hasFilter) {
ItemStack moved = InvTools.moveOneItem(chests, cartInv);
ItemStack moved = InvFilteredHelper.acceptAll().moveOneItem(chests, cartInv);
if (moved != null) {
movedItemCart = true;
break;
Expand Down Expand Up @@ -260,7 +277,9 @@ private boolean isTransferComplete(ItemStack[] filters) {
}
hasFilter = true;
Short numMoved = transferredItems.get(filter);
if (numMoved == null || numMoved < InvTools.countItems(getItemFilters(), filter)) {

if (numMoved == null || numMoved < InvFilteredHelper
.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), filter).countItems(getItemFilters())) {
return false;
}
}
Expand All @@ -276,8 +295,11 @@ private boolean isStockComplete(IInventory cart, ItemStack[] filters) {
if (!checkedItems.add(filter)) {
continue;
}
int stocked = InvTools.countItems(cart, filter);
if (stocked < InvTools.countItems(getItemFilters(), filter)) {

InvFilteredHelper helper = InvFilteredHelper.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), filter);

int stocked = helper.countItems(cart);
if (stocked < helper.countItems(getItemFilters())) {
return false;
}
}
Expand All @@ -294,16 +316,16 @@ private boolean isExcessComplete(List<IInventory> chests, ItemStack[] filters) {
if (!checkedItems.add(filter)) {
continue;
}
int stocked = InvTools.countItems(chests, filter);

InvFilteredHelper helper = InvFilteredHelper.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), filter);

int stocked = helper.countItems(chests);
max += filter.stackSize;
if (stocked > InvTools.countItems(getItemFilters(), filter)) {
if (stocked > helper.countItems(getItemFilters())) {
return false;
}
}
if (InvTools.countItems(chests) > max) {
return false;
}
return true;
return InvFilteredHelper.acceptAll().countItems(chests) <= max;
}

private boolean isAllComplete(IInventory cart, ItemStack[] filters) {
Expand All @@ -317,7 +339,8 @@ private boolean isAllComplete(IInventory cart, ItemStack[] filters) {
continue;
}
hasFilter = true;
if (InvTools.countItems(cart, filter) > 0) {

if (InvFilteredHelper.filteredByStacks(isMatchByNBT(), isMatchByMetadata(), filter).countItems(cart) > 0) {
return false;
}
}
Expand Down
Loading