Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.crilleslo.tileentity;
- import com.crilleslo.testcraft.TestCraft;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.item.EntityItem;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.network.Packet;
- import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraftforge.common.util.Constants;
- public class TileEntityWindmill extends TileEntity implements IInventory {
- private float rotation = TestCraft.rand.nextFloat(), rotationSpeed = 0F, energy = 0F, maxEnergy = 3000F;
- private boolean isActive = false;
- private ItemStack[] slots = new ItemStack[5];
- public float getRotation() {
- return rotation;
- }
- public boolean isActive() {
- return this.isActive;
- }
- public void setActive(boolean active) {
- if(active) {
- worldObj.getBlock(xCoord, yCoord, zCoord).setLightLevel(0.5F);
- worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
- } else {
- worldObj.getBlock(xCoord, yCoord, zCoord).setLightLevel(0.2F);
- worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
- }
- this.isActive = active;
- }
- public void writeToNBT(NBTTagCompound nbt) {
- nbt.setFloat("energyStored", this.energy);
- NBTTagList itemList = new NBTTagList();
- for (int i = 0; i < slots.length; i++) {
- ItemStack stack = slots[i];
- if (stack != null) {
- NBTTagCompound tag = new NBTTagCompound();
- tag.setByte("Slot", (byte) i);
- stack.writeToNBT(tag);
- itemList.appendTag(tag);
- }
- }
- nbt.setTag("Inventory", itemList);
- super.writeToNBT(nbt);
- }
- public void readFromNBT(NBTTagCompound nbt) {
- this.energy = nbt.getFloat("energyStored");
- NBTTagList tagList = nbt.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
- for (int i = 0; i < tagList.tagCount(); i++) {
- NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i);
- byte slot = tag.getByte("Slot");
- if (slot >= 0 && slot < slots.length) {
- slots[slot] = ItemStack.loadItemStackFromNBT(tag);
- }
- }
- super.readFromNBT(nbt);
- }
- public Packet getDescriptionPacket() {
- NBTTagCompound nbt = new NBTTagCompound();
- writeToNBT(nbt);
- return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, nbt);
- }
- @Override
- public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
- readFromNBT(packet.func_148857_g());
- super.onDataPacket(net, packet);
- }
- public void updateEntity() {
- worldObj.setRainStrength(0);
- worldObj.setThunderStrength(0);
- float blockModifier = 0;
- int metadata = getBlockMetadata();
- if(worldObj.isRaining()) {
- blockModifier-=.5;
- }
- if(metadata == 1 || metadata == 3) {
- if(!worldObj.getBlock(xCoord+1, yCoord, zCoord).getMaterial().equals(Material.air)) {
- blockModifier+=.5;
- }
- if(!worldObj.getBlock(xCoord-1, yCoord, zCoord).getMaterial().equals(Material.air)) {
- blockModifier+=.5;
- }
- } else {
- if(!worldObj.getBlock(xCoord, yCoord, zCoord+1).getMaterial().equals(Material.air)) {
- blockModifier+=.5;
- }
- if(!worldObj.getBlock(xCoord, yCoord, zCoord-1).getMaterial().equals(Material.air)) {
- blockModifier+=.5;
- }
- }
- if(blockModifier > 1) {
- blockModifier = 1;
- }
- if(worldObj.canBlockSeeTheSky(xCoord, yCoord, zCoord)) {
- float speed = yCoord*0.1F;
- if((energy+((speed-(speed*blockModifier))/2)) < maxEnergy) {
- energy+=(((speed-(speed*blockModifier))*TestCraft.rand.nextFloat())/2);
- setActive(true);
- if(rotationSpeed < (speed-(speed*blockModifier))) {
- rotationSpeed+=(TestCraft.rand.nextFloat()*TestCraft.rand.nextFloat());
- }
- } else {
- setActive(false);
- if(rotationSpeed > 0) {
- rotationSpeed-=(TestCraft.rand.nextFloat()*TestCraft.rand.nextFloat());
- }
- if(rotationSpeed < 0) {
- rotationSpeed = 0;
- }
- }
- if(rotationSpeed > (speed-(speed*blockModifier))) {
- rotationSpeed-=(TestCraft.rand.nextFloat()*TestCraft.rand.nextFloat());
- }
- } else {
- setActive(false);
- if(rotationSpeed > 0) {
- rotationSpeed-=(TestCraft.rand.nextFloat()*TestCraft.rand.nextFloat());
- }
- if(rotationSpeed < 0) {
- rotationSpeed = 0;
- }
- }
- rotation+=rotationSpeed;
- }
- public int getSizeInventory() {
- return this.slots.length;
- }
- public ItemStack getStackInSlot(int slot) {
- return this.slots[slot];
- }
- public ItemStack decrStackSize(int slot, int amount) {
- if(this.slots[slot] != null){
- ItemStack itemstack;
- if(this.slots[slot].stackSize <= amount){
- itemstack = this.slots[slot];
- this.slots[slot] = null;
- return itemstack;
- }else{
- itemstack = this.slots[slot].splitStack(amount);
- if(this.slots[slot].stackSize == 0){
- this.slots[slot] = null;
- }
- return itemstack;
- }
- }
- return null;
- }
- public ItemStack getStackInSlotOnClosing(int slot) {
- if(this.slots[slot] != null){
- ItemStack itemstack = this.slots[slot];
- this.slots[slot] = null;
- return itemstack;
- }
- return null;
- }
- public void setInventorySlotContents(int slot, ItemStack itemstack) {
- this.slots[slot] = itemstack;
- if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
- itemstack.stackSize = this.getInventoryStackLimit();
- }
- }
- public String getInventoryName() {
- return "Windmill";
- }
- public boolean hasCustomInventoryName() {
- return true;
- }
- public int getInventoryStackLimit() {
- return 64;
- }
- public boolean isUseableByPlayer(EntityPlayer player) {
- 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;
- }
- public void openInventory() {
- }
- public void closeInventory() {
- for(ItemStack is : slots) {
- new EntityItem(worldObj, xCoord, yCoord, zCoord, is);
- }
- }
- @Override
- public boolean isItemValidForSlot(int slot, ItemStack is) {
- return true;
- }
- public int getEnergyStoredScaled(int i) {
- return (int) (this.energy * i / this.maxEnergy);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment