Jump to content

Recommended Posts

Posted

After i realized that i am an idiot i tried to add a nice gui and a progress bar to my furnance.

No errors, everything looks fine, minecraft starts with 0 errors, i can open block, but energy bar doesn't change...

 

After trying around a little bit i think that my energy is never set to anything else than 0, but my update function says that it is increasing.

 

 

TileEntity

 

 

package net.dimensionshift.mod.tileentity;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.dimensionshift.mod.DimensionShift;
import net.dimensionshift.mod.Fuel;
import net.dimensionshift.mod.block.BlockSimpleController;
import net.dimensionshift.mod.blockplacing.TeleportSimpleController;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntitySimpleController extends TileEntity implements ISidedInventory{

private String localizedName;

//What slots can be accesed from which side?
	private static final int[] slots_top = new int[]{0};
	//first take out items from slot 1, than from slot number 0
	private static final int[] slots_bottom = new int[]{1, 0};
	private static final int[] slots_sides = new int[]{};

	private ItemStack[] fuel=Fuel.getFuels();//new ItemStack[]{new ItemStack(Items.ender_pearl), new ItemStack(Items.ender_eye)};



//How many Slots does Block have
	private ItemStack[] slots = new ItemStack[2];

//stored Dimension Energy
public int energy;



//stored Dimension Energy
public int dimensionEnergy;// = 0;

//needed Dimension Energy
public final int requiredDimensionEnergy = 2000;

//energy that has to be stored yet
public int dimensionEnergyCharging=0;

//time until ready to tp
public final int tpTime = 1000;

//how long it is already preparing for tp
public int timeUntillTp;

//needed to check if status has changed...
public boolean activeLastTick=false;






@Override
public void updateEntity(){

	//Will be set to true if anything has been done this round. Otherwise it will be false
	boolean active=false;



	boolean flag1=false;

	//getting rid of stack with size 0
	if(this.slots[1]!=null) {
	if(this.slots[1].stackSize == 0){
		this.slots[1]=this.slots[1].getItem().getContainerItem(this.slots[1]);
	}}



		//converting item to energy
	if(this.dimensionEnergyCharging>0 && this.dimensionEnergy != this.requiredDimensionEnergy) {
		++this.dimensionEnergy;
		--this.dimensionEnergyCharging;
		System.out.println("Energy stored: " + dimensionEnergy);

		active=true;
	}


	if (!this.worldObj.isRemote && this.slots[1]!=null) {
		if(this.dimensionEnergyCharging==0 && Fuel.getFuelValue(slots[1])>0){
			System.out.println("adding energy to charging and removing item from slot");
			this.dimensionEnergyCharging=Fuel.getFuelValue(slots[1]);
			--this.slots[1].stackSize;
		}
		active=true;
	}


	//checking if status has changed
	if(active!=this.activeLastTick) {
		BlockSimpleController.updateBlockType(active, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		this.activeLastTick=active;
	}




}


private boolean isTeleporting() {
	return this.timeUntillTp>0;
}

private boolean canTeleport() {
	if(this.slots[0] == null) {
		return false;
	} else {
		return true;
	}
}


public int getSizeInventory(){
	return this.slots.length;
}


public boolean isInvNameLocalized() {
	return this.localizedName!=null && this.localizedName.length() > 0;
}

public String getInvName() {
	return this.isInvNameLocalized() ? this.localizedName : "container.simpleController";
}

public void setGuiDisplayName(String displayName) {
	this.localizedName = displayName;
}

public ItemStack getStackInSlot(int i) {
	return this.slots[i];
}

public ItemStack decrStackSize(int i, int j) {
	if(this.slots[i] != null){
		ItemStack itemstack;

		if(this.slots[i].stackSize<=j) {
			itemstack = this.slots[i];

			this.slots[i] = null;
			return itemstack;
		} else {
			itemstack=this.slots[i].splitStack(j);

			if(this.slots[i].stackSize==0){
				this.slots[i]=null;
			}
			return itemstack;
		}
	}
	return null;
}

public ItemStack getStackInSlotOnClosing(int i) {
	if(this.slots[i] != null){
		ItemStack itemstack = this.slots[i];
		this.slots[i]=null;
		return itemstack;
	}
	return null;
}

public void setInventorySlotContents(int i, ItemStack itemstack) {
	this.slots[i] = itemstack;

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

public String getInventoryName() {
	return null;
}

public boolean hasCustomInventoryName() {
	return false;
}

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() {

}

public boolean isItemValidForSlot(int i, ItemStack item) {
	boolean isValid=false;
	if (i==0) {			//id card

		isValid=true;
	} else if(i==1) {  //fuel
		isValid=isItemFuel(item);
	}
	return isValid;
}

private boolean isItemFuel(ItemStack item) {
	boolean isFuel=false;


	for (ItemStack item1 : fuel) {
	    if (item1.equals(item)) { 
	    	isFuel=true;
	        break;
	    }
	}


	return isFuel;
}

public void writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);

	//saving energy
	nbt.setShort("DimensionEnergy", (short)this.dimensionEnergy);
	nbt.setShort("DimensionEnergyCharging", (short)this.dimensionEnergyCharging);
	nbt.setShort("Energy", (short)this.energy);


	//items
	NBTTagList list = new NBTTagList();
	for(int i=0; i<slots.length; i++){
		if(this.slots[i]!=null){
			NBTTagCompound compound = new NBTTagCompound();
			compound.setByte("Slot", (byte)i);
			this.slots[i].writeToNBT(compound);
			list.appendTag(compound);
		}
	}
	nbt.setTag("Items", list);

	if(this.isInvNameLocalized()){
		nbt.setString("CustomName", this.localizedName);
	}

}

public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);

	//items
	NBTTagList list = nbt.getTagList("Items", 10);
	this.slots = new ItemStack[this.getSizeInventory()];

	for(int i=0; i< list.tagCount(); i++){
		NBTTagCompound compound=list.getCompoundTagAt(i);
		byte b = compound.getByte("Slot");
			if(b >= 0 && b<this.slots.length){
				this.slots[b]= ItemStack.loadItemStackFromNBT(compound);
			}

	}

	this.dimensionEnergy=nbt.getShort("DimensionEnergy");
	this.dimensionEnergyCharging=nbt.getShort("DimensionEnergyCharging");
	this.energy=nbt.getShort("Energy");

	if(nbt.hasKey("CustomName")){
		this.localizedName=nbt.getString("CustomName");
	}

}



