storage cells + drives

This commit is contained in:
Raoul Van den Berge
2015-12-12 21:22:01 +01:00
parent 9cb06025b1
commit 8e443e5fd9
19 changed files with 396 additions and 38 deletions

View File

@@ -9,8 +9,16 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import java.util.Random;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import storagecraft.proxy.CommonProxy;
@Mod(modid = SC.ID, version = SC.VERSION)
@@ -18,6 +26,7 @@ public class SC {
public static class GUI {
public static final int CONTROLLER = 0;
public static final int GRID = 1;
public static final int DRIVE = 2;
}
public static final String ID = "storagecraft";
@@ -48,4 +57,78 @@ public class SC {
public void postInit(FMLPostInitializationEvent e) {
PROXY.postInit(e);
}
public static void saveInventory(IInventory inventory, NBTTagCompound nbt) {
NBTTagList tagList = new NBTTagList();
for (int i = 0; i < inventory.getSizeInventory(); i++) {
if (inventory.getStackInSlot(i) != null) {
NBTTagCompound compoundTag = new NBTTagCompound();
compoundTag.setInteger("Slot", i);
inventory.getStackInSlot(i).writeToNBT(compoundTag);
tagList.appendTag(compoundTag);
}
}
nbt.setTag("Inventory", tagList);
}
public static void restoreInventory(IInventory inventory, NBTTagCompound nbt) {
if (nbt.hasKey("Inventory")) {
NBTTagList tagList = nbt.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.tagCount(); i++) {
int slot = tagList.getCompoundTagAt(i).getInteger("Slot");
ItemStack stack = ItemStack.loadItemStackFromNBT(tagList.getCompoundTagAt(i));
inventory.setInventorySlotContents(slot, stack);
}
}
}
/**
* @author cpw
* @see https://github.com/cpw/ironchest/blob/master/src/main/java/cpw/mods/ironchest/BlockIronChest.java#L195
*/
public static void dropInventoryContent(World world, IInventory inventory, int xCoord, int yCoord, int zCoord, int newSize) {
Random random = world.rand;
for (int i = newSize; i < inventory.getSizeInventory(); ++i) {
ItemStack stack = inventory.getStackInSlot(i);
if (stack == null) {
continue;
}
float f = random.nextFloat() * 0.8F + 0.1F;
float f1 = random.nextFloat() * 0.8F + 0.1F;
float f2 = random.nextFloat() * 0.8F + 0.1F;
while (stack.stackSize > 0) {
int i1 = random.nextInt(21) + 10;
if (i1 > stack.stackSize) {
i1 = stack.stackSize;
}
stack.stackSize -= i1;
EntityItem entityItem = new EntityItem(world, (float) xCoord + f, (float) yCoord + (newSize > 0 ? 1 : 0) + f1, (float) zCoord + f2, new ItemStack(stack.getItem(), i1, stack.getItemDamage()));
entityItem.motionX = (float) random.nextGaussian() * 0.05F;
entityItem.motionY = (float) random.nextGaussian() * 0.05F + 0.2F;
entityItem.motionZ = (float) random.nextGaussian() * 0.05F;
if (stack.hasTagCompound()) {
entityItem.getEntityItem().setTagCompound((NBTTagCompound) stack.getTagCompound().copy());
}
world.spawnEntityInWorld(entityItem);
}
}
}
}

View File

@@ -2,10 +2,12 @@ package storagecraft;
import storagecraft.block.BlockCable;
import storagecraft.block.BlockController;
import storagecraft.block.BlockDrive;
import storagecraft.block.BlockGrid;
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();
}

View File

@@ -1,4 +1,7 @@
package storagecraft;
import storagecraft.item.ItemStorageCell;
public class SCItems {
public static final ItemStorageCell STORAGE_CELL = new ItemStorageCell();
}

View File

@@ -0,0 +1,28 @@
package storagecraft.block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import storagecraft.SC;
import storagecraft.tile.TileDrive;
public class BlockDrive extends BlockSC implements ITileEntityProvider {
public BlockDrive() {
super("drive");
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
if (!world.isRemote) {
player.openGui(SC.INSTANCE, SC.GUI.DRIVE, world, x, y, z);
}
return true;
}
@Override
public TileEntity createNewTileEntity(World world, int meta) {
return new TileDrive();
}
}

View File

