forked from RS485/LogisticsPipes
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Martin Robertz <[email protected]>
- Loading branch information
1 parent
e5aa024
commit 1ba7536
Showing
7 changed files
with
286 additions
and
266 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
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
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
167 changes: 167 additions & 0 deletions
167
src/main/java/logisticspipes/proxy/specialinventoryhandler/DSULikeInventoryHandler.java
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,167 @@ | ||
package logisticspipes.proxy.specialinventoryhandler; | ||
|
||
import java.util.HashMap; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
import net.minecraft.item.ItemStack; | ||
import net.minecraftforge.common.util.ForgeDirection; | ||
|
||
import logisticspipes.utils.item.ItemIdentifier; | ||
|
||
abstract class DSULikeInventoryHandler extends SpecialInventoryHandler { | ||
|
||
protected boolean _hideOnePerStack; | ||
|
||
@Override | ||
public int itemCount(ItemIdentifier itemIdent) { | ||
if (isEmpty() || itemIdent.tag != null) return 0; | ||
return getTypeIdent().equals(itemIdent) ? getCurrent() : 0; | ||
} | ||
|
||
@Override | ||
public ItemStack getMultipleItems(ItemIdentifier itemIdent, int count) { | ||
if (isEmpty()) return null; | ||
if (!getTypeIdent().equals(itemIdent)) { | ||
return null; | ||
} | ||
int current = getCurrent(); | ||
int toTake = Math.max(Math.min(_hideOnePerStack ? current - 1 : current, count), 0); | ||
setContent(current - toTake); | ||
markDirty(); | ||
return itemIdent.makeNormalStack(toTake); | ||
} | ||
|
||
@Override | ||
public Set<ItemIdentifier> getItems() { | ||
Set<ItemIdentifier> result = new TreeSet<>(); | ||
if (!isEmpty()) { | ||
result.add(getTypeIdent()); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public HashMap<ItemIdentifier, Integer> getItemsAndCount() { | ||
HashMap<ItemIdentifier, Integer> result = new HashMap<>(); | ||
if (!isEmpty()) { | ||
result.put(getTypeIdent(), getReportedCount()); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public ItemStack getSingleItem(ItemIdentifier itemIdent) { | ||
return getMultipleItems(itemIdent, 1); | ||
} | ||
|
||
@Override | ||
public boolean containsUndamagedItem(ItemIdentifier itemIdent) { | ||
return !isEmpty() && getTypeIdent().getUndamaged().equals(itemIdent); | ||
} | ||
|
||
int roomForItemNoTag(ItemStack stack) { | ||
if (stack.stackTagCompound != null) { | ||
return 0; | ||
} | ||
if (isEmpty()) { | ||
return getSize(); | ||
} | ||
if (stack.isItemEqual(getType())) { | ||
return getSize() - getCurrent(); | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int roomForItem(ItemIdentifier item) { | ||
return roomForItem(item, 0); | ||
} | ||
|
||
@Override | ||
public int roomForItem(ItemIdentifier itemIdent, int count) { | ||
if (itemIdent.tag != null) { | ||
return 0; | ||
} | ||
if (isEmpty()) { | ||
return getSize(); | ||
} | ||
if (getTypeIdent().equals(itemIdent)) { | ||
return getSize() - getCurrent(); | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public ItemStack add(ItemStack stack, ForgeDirection from, boolean doAdd) { | ||
ItemStack st = stack.copy(); | ||
st.stackSize = 0; | ||
if (stack.getTagCompound() != null) { | ||
return st; | ||
} | ||
st.stackSize = Math.min(roomForItemNoTag(stack), stack.stackSize); | ||
if (st.stackSize == 0) { | ||
return st; | ||
} | ||
if (doAdd) { | ||
if (isEmpty()) { | ||
setContent(st, st.stackSize); | ||
} else { | ||
setContent(getCurrent() + st.stackSize); | ||
} | ||
markDirty(); | ||
} | ||
return st; | ||
} | ||
|
||
@Override | ||
public boolean isSpecialInventory() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getSizeInventory() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public ItemStack getStackInSlot(int i) { | ||
if (i != 0 || isEmpty()) return null; | ||
ItemStack res = getType(); | ||
res.stackSize = getReportedCount(); | ||
return res; | ||
} | ||
|
||
@Override | ||
public ItemStack decrStackSize(int i, int j) { | ||
if (i != 0 || isEmpty()) return null; | ||
return getMultipleItems(ItemIdentifier.get(getType()), j); | ||
} | ||
|
||
/** | ||
* @return return false if this does not have anything inside, including ghosts with 0 stack size. return true | ||
* otherwise | ||
*/ | ||
abstract boolean isEmpty(); | ||
|
||
abstract int getSize(); | ||
|
||
abstract int getCurrent(); | ||
|
||
abstract ItemStack getType(); | ||
|
||
ItemIdentifier getTypeIdent() { | ||
return ItemIdentifier.get(getType()); | ||
} | ||
|
||
abstract void setContent(int count); | ||
|
||
abstract void setContent(ItemStack stack, int size); | ||
|
||
abstract void markDirty(); | ||
|
||
int getReportedCount() { | ||
if (_hideOnePerStack) return Math.max(0, getCurrent() - 1); | ||
return getCurrent(); | ||
} | ||
} |
Oops, something went wrong.