added stuff

- storage proxy block: reads an another inventory into system
- right clicking to push 1 item works
- when take() returns 0, don't give any item stack to the player
- tweaked energy usage
This commit is contained in:
Raoul Van den Berge
2015-12-15 21:58:21 +01:00
parent 62cca754a3
commit b47dc933c8
18 changed files with 326 additions and 121 deletions

View File

@@ -101,9 +101,9 @@ public class SC {
continue;
}
float xx = random.nextFloat() * 0.8F + 0.1F;
float yy = random.nextFloat() * 0.8F + 0.1F;
float zz = random.nextFloat() * 0.8F + 0.1F;
float xo = random.nextFloat() * 0.8F + 0.1F;
float yo = random.nextFloat() * 0.8F + 0.1F;
float zo = random.nextFloat() * 0.8F + 0.1F;
while (stack.stackSize > 0) {
int amount = random.nextInt(21) + 10;
@@ -114,7 +114,7 @@ public class SC {
stack.stackSize -= amount;
EntityItem entity = new EntityItem(world, (float) x + xx, (float) y + (newSize > 0 ? 1 : 0) + yy, (float) z + zz, new ItemStack(stack.getItem(), amount, stack.getItemDamage()));
EntityItem entity = new EntityItem(world, (float) x + xo, (float) y + (newSize > 0 ? 1 : 0) + yo, (float) z + zo, new ItemStack(stack.getItem(), amount, stack.getItemDamage()));
entity.motionX = (float) random.nextGaussian() * 0.05F;
entity.motionY = (float) random.nextGaussian() * 0.05F + 0.2F;

View File

@@ -4,10 +4,12 @@ import storagecraft.block.BlockCable;
import storagecraft.block.BlockController;
import storagecraft.block.BlockDrive;
import storagecraft.block.BlockGrid;
import storagecraft.block.BlockStorageProxy;
public class SCBlocks {
public static final BlockController CONTROLLER = new BlockController();
public static final BlockCable CABLE = new BlockCable();
public static final BlockGrid GRID = new BlockGrid();
public static final BlockDrive DRIVE = new BlockDrive();
public static final BlockStorageProxy STORAGE_PROXY = new BlockStorageProxy();
}

View File

@@ -0,0 +1,17 @@
package storagecraft.block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import storagecraft.tile.TileStorageProxy;
public class BlockStorageProxy extends BlockSC implements ITileEntityProvider {
public BlockStorageProxy() {
super("storageProxy");
}
@Override
public TileEntity createNewTileEntity(World world, int meta) {
return new TileStorageProxy();
}
}

View File

@@ -113,7 +113,7 @@ public class GuiGrid extends GuiContainer {
if (mouseX >= getGridXStart() && mouseX <= getGridXEnd() && mouseY >= getGridYStart() && mouseY <= getGridYEnd()) {
if (container.getPlayer().inventory.getItemStack() != null) {
SC.NETWORK.sendToServer(new MessagePushToStorage(controller.xCoord, controller.yCoord, controller.zCoord, -1));
SC.NETWORK.sendToServer(new MessagePushToStorage(controller.xCoord, controller.yCoord, controller.zCoord, -1, clickedButton == 1));
} else {
int slotX = ((mouseX - getGridXStart()) / 18) + 1;
int slotY = ((mouseY - getGridYStart()) / 18) + 1;
@@ -127,7 +127,7 @@ public class GuiGrid extends GuiContainer {
if (func_146978_c(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, mouseX, mouseY)) {
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
SC.NETWORK.sendToServer(new MessagePushToStorage(controller.xCoord, controller.yCoord, controller.zCoord, slot.slotNumber));
SC.NETWORK.sendToServer(new MessagePushToStorage(controller.xCoord, controller.yCoord, controller.zCoord, slot.slotNumber, clickedButton == 1));
}
}
}

View File

@@ -2,7 +2,7 @@ package storagecraft.inventory;
import net.minecraft.entity.player.EntityPlayer;
import storagecraft.SCItems;
import storagecraft.inventory.slot.SlotDrive;
import storagecraft.inventory.slot.SlotItemFilter;
import storagecraft.tile.TileDrive;
public class ContainerDrive extends ContainerSC {
@@ -15,7 +15,7 @@ public class ContainerDrive extends ContainerSC {
int y = 20;
for (int i = 0; i < 8; ++i) {
addSlotToContainer(new SlotDrive(drive, i, x, y, SCItems.STORAGE_CELL, drive));
addSlotToContainer(new SlotItemFilter(drive, i, x, y, SCItems.STORAGE_CELL));
if ((i + 1) % 2 == 0) {
x = 71;

View File

@@ -1,30 +0,0 @@
package storagecraft.inventory.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import storagecraft.tile.TileDrive;
public class SlotDrive extends SlotItemFilter {
private TileDrive drive;
public SlotDrive(IInventory inventory, int id, int x, int y, Item item, TileDrive drive) {
super(inventory, id, x, y, item);
this.drive = drive;
}
@Override
public boolean isItemValid(ItemStack item) {
return drive.isConnected() && super.isItemValid(item);
}
@Override
public void onSlotChanged() {
super.onSlotChanged();
if (drive.isConnected()) {
drive.getController().syncStorage();
}
}
}

View File

@@ -71,14 +71,16 @@ public class MessagePullFromStorage implements IMessage, IMessageHandler<Message
ItemStack stack = controller.take(item.copy(quantity).toItemStack());
if (message.shift) {
// @TODO: This doesn't work
if (!player.inventory.addItemStackToInventory(stack.copy())) {
controller.push(stack);
if (stack.stackSize > 0) {
if (message.shift) {
// @TODO: This doesn't work
if (!player.inventory.addItemStackToInventory(stack.copy())) {
controller.push(stack);
}
} else {
player.inventory.setItemStack(stack);
player.updateHeldItem();
}
} else {
player.inventory.setItemStack(stack);
player.updateHeldItem();
}
}
}

View File

@@ -14,15 +14,17 @@ public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePu
private int y;
private int z;
private int slot;
private boolean one;
public MessagePushToStorage() {
}
public MessagePushToStorage(int x, int y, int z, int slot) {
public MessagePushToStorage(int x, int y, int z, int slot, boolean one) {
this.x = x;
this.y = y;
this.z = z;
this.slot = slot;
this.one = one;
}
@Override
@@ -31,6 +33,7 @@ public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePu
y = buf.readInt();
z = buf.readInt();
slot = buf.readInt();
one = buf.readBoolean();
}
@Override
@@ -39,6 +42,7 @@ public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePu
buf.writeInt(y);
buf.writeInt(z);
buf.writeInt(slot);
buf.writeBoolean(one);
}
@Override
@@ -50,14 +54,33 @@ public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePu
if (tile instanceof TileController) {
TileController controller = (TileController) tile;
ItemStack stack = message.slot == -1 ? player.inventory.getItemStack() : player.inventory.getStackInSlot(message.slot);
ItemStack stack;
if (message.slot == -1) {
stack = player.inventory.getItemStack().copy();
if (message.one) {
stack.stackSize = 1;
}
} else {
stack = player.inventory.getStackInSlot(message.slot);
}
if (stack != null) {
boolean success = controller.push(stack);
if (success) {
if (message.slot == -1) {
player.inventory.setItemStack(null);
if (message.one) {
player.inventory.getItemStack().stackSize--;
if (player.inventory.getItemStack().stackSize == 0) {
player.inventory.setItemStack(null);
}
} else {
player.inventory.setItemStack(null);
}
player.updateHeldItem();
} else {
player.inventory.setInventorySlotContents(message.slot, null);

View File

@@ -17,6 +17,7 @@ import storagecraft.tile.TileCable;
import storagecraft.tile.TileController;
import storagecraft.tile.TileDrive;
import storagecraft.tile.TileGrid;
import storagecraft.tile.TileStorageProxy;
public class CommonProxy {
public void preInit(FMLPreInitializationEvent e) {
@@ -30,11 +31,13 @@ public class CommonProxy {
GameRegistry.registerTileEntity(TileCable.class, "cable");
GameRegistry.registerTileEntity(TileGrid.class, "grid");
GameRegistry.registerTileEntity(TileDrive.class, "drive");
GameRegistry.registerTileEntity(TileStorageProxy.class, "storageProxy");
GameRegistry.registerBlock(SCBlocks.CONTROLLER, "controller");
GameRegistry.registerBlock(SCBlocks.CABLE, "cable");
GameRegistry.registerBlock(SCBlocks.GRID, "grid");
GameRegistry.registerBlock(SCBlocks.DRIVE, "drive");
GameRegistry.registerBlock(SCBlocks.STORAGE_PROXY, "storageProxy");
GameRegistry.registerItem(SCItems.STORAGE_CELL, "storageCell");
}

View File

@@ -1,6 +1,5 @@
package storagecraft.storage;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@@ -27,16 +26,12 @@ public class CellStorage implements IStorage {
}
@Override
public List<StorageItem> getAll() {
List<StorageItem> items = new ArrayList<StorageItem>();
public void addItems(List<StorageItem> items) {
NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag(NBT_ITEMS);
for (int i = 0; i < list.tagCount(); ++i) {
items.add(createItemFromNBT(list.getCompoundTagAt(i)));
}
return items;
}
@Override
@@ -50,11 +45,7 @@ public class CellStorage implements IStorage {
StorageItem item = createItemFromNBT(tag);
if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
if (item.getTag() != null && !item.getTag().equals(stack.stackTagCompound)) {
continue;
}
if (item.equalsIgnoreQuantity(stack)) {
tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() + stack.stackSize);
return;
@@ -85,11 +76,7 @@ public class CellStorage implements IStorage {
StorageItem item = createItemFromNBT(tag);
if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
if (item.getTag() != null && !item.getTag().equals(stack.stackTagCompound)) {
continue;
}
if (item.equalsIgnoreQuantity(stack)) {
if (quantity > item.getQuantity()) {
quantity = item.getQuantity();
}

View File

@@ -4,7 +4,7 @@ import java.util.List;
import net.minecraft.item.ItemStack;
public interface IStorage {
public List<StorageItem> getAll();
public void addItems(List<StorageItem> items);
public void push(ItemStack stack);

View File

@@ -17,10 +17,8 @@ public class StorageItem {
this.tag = tag;
}
public StorageItem(Item type) {
this.type = type;
this.meta = 0;
this.quantity = 1;
public StorageItem(ItemStack stack) {
this(stack.getItem(), stack.stackSize, stack.getItemDamage(), stack.stackTagCompound);
}
public Item getType() {
@@ -70,4 +68,32 @@ public class StorageItem {
return stack;
}
public boolean equalsIgnoreQuantity(ItemStack other) {
if (tag != null && !tag.equals(other.stackTagCompound)) {
return false;
}
return type == other.getItem() && meta == other.getItemDamage();
}
public boolean equalsIgnoreQuantity(StorageItem other) {
if (tag != null && !tag.equals(other.getTag())) {
return false;
}
return type == other.getType() && meta == other.getMeta();
}
public static boolean equalsIgnoreQuantity(ItemStack first, ItemStack second) {
if (first.stackTagCompound != null && !first.stackTagCompound.equals(second.stackTagCompound)) {
return false;
}
return first.getItem() == second.getItem() && first.getItemDamage() == second.getItemDamage();
}
public boolean equals(StorageItem other) {
return other.getQuantity() == quantity && equalsIgnoreQuantity(other);
}
}

View File

@@ -26,15 +26,11 @@ public class TileCable extends TileSC {
return true;
}
public List<TileMachine> findMachines(TileController controller) {
List<TileMachine> machines = new ArrayList<TileMachine>();
findMachinesInternal(new ArrayList<Vec3>(), machines, controller);
return machines;
public void addMachines(List<TileMachine> machines, TileController controller) {
addMachines(new ArrayList<Vec3>(), machines, controller);
}
private void findMachinesInternal(List<Vec3> visited, List<TileMachine> machines, TileController controller) {
private void addMachines(List<Vec3> visited, List<TileMachine> machines, TileController controller) {
for (Vec3 visitedBlock : visited) {
if (visitedBlock.xCoord == xCoord && visitedBlock.yCoord == yCoord && visitedBlock.zCoord == zCoord) {
return;
@@ -67,7 +63,7 @@ public class TileCable extends TileSC {
visited.add(Vec3.createVectorHelper(x, y, z));
} else if (tile instanceof TileCable) {
((TileCable) tile).findMachinesInternal(visited, machines, controller);
((TileCable) tile).addMachines(visited, machines, controller);
} else if (tile instanceof TileController && (x != controller.xCoord || y != controller.yCoord || z != controller.zCoord)) {
worldObj.createExplosion(null, x, y, z, 4.5f, true);
}

View File

@@ -16,17 +16,15 @@ import storagecraft.storage.IStorageProvider;
import storagecraft.storage.StorageItem;
public class TileController extends TileSC implements IEnergyReceiver, INetworkTile {
public static final int BASE_ENERGY_USAGE = 100;
private List<StorageItem> items = new ArrayList<StorageItem>();
private List<IStorage> storages = new ArrayList<IStorage>();
private List<TileMachine> connectedMachines = new ArrayList<TileMachine>();
private List<TileMachine> machines = new ArrayList<TileMachine>();
private EnergyStorage energy = new EnergyStorage(32000);
private int energyUsage;
private boolean destroyed = false;
private int ticks = 0;
@Override
public void updateEntity() {
@@ -36,43 +34,49 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
return;
}
++ticks;
if (!worldObj.isRemote) {
if (ticks % 40 == 0) {
if (!isActive()) {
disconnectAll();
} else {
List<TileMachine> machines = new ArrayList<TileMachine>();
List<TileMachine> newMachines = new ArrayList<TileMachine>();
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
if (tile instanceof TileCable) {
machines.addAll(((TileCable) tile).findMachines(this));
}
}
for (TileMachine machine : connectedMachines) {
if (!machines.contains(machine)) {
machine.onDisconnected();
((TileCable) tile).addMachines(newMachines, this);
}
}
for (TileMachine machine : machines) {
if (!connectedMachines.contains(machine)) {
if (!newMachines.contains(machine)) {
machine.onDisconnected();
}
}
for (TileMachine machine : newMachines) {
if (!machines.contains(machine)) {
machine.onConnected(this);
}
}
connectedMachines = machines;
machines = newMachines;
syncStorage();
storages.clear();
for (TileMachine machine : machines) {
if (machine instanceof IStorageProvider) {
((IStorageProvider) machine).addStorages(storages);
}
}
syncItems();
}
energyUsage = BASE_ENERGY_USAGE;
energyUsage = 10;
for (TileMachine machine : connectedMachines) {
for (TileMachine machine : machines) {
energyUsage += machine.getEnergyUsage();
}
}
@@ -90,39 +94,63 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
}
private void disconnectAll() {
for (TileMachine machine : connectedMachines) {
for (TileMachine machine : machines) {
machine.onDisconnected();
}
connectedMachines.clear();
machines.clear();
}
public List<TileMachine> getMachines() {
return connectedMachines;
return machines;
}
public List<StorageItem> getItems() {
return items;
}
public void syncStorage() {
storages.clear();
for (TileMachine machine : connectedMachines) {
if (machine instanceof IStorageProvider) {
((IStorageProvider) machine).addStorages(storages);
}
}
syncStorageItems();
}
private void syncStorageItems() {
private void syncItems() {
items.clear();
for (IStorage storage : storages) {
items.addAll(storage.getAll());
storage.addItems(items);
}
combineItems();
}
private void combineItems() {
List<Integer> markedIndexes = new ArrayList<Integer>();
for (int i = 0; i < items.size(); ++i) {
if (markedIndexes.contains(i)) {
continue;
}
StorageItem item = items.get(i);
for (int j = i + 1; j < items.size(); ++j) {
if (markedIndexes.contains(j)) {
continue;
}
StorageItem other = items.get(j);
if (item.equalsIgnoreQuantity(other)) {
item.setQuantity(item.getQuantity() + other.getQuantity());
markedIndexes.add(j);
}
}
}
List<StorageItem> markedItems = new ArrayList<StorageItem>();
for (int i : markedIndexes) {
markedItems.add(items.get(i));
}
items.removeAll(markedItems);
}
public boolean push(ItemStack stack) {
@@ -142,7 +170,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
foundStorage.push(stack);
syncStorageItems();
syncItems();
return true;
}
@@ -159,7 +187,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
}
}
syncStorageItems();
syncItems();
ItemStack newStack = stack.copy();
@@ -223,11 +251,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
Item type = Item.getItemById(buf.readInt());
int quantity = buf.readInt();
int meta = buf.readInt();
NBTTagCompound tag = null;
if (buf.readBoolean()) {
tag = ByteBufUtils.readTag(buf);
}
NBTTagCompound tag = buf.readBoolean() ? ByteBufUtils.readTag(buf) : null;
items.add(new StorageItem(type, quantity, meta, tag));
}

View File

@@ -16,7 +16,15 @@ public class TileDrive extends TileMachine implements IInventory, IStorageProvid
@Override
public int getEnergyUsage() {
return 5;
int base = 5;
for (int i = 0; i < getSizeInventory(); ++i) {
if (getStackInSlot(i) != null) {
base += 2;
}
}
return base;
}
@Override

View File

@@ -15,10 +15,14 @@ public class TileSC extends TileEntity {
private ForgeDirection direction;
protected int ticks;
@Override
public void updateEntity() {
super.updateEntity();
++ticks;
if (!worldObj.isRemote) {
if (this instanceof INetworkTile) {
TargetPoint target = new TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, UPDATE_RANGE);

View File

@@ -0,0 +1,142 @@
package storagecraft.tile;
import java.util.List;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import storagecraft.storage.IStorage;
import storagecraft.storage.IStorageProvider;
import storagecraft.storage.StorageItem;
public class TileStorageProxy extends TileMachine implements IStorageProvider, IStorage {
private IInventory inventory;
@Override
public void updateEntity() {
super.updateEntity();
if (!worldObj.isRemote && isConnected()) {
TileEntity tile = worldObj.getTileEntity(xCoord + getDirection().getOpposite().offsetX, yCoord + getDirection().getOpposite().offsetY, zCoord + getDirection().getOpposite().offsetZ);
if (tile instanceof IInventory) {
inventory = (IInventory) tile;
}
} else {
inventory = null;
}
}
@Override
public int getEnergyUsage() {
return 5;
}
@Override
public void addItems(List<StorageItem> items) {
if (inventory != null) {
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
if (inventory.getStackInSlot(i) != null) {
items.add(new StorageItem(inventory.getStackInSlot(i)));
}
}
}
}
@Override
public void push(ItemStack stack) {
if (inventory == null) {
return;
}
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 (StorageItem.equalsIgnoreQuantity(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
public int take(ItemStack stack) {
if (inventory == null) {
return 0;
}
int quantity = stack.stackSize;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
ItemStack slot = inventory.getStackInSlot(i);
if (slot != null && StorageItem.equalsIgnoreQuantity(slot, stack)) {
if (quantity > slot.stackSize) {
quantity = slot.stackSize;
}
slot.stackSize -= quantity;
if (slot.stackSize == 0) {
inventory.setInventorySlotContents(i, null);
}
return quantity;
}
}
return 0;
}
@Override
public boolean canPush(ItemStack stack) {
if (inventory == null) {
return false;
}
int toGo = stack.stackSize;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
ItemStack slot = inventory.getStackInSlot(i);
if (slot == null) {
return true;
} else if (StorageItem.equalsIgnoreQuantity(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
public void addStorages(List<IStorage> storages) {
storages.add(this);
}
}

View File

@@ -14,6 +14,7 @@ block.storagecraft:controller.name=Controller
block.storagecraft:cable.name=Cable
block.storagecraft:grid.name=Grid
block.storagecraft:drive.name=Drive
block.storagecraft:storageProxy.name=Storage Proxy
item.storagecraft:storageCell.name=Storage Cell
item.storagecraft:storageCell.0.name=1k Storage Cell