@@ -3,7 +3,9 @@ package storagecraft.block;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
@@ -31,8 +33,10 @@ public class BlockSC extends Block {
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemStack) {
super.onBlockPlacedBy(world, x, y, z, entityLiving, itemStack);
if (world.getTileEntity(x, y, z) instanceof TileSC) {
ForgeDirection direction = ForgeDirection.UNKNOWN;
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileSC) {
ForgeDirection direction = null;
int facing = MathHelper.floor_double(entityLiving.rotationYaw * 4.0F / 360.0F + 0.5D) & 3;
@@ -51,7 +55,18 @@ public class BlockSC extends Block {
break;
}
((TileSC) world.getTileEntity(x, y, z)).setDirection(direction);
((TileSC) tile).setDirection(direction);
}
}
@Override
public void onBlockPreDestroy(World world, int x, int y, int z, int meta) {
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof IInventory) {
SC.dropInventoryContent(world, (IInventory) tile, x, y, z, 0);
}
super.onBlockPreDestroy(world, x, y, z, meta);
}
}

View File

@@ -0,0 +1,32 @@
package storagecraft.gui;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import storagecraft.inventory.ContainerDrive;
public class GuiDrive extends GuiContainer {
public static final ResourceLocation DRIVE_RESOURCE = new ResourceLocation("storagecraft:textures/gui/drive.png");
public GuiDrive(ContainerDrive container) {
super(container);
this.xSize = 176;
this.ySize = 190;
}
@Override
protected void drawGuiContainerBackgroundLayer(float renderPartialTicks, int mouseX, int mouseY) {
GL11.glColor3f(1.0F, 1.0F, 1.0F);
mc.getTextureManager().bindTexture(DRIVE_RESOURCE);
int x = (this.width - xSize) / 2;
int y = (this.height - ySize) / 2;
drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
fontRendererObj.drawString("Drive", x + 7, y + 7, 4210752);
fontRendererObj.drawString("Inventory", x + 7, y + 96, 4210752);
}
}

View File