public int[] getAccessibleSlotsFromSide(int side) {
	return side == 0 ? slots_bottom : (side == 1 ? slots_top : slots_sides);
}

public boolean canInsertItem(int i, ItemStack item, int side) {
	return this.isItemValidForSlot(i, item);
}

public boolean canExtractItem(int i, ItemStack item, int side) {
		//if extracts from down slot 1(fuel) than true
	return side == 0 || i== 1;
}




public static ItemStack[] getFuels(){
	ItemStack[] fuels=new ItemStack[7];
	fuels[0] = new ItemStack(Items.ender_pearl);
	fuels[1] = new ItemStack(Items.ender_eye);
	fuels[3] = new ItemStack(Items.nether_star);
	fuels[4] = new ItemStack(Blocks.dragon_egg);

	fuels[5] = new ItemStack(DimensionShift.itemEnderCrystal);
	fuels[6] = new ItemStack(DimensionShift.itemEnderDust);


	return fuels;
}

public static int getFuelValue(ItemStack itemstack) {
	int fuelValue=0;

	if(itemstack!=null) {
	//if (itemstack.getItem()==DimensionShift.itemEnderDust) fuelValue=1000;
	//if (itemstack.getItem()==DimensionShift.itemEnderDust) fuelValue=1500;
	if (itemstack.getItem()==Items.ender_eye) fuelValue=800;
	if (itemstack.getItem()==Items.ender_pearl) fuelValue=300;
	if (itemstack.getItem()==Items.nether_star) fuelValue=5000;
	//if (itemstack.getItem()==Blocks.dragon_egg.) fuelValue=5500;
	}

	return fuelValue;
}

    @SideOnly(Side.CLIENT)
public int getDimensionEnergyScaled(int heightDimensionEnergyBar) { 
    	
    	System.out.println(this.dimensionEnergy);
    	return this.dimensionEnergy * heightDimensionEnergyBar / this.requiredDimensionEnergy;
    
}


}

 

 

 

and gui:

 

 

package net.dimensionshift.mod.gui;

import org.lwjgl.opengl.GL11;

import net.dimensionshift.mod.DimensionShift;
import net.dimensionshift.mod.container.ContainerSimpleController;
import net.dimensionshift.mod.tileentity.TileEntitySimpleController;
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.client.resources.I18n;


public class GuiSimpleController extends GuiContainer{

public static final ResourceLocation texture = new ResourceLocation(DimensionShift.MODID, "textures/gui/simpleController.png");

public TileEntitySimpleController simpleController;


public GuiSimpleController(InventoryPlayer inventory, TileEntitySimpleController entity) {
	super(new ContainerSimpleController(inventory, entity));

	this.simpleController=entity;

	this.xSize = 176;
	this.ySize = 166;
}

@Override
public void drawGuiContainerForegroundLayer(int par1, int par2) {
	String name= this.simpleController.isInvNameLocalized() ? this.simpleController.getInvName() : I18n.format(this.simpleController.getInvName(), DimensionShift.instance);

	//Block Inventory Name
	this.fontRendererObj.drawString(name, this.xSize/2-fontRendererObj.getStringWidth(name), 6, 4210752);

	//Player Inventory Title
	this.fontRendererObj.drawString(I18n.format("container.inventory",  DimensionShift.instance), 8, this.ySize-96, 4210752);
}

@Override
public void drawGuiContainerBackgroundLayer(float var1, int var2,int var3) {
	/**
	 * coords are x from left border and y from !!!!top!!!!
	 * 
	 * 
	 * this.drawTexturedModalRect(where to draw lefttop corner X, where to draw lefttop corner Y, From where to take image lefttop corner X,  From where to take image lefttop corner Y, image width, image height
	 * 
	 * 
	 * 
	 */




	GL11.glColor4f(1F, 1F, 1F, 1F);

	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        
        
        
        
	this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);

