add expxorter tile..
This commit is contained in:
@@ -14,10 +14,10 @@ public class MessageImporterUpdate implements IMessage, IMessageHandler<MessageI
|
|||||||
private int z;
|
private int z;
|
||||||
private int compareFlags;
|
private int compareFlags;
|
||||||
private boolean nextMode;
|
private boolean nextMode;
|
||||||
|
|
||||||
public MessageImporterUpdate() {
|
public MessageImporterUpdate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public MessageImporterUpdate(int x, int y, int z, int compareFlags, boolean nextMode) {
|
public MessageImporterUpdate(int x, int y, int z, int compareFlags, boolean nextMode) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
@@ -25,7 +25,7 @@ public class MessageImporterUpdate implements IMessage, IMessageHandler<MessageI
|
|||||||
this.compareFlags = compareFlags;
|
this.compareFlags = compareFlags;
|
||||||
this.nextMode = nextMode;
|
this.nextMode = nextMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fromBytes(ByteBuf buf) {
|
public void fromBytes(ByteBuf buf) {
|
||||||
x = buf.readInt();
|
x = buf.readInt();
|
||||||
@@ -34,7 +34,7 @@ public class MessageImporterUpdate implements IMessage, IMessageHandler<MessageI
|
|||||||
compareFlags = buf.readInt();
|
compareFlags = buf.readInt();
|
||||||
nextMode = buf.readBoolean();
|
nextMode = buf.readBoolean();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void toBytes(ByteBuf buf) {
|
public void toBytes(ByteBuf buf) {
|
||||||
buf.writeInt(x);
|
buf.writeInt(x);
|
||||||
@@ -43,23 +43,23 @@ public class MessageImporterUpdate implements IMessage, IMessageHandler<MessageI
|
|||||||
buf.writeInt(compareFlags);
|
buf.writeInt(compareFlags);
|
||||||
buf.writeBoolean(nextMode);
|
buf.writeBoolean(nextMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IMessage onMessage(MessageImporterUpdate message, MessageContext context) {
|
public IMessage onMessage(MessageImporterUpdate message, MessageContext context) {
|
||||||
EntityPlayerMP player = context.getServerHandler().playerEntity;
|
EntityPlayerMP player = context.getServerHandler().playerEntity;
|
||||||
|
|
||||||
TileEntity tile = player.worldObj.getTileEntity(message.x, message.y, message.z);
|
TileEntity tile = player.worldObj.getTileEntity(message.x, message.y, message.z);
|
||||||
|
|
||||||
if (tile instanceof TileImporter) {
|
if (tile instanceof TileImporter) {
|
||||||
TileImporter importer = (TileImporter) tile;
|
TileImporter importer = (TileImporter) tile;
|
||||||
|
|
||||||
importer.setCompareFlags(message.compareFlags);
|
importer.setCompareFlags(message.compareFlags);
|
||||||
|
|
||||||
if (message.nextMode) {
|
if (message.nextMode) {
|
||||||
importer.setMode(importer.getMode().next());
|
importer.setMode(importer.getMode().next());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
138
src/main/java/storagecraft/tile/TileExporter.java
Normal file
138
src/main/java/storagecraft/tile/TileExporter.java
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
package storagecraft.tile;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraft.inventory.IInventory;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
import storagecraft.inventory.InventorySimple;
|
||||||
|
import storagecraft.util.InventoryUtils;
|
||||||
|
|
||||||
|
public class TileExporter extends TileMachine implements IInventory {
|
||||||
|
public static final String NBT_COMPARE_FLAGS = "CompareFlags";
|
||||||
|
|
||||||
|
private InventorySimple inventory = new InventorySimple("exporter", 1);
|
||||||
|
|
||||||
|
private int compareFlags = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getEnergyUsage() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateMachine() {
|
||||||
|
TileEntity tile = worldObj.getTileEntity(xCoord + getDirection().offsetX, yCoord + getDirection().offsetY, zCoord + getDirection().offsetZ);
|
||||||
|
|
||||||
|
if (tile instanceof IInventory) {
|
||||||
|
IInventory connectedInventory = (IInventory) tile;
|
||||||
|
|
||||||
|
if (ticks % 5 == 0) {
|
||||||
|
// @TODO: ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCompareFlags() {
|
||||||
|
return compareFlags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompareFlags(int flags) {
|
||||||
|
this.compareFlags = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSizeInventory() {
|
||||||
|
return inventory.getSizeInventory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack getStackInSlot(int slot) {
|
||||||
|
return inventory.getStackInSlot(slot);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack decrStackSize(int slot, int amount) {
|
||||||
|
return inventory.decrStackSize(slot, amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack getStackInSlotOnClosing(int slot) {
|
||||||
|
return inventory.getStackInSlotOnClosing(slot);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setInventorySlotContents(int slot, ItemStack stack) {
|
||||||
|
inventory.setInventorySlotContents(slot, stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInventoryName() {
|
||||||
|
return inventory.getInventoryName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasCustomInventoryName() {
|
||||||
|
return inventory.hasCustomInventoryName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInventoryStackLimit() {
|
||||||
|
return inventory.getInventoryStackLimit();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isUseableByPlayer(EntityPlayer player) {
|
||||||
|
return inventory.isUseableByPlayer(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void openInventory() {
|
||||||
|
inventory.openInventory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void closeInventory() {
|
||||||
|
inventory.closeInventory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isItemValidForSlot(int slot, ItemStack stack) {
|
||||||
|
return inventory.isItemValidForSlot(slot, stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readFromNBT(NBTTagCompound nbt) {
|
||||||
|
super.readFromNBT(nbt);
|
||||||
|
|
||||||
|
if (nbt.hasKey(NBT_COMPARE_FLAGS)) {
|
||||||
|
compareFlags = nbt.getInteger(NBT_COMPARE_FLAGS);
|
||||||
|
}
|
||||||
|
|
||||||
|
InventoryUtils.restoreInventory(this, nbt);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToNBT(NBTTagCompound nbt) {
|
||||||
|
super.writeToNBT(nbt);
|
||||||
|
|
||||||
|
nbt.setInteger(NBT_COMPARE_FLAGS, compareFlags);
|
||||||
|
|
||||||
|
InventoryUtils.saveInventory(this, nbt);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fromBytes(ByteBuf buf) {
|
||||||
|
super.fromBytes(buf);
|
||||||
|
|
||||||
|
compareFlags = buf.readInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toBytes(ByteBuf buf) {
|
||||||
|
super.toBytes(buf);
|
||||||
|
|
||||||
|
buf.writeInt(compareFlags);
|
||||||
|
}
|
||||||
|
}
|
@@ -50,31 +50,7 @@ public class TileStorageProxy extends TileMachine implements IStorageProvider, I
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int toGo = stack.stackSize;
|
InventoryUtils.pushToInventory(inventory, stack);
|
||||||
|
|
||||||
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
|
||||||
ItemStack slot = inventory.getStackInSlot(i);
|
|
||||||
|
|
||||||
if (slot == null) {
|
|
||||||
inventory.setInventorySlotContents(i, stack);
|
|
||||||
|
|
||||||
return;
|
|
||||||
} else if (InventoryUtils.compareStackNoQuantity(slot, stack)) {
|
|
||||||
int toAdd = toGo;
|
|
||||||
|
|
||||||
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
|
|
||||||
toAdd = slot.getMaxStackSize() - slot.stackSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
slot.stackSize += toAdd;
|
|
||||||
|
|
||||||
toGo -= toAdd;
|
|
||||||
|
|
||||||
if (toGo == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -116,29 +92,7 @@ public class TileStorageProxy extends TileMachine implements IStorageProvider, I
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int toGo = stack.stackSize;
|
return InventoryUtils.canPushToInventory(inventory, stack);
|
||||||
|
|
||||||
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
|
||||||
ItemStack slot = inventory.getStackInSlot(i);
|
|
||||||
|
|
||||||
if (slot == null) {
|
|
||||||
return true;
|
|
||||||
} else if (InventoryUtils.compareStackNoQuantity(slot, stack)) {
|
|
||||||
int toAdd = toGo;
|
|
||||||
|
|
||||||
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
|
|
||||||
toAdd = slot.getMaxStackSize() - slot.stackSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
toGo -= toAdd;
|
|
||||||
|
|
||||||
if (toGo == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return toGo == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -88,8 +88,58 @@ public class InventoryUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean compareStackNoQuantity(ItemStack first, ItemStack second) {
|
public static void pushToInventory(IInventory inventory, ItemStack stack) {
|
||||||
return compareStack(first, second, COMPARE_NBT | COMPARE_DAMAGE);
|
int toGo = stack.stackSize;
|
||||||
|
|
||||||
|
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
||||||
|
ItemStack slot = inventory.getStackInSlot(i);
|
||||||
|
|
||||||
|
if (slot == null) {
|
||||||
|
inventory.setInventorySlotContents(i, stack);
|
||||||
|
|
||||||
|
return;
|
||||||
|
} else if (compareStackNoQuantity(slot, stack)) {
|
||||||
|
int toAdd = toGo;
|
||||||
|
|
||||||
|
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
|
||||||
|
toAdd = slot.getMaxStackSize() - slot.stackSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
slot.stackSize += toAdd;
|
||||||
|
|
||||||
|
toGo -= toAdd;
|
||||||
|
|
||||||
|
if (toGo == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean canPushToInventory(IInventory inventory, ItemStack stack) {
|
||||||
|
int toGo = stack.stackSize;
|
||||||
|
|
||||||
|
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
||||||
|
ItemStack slot = inventory.getStackInSlot(i);
|
||||||
|
|
||||||
|
if (slot == null) {
|
||||||
|
return true;
|
||||||
|
} else if (compareStackNoQuantity(slot, stack)) {
|
||||||
|
int toAdd = toGo;
|
||||||
|
|
||||||
|
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
|
||||||
|
toAdd = slot.getMaxStackSize() - slot.stackSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
toGo -= toAdd;
|
||||||
|
|
||||||
|
if (toGo == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toGo == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean compareStack(ItemStack first, ItemStack second) {
|
public static boolean compareStack(ItemStack first, ItemStack second) {
|
||||||
@@ -117,4 +167,8 @@ public class InventoryUtils {
|
|||||||
|
|
||||||
return first.getItem() == second.getItem();
|
return first.getItem() == second.getItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean compareStackNoQuantity(ItemStack first, ItemStack second) {
|
||||||
|
return compareStack(first, second, COMPARE_NBT | COMPARE_DAMAGE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user