Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. package com.jolteffect.teslamodularsolars.block;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import com.jolteffect.teslamodularsolars.handler.TeslaModularSolarsConfig;
  6. import com.jolteffect.teslamodularsolars.utility.LogHelper;
  7.  
  8. import net.darkhax.tesla.api.ITeslaConsumer;
  9. import net.darkhax.tesla.api.implementation.BaseTeslaContainer;
  10. import net.darkhax.tesla.capability.TeslaCapabilities;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.network.NetworkManager;
  14. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  15. import net.minecraft.tileentity.TileEntity;
  16. import net.minecraft.util.EnumFacing;
  17. import net.minecraft.util.ITickable;
  18. import net.minecraftforge.common.capabilities.Capability;
  19. import net.minecraftforge.items.ItemStackHandler;
  20.  
  21. public class TileEntitySolarPanel extends TileEntity implements ITickable {
  22.  
  23. // An instance of something that implements ITeslaConsumer, ITeslaProducer or
  24. // ITeslaHandler. In this case we use the BaseTeslaContainer which makes use of all three.
  25. // The purpose of this instance is to handle all tesla related logic for the TileEntity.
  26. private ContainerSolarPanel container;
  27.  
  28.  
  29. public ItemStackHandler itemStackHandler = new ItemStackHandler(10);
  30.  
  31. public TileEntitySolarPanel() {
  32.  
  33. // Initializes the container. Very straight forward.
  34. this.container = new ContainerSolarPanel();
  35. }
  36.  
  37. @Override
  38. public void readFromNBT (NBTTagCompound compound) {
  39. super.readFromNBT(compound);
  40. this.container.setCapacity(compound.getLong("Capacity"));
  41. LogHelper.logInfo("TeslaModularSolars --------------readFromNBT Capacity = " + compound.getLong("Capacity"));
  42. this.container.setPower(compound.getLong("StoredPower"));
  43. LogHelper.logInfo("TeslaModularSolars --------------readFromNBT StoredPower = " + compound.getLong("StoredPower"));
  44.  
  45. }
  46.  
  47. @Override
  48. public NBTTagCompound writeToNBT (NBTTagCompound compound) {
  49.  
  50. compound.setLong("Capacity", this.container.getCapacity());
  51. LogHelper.logInfo("TeslaModularSolars --------------writeToNBT Capacity = " + this.container.getStoredPower());
  52. compound.setLong("StoredPower", this.container.getStoredPower());
  53. LogHelper.logInfo("TeslaModularSolars --------------writeToNBT StoredPower = " + this.container.getStoredPower());
  54.  
  55. return super.writeToNBT(compound);
  56. }
  57.  
  58.  
  59. @Override
  60. @SuppressWarnings("unchecked")
  61. public <T> T getCapability (Capability<T> capability, EnumFacing facing) {
  62.  
  63. if (facing == EnumFacing.DOWN && (capability == TeslaCapabilities.CAPABILITY_PRODUCER || capability == TeslaCapabilities.CAPABILITY_HOLDER))
  64. return (T) this.container;
  65.  
  66. return super.getCapability(capability, facing);
  67. }
  68.  
  69. @Override
  70. public boolean hasCapability (Capability<?> capability, EnumFacing facing) {
  71.  
  72. if (facing == EnumFacing.DOWN && (capability == TeslaCapabilities.CAPABILITY_PRODUCER || capability == TeslaCapabilities.CAPABILITY_HOLDER))
  73. return true;
  74.  
  75. return super.hasCapability(capability, facing);
  76. }
  77.  
  78. @Override
  79. public void update() {
  80.  
  81.  
  82. if (this.hasWorldObj()) {
  83. if (!this.worldObj.provider.getHasNoSky() && this.worldObj.canBlockSeeSky(this.pos.offset(EnumFacing.UP)) && !this.worldObj.isRaining() && this.worldObj.getSkylightSubtracted() == 0 && this.container.getStoredPower() != this.container.getCapacity())
  84. this.container.generatePower();
  85.  
  86. final TileEntity tile = this.getWorld().getTileEntity(this.getPos().offset(EnumFacing.DOWN));
  87. if (tile != null && !tile.isInvalid() && tile.hasCapability(TeslaCapabilities.CAPABILITY_CONSUMER, EnumFacing.UP))
  88. this.container.takePower(((ITeslaConsumer) tile.getCapability(TeslaCapabilities.CAPABILITY_CONSUMER, EnumFacing.UP)).givePower(Math.min(this.container.getStoredPower(), container.getTransferRate()), false), false);
  89. }
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96. @Override
  97. public NBTTagCompound getUpdateTag() {
  98. // getUpdateTag() is called whenever the chunkdata is sent to the
  99. // client. In contrast getUpdatePacket() is called when the tile entity
  100. // itself wants to sync to the client. In many cases you want to send
  101. // over the same information in getUpdateTag() as in getUpdatePacket().
  102. return writeToNBT(new NBTTagCompound());
  103. }
  104.  
  105. @Nullable
  106. @Override
  107. public SPacketUpdateTileEntity getUpdatePacket() {
  108. // Prepare a packet for syncing our TE to the client. Since we only have to sync the stack
  109. // and that's all we have we just write our entire NBT here. If you have a complex
  110. // tile entity that doesn't need to have all information on the client you can write
  111. // a more optimal NBT here.
  112. NBTTagCompound nbtTag = new NBTTagCompound();
  113. this.writeToNBT(nbtTag);
  114. return new SPacketUpdateTileEntity(getPos(), 1, nbtTag);
  115. }
  116.  
  117. @Override
  118. public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
  119. // Here we get the packet from the server and read it into our client side tile entity
  120. this.readFromNBT(packet.getNbtCompound());
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement