Jump to content

Recommended Posts

Posted

Hey guys i am having yet another issue with my power bar you see i want my solar panel to gain power when its daytime and using the blocks updatetick method this is possible but when the block gives the tileentity power the power bar doesn't update untill i reload the world any ideas?

here is my code

 

Block

 

 

package Mine_EE_GUI;

import java.util.List;
import java.util.Random;

import Mine_EE.Mine_EE;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.FMLNetworkHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class Solar extends BlockContainer {

public Solar(int id) {

	super(id, Material.iron);
	setCreativeTab(Mine_EE.ModTab2);
	this.setTickRandomly(true);
}

@Override
public TileEntity createNewTileEntity(World world) {
	return new TileEntitySolar();

}

@Override
    public void breakBlock(World world, int x, int y, int z, int par5, int par6) {
            dropItems(world, x, y, z);
            super.breakBlock(world, x, y, z, par5, par6);
    }

  private void dropItems(World world, int x, int y, int z){
          Random rand = new Random();

          TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
          if (!(tileEntity instanceof IInventory)) {
                  return;
          }
          IInventory inventory = (IInventory) tileEntity;

          for (int i = 0; i < inventory.getSizeInventory(); i++) {
                  ItemStack item = inventory.getStackInSlot(i);

                  if (item != null && item.stackSize > 0) {
                          float rx = rand.nextFloat() * 0.8F + 0.1F;
                          float ry = rand.nextFloat() * 0.8F + 0.1F;
                          float rz = rand.nextFloat() * 0.8F + 0.1F;

                          EntityItem entityItem = new EntityItem(world,
                                          x + rx, y + ry, z + rz,
                                          new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));

                          if (item.hasTagCompound()) {
                                  entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
                          }

                          float factor = 0.05F;
                          entityItem.motionX = rand.nextGaussian() * factor;
                          entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
                          entityItem.motionZ = rand.nextGaussian() * factor;
                          world.spawnEntityInWorld(entityItem);
                          item.stackSize = 0;
                  }
          }
  }
	 public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
	    {
	        float f = 0.01F;
	        return AxisAlignedBB.getAABBPool().getAABB((double)par2, (double)par3, (double)par4, (double)(par2 + 1), (double)((float)(par3 + 1) - f), (double)(par4 + 1));
	    }
  
  public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
	{


		for (int l = 0; l < 20; ++l)
			{
				Random clock = new Random();
				int Timer;
				Timer = clock.nextInt(10 + 1);

				if (par1World.isDaytime())
				{
					TileEntitySolar t = (TileEntitySolar) par1World.getBlockTileEntity(par2, par3, par4);
					t.addpower(Timer);
				}
			}
		}
  

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
	 ItemStack itemstack = player.inventory.getCurrentItem();
	 TileEntitySolar t = (TileEntitySolar) world.getBlockTileEntity(x, y, z);


	 if(!world.isRemote && player.isSneaking()) 
		{
			player.addChatMessage(t.power + " MPW");
		}

	 if(!world.isRemote && !player.isSneaking()) 
		{
			//t.processActivate(Minecraft.getMinecraft().thePlayer,world);
		 if(world.isDaytime())
		 {
			FMLNetworkHandler.openGui(player, Mine_EE.instance, 71, world, x, y, z);
		 }
		}

	return true;
}

@Override
public void onBlockAdded(World world, int x, int y, int z) {
	super.onBlockAdded(world, x, y, z);
	setDefaultDirection(world, x, y, z);

		}



@SideOnly(Side.CLIENT)
public static Icon topIcon;
@SideOnly(Side.CLIENT)
public static Icon sideIcon;
@SideOnly(Side.CLIENT)
public static Icon frontIcon;
@SideOnly(Side.CLIENT)
public static Icon bottomIcon;

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister icon) 
{
	topIcon = icon.registerIcon(Mine_EE.modid.toLowerCase() + ":" + "solarpanel_top");
	sideIcon = icon.registerIcon(Mine_EE.modid.toLowerCase() + ":" + "solarpanel_sides");
	frontIcon = icon.registerIcon(Mine_EE.modid.toLowerCase() + ":" + "solarpanel_side");
	bottomIcon = icon.registerIcon(Mine_EE.modid.toLowerCase() + ":" + "solarpanel_bottom");
}


private void iconz(IconRegister icon, int x,int y,int z) {
	// TODO Auto-generated method stub

}

@Override
public Icon getIcon(int side, int meta) {
	if( side == 1) {
		return topIcon;
	} else if(side != meta) {
		return sideIcon;
	} 
	else if(side == 0)
	{
		return bottomIcon;
	}
	else {
		return frontIcon;
	}
}