@@ -7,17 +7,21 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import storagecraft.SC;
import storagecraft.inventory.ContainerController;
import storagecraft.inventory.ContainerDrive;
import storagecraft.inventory.ContainerGrid;
import storagecraft.tile.TileController;
import storagecraft.tile.TileDrive;
import storagecraft.tile.TileGrid;
public class GuiHandler implements IGuiHandler {
private Container getContainer(int ID, EntityPlayer player) {
private Container getContainer(int ID, EntityPlayer player, TileEntity tile) {
switch (ID) {
case SC.GUI.CONTROLLER:
return new ContainerController(player);
case SC.GUI.GRID:
return new ContainerGrid(player);
case SC.GUI.DRIVE:
return new ContainerDrive(player, (TileDrive) tile);
default:
return null;
}
@@ -25,7 +29,7 @@ public class GuiHandler implements IGuiHandler {
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
return getContainer(ID, player);
return getContainer(ID, player, world.getTileEntity(x, y, z));
}
@Override
@@ -34,9 +38,11 @@ public class GuiHandler implements IGuiHandler {
switch (ID) {
case SC.GUI.CONTROLLER:
return new GuiController((ContainerController) getContainer(ID, player), (TileController) tile);
return new GuiController((ContainerController) getContainer(ID, player, tile), (TileController) tile);
case SC.GUI.GRID:
return new GuiGrid((ContainerGrid) getContainer(ID, player), (TileGrid) tile);
return new GuiGrid((ContainerGrid) getContainer(ID, player, tile), (TileGrid) tile);
case SC.GUI.DRIVE:
return new GuiDrive((ContainerDrive) getContainer(ID, player, tile));
default:
return null;
}

View File

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

View File

@@ -21,12 +21,14 @@ public class ContainerSC extends Container {
for (int i = 0; i < 9; i++) {
addSlotToContainer(new Slot(player.inventory, id, xInventory + i * 18, yInventory + 4 + (3 * 18)));
id++;
}
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 9; x++) {
addSlotToContainer(new Slot(player.inventory, id, xInventory + x * 18, yInventory + y * 18));
id++;
}
}

View File

@@ -6,22 +6,18 @@ import net.minecraft.item.ItemStack;
public class InventoryBasic implements IInventory {
private ItemStack[] inventory;
private int inventorySize;
private int size;
private String name;
public InventoryBasic(String name, int size) {
this.name = name;
this.inventorySize = size;
this.inventory = new ItemStack[inventorySize];
}
public void setInventoryStack(ItemStack[] stack) {
this.inventory = stack;
this.size = size;
this.inventory = new ItemStack[size];
}
@Override
public int getSizeInventory() {
return inventory.length;
return size;
}
@Override
@@ -30,38 +26,42 @@ public class InventoryBasic implements IInventory {
}
@Override
public ItemStack decrStackSize(int slotIndex, int decrementAmount) {
ItemStack itemStack = getStackInSlot(slotIndex);
if (itemStack != null) {
if (itemStack.stackSize <= decrementAmount) {
setInventorySlotContents(slotIndex, null);
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
if (stack.stackSize <= amount) {
setInventorySlotContents(slot, null);
} else {
itemStack = itemStack.splitStack(decrementAmount);
stack = stack.splitStack(amount);
if (itemStack.stackSize == 0) {
setInventorySlotContents(slotIndex, null);
if (stack.stackSize == 0) {
setInventorySlotContents(slot, null);
}
}
}
return itemStack;
return stack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slotIndex) {
ItemStack itemStack = getStackInSlot(slotIndex);
if (itemStack != null) {
setInventorySlotContents(slotIndex, null);
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
setInventorySlotContents(slot, null);
}
return itemStack;
return stack;
}
@Override
public void setInventorySlotContents(int slotIndex, ItemStack itemStack) {
if (itemStack != null && itemStack.stackSize > getInventoryStackLimit()) {
itemStack.stackSize = getInventoryStackLimit();
public void setInventorySlotContents(int slot, ItemStack stack) {
if (stack != null && stack.stackSize > getInventoryStackLimit()) {
stack.stackSize = getInventoryStackLimit();
}
inventory[slotIndex] = itemStack;
inventory[slot] = stack;
}
@Override
@@ -80,7 +80,7 @@ public class InventoryBasic implements IInventory {
}
@Override
public boolean isUseableByPlayer(EntityPlayer var1) {
public boolean isUseableByPlayer(EntityPlayer player) {
return true;
}
@@ -93,7 +93,7 @@ public class InventoryBasic implements IInventory {
}
@Override
public boolean isItemValidForSlot(int var1, ItemStack var2) {
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return true;
}

View File

@@ -0,0 +1,21 @@
package storagecraft.inventory.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class SlotItemFilter extends Slot {
private Item item;
public SlotItemFilter(IInventory inventory, int id, int x, int y, Item item) {
super(inventory, id, x, y);
this.item = item;
}
@Override
public boolean isItemValid(ItemStack item) {
return item.getItem() == this.item;
}
}

View File

@@ -0,0 +1,26 @@
package storagecraft.item;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import storagecraft.SC;
public class ItemSC extends Item {
private String name;
public ItemSC(String name) {
this.name = name;
setCreativeTab(SC.TAB);
setTextureName("storagecraft:" + name);
}
@Override
public String getUnlocalizedName() {
return "item." + SC.ID + ":" + name;
}
@Override
public String getUnlocalizedName(ItemStack stack) {
return getUnlocalizedName();
}
}

View File

@@ -0,0 +1,9 @@
package storagecraft.item;
public class ItemStorageCell extends ItemSC {
public ItemStorageCell() {
super("storageCell");
setMaxStackSize(1);
}
}

View File

@@ -8,12 +8,14 @@ import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import storagecraft.SC;
import storagecraft.SCBlocks;
import storagecraft.SCItems;
import storagecraft.gui.GuiHandler;
import storagecraft.network.MessagePullFromStorage;
import storagecraft.network.MessagePushToStorage;
import storagecraft.network.MessageTileUpdate;
import storagecraft.tile.TileCable;
import storagecraft.tile.TileController;
import storagecraft.tile.TileDrive;
import storagecraft.tile.TileGrid;
public class CommonProxy {
@@ -27,10 +29,14 @@ public class CommonProxy {
GameRegistry.registerTileEntity(TileController.class, "controller");
GameRegistry.registerTileEntity(TileCable.class, "cable");
GameRegistry.registerTileEntity(TileGrid.class, "grid");
GameRegistry.registerTileEntity(TileDrive.class, "drive");
GameRegistry.registerBlock(SCBlocks.CONTROLLER, "controller");
GameRegistry.registerBlock(SCBlocks.CABLE, "cable");
GameRegistry.registerBlock(SCBlocks.GRID, "grid");
GameRegistry.registerBlock(SCBlocks.DRIVE, "drive");
GameRegistry.registerItem(SCItems.STORAGE_CELL, "storageCell");
}
public void init(FMLInitializationEvent e) {

View File

@@ -0,0 +1,91 @@
package storagecraft.tile;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import storagecraft.SC;
import storagecraft.inventory.InventoryBasic;
public class TileDrive extends TileMachine implements IInventory {
private InventoryBasic inventory = new InventoryBasic("drive", 8);
@Override
public int getEnergyUsage() {
return 5;
}
@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);
SC.restoreInventory(this, nbt);
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
SC.saveInventory(this, nbt);
}
}

View File

@@ -4,7 +4,10 @@ import io.netty.buffer.ByteBuf;
public abstract class TileMachine extends TileSC implements INetworkTile {
protected boolean connected = false;
private int xController, yController, zController;
private int xController;
private int yController;
private int zController;
public void onConnected(TileController controller) {
this.connected = true;

View File

@@ -3,3 +3,6 @@ itemGroup.storagecraft=StorageCraft
block.storagecraft:controller.name=Controller
block.storagecraft:cable.name=Cable
block.storagecraft:grid.name=Grid
block.storagecraft:drive.name=Drive
item.storagecraft:storageCell.name=Storage Cell

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 B