	//dimensionEnergy energy bar...

		//heightDimensionEnergyBar is how height the dimension energy display is, needed for scaling
		int heightDimensionEnergyBar = 54;
		//width...
		int widthDimensionEnergyBar = 20;


		int currentDimensionEnergy = this.simpleController.getDimensionEnergyScaled(heightDimensionEnergyBar);

		this.drawTexturedModalRect(guiLeft + 15, guiTop+20+heightDimensionEnergyBar-currentDimensionEnergy, 176, heightDimensionEnergyBar-currentDimensionEnergy, widthDimensionEnergyBar, currentDimensionEnergy);



	// TODO Auto-generated method stub

}

}

 

 

Here could be your advertisement!

Posted

The client and the server are desynchronized. You should look into getDescriptionPacket() and onPacketData().

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

You should look into getDescriptionPacket() and onPacketData().

The description packet is definitely not the right tool for a progressbar.

@OP Look at ContainerFurnace on how to handle progress bars.

 

Ups, forgot to post the container...

 

 

 

package net.dimensionshift.mod.container;

import net.dimensionshift.mod.tileentity.TileEntitySimpleController;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ContainerSimpleController extends Container{

private TileEntitySimpleController simpleController;

//stored energy
public int energy;
//energy to be stored
public int charging=0;
//time until tp
public int tpTime = 1000;
//how long it is already preparing for tp
public int timeUntillTp;
//is just teleporting?
public boolean isTeleporting=false;

private int lastDimensionEnergy;

private int lastEnergy;

private int lastTimeUntillTp;





public ContainerSimpleController(InventoryPlayer inventory, TileEntitySimpleController entity){
	this.simpleController = entity;

	//goal Entity, SlotID, x and y Coordinates
	this.addSlotToContainer(new Slot(entity, 0, 62, 20));
	this.addSlotToContainer(new Slot(entity, 1, 44, 58));



	//top Player Inventory
	for (int i=0; i<3; i++) {
		for (int j=0; j<9; j++) {
			this.addSlotToContainer(new Slot(inventory, j+i*9+9, 8 + 18*j, 84 + 18*i));
		}
	}
	//bottom Player Inventory
	for (int i=0; i<9; i++) {
		this.addSlotToContainer(new Slot(inventory, i, 8 + 18*i, 142));
	}

}

public void addCraftingToCrafters(ICrafting icrafting) {
	super.addCraftingToCrafters(icrafting);
	icrafting.sendProgressBarUpdate(this, 0, this.simpleController.dimensionEnergy);
	icrafting.sendProgressBarUpdate(this, 1, this.simpleController.dimensionEnergy);
	icrafting.sendProgressBarUpdate(this, 2, this.simpleController.timeUntillTp);

}


public void detectAndSendChanges(){
    
        super.detectAndSendChanges();

        for (int i = 0; i < this.crafters.size(); ++i)
        {
            ICrafting icrafting = (ICrafting)this.crafters.get(i);

            if (this.lastDimensionEnergy != this.simpleController.dimensionEnergy)
            {
                icrafting.sendProgressBarUpdate(this, 0, this.simpleController.dimensionEnergy);
            }

            if (this.lastEnergy != this.simpleController.energy)
            {
                icrafting.sendProgressBarUpdate(this, 1, this.simpleController.energy);
            }
            
            if (this.lastTimeUntillTp != this.simpleController.timeUntillTp)
            {
                icrafting.sendProgressBarUpdate(this, 2, this.simpleController.timeUntillTp);
            }
            
        }

        this.lastDimensionEnergy = this.simpleController.dimensionEnergy;
        this.lastEnergy = this.simpleController.energy;
        this.lastTimeUntillTp =this.simpleController.timeUntillTp;
        //this.lastItemBurnTime = this.tileFurnace.currentItemBurnTime;
    }


@SideOnly(Side.CLIENT)
public void updateEnergyBar(int slot, int value){
	if(slot==0)this.simpleController.dimensionEnergy=value;
	if(slot==1)this.simpleController.energy=value;
	if(slot==2)this.simpleController.timeUntillTp=value;
}


 @Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot){
	 return null;
	 //not added yet

}

public boolean canInteractWith(EntityPlayer player) {
	// TODO Auto-generated method stub
	return this.simpleController.isUseableByPlayer(player);
}

}

 

 

Here could be your advertisement!

Posted

public void updateEnergyBar

Where do you call that method? And if you don't call it, how do you expect it to be called?

 

Maybe i should start a petition to add this smiley:banghead.gif

Here could be your advertisement!

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.