Guest User

Untitled

a guest
Sep 28th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. package com.crilleslo.tileentity;
  2.  
  3. import com.crilleslo.testcraft.TestCraft;
  4.  
  5. import net.minecraft.block.material.Material;
  6. import net.minecraft.entity.item.EntityItem;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.init.Blocks;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.nbt.NBTTagList;
  13. import net.minecraft.network.NetworkManager;
  14. import net.minecraft.network.Packet;
  15. import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
  16. import net.minecraft.tileentity.TileEntity;
  17. import net.minecraftforge.common.util.Constants;
  18.  
  19. public class TileEntityWindmill extends TileEntity implements IInventory {
  20.  
  21. private float rotation = TestCraft.rand.nextFloat(), rotationSpeed = 0F, energy = 0F, maxEnergy = 3000F;
  22. private boolean isActive = false;
  23.  
  24. private ItemStack[] slots = new ItemStack[5];
  25.  
  26. public float getRotation() {
  27. return rotation;
  28. }
  29.  
  30. public boolean isActive() {
  31. return this.isActive;
  32. }
  33.  
  34. public void setActive(boolean active) {
  35. if(active) {
  36. worldObj.getBlock(xCoord, yCoord, zCoord).setLightLevel(0.5F);
  37. worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
  38. } else {
  39. worldObj.getBlock(xCoord, yCoord, zCoord).setLightLevel(0.2F);
  40. worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
  41. }
  42. this.isActive = active;
  43. }
  44.  
  45. public void writeToNBT(NBTTagCompound nbt) {
  46. nbt.setFloat("energyStored", this.energy);
  47.  
  48. NBTTagList itemList = new NBTTagList();
  49. for (int i = 0; i < slots.length; i++) {
  50. ItemStack stack = slots[i];
  51. if (stack != null) {
  52. NBTTagCompound tag = new NBTTagCompound();
  53. tag.setByte("Slot", (byte) i);
  54. stack.writeToNBT(tag);
  55. itemList.appendTag(tag);
  56. }
  57. }
  58. nbt.setTag("Inventory", itemList);
  59.  
  60. super.writeToNBT(nbt);
  61. }
  62.  
  63. public void readFromNBT(NBTTagCompound nbt) {
  64. this.energy = nbt.getFloat("energyStored");
  65.  
  66. NBTTagList tagList = nbt.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
  67. for (int i = 0; i < tagList.tagCount(); i++) {
  68. NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i);
  69. byte slot = tag.getByte("Slot");
  70. if (slot >= 0 && slot < slots.length) {
  71. slots[slot] = ItemStack.loadItemStackFromNBT(tag);
  72. }
  73. }
  74.  
  75. super.readFromNBT(nbt);
  76. }
  77.  
  78. public Packet getDescriptionPacket() {
  79. NBTTagCompound nbt = new NBTTagCompound();
  80. writeToNBT(nbt);
  81. return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, nbt);
  82. }
  83.  
  84. @Override
  85. public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
  86. readFromNBT(packet.func_148857_g());
  87. super.onDataPacket(net, packet);
  88. }
  89.  
  90. public void updateEntity() {
  91. worldObj.setRainStrength(0);
  92. worldObj.setThunderStrength(0);
  93.  
  94. float blockModifier = 0;
  95. int metadata = getBlockMetadata();
  96. if(worldObj.isRaining()) {
  97. blockModifier-=.5;
  98. }
  99. if(metadata == 1 || metadata == 3) {
  100. if(!worldObj.getBlock(xCoord+1, yCoord, zCoord).getMaterial().equals(Material.air)) {
  101. blockModifier+=.5;
  102. }
  103. if(!worldObj.getBlock(xCoord-1, yCoord, zCoord).getMaterial().equals(Material.air)) {
  104. blockModifier+=.5;
  105. }
  106. } else {
  107. if(!worldObj.getBlock(xCoord, yCoord, zCoord+1).getMaterial().equals(Material.air)) {
  108. blockModifier+=.5;
  109. }
  110. if(!worldObj.getBlock(xCoord, yCoord, zCoord-1).getMaterial().equals(Material.air)) {
  111. blockModifier+=.5;
  112. }
  113. }
  114.  
  115. if(blockModifier > 1) {
  116. blockModifier = 1;
  117. }
  118.  
  119. if(worldObj.canBlockSeeTheSky(xCoord, yCoord, zCoord)) {
  120. float speed = yCoord*0.1F;
  121. if((energy+((speed-(speed*blockModifier))/2)) < maxEnergy) {
  122. energy+=(((speed-(speed*blockModifier))*TestCraft.rand.nextFloat())/2);
  123. setActive(true);
  124. if(rotationSpeed < (speed-(speed*blockModifier))) {
  125. rotationSpeed+=(TestCraft.rand.nextFloat()*TestCraft.rand.nextFloat());
  126. }
  127. } else {
  128. setActive(false);
  129. if(rotationSpeed > 0) {
  130. rotationSpeed-=(TestCraft.rand.nextFloat()*TestCraft.rand.nextFloat());
  131. }
  132. if(rotationSpeed < 0) {
  133. rotationSpeed = 0;
  134. }
  135. }
  136. if(rotationSpeed > (speed-(speed*blockModifier))) {
  137. rotationSpeed-=(TestCraft.rand.nextFloat()*TestCraft.rand.nextFloat());
  138. }
  139. } else {
  140. setActive(false);
  141. if(rotationSpeed > 0) {
  142. rotationSpeed-=(TestCraft.rand.nextFloat()*TestCraft.rand.nextFloat());
  143. }
  144. if(rotationSpeed < 0) {
  145. rotationSpeed = 0;
  146. }
  147. }
  148. rotation+=rotationSpeed;
  149. }
  150.  
  151. public int getSizeInventory() {
  152. return this.slots.length;
  153. }
  154.  
  155. public ItemStack getStackInSlot(int slot) {
  156. return this.slots[slot];
  157. }
  158.  
  159. public ItemStack decrStackSize(int slot, int amount) {
  160. if(this.slots[slot] != null){
  161. ItemStack itemstack;
  162.  
  163. if(this.slots[slot].stackSize <= amount){
  164. itemstack = this.slots[slot];
  165. this.slots[slot] = null;
  166. return itemstack;
  167. }else{
  168. itemstack = this.slots[slot].splitStack(amount);
  169.  
  170. if(this.slots[slot].stackSize == 0){
  171. this.slots[slot] = null;
  172. }
  173.  
  174. return itemstack;
  175. }
  176. }
  177. return null;
  178. }
  179.  
  180. public ItemStack getStackInSlotOnClosing(int slot) {
  181. if(this.slots[slot] != null){
  182. ItemStack itemstack = this.slots[slot];
  183. this.slots[slot] = null;
  184. return itemstack;
  185. }
  186.  
  187. return null;
  188. }
  189.  
  190. public void setInventorySlotContents(int slot, ItemStack itemstack) {
  191. this.slots[slot] = itemstack;
  192.  
  193. if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
  194. itemstack.stackSize = this.getInventoryStackLimit();
  195. }
  196. }
  197.  
  198. public String getInventoryName() {
  199. return "Windmill";
  200. }
  201.  
  202. public boolean hasCustomInventoryName() {
  203. return true;
  204. }
  205.  
  206. public int getInventoryStackLimit() {
  207. return 64;
  208. }
  209.  
  210. public boolean isUseableByPlayer(EntityPlayer player) {
  211. return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  212. }
  213.  
  214. public void openInventory() {
  215.  
  216. }
  217.  
  218. public void closeInventory() {
  219. for(ItemStack is : slots) {
  220. new EntityItem(worldObj, xCoord, yCoord, zCoord, is);
  221. }
  222. }
  223.  
  224. @Override
  225. public boolean isItemValidForSlot(int slot, ItemStack is) {
  226. return true;
  227. }
  228.  
  229. public int getEnergyStoredScaled(int i) {
  230. return (int) (this.energy * i / this.maxEnergy);
  231. }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment