Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Fajita
- * This takes care of the general-ness of Tile Entities that use my Energy System
- */
- public class EnergyTE extends TileEntity implements IInventory{
- public ItemStack[] inventory;
- private String customName;
- /** Normally the slot the capsule or EO Item goes into */
- private int inventorySize = 1;
- /** energy used per use */
- private int eupu;
- // Usually the energy that is stored in either the Energy Capsule or the EO Item
- private int currentEnergy;
- public EnergyTE(int invSize) {
- inventorySize = invSize;
- inventory = new ItemStack[inventorySize];
- }
- /** Sets Current Energy for te */
- public final void setCurrentEnergy(int currentEnergy){
- this.currentEnergy = currentEnergy;
- }
- /** Gets Current Energy from TE*/
- public final int getCurrentEnergy(){
- return currentEnergy;
- }
- /**
- * @param eupu mean Energy Used per use
- */
- public final void setEUPU(int eupu){
- this.eupu = eupu;
- }
- /** Returns Energy Used per use */
- public final int getEUPU(){
- return eupu;
- }
- /** Use Current energy from te */
- public final void useEnergy(int energyUsed){
- this.currentEnergy -= energyUsed;
- }
- /**
- * Use this when Energy Capsule is in inventory
- * If no Energy Capsule is present, returns false
- */
- public final boolean saveEnergyCapsule(){
- ItemStack stack = getStackInSlot(0);
- if(stack != null && stack.getItem() instanceof EnergyItem){
- EnergyItem.saveCurrentEnergy(stack, currentEnergy);
- }
- return false;
- }
- /**
- * Use this when Energy Capsule isn't in inventory
- * @param stack: The stack that's saving
- */
- public final void saveEnergyCapsule(ItemStack stack){
- if(stack != null && stack.getItem() instanceof EnergyItem){
- EnergyItem.saveCurrentEnergy(stack, currentEnergy);
- }
- }
- @Override
- public int getSizeInventory() {
- return inventorySize;
- }
- @Override
- public final String getCommandSenderName() {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public final boolean hasCustomName() {
- // TODO Auto-generated method stub
- return (customName != null && !customName.equals(""));
- }
- public final void setCustomName(String name){
- customName = name;
- }
- public final String getCustomName(){
- return customName;
- }
- @Override
- public final IChatComponent getDisplayName() {
- return this.hasCustomName() ? new ChatComponentText(this.getCustomName()) : new ChatComponentTranslation(this.getCustomName());
- }
- @Override
- public final ItemStack getStackInSlot(int index) {
- if(index >= 0 && index <= inventorySize){
- return inventory[index];
- }
- return null;
- }
- @Override
- public final ItemStack decrStackSize(int index, int count) {
- if (this.getStackInSlot(index) != null) {
- ItemStack itemstack;
- if (this.getStackInSlot(index).stackSize <= count) {
- itemstack = this.getStackInSlot(index);
- this.setInventorySlotContents(index, null);
- this.markDirty();
- return itemstack;
- } else {
- itemstack = this.getStackInSlot(index).splitStack(count);
- if (this.getStackInSlot(index).stackSize <= 0) {
- this.setInventorySlotContents(index, null);
- } else {
- //Just to show that changes happened
- this.setInventorySlotContents(index, this.getStackInSlot(index));
- }
- this.markDirty();
- return itemstack;
- }
- } else {
- return null;
- }
- }
- @Override
- public final ItemStack getStackInSlotOnClosing(int index) {
- ItemStack stack = this.getStackInSlot(index);
- this.setInventorySlotContents(index, null);
- return stack;
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- if (index >= 0 && index <= getSizeInventory()){
- if(stack != null){
- if(stack.stackSize > this.getInventoryStackLimit()){
- stack.stackSize = getInventoryStackLimit();
- }
- else if(stack.stackSize == 0){
- stack = null;
- }
- }
- }
- else{
- return;
- }
- inventory[index] = stack;
- markDirty();
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public final boolean isUseableByPlayer(EntityPlayer player) {
- return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
- }
- @Override
- public void openInventory(EntityPlayer player) {}
- @Override
- public void closeInventory(EntityPlayer player) {}
- /**
- * @return True if index is 0 and if item being placed is of energyItem;
- */
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack){
- Item item = (stack != null) ? stack.getItem(): Items.beef;
- return (index == 0 && item instanceof EnergyItem);
- }
- /**
- * Keeps currentEnergy of the TE synced with client
- * @param id 0: Current Energy
- * @param id 1: eupu
- */
- @Override
- public int getField(int id) {
- switch(id){
- case 0: return this.currentEnergy;
- case 1: return (int) eupu;
- }
- return 0;
- }
- /**
- * Syncs CurrentEnergy and eupu from TE with client
- * @param id 0: Current Energy
- * @param id 1: eupu
- */
- @Override
- public void setField(int id, int value) {
- switch(id){
- case 0: setCurrentEnergy(value); break;
- case 1: setEUPU(value); break;
- }
- }
- /**
- * call this and add to it for child classes that have more than the setField option
- */
- @Override
- public int getFieldCount() {
- return 2;
- }
- /** Deletes inventory */
- @Override
- public final void clear() {
- for(int i = 0; i < getSizeInventory(); i++){
- inventory[i] = null;
- }
- markDirty();
- }
- /**
- * ONLY saves the inventory. Be SURE to call super, and then save whatever else you need to
- */
- @Override
- public void writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- NBTTagList items = new NBTTagList();
- for(int i = 0; i < getSizeInventory(); i++){
- if(getStackInSlot(i) != null){
- NBTTagCompound item = new NBTTagCompound();
- item.setByte("Slot", (byte) i);
- getStackInSlot(i).writeToNBT(item);
- items.appendTag(item);
- }
- }
- compound.setTag("Items", items);
- }
- /**
- * ONLY reads the inventory. Be SURE to call super, and then read whatever else you need to
- */
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- NBTTagList items = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
- for(int i = 0; i < items.tagCount(); i++){
- NBTTagCompound item = items.getCompoundTagAt(i);
- int slot = item.getByte("Slot");
- setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
- }
- if(compound.hasKey("CustomName", 8)){
- setCustomName(compound.getString("CustomName"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement