diff --git a/cubyz-client/src/io/cubyz/client/Cubyz.java b/cubyz-client/src/io/cubyz/client/Cubyz.java index 53f3d086..3395f446 100644 --- a/cubyz-client/src/io/cubyz/client/Cubyz.java +++ b/cubyz-client/src/io/cubyz/client/Cubyz.java @@ -40,6 +40,7 @@ import io.cubyz.blocks.Block; import io.cubyz.blocks.BlockInstance; import io.cubyz.entity.Entity; import io.cubyz.entity.Player; +import io.cubyz.items.Inventory; import io.cubyz.modding.ModLoader; import io.cubyz.multiplayer.client.CubzClient; import io.cubyz.ui.DebugGUI; @@ -68,7 +69,8 @@ public class Cubyz implements IGameLogic { public static UISystem gameUI; public static World world; - private int inventorySelection = 1; // Selected slot in inventory + private int inventorySelection = 0; // Selected slot in inventory + private static Inventory inventory; private CubyzMeshSelectionDetector msd; @@ -117,6 +119,7 @@ public class Cubyz implements IGameLogic { world.synchronousSeek(dx, dz); int highestY = world.getHighestBlock(dx, dz); world.getLocalPlayer().setPosition(new Vector3f(dx, highestY+2, dz)); + inventory = new Inventory(); } public static void requestJoin(String host) { @@ -373,8 +376,8 @@ public class Cubyz implements IGameLogic { breakCooldown = 10; BlockInstance bi = msd.getSelectedBlockInstance(); if (bi != null && bi.getBlock().getHardness() != -1f) { - inventorySelection = bi.getID();// To be able to build that block again world.removeBlock(bi.getX(), bi.getY(), bi.getZ()); + inventory.addBlock(bi.getBlock(), 1); } } } @@ -383,9 +386,10 @@ public class Cubyz implements IGameLogic { if (buildCooldown == 0) { buildCooldown = 10; Vector3i pos = msd.getEmptyPlace(ctx.getCamera().getPosition()); - Block b = world.getBlocks()[inventorySelection]; // TODO: add inventory + Block b = inventory.getBlock(inventorySelection); // TODO: add inventory if (b != null && pos != null) { world.placeBlock(pos.x, pos.y, pos.z, b); + inventory.addBlock(b, -1); } } } diff --git a/cubyz-common/src/io/cubyz/items/Inventory.java b/cubyz-common/src/io/cubyz/items/Inventory.java new file mode 100644 index 00000000..da843815 --- /dev/null +++ b/cubyz-common/src/io/cubyz/items/Inventory.java @@ -0,0 +1,60 @@ +package io.cubyz.items; + +import io.cubyz.blocks.Block; + +public class Inventory { + private ItemStack[] items = new ItemStack[32]; // First 8 item stacks are the hotbar + + public void addItem(Item i, int amount) { + for(int j = 0; j < items.length; j++) { + if(items[j] != null && items[j].getItem() == i && !items[j].filled()) { + amount -= items[j].add(amount); + if(items[j].empty()) { + items[j] = null; + } + if(amount == 0) { + return; + } + } + } + for(int j = 0; j < items.length; j++) { + if(items[j] == null) { + items[j] = new ItemStack(i); + amount -= items[j].add(amount); + if(amount == 0) { + return; + } + } + } + // TODO: Consider a full inventory + } + + public void addBlock(Block b, int amount) { + for(int j = 0; j < items.length; j++) { + if(items[j] != null && items[j].getBlock() == b && !items[j].filled()) { + amount -= items[j].add(amount); + if(items[j].empty()) { + items[j] = null; + } + if(amount == 0) { + return; + } + } + } + for(int j = 0; j < items.length; j++) { + if(items[j] == null) { + items[j] = new ItemStack(b); + amount -= items[j].add(amount); + if(amount == 0) { + return; + } + } + } + } + + public Block getBlock(int selection) { + if(items[selection] == null) + return null; + return items[selection].getBlock(); + } +} diff --git a/cubyz-common/src/io/cubyz/items/Item.java b/cubyz-common/src/io/cubyz/items/Item.java index f089d064..ee55cb79 100644 --- a/cubyz-common/src/io/cubyz/items/Item.java +++ b/cubyz-common/src/io/cubyz/items/Item.java @@ -65,5 +65,11 @@ public class Item implements IRegistryElement { public Resource getRegistryID() { return id; } + + @Override + public void setID(int ID) { + // TODO Auto-generated method stub + + } } \ No newline at end of file diff --git a/cubyz-common/src/io/cubyz/items/ItemBlock.java b/cubyz-common/src/io/cubyz/items/ItemBlock.java deleted file mode 100644 index b20d856a..00000000 --- a/cubyz-common/src/io/cubyz/items/ItemBlock.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.cubyz.items; - -import io.cubyz.blocks.Block; - -public class ItemBlock extends Item { - - public ItemBlock(Block block) { - this.fullTexturePath = "block/" + block.getTexture(); - this.itemDisplayName = block.getClass().getSimpleName(); - } - -} diff --git a/cubyz-common/src/io/cubyz/items/ItemStack.java b/cubyz-common/src/io/cubyz/items/ItemStack.java index 1b321225..fe6c9a42 100644 --- a/cubyz-common/src/io/cubyz/items/ItemStack.java +++ b/cubyz-common/src/io/cubyz/items/ItemStack.java @@ -6,16 +6,25 @@ import org.jungle.Texture; import org.jungle.util.Material; import org.jungle.util.OBJLoader; -@SuppressWarnings("deprecation") +import io.cubyz.blocks.Block; + public class ItemStack { private Item item; + private Block block; private Spatial spatial; + int number = 0; public ItemStack(Item item) { this.item = item; } + public ItemStack(Block block) { + this.block = block; + item = new Item(); + item.setTexture(block.getTexture()); + } + public void update() {} public Mesh getMesh() { @@ -36,10 +45,35 @@ public class ItemStack { return item._meshCache; } + public boolean filled() { + return number >= item.stackSize; + } + + public boolean empty() { + return number <= 0; + } + + public int add(int number) { + this.number += number; + if(this.number > item.stackSize) { + number = number-this.number+item.stackSize; + this.number = item.stackSize; + } + if(this.number < 0) { + number = number-this.number; + this.number = 0; + } + return number; + } + public Item getItem() { return item; } + public Block getBlock() { + return block; + } + public Spatial getSpatial() { if (spatial == null) { spatial = new Spatial(getMesh());