Skip to content

Commit

Permalink
New inventory handlers. Also add FIX comment for future tinkering as par
Browse files Browse the repository at this point in the history
issue #243.
  • Loading branch information
AfterLifeLochie committed Oct 4, 2014
1 parent b30ffea commit 02d6092
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/main/java/lc/common/base/LCInventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package lc.common.base;

import lc.common.util.game.SlotFilter;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;

public abstract class LCInventory implements ISidedInventory {

protected ItemStack[] items;
protected SlotFilter[] rules;

public LCInventory(int size) {
items = new ItemStack[size];
rules = new SlotFilter[size];
}

public void setFilterRule(int slot, SlotFilter rule) {
rules[slot] = rule;
}

public SlotFilter getFilterRule(int slot) {
return rules[slot];
}

@Override
public int getSizeInventory() {
return items.length;
}

@Override
public ItemStack getStackInSlot(int i) {
return items[i];
}

@Override
public ItemStack decrStackSize(int slot, int take) {
if (items[slot] == null)
return null;
if (items[slot].stackSize == 0)
return null;
return items[slot].splitStack(Math.min(take, items[slot].stackSize));
}

@Override
public ItemStack getStackInSlotOnClosing(int i) {
return null;
}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
items[i] = itemstack;
}

@Override
public int getInventoryStackLimit() {
return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
return true;
}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
if (rules[i] != null && !rules[i].test(itemstack))
return false;
return true;
}

@Override
public void markDirty() {
// TODO Auto-generated method stub

}

@Override
public void openInventory() {
// TODO Auto-generated method stub

}

@Override
public void closeInventory() {
// TODO Auto-generated method stub

}

}
46 changes: 46 additions & 0 deletions src/main/java/lc/common/base/LCInventorySlot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package lc.common.base;

import lc.common.util.game.SlotFilter;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class LCInventorySlot extends Slot {

private final boolean readonly;
private final int slotIndex;

public LCInventorySlot(IInventory host, int slotIndex, int xDisplayPosition, int yDisplayPosition, boolean readonly) {
super(host, slotIndex, xDisplayPosition, yDisplayPosition);
this.readonly = readonly;
this.slotIndex = slotIndex;
}

@Override
public void putStack(ItemStack par1ItemStack) {
if (inventory instanceof LCInventory) {
LCInventory fint = (LCInventory) inventory;
SlotFilter rule = fint.getFilterRule(slotIndex);
if (rule == null || rule.test(par1ItemStack))
super.putStack(par1ItemStack);
} else
super.putStack(par1ItemStack);
}

@Override
public boolean isItemValid(ItemStack par1ItemStack) {
if (inventory instanceof LCInventory) {
LCInventory fint = (LCInventory) inventory;
SlotFilter rule = fint.getFilterRule(slotIndex);
return rule == null || rule.test(par1ItemStack);
} else
return super.isItemValid(par1ItemStack);
}

@Override
public boolean canTakeStack(EntityPlayer par1EntityPlayer) {
return !readonly || super.canTakeStack(par1EntityPlayer);
}

}
16 changes: 16 additions & 0 deletions src/main/java/lc/common/base/ux/LCContainerGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@

import java.util.HashMap;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import lc.common.base.LCContainer;
import lc.core.ResourceAccess;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;

Expand Down Expand Up @@ -50,6 +63,7 @@ protected void drawGuiContainerBackgroundLayer(float partialTickCount, int mouse
// TODO Auto-generated method stub
if (activeTab != null)
activeTab.drawBackgroundLayer(partialTickCount, mouseX, mouseY);

}

@Override
Expand Down Expand Up @@ -82,6 +96,8 @@ public void drawScreen(int par1, int par2, float par3) {
resetColor();
textColor = defaultTextColor;
textShadow = false;
// FIXME: Some clever overriding is required here in order to change the way
// slots are rendered (visibility, ghost items, etc)
super.drawScreen(par1, par2, par3);
}

Expand Down
135 changes: 135 additions & 0 deletions src/main/java/lc/common/util/game/SlotFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package lc.common.util.game;

import java.util.ArrayList;

import net.minecraft.item.ItemStack;

public class SlotFilter {
private ArrayList<ItemStack> accept = new ArrayList<ItemStack>();
private ArrayList<ItemStack> deny = new ArrayList<ItemStack>();

private boolean whitelist;
private boolean observeMetadata;

/**
* Creates a blank SlotFilter.
*/
public SlotFilter() {
new SlotFilter(null, null, false, true);
}

/**
* Creates a new SlotFilter with the provided Lists of accept and deny rules
*
* @param accept
* The list of accept rules
* @param deny
* The list of deny rules
*/
public SlotFilter(ItemStack[] accept, ItemStack[] deny) {
new SlotFilter(accept, deny, false, true);
}

/**
* Creates a new SlotFilter with the provided Lists of accept and deny
* rules, and a given mode
*
* @param accept
* The list of accept rules
* @param deny
* The list of deny rules
* @param whitelist
* If this FilterRule is a white-list rule-set
* @param metadata
* If this FilterRule is metadata sensitive.
*/
public SlotFilter(ItemStack[] accept, ItemStack[] deny, boolean whitelist, boolean metadata) {
if (accept != null)
for (ItemStack item : accept)
this.accept.add(new ItemStack(item.getItem(), 1));
if (deny != null)
for (ItemStack item : deny)
this.deny.add(new ItemStack(item.getItem(), 1));
this.whitelist = whitelist;
observeMetadata = metadata;
}

/**
* Adds an item to the accept list. This also removes the item from the deny
* list.
*
* @param item
* The item to accept
*/
public void accept(ItemStack item) {
deny.remove(item);
accept.add(item);
}

/**
* Adds an item to the deny list. This also removes the item from the accept
* list.
*
* @param item
* The item to deny
*/
public void deny(ItemStack item) {
accept.remove(item);
deny.add(item);
}

/**
* Removes an item from all accept and deny rules.
*
* @param item
* The item to remove
*/
public void remove(ItemStack item) {
accept.remove(item);
deny.remove(item);
}

/**
* Sets the SlotFilter white-list rule-set mode
*
* @param whitelist
* If this FilterRule is a white-list rule-set
*/
public void setMode(boolean whitelist) {
this.whitelist = whitelist;
}

/**
* Tests an ItemStack of any size to see if the underlying Item is permitted
* in this particular rule-set.
*
* @param testing
* The ItemStack to test
* @return If the ItemStack matches the rule-set
*/
public boolean test(ItemStack testing) {
if (testing == null || testing.getItem() == null)
return true;
if (whitelist) {
for (ItemStack s : accept)
if (isItemVirtuallyEqual(s, testing))
return true;
return false;
} else {
for (ItemStack s : deny)
if (isItemVirtuallyEqual(s, testing))
return false;
return true;
}
}

private boolean isItemVirtuallyEqual(ItemStack a, ItemStack b) {
if (a == null || a.getItem() == null)
return false;
if (!a.getItem().equals(b.getItem()))
return false;
if (observeMetadata && a.getItemDamage() != b.getItemDamage())
return false;
return true;
}
}

0 comments on commit 02d6092

Please sign in to comment.