private void setDefaultDirection(World world, int x, int y, int z) {
	if(!world.isRemote) {
		int zNeg = world.getBlockId(x, y, z - 1);
		int zPos = world.getBlockId(x, y, z + 1);
		int xNeg = world.getBlockId(x - 1, y, z);
		int xPos = world.getBlockId(x + 1, y, z);
		byte meta = 3;

		if(Block.opaqueCubeLookup[xNeg] && !Block.opaqueCubeLookup[xPos]) meta = 5;
		if(Block.opaqueCubeLookup[xPos] && !Block.opaqueCubeLookup[xNeg]) meta = 4;
		if(Block.opaqueCubeLookup[zNeg] && !Block.opaqueCubeLookup[zPos]) meta = 3;
		if(Block.opaqueCubeLookup[zPos] && !Block.opaqueCubeLookup[zNeg]) meta = 2;

		world.setBlockMetadataWithNotify(x, y, z, meta, 2);
	}
}

@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack itemstack) {
	int rotation = MathHelper.floor_double((double)(entity.rotationYaw * 4F / 360F) + 0.5D) & 3;

	if(rotation == 0) {
		world.setBlockMetadataWithNotify(x, y, z, 2, 2);
	}

	if(rotation == 1) {
		world.setBlockMetadataWithNotify(x, y, z, 5, 2);
	}

	if(rotation == 2) {
		world.setBlockMetadataWithNotify(x, y, z, 3, 2);
	}

	if(rotation == 3) {
		world.setBlockMetadataWithNotify(x, y, z, 4, 2);
	}
}
}

 

 

TileEntity

 

 

package Mine_EE_GUI;

import java.util.Random;

import Mine_EE.Mine_EE;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntitySolar extends TileEntity implements IInventory {
public float power;
public float MAX;
public float LOW;
public float display_power;
public boolean day;
Solar s;
private ItemStack[] inventory;

public TileEntitySolar() {
inventory = new ItemStack[1];
MAX = 100;
LOW = 0;
}

private int item;


public void updateEntity()
{

	display_power = this.power;
	if(this.power > 100){
		power = 100;
	}
}

public int GetPowerScaled(int scaled)
{
	return (int) (this.power * scaled /this.MAX);
}

@Override
public int getSizeInventory() {
return inventory.length;
}

@Override
public ItemStack getStackInSlot(int i) {
return inventory[i];
}


@Override
public ItemStack decrStackSize(int slot, int count) {
ItemStack itemstack = getStackInSlot(slot);


if(itemstack != null) 
{

if(itemstack.stackSize <= count) {
setInventorySlotContents(slot, null);
} else {
itemstack = itemstack.splitStack(count);
onInventoryChanged();
}
}
return itemstack;
}


@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack itemstack = getStackInSlot(slot);

setInventorySlotContents(slot, null);
return itemstack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
inventory[slot] = itemstack;

this.recipes(slot, itemstack);

if(itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
itemstack.stackSize = getInventoryStackLimit();
}
onInventoryChanged();
}


private void recipes(int slot, ItemStack itemstack) 
{
	inventory[slot] = itemstack;

	/*
	ItemStack stack = getStackInSlot(2);
	if (stack != null && stack.getItem() == Item.appleRed) {
		Minecraft.getMinecraft().thePlayer.addChatMessage("APPLE?");
		}
		*/

	if(power < MAX){

	if(itemstack != null && itemstack.itemID == Item.redstone.itemID && power < MAX)
	{

		this.removestack(slot,itemstack);
		this.addpower(2 * itemstack.stackSize);
		onInventoryChanged();
	}

	}
	else if(power == MAX)
	{

	}

}

private void removestack(int slot, ItemStack itemstack) 
{

	setInventorySlotContents(0, null);
}




public void addpower(int mpw) {

	power = this.power + mpw;
	if(this.power <= 0)
	{
		power = LOW;
	}
	if(this.power >= MAX)
	{
		power = MAX;
	}
	display_power = this.power;
}

@Override
public String getInvName() {
return "Another Test Gui";
}

@Override
public boolean isInvNameLocalized() {
return true;
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	if(player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64) {
		return true;
		} else {
		return false;
		}
}

@Override
public void openChest() {
}

@Override
public void closeChest() {
}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return false;
}


@Override
public void writeToNBT(NBTTagCompound compound) {
  super.writeToNBT(compound);
  
  
  compound.setFloat("power", power);
  
  NBTTagList list = new NBTTagList();

  for(int i = 0; i < getSizeInventory(); i++) {
    ItemStack itemstack = getStackInSlot(i);

    if(itemstack != null) {
      NBTTagCompound item = new NBTTagCompound();

      item.setByte("SlotDeployer", (byte) i);
      itemstack.writeToNBT(item);
      list.appendTag(item);
    }
  }

  compound.setTag("ItemsDeployer", list);
}

@Override
public void readFromNBT(NBTTagCompound compound) {
  super.readFromNBT(compound);

  this.power = compound.getFloat("power");
  
  NBTTagList list = compound.getTagList("ItemsDeployer");

  for(int i = 0; i < list.tagCount(); i++) {
    NBTTagCompound item = (NBTTagCompound) list.tagAt(i);
    int slot = item.getByte("SlotDeployer");

    if(slot >= 0 && slot < getSizeInventory()) {
      setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
    }
  }
}
  @Override
    public Packet getDescriptionPacket() {
        Packet132TileEntityData packet = (Packet132TileEntityData) super.getDescriptionPacket();
        NBTTagCompound tag = packet != null ? packet.data : new NBTTagCompound();

        writeToNBT(tag);

        return new Packet132TileEntityData(xCoord, yCoord, zCoord, 1, tag);
    }
  
    @Override
    public void onDataPacket(INetworkManager net, Packet132TileEntityData pkt) {
        super.onDataPacket(net, pkt);
        NBTTagCompound tag = pkt.data;
        readFromNBT(tag);
    }




}

 

 

