-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New inventory handlers. Also add FIX comment for future tinkering as par
issue #243.
- Loading branch information
AfterLifeLochie
committed
Oct 4, 2014
1 parent
b30ffea
commit 02d6092
Showing
4 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |