Guest User

UpdateEntity MC

a guest
Jul 26th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.54 KB | None | 0 0
  1. package firstetestmod;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import ic2.api.energy.event.EnergyTileLoadEvent;
  7. import ic2.api.energy.event.EnergyTileUnloadEvent;
  8. import ic2.api.energy.prefab.BasicSink;
  9. import ic2.api.energy.tile.IEnergySink;
  10. import ic2.api.energy.tile.IEnergyTile;
  11. import ic2.api.info.Info;
  12. import ic2.api.network.INetworkDataProvider;
  13. import ic2.api.network.INetworkTileEntityEventListener;
  14. import ic2.api.network.INetworkUpdateListener;
  15. import ic2.api.tile.IWrenchable;
  16. import net.minecraft.entity.EntityLivingBase;
  17. import net.minecraft.entity.item.EntityTNTPrimed;
  18. import net.minecraft.entity.player.EntityPlayer;
  19. import net.minecraft.init.Items;
  20. import net.minecraft.inventory.IInventory;
  21. import net.minecraft.inventory.ISidedInventory;
  22. import net.minecraft.item.Item;
  23. import net.minecraft.item.ItemStack;
  24. import net.minecraft.nbt.NBTTagCompound;
  25. import net.minecraft.nbt.NBTTagList;
  26. import net.minecraft.tileentity.TileEntity;
  27. import net.minecraftforge.common.MinecraftForge;
  28. import net.minecraftforge.common.util.ForgeDirection;
  29. import cpw.mods.fml.common.FMLCommonHandler;
  30. import cpw.mods.fml.relauncher.Side;
  31. import cpw.mods.fml.relauncher.SideOnly;
  32.  
  33. public class TileEntityPlateMachine extends BasicSink implements
  34.         ISidedInventory, IWrenchable, IEnergyTile, IInventory, IEnergySink {
  35.         public TileEntityPlateMachine(TileEntity TileEntityPlateMachine, int maxPower, int tier1) {
  36.         super(TileEntityPlateMachine, maxPower, tier1);
  37.  
  38.     }
  39.  
  40.     private static final int[] slots_top = new int[] { 0 };
  41.     private static final int[] slots_bottom = new int[] { 2, 1 };
  42.     private static final int[] slots_sides = new int[] { 1 };
  43.  
  44.     /**
  45.      * The ItemStacks that hold the items currently being used in the furnace
  46.      */
  47.     private ItemStack[] slots = new ItemStack[3];
  48.  
  49.     /**
  50.      * the speed of this furnace, 200 is normal / how many ticks it takes : 30
  51.      * ticks = 1 second
  52.      */
  53.     public int maceratingSpeed = 500;
  54.  
  55.     /** Power */
  56.     public double power = 0.0D;
  57.     /** Max Power */
  58.     public double maxPower = 32000.0D;
  59.     public boolean addedToEnet;
  60.  
  61.     /** The number of ticks that the current item has been cooking for */
  62.     public int cookTime;
  63.  
  64.     private String field_94130_e;
  65.     private String PlateMachineName;
  66.     private Object field_145850_b;
  67.  
  68.     public void PlateMachineName(String string) {
  69.         this.PlateMachineName = string;
  70.     }
  71.  
  72.     /**
  73.      * Returns the number of slots in the inventory.
  74.      */
  75.     public int getSizeInventory() {
  76.         return this.slots.length;
  77.     }
  78.  
  79.     /**
  80.      * Returns the stack in slot i
  81.      */
  82.     public ItemStack getStackInSlot(int par1) {
  83.         return this.slots[par1];
  84.     }
  85.  
  86.     /**
  87.      * Removes from an inventory slot (first arg) up to a specified number
  88.      * (second arg) of items and returns them in a new stack.
  89.      */
  90.     public ItemStack decrStackSize(int par1, int par2) {
  91.         if (this.slots[par1] != null) {
  92.             ItemStack itemstack;
  93.  
  94.             if (this.slots[par1].stackSize <= par2) {
  95.                 itemstack = this.slots[par1];
  96.                 this.slots[par1] = null;
  97.                 return itemstack;
  98.             } else {
  99.                 itemstack = this.slots[par1].splitStack(par2);
  100.  
  101.                 if (this.slots[par1].stackSize == 0) {
  102.                     this.slots[par1] = null;
  103.                 }
  104.  
  105.                 return itemstack;
  106.             }
  107.         } else {
  108.             return null;
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * When some containers are closed they call this on each slot, then drop
  114.      * whatever it returns as an EntityItem - like when you close a workbench
  115.      * GUI.
  116.      */
  117.     public ItemStack getStackInSlotOnClosing(int par1) {
  118.         if (this.slots[par1] != null) {
  119.             ItemStack itemstack = this.slots[par1];
  120.             this.slots[par1] = null;
  121.             return itemstack;
  122.         } else {
  123.             return null;
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * Sets the given item stack to the specified slot in the inventory (can be
  129.      * crafting or armor sections).
  130.      */
  131.     public void setInventorySlotContents(int par1, ItemStack par2ItemStack) {
  132.         this.slots[par1] = par2ItemStack;
  133.  
  134.         if (par2ItemStack != null
  135.                 && par2ItemStack.stackSize > this.getInventoryStackLimit()) {
  136.             par2ItemStack.stackSize = this.getInventoryStackLimit();
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Returns the name of the inventory.
  142.      */
  143.     public String getInvName() {
  144.         return this.isInvNameLocalized() ? this.field_94130_e
  145.                 : "container.PlateMachine";
  146.     }
  147.  
  148.     /**
  149.      * If this returns false, the inventory name will be used as an unlocalized
  150.      * name, and translated into the player's language. Otherwise it will be
  151.      * used directly.
  152.      */
  153.     public boolean isInvNameLocalized() {
  154.         return this.field_94130_e != null && this.field_94130_e.length() > 0;
  155.     }
  156.  
  157.     @Override
  158.     public String getInventoryName() {
  159.         return this.hasCustomInventoryName() ? this.field_94130_e
  160.                 : "container.PlateMachine";
  161.     }
  162.  
  163.     public boolean isInventoryName() {
  164.         return this.field_94130_e != null && this.field_94130_e.length() > 0;
  165.     }
  166.  
  167.     /**
  168.      * Sets the custom display name to use when opening a GUI linked to this
  169.      * tile entity.
  170.      */
  171.     public void setGuiDisplayName(String par1Str) {
  172.         this.field_94130_e = par1Str;
  173.     }
  174.  
  175.     /**
  176.      * Reads a tile entity from NBT.
  177.      */
  178.     public void readFromNBT(NBTTagCompound par1NBTTagCompound) {
  179.         super.readFromNBT(par1NBTTagCompound);
  180.  
  181.         if (par1NBTTagCompound.hasKey("power")) {
  182.             this.power = par1NBTTagCompound.getDouble("power");
  183.         }
  184.  
  185.         NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items", 10);
  186.         this.slots = new ItemStack[this.getSizeInventory()];
  187.  
  188.         for (int i = 0; i < nbttaglist.tagCount(); ++i) {
  189.             NBTTagCompound nbttagcompound1 = (NBTTagCompound) nbttaglist
  190.                     .getCompoundTagAt(i);
  191.             byte b0 = nbttagcompound1.getByte("Slot");
  192.  
  193.             if (b0 >= 0 && b0 < this.slots.length) {
  194.                 this.slots[b0] = ItemStack
  195.                         .loadItemStackFromNBT(nbttagcompound1);
  196.             }
  197.         }
  198.         this.cookTime = par1NBTTagCompound.getShort("CookTime");
  199.  
  200.         if (par1NBTTagCompound.hasKey("container.PlateMachineName")) {
  201.             this.PlateMachineName = par1NBTTagCompound
  202.                     .getString("container.PlateMachineName");
  203.         }
  204.     }
  205.  
  206.     /**
  207.      * Writes a tile entity to NBT.
  208.      */
  209.     public void writeToNBT(NBTTagCompound par1NBTTagCompound) {
  210.         super.writeToNBT(par1NBTTagCompound);
  211.  
  212.         par1NBTTagCompound.setDouble("power", this.power);
  213.         par1NBTTagCompound.setShort("power", (short) this.power);
  214.         par1NBTTagCompound.setShort("CookTime", (short) this.cookTime);
  215.         NBTTagList nbttaglist = new NBTTagList();
  216.  
  217.         for (int i = 0; i < this.slots.length; ++i) {
  218.             if (this.slots[i] != null) {
  219.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  220.                 nbttagcompound1.setByte("Slot", (byte) i);
  221.                 this.slots[i].writeToNBT(nbttagcompound1);
  222.                 nbttaglist.appendTag(nbttagcompound1);
  223.             }
  224.         }
  225.         par1NBTTagCompound.setTag("Items", nbttaglist);
  226.  
  227.         if (this.isInventoryName()) {
  228.             par1NBTTagCompound.setString("container.PlateMachineName",
  229.                     this.PlateMachineName);
  230.         }
  231.     }
  232.  
  233.     /**
  234.      * Returns the maximum stack size for a inventory slot. Seems to always be
  235.      * 64, possibly will be extended. *Isn't this more of a set than a get?*
  236.      */
  237.     public int getInventoryStackLimit() {
  238.         return 64;
  239.     }
  240.  
  241.     @SideOnly(Side.CLIENT)
  242.     /**
  243.      * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  244.      * cooked
  245.      */
  246.     public int getCookProgressScaled(int par1) {
  247.         return this.cookTime * par1 / this.maceratingSpeed;
  248.     }
  249.  
  250.     public double getPowerRemainingScaled(double par1) {
  251.         return this.power * par1 / this.maxPower;
  252.     }
  253.  
  254.     /**
  255.      * Returns true if the furnace is currently burning
  256.      */
  257.     public boolean hasPower() {
  258.         return this.power > 0;
  259.     }
  260.  
  261.     public boolean isMacerating() {
  262.         return this.cookTime > 0;
  263.     }
  264.  
  265.     /**
  266.      * Allows the entity to update its state. Overridden in most subclasses,
  267.      * e.g. the mob spawner uses this to count ticks and creates a new spawn
  268.      * inside its implementation.
  269.      */
  270.     public void updateEntity() {
  271.         boolean flag = this.power > 0;
  272.         boolean flag1 = false;
  273.         if (hasPower() && isMacerating()) {
  274.             this.power--;
  275.         }
  276.         if (!this.worldObj.isRemote) {
  277.    
  278.             if (this.canSmelt()) {
  279.                 ++this.cookTime;
  280.  
  281.                 if (this.cookTime == this.maceratingSpeed) {
  282.                     this.cookTime = 0;
  283.                     this.smeltItem();
  284.                     flag1 = true;
  285.                 }
  286.             } else {
  287.                 this.cookTime = 0;
  288.             }
  289.  
  290.             if (this.isMacerating()) {
  291.                 flag1 = true;
  292.                 PlateMachine.updateFurnaceBlockState(this.cookTime > 0,this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  293.        
  294.             }
  295.         }
  296.  
  297.  
  298.         if (flag1) {
  299.             this.markDirty();
  300.         }
  301.         {
  302.             if (!addedToEnet) onLoaded();
  303.             }
  304.     }
  305.  
  306.     /**
  307.      * Returns true if the furnace can smelt an item, i.e. has a source item,
  308.      * destination stack isn't full, etc.
  309.      */
  310.  
  311.     private boolean canSmelt() {
  312.         if (this.slots[0] == null) {
  313.             return false;
  314.         } else if(this.power > 0) {
  315.             ItemStack itemstack = PlateMachineRecipe
  316.                     .getPlateMachineResult(this.slots[0].getItem());
  317.             if (itemstack == null)
  318.                 return false;
  319.             if (this.slots[2] == null)
  320.                 return true;
  321.             if (!this.slots[2].isItemEqual(itemstack))
  322.                 return false;
  323.  
  324.             return slots[2].stackSize < itemstack.getMaxStackSize();
  325.         }
  326.         return false;
  327.     }
  328.  
  329.     /**
  330.      * Turn one item from the furnace source stack into the appropriate smelted
  331.      * item in the furnace result stack
  332.      */
  333.     public void smeltItem() {
  334.         if(this.canSmelt()) {
  335.             ItemStack itemstack = PlateMachineRecipe.getPlateMachineResult(this.slots[0].getItem());
  336.  
  337.             if (this.slots[2] == null) {
  338.                 this.slots[2] = itemstack.copy();
  339.             } else if (this.slots[2].isItemEqual(itemstack)) {
  340.                 slots[2].stackSize += (itemstack.stackSize);
  341.             }
  342.  
  343.             --this.slots[0].stackSize;
  344.  
  345.             if (this.slots[0].stackSize <= 0) {
  346.                 this.slots[0] = null;
  347.             }
  348.         }
  349.     }
  350.  
  351.     /**
  352.      * Returns the number of ticks that the supplied fuel item will keep the
  353.      * furnace burning, or 0 if the item isn't fuel
  354.      * @deprecated
  355.      */
  356.     public static double getItemPower(ItemStack itemstack) {
  357.         if (itemstack == null) {
  358.             return 0;
  359.         } else {
  360.             Item item = itemstack.getItem();
  361.  
  362.             if (item == Items.redstone)
  363.                 return 10.0D;
  364.             if (item == Items.nether_star)
  365.                 return 50.0D;
  366.             if (item == firsttestmodmain.LacirisItem)
  367.                 return 500.0D;
  368.             if (item == firsttestmodmain.UniversalMatter)
  369.                 return 1000.0D;
  370.             return 0.0D;
  371.         }
  372.     }
  373.  
  374.     /**
  375.      * Return true if item is a fuel source (getItempower() > 0).
  376.      * @deprecated
  377.      */
  378.     public static boolean isItemFuel(ItemStack par0ItemStack) {
  379.         return getItemPower(par0ItemStack) > 0;
  380.     }
  381.  
  382.     /**
  383.      * Do not make give this method the name canInteractWith because it clashes
  384.      * with Container
  385.      */
  386.     public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) {
  387.         return this.worldObj.getTileEntity(this.xCoord, this.yCoord,
  388.                 this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq(
  389.                 (double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D,
  390.                 (double) this.zCoord + 0.5D) <= 64.0D;
  391.     }
  392.  
  393.     public void openChest() {
  394.     }
  395.  
  396.     public void closeChest() {
  397.     }
  398.  
  399.     /**
  400.      * Returns true if automation is allowed to insert the given stack (ignoring
  401.      * stack size) into the given slot.
  402.      */
  403.     public boolean isItemValidForSlot(int par1, ItemStack par2ItemStack) {
  404.         return par1 == 2 ? false : (par1 == 1 ? isItemFuel(par2ItemStack)
  405.                 : true);
  406.     }
  407.  
  408.     /**
  409.      * Returns an array containing the indices of the slots that can be accessed
  410.      * by automation on the given side of this block.
  411.      */
  412.     public int[] getAccessibleSlotsFromSide(int par1) {
  413.         return par1 == 0 ? slots_bottom : (par1 == 1 ? slots_top : slots_sides);
  414.     }
  415.  
  416.     /**
  417.      * Returns true if automation can insert the given item in the given slot
  418.      * from the given side. Args: Slot, item, side
  419.      */
  420.     public boolean canInsertItem(int par1, ItemStack par2ItemStack, int par3) {
  421.         return this.isItemValidForSlot(par1, par2ItemStack);
  422.     }
  423.  
  424.     /**
  425.      * Returns true if automation can extract the given item in the given slot
  426.      * from the given side. Args: Slot, item, side
  427.      */
  428.     public boolean canExtractItem(int par1, ItemStack par2ItemStack, int par3) {
  429.         return par3 != 0 || par1 != 1
  430.                 || par2ItemStack.getItem() == Items.bucket;
  431.     }
  432.  
  433.     public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
  434.         return true;
  435.     }
  436.  
  437.     public float getWrenchDropRate() {
  438.         return 1.0F;
  439.     }
  440.  
  441.     @Override
  442.     public boolean hasCustomInventoryName() {
  443.         return false;
  444.     }
  445.  
  446.     @Override
  447.     public void openInventory() {
  448.  
  449.     }
  450.  
  451.     @Override
  452.     public void closeInventory() {
  453.  
  454.     }
  455.  
  456.     @Override
  457.     public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
  458.         return false;
  459.     }
  460.  
  461.     @Override
  462.     public short getFacing() {
  463.         return 0;
  464.     }
  465.  
  466.     @Override
  467.     public void setFacing(short facing) {
  468.  
  469.     }
  470.  
  471.     @Override
  472.     public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
  473.         return null;
  474.     }
  475.  
  476.     @Override
  477.     public boolean acceptsEnergyFrom(TileEntity emitter,
  478.             ForgeDirection direction) {
  479.         return true;
  480.     }
  481.  
  482.     @Override
  483.     public double getDemandedEnergy() {
  484.         return this.maxPower - this.power;
  485.     }
  486.  
  487.     @Override
  488.     public int getSinkTier() {
  489.         return 1;
  490.     }
  491.  
  492.     @Override
  493.     public double injectEnergy(ForgeDirection forgeDirection, double v,
  494.             double v2) {
  495.         if (this.power >= this.maxPower) {
  496.             return v;
  497.         }
  498.         this.power += v;
  499.  
  500.         return 0.0D;
  501.     }
  502.  
  503.  
  504.     @Override
  505.     public void invalidate() {
  506.         super.invalidate();
  507.  
  508.         onChunkUnload();
  509.     }
  510.  
  511.     public void onLoaded() {
  512.         if (!addedToEnet && !FMLCommonHandler.instance().getEffectiveSide().isClient() && Info.isIc2Available()) {
  513.         MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
  514.         this.addedToEnet = true;
  515.  
  516.         }
  517.     }
  518.     @Override
  519.     public void onChunkUnload() {
  520.         if (addedToEnet && Info.isIc2Available()) {
  521.             MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
  522.  
  523.             addedToEnet = false;
  524.         }
  525.     }
  526.  
  527.  
  528.  
  529.     public boolean isAddedToEnergyNet() {
  530.         return this.addedToEnet;
  531.     }
  532. }
Add Comment
Please, Sign In to add comment