mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-22 05:02:32 +00:00
refactor: port inventory tile trait to java
Keep TIInventoryTile as AE2's public rebuild API and JInventoryTile as the registered mixin input, preserving both generated interfaces and flattened sided-slot behavior.
This commit is contained in:
1 parent
48cbb7ff3e
commit
4e8d238b57
6 files changed
+391
-144
No files matched your search
@@ -0,0 +1,224 @@
|
||||
package codechicken.multipart.scalatraits;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import codechicken.multipart.TMultiPart;
|
||||
import codechicken.multipart.TileMultipart;
|
||||
import scala.Tuple2;
|
||||
|
||||
/** Registered Java trait input that distributes inventory access among multipart inventories. */
|
||||
public class JInventoryTile extends TileMultipart implements TIInventoryTile {
|
||||
|
||||
private LinkedList<IInventory> invList = new LinkedList<>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Tuple2<IInventory, Object>[] slotMap = (Tuple2<IInventory, Object>[]) new Tuple2[0];
|
||||
|
||||
@Override
|
||||
public LinkedList<IInventory> invList() {
|
||||
return invList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invList_$eq(LinkedList<IInventory> inventories) {
|
||||
invList = inventories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple2<IInventory, Object>[] slotMap() {
|
||||
return slotMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void slotMap_$eq(Tuple2<IInventory, Object>[] slots) {
|
||||
slotMap = slots;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codechicken$multipart$scalatraits$TIInventoryTile$$super$copyFrom(TileMultipart that) {
|
||||
super.copyFrom(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codechicken$multipart$scalatraits$TIInventoryTile$$super$bindPart(TMultiPart part) {
|
||||
super.bindPart(part);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codechicken$multipart$scalatraits$TIInventoryTile$$super$partRemoved(TMultiPart part, int position) {
|
||||
super.partRemoved(part, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codechicken$multipart$scalatraits$TIInventoryTile$$super$clearParts() {
|
||||
super.clearParts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copyFrom(TileMultipart that) {
|
||||
codechicken$multipart$scalatraits$TIInventoryTile$$super$copyFrom(that);
|
||||
if (that instanceof TIInventoryTile) {
|
||||
invList = ((TIInventoryTile) that).invList();
|
||||
rebuildSlotMap();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindPart(TMultiPart part) {
|
||||
codechicken$multipart$scalatraits$TIInventoryTile$$super$bindPart(part);
|
||||
if (part instanceof IInventory) {
|
||||
invList.add((IInventory) part);
|
||||
rebuildSlotMap();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void partRemoved(TMultiPart part, int position) {
|
||||
codechicken$multipart$scalatraits$TIInventoryTile$$super$partRemoved(part, position);
|
||||
if (part instanceof IInventory) {
|
||||
invList.remove(part);
|
||||
rebuildSlotMap();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void clearParts() {
|
||||
codechicken$multipart$scalatraits$TIInventoryTile$$super$clearParts();
|
||||
invList.clear();
|
||||
slotMap = (Tuple2<IInventory, Object>[]) new Tuple2[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void rebuildSlotMap() {
|
||||
int totalSlots = 0;
|
||||
for (IInventory inventory : invList) {
|
||||
totalSlots += inventory.getSizeInventory();
|
||||
}
|
||||
|
||||
Tuple2<IInventory, Object>[] rebuilt = (Tuple2<IInventory, Object>[]) new Tuple2[totalSlots];
|
||||
int index = 0;
|
||||
for (IInventory inventory : invList) {
|
||||
for (int slot = 0; slot < inventory.getSizeInventory(); slot++) {
|
||||
rebuilt[index++] = new Tuple2<IInventory, Object>(inventory, Integer.valueOf(slot));
|
||||
}
|
||||
}
|
||||
slotMap = rebuilt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSizeInventory() {
|
||||
return slotMap.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
Tuple2<IInventory, Object> entry = slotMap[slot];
|
||||
return entry._1().getStackInSlot(((Number) entry._2()).intValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize(int slot, int amount) {
|
||||
Tuple2<IInventory, Object> entry = slotMap[slot];
|
||||
return entry._1().decrStackSize(((Number) entry._2()).intValue(), amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlotOnClosing(int slot) {
|
||||
Tuple2<IInventory, Object> entry = slotMap[slot];
|
||||
return entry._1().getStackInSlotOnClosing(((Number) entry._2()).intValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInventorySlotContents(int slot, ItemStack stack) {
|
||||
Tuple2<IInventory, Object> entry = slotMap[slot];
|
||||
entry._1().setInventorySlotContents(((Number) entry._2()).intValue(), stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInventoryName() {
|
||||
return "Multipart Inventory";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomInventoryName() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInventoryStackLimit() {
|
||||
return 64;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUseableByPlayer(EntityPlayer player) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openInventory() {}
|
||||
|
||||
@Override
|
||||
public void closeInventory() {}
|
||||
|
||||
@Override
|
||||
public boolean isItemValidForSlot(int slot, ItemStack stack) {
|
||||
Tuple2<IInventory, Object> entry = slotMap[slot];
|
||||
return entry._1().isItemValidForSlot(((Number) entry._2()).intValue(), stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getAccessibleSlotsFromSide(int side) {
|
||||
List<Integer> accessibleSlots = new ArrayList<>();
|
||||
int base = 0;
|
||||
for (IInventory inventory : invList) {
|
||||
if (inventory instanceof ISidedInventory) {
|
||||
int[] localSlots = ((ISidedInventory) inventory).getAccessibleSlotsFromSide(side);
|
||||
for (int localSlot : localSlots) {
|
||||
accessibleSlots.add(Integer.valueOf(localSlot + base));
|
||||
}
|
||||
}
|
||||
base += inventory.getSizeInventory();
|
||||
}
|
||||
|
||||
return JInventoryTileAccess.toIntArray(accessibleSlots);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInsertItem(int slot, ItemStack stack, int side) {
|
||||
Tuple2<IInventory, Object> entry = slotMap[slot];
|
||||
IInventory inventory = entry._1();
|
||||
return !(inventory instanceof ISidedInventory)
|
||||
|| ((ISidedInventory) inventory).canInsertItem(((Number) entry._2()).intValue(), stack, side);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtractItem(int slot, ItemStack stack, int side) {
|
||||
Tuple2<IInventory, Object> entry = slotMap[slot];
|
||||
IInventory inventory = entry._1();
|
||||
return !(inventory instanceof ISidedInventory)
|
||||
|| ((ISidedInventory) inventory).canExtractItem(((Number) entry._2()).intValue(), stack, side);
|
||||
}
|
||||
}
|
||||
|
||||
/** Keeps primitive-array allocation outside the generated trait transformer. */
|
||||
final class JInventoryTileAccess {
|
||||
|
||||
private JInventoryTileAccess() {}
|
||||
|
||||
static int[] toIntArray(List<Integer> values) {
|
||||
int[] result = new int[values.size()];
|
||||
for (int index = 0; index < result.length; index++) {
|
||||
result[index] = values.get(index).intValue();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package codechicken.multipart.scalatraits;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import codechicken.multipart.TMultiPart;
|
||||
import codechicken.multipart.TileMultipart;
|
||||
import scala.Tuple2;
|
||||
|
||||
/** Consumer-facing inventory contract implemented by the generated multipart tile. */
|
||||
public interface TIInventoryTile extends ISidedInventory {
|
||||
|
||||
void codechicken$multipart$scalatraits$TIInventoryTile$$super$copyFrom(TileMultipart that);
|
||||
|
||||
void codechicken$multipart$scalatraits$TIInventoryTile$$super$bindPart(TMultiPart part);
|
||||
|
||||
void codechicken$multipart$scalatraits$TIInventoryTile$$super$partRemoved(TMultiPart part, int position);
|
||||
|
||||
void codechicken$multipart$scalatraits$TIInventoryTile$$super$clearParts();
|
||||
|
||||
LinkedList<IInventory> invList();
|
||||
|
||||
void invList_$eq(LinkedList<IInventory> inventories);
|
||||
|
||||
Tuple2<IInventory, Object>[] slotMap();
|
||||
|
||||
void slotMap_$eq(Tuple2<IInventory, Object>[] slots);
|
||||
|
||||
void copyFrom(TileMultipart that);
|
||||
|
||||
void bindPart(TMultiPart part);
|
||||
|
||||
void partRemoved(TMultiPart part, int position);
|
||||
|
||||
void clearParts();
|
||||
|
||||
void rebuildSlotMap();
|
||||
|
||||
@Override
|
||||
int getSizeInventory();
|
||||
|
||||
@Override
|
||||
ItemStack getStackInSlot(int slot);
|
||||
|
||||
@Override
|
||||
ItemStack decrStackSize(int slot, int amount);
|
||||
|
||||
@Override
|
||||
ItemStack getStackInSlotOnClosing(int slot);
|
||||
|
||||
@Override
|
||||
void setInventorySlotContents(int slot, ItemStack stack);
|
||||
|
||||
@Override
|
||||
String getInventoryName();
|
||||
|
||||
@Override
|
||||
boolean hasCustomInventoryName();
|
||||
|
||||
@Override
|
||||
int getInventoryStackLimit();
|
||||
|
||||
@Override
|
||||
boolean isUseableByPlayer(EntityPlayer player);
|
||||
|
||||
@Override
|
||||
void openInventory();
|
||||
|
||||
@Override
|
||||
void closeInventory();
|
||||
|
||||
@Override
|
||||
boolean isItemValidForSlot(int slot, ItemStack stack);
|
||||
|
||||
@Override
|
||||
int[] getAccessibleSlotsFromSide(int side);
|
||||
|
||||
@Override
|
||||
boolean canInsertItem(int slot, ItemStack stack, int side);
|
||||
|
||||
@Override
|
||||
boolean canExtractItem(int slot, ItemStack stack, int side);
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package codechicken.multipart.scalatraits
|
||||
|
||||
import net.minecraft.inventory.{IInventory, ISidedInventory}
|
||||
import codechicken.multipart.{TMultiPart, TileMultipart}
|
||||
import java.util.LinkedList
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import scala.collection.JavaConversions._
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
|
||||
trait TIInventoryTile extends TileMultipart with ISidedInventory {
|
||||
var invList = new LinkedList[IInventory]()
|
||||
var slotMap = Array[(IInventory, Int)]()
|
||||
|
||||
override def copyFrom(that: TileMultipart) {
|
||||
super.copyFrom(that)
|
||||
if (that.isInstanceOf[TIInventoryTile]) {
|
||||
invList = that.asInstanceOf[TIInventoryTile].invList
|
||||
rebuildSlotMap()
|
||||
}
|
||||
}
|
||||
|
||||
override def bindPart(part: TMultiPart) {
|
||||
super.bindPart(part)
|
||||
if (part.isInstanceOf[IInventory]) {
|
||||
invList += part.asInstanceOf[IInventory]
|
||||
rebuildSlotMap()
|
||||
}
|
||||
}
|
||||
|
||||
override def partRemoved(part: TMultiPart, p: Int) {
|
||||
super.partRemoved(part, p)
|
||||
if (part.isInstanceOf[IInventory]) {
|
||||
invList -= part.asInstanceOf[IInventory]
|
||||
rebuildSlotMap()
|
||||
}
|
||||
}
|
||||
|
||||
override def clearParts() {
|
||||
super.clearParts()
|
||||
invList.clear()
|
||||
slotMap = Array()
|
||||
}
|
||||
|
||||
def rebuildSlotMap() {
|
||||
slotMap = Array.ofDim(invList.map(_.getSizeInventory).sum)
|
||||
var i = 0
|
||||
for (inv <- invList; s <- 0 until inv.getSizeInventory) {
|
||||
slotMap(i) = (inv, s)
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
|
||||
def getSizeInventory: Int = slotMap.length
|
||||
|
||||
def getStackInSlot(i: Int) = {
|
||||
val (inv, slot) = slotMap(i)
|
||||
inv.getStackInSlot(slot)
|
||||
}
|
||||
|
||||
def decrStackSize(i: Int, j: Int) = {
|
||||
val (inv, slot) = slotMap(i)
|
||||
inv.decrStackSize(slot, j)
|
||||
}
|
||||
|
||||
def getStackInSlotOnClosing(i: Int) = {
|
||||
val (inv, slot) = slotMap(i)
|
||||
inv.getStackInSlotOnClosing(slot)
|
||||
}
|
||||
|
||||
def setInventorySlotContents(i: Int, itemstack: ItemStack) = {
|
||||
val (inv, slot) = slotMap(i)
|
||||
inv.setInventorySlotContents(slot, itemstack)
|
||||
}
|
||||
|
||||
def getInventoryName = "Multipart Inventory"
|
||||
|
||||
def hasCustomInventoryName = false
|
||||
|
||||
def getInventoryStackLimit = 64
|
||||
|
||||
def isUseableByPlayer(entityplayer: EntityPlayer) = true
|
||||
|
||||
def openInventory() {}
|
||||
|
||||
def closeInventory() {}
|
||||
|
||||
def isItemValidForSlot(i: Int, itemstack: ItemStack) = {
|
||||
val (inv, slot) = slotMap(i)
|
||||
inv.isItemValidForSlot(slot, itemstack)
|
||||
}
|
||||
|
||||
def getAccessibleSlotsFromSide(side: Int) = {
|
||||
val buf = new ArrayBuffer[Int]()
|
||||
var base = 0
|
||||
for (inv <- invList) {
|
||||
inv match {
|
||||
case isided: ISidedInventory =>
|
||||
buf ++= isided.getAccessibleSlotsFromSide(side).map(_ + base)
|
||||
case _ =>
|
||||
}
|
||||
base += inv.getSizeInventory
|
||||
}
|
||||
buf.toArray
|
||||
}
|
||||
|
||||
def canInsertItem(i: Int, itemstack: ItemStack, j: Int) = {
|
||||
val (inv, slot) = slotMap(i)
|
||||
inv match {
|
||||
case isided: ISidedInventory => isided.canInsertItem(slot, itemstack, j)
|
||||
case _ => true
|
||||
}
|
||||
}
|
||||
|
||||
def canExtractItem(i: Int, itemstack: ItemStack, j: Int) = {
|
||||
val (inv, slot) = slotMap(i)
|
||||
inv match {
|
||||
case isided: ISidedInventory => isided.canExtractItem(slot, itemstack, j)
|
||||
case _ => true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** To handle obfuscation issues, this is registered as a java trait.
|
||||
*/
|
||||
class JInventoryTile extends TileMultipart with TIInventoryTile {}
|
||||
Reference in new issue
Block a user