Container

 

 

package Mine_EE_GUI;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;



public class ContainerSolar extends Container {
private TileEntitySolar solar;
private Solar blocksolar;



public ContainerSolar(InventoryPlayer invPlayer, TileEntitySolar entity) 
{



	this.solar = entity;

	 for(int x = 0; x < 9; x++) {
		 this.addSlotToContainer(new Slot(invPlayer, x, 8 + x * 18, 142));
	  }
	 for(int y = 0; y < 3; y++) {
		  for(int x = 0; x < 9; x++) {
		    this.addSlotToContainer(new Slot(invPlayer, 9 + x + y * 9, 8 + x * 18, 84 + y * 18));
		  }
		}
	 for(int y = 0; y < 1; y++) {
		  for(int x = 0; x < 1; x++) {
			    this.addSlotToContainer(new Slot(entity, x + y * 3,80,35));
		  }
		}
	// this.addSlotToContainer(new Slot(         ID, X, Y));
	// this.addSlotToContainer(new Slot(entity, 237, 62, 17));

}


@Override
public ItemStack transferStackInSlot(EntityPlayer player, int i) {
  Slot slot = getSlot(i);

  if(slot != null && slot.getHasStack()) {
    ItemStack itemstack = slot.getStack();
    ItemStack result = itemstack.copy();

    if(i >= 36) {
      if(!mergeItemStack(itemstack, 0, 36, false)) {
        return null;
      }
    } else if(!mergeItemStack(itemstack, 36, 36 + solar.getSizeInventory(), false)) {
      return null;
    }

    if(itemstack.stackSize == 0) {
      slot.putStack(null);
    } else {
      slot.onSlotChanged();
    }
    slot.onPickupFromSlot(player, itemstack); 
    return result;
  }
  return null;
}

@Override
public boolean canInteractWith(EntityPlayer player) 
{

return solar.isUseableByPlayer(player);

}






}

 

 

Gui

 

 

package Mine_EE_GUI;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import Mine_EE.*;
import Mine_EE_GUI.*;

public class GuiSolar extends GuiContainer 
{


public static final ResourceLocation texture = new ResourceLocation(Mine_EE.modid.toLowerCase(), "textures/gui/Solar.png");

private TileEntitySolar solar;
public GuiSolar(InventoryPlayer invPlayer, TileEntitySolar tile_entity) {
	super(new ContainerSolar(invPlayer, tile_entity));

	this.solar = tile_entity;
	xSize = 176;
	ySize = 165;

}

 protected void drawGuiContainerForegroundLayer(int par1, int par2)
    {
	// this.fontRenderer.drawString(StatCollector.translateToLocal(solar.display_power + " MPW"), 103, 5, 0x404040);
    }

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
	GL11.glColor4f(1F, 1F, 1F, 1F);
	this.mc.getTextureManager().bindTexture(texture);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);


	int draw = this.solar.GetPowerScaled(40);
	drawTexturedModalRect(guiLeft +47, guiTop+14+40-draw, 177, 40 - draw, 12, draw);
}
}

 

 

 

Its best to only ask questions when you did most of the work yourself.

Posted

Hi,

 

It is in the updateEntity of the TileEntity that you need to update the energy not in the updateTick of the Block.

 

TileEntity.updateEntity is called every tick while Block.updateTick is randomly called now and then.

Posted

Hi,

 

It is in the updateEntity of the TileEntity that you need to update the energy not in the updateTick of the Block.

 

TileEntity.updateEntity is called every tick while Block.updateTick is randomly called now and then.

yes i am aware but whenever i tried to do it in the tileentitys UpdateEntity method i could not get it to get the time of day and when i did get it to get the time of day the client and server were not synced

Its best to only ask questions when you did most of the work yourself.

Posted

Well first start by adding @Override to your TileEntity.updateEntity method.

Since it is just a method that isn't called right now.

 

Furthermore you just say worldObj.isDaytime() to check if it is daytime in your tileentity.

Secondly, check whether it is the server you need or the client with worldObj.isRemote,

I gather the cycle is set on the server side.

That's about all you need I think.

 

Posted

Well first start by adding @Override to your TileEntity.updateEntity method.

Since it is just a method that isn't called right now.

 

Furthermore you just say worldObj.isDaytime() to check if it is daytime in your tileentity.

Secondly, check whether it is the server you need or the client with worldObj.isRemote,

I gather the cycle is set on the server side.

That's about all you need I think.

thats good i now have that code but the issue i have now is the progress bar in the gui isn't updating untill i reload the world

Its best to only ask questions when you did most of the work yourself.

Posted

Look at ContainerFurnace - you need to detect and send changes, update the progress bar, etc., all in your Container class.

Fixed it Thanks :D

Its best to only ask questions when you did most of the work yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.