Jump to content

[1.6.4] Create a machine block which can contain water


minipopov1

Recommended Posts

Hello,

 

I m doing my first mod and i wanna to make a block which use water. In gui i want see how many water are in this block. So, this is my tile entity of my block

public class StrikingMachineTileEntity extends TileEntity implements ISidedInventory, IFluidHandler {
public class Tank extends GenericFluidTank{
	public Tank(int capacity)
        {
            super(capacity);
        }
	@Override
	public boolean canAccept(FluidStack fluid){
		switch(fluid.fluidID)
		{
			case 1:
				return true;
			default:
				return false;
		}
	}
}
public Tank tank;
...
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
	return tank.fill(resource, doFill);
}

@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
	return tank.drain(resource, doDrain);
}

@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
	return tank.drain(maxDrain, doDrain);
}

@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
	return true;
}

@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
	return true;
}

@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
	return new FluidTankInfo[]{tank.getInfo()};
}

public int getFluidAmount(){
	return tank.getFluidAmount();
}
    public FluidStack getFluid()
    {
        return tank.getFluid();
    }
    public int getCapacity(){
    	return tank.getCapacity();
    }

}

 

Now this is my class for my tnak

 

public class GenericFluidTank implements IFluidTank{

private FluidStack fluid;
private boolean changeType;
private int capacity;

public GenericFluidTank(FluidStack type, int capacity)
    {
        if(type == null)
        {
            fluid = new FluidStack(0, 0);
            this.changeType = true;
        }
        else
            fluid = FluidUtils.copy(type, 0);
        this.capacity = capacity;
    }
public GenericFluidTank(int capacity){
	this(null, capacity);
}

@Override
public int getCapacity(){
	return capacity;
}

@Override
public FluidStack getFluid() {
	return fluid.copy();
}

@Override
public FluidTankInfo getInfo() {
	return new FluidTankInfo(this);
}

@Override
public int fill(FluidStack resource, boolean doFill) {
	if(resource == null || resource.fluidID < 0){
		return 0;
	}
	if(!canAccept(resource)){
		return 0;
	}
	Console.println("Fluid amout before : " +this.getFluidAmount());
	int tofill = Math.min(getCapacity()-this.fluid.amount, resource.amount);
        if(doFill && tofill > 0)
        {
            if(!this.fluid.isFluidEqual(resource)){
                this.fluid = copy(resource, fluid.amount+tofill);
            }else{
                this.fluid.amount+=tofill;
            }
            onLiquidChanged();
        }
        Console.println("Fluid amout after : " +this.getFluidAmount());
	return tofill;
}

@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
	if(fluid.amount == 0 || maxDrain <= 0)
            return null;
	int todrain = Math.min(maxDrain, fluid.amount);
        if(doDrain && todrain > 0)
        {
            fluid.amount-=todrain;
            onLiquidChanged();
        }
        return FluidUtils.copy(fluid, todrain);
}
public FluidStack drain(FluidStack resource, boolean doDrain)
    {
        if (resource == null || !resource.isFluidEqual(fluid))
            return null;

        return drain(resource.amount, doDrain);
    }

public void onLiquidChanged()
    {

    }

public boolean canAccept(FluidStack type){
	return type == null || type.fluidID <= 0 || (fluid.amount == 0 && changeType) || fluid.isFluidEqual(type);
}

public static FluidStack copy(FluidStack liquid, int quantity)
    {
        liquid = liquid.copy();
        liquid.amount = quantity;
        return liquid;
    }


public NBTTagCompound toTag()
    {
        return FluidUtils.write(fluid, new NBTTagCompound());
    }
public void fromTag(NBTTagCompound tag)
    {
        fluid = FluidUtils.read(tag);
    }

@Override
public int getFluidAmount() {
	return this.fluid.amount;
}
}

 

And now my class for my gui

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
	// TODO Auto-generated method stub
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(background);
        int var5 = (this.width - this.xSize) / 2;
        int var6 = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
        // Progress Bar
        int x = (this.width - this.xSize) / 2;
        int y = (this.height - this.ySize) / 2;
        double ratioPixelBar = (double) 112/tileEntity.getCapacity();
        int stored = tileEntity.tank.getFluidAmount();
        Console.println("Fluid amount for gui :" +stored);
        //int stored = (int)(tileEntity.( ) *ratioPixelBar);
        //int stored = 0;
        this.drawTexturedModalRect(x + 32, y + 24, 0, 166, stored, 7);
}

 

Well, now i ll show you the output

So when i don't open the machine block, we can see the fluid amount up

 

2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 0
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 0
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 0
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 90
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 90
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 90
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 90
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 180
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 180
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 180
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 180
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 268
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 268
...
2014-06-14 14:44:34 [iNFOS] [sTDOUT] Fluid amout after : 12000
2014-06-14 14:44:34 [iNFOS] [sTDOUT] Fluid amout before : 12000

 

I get the fluidamount by the function in my GenericTankFluid (getFluidAmount);

 

But the probleme is when i open the gui

2014-06-14 14:44:42 [iNFOS] [sTDOUT] Fluid amount for gui :0

Somebody can tell me where my fluid is gone? i don't realy understand why i don't get the good value. While i open the gui, i can already see this line

2014-06-14 14:44:42 [iNFOS] [sTDOUT] Fluid amount for gui :0

2014-06-14 14:44:42 [iNFOS] [sTDOUT] Fluid amout before : 12000

2014-06-14 14:44:42 [iNFOS] [sTDOUT] Fluid amout after : 12000

 

Tanks if people read all my post ^^

Link to comment
Share on other sites

I don't understand why i need packet handler. Cause if i set :

public int famount = 10000;

And in my gui i set my function getFluidAmount from :

public int getFluidAmount(){
	return tank.getFluidAmount();
}

to

	public int getFluidAmount(){
	return famout;
}

My function return to my gui 10000. So packet handler is needed for my tileentity to get the value of one of his var? That s something strange..?

Link to comment
Share on other sites

All variables in your class will have 2 instances: sevrer, and client side, if you set it manually it will be the same, but all stuff you do (like smelt) are done server side (no cheating), so you need to sync the 2 vars when you change them servervar-->clientvar.

Link to comment
Share on other sites

Sorry, i don't resolve my problem.

I change this to my function:

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int idk, float what, float these, float are) {
	StrikingMachineTileEntity tileEntity = (StrikingMachineTileEntity) world.getBlockTileEntity(x, y, z);

	if (tileEntity == null || player.isSneaking()){
		return false;
	}      

	ByteArrayOutputStream bos = new ByteArrayOutputStream(;
	DataOutputStream outputStream = new DataOutputStream(bos);
	try {
	        outputStream.writeInt(x);
	        outputStream.writeInt(y);
	        outputStream.writeInt(z);
	} catch (Exception ex) {
	        ex.printStackTrace();
	}

	Packet250CustomPayload packet = new Packet250CustomPayload();
	packet.channel = Utilities.CHANNEL;
	packet.data = bos.toByteArray();
	packet.length = bos.size();
	player.openGui(Utilities.instance, GUIHandler.GUI_STRINKINGMACHINE, world, x, y, z);
	return true;
}

And to update, i make my packet handler and i try to copy the tile entity from the server to the client.

 

	@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
	DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
	try{
		int x = inputStream.readInt();
		int y = inputStream.readInt();
		int z = inputStream.readInt();

		EntityPlayerMP playerMP = (EntityPlayerMP)player;
		TileEntity te = playerMP.worldObj.getBlockTileEntity(x, y, z);
	    if(te != null){
	        if(te instanceof StrikingMachineTileEntity){
	        	StrikingMachineTileEntity tet = (StrikingMachineTileEntity)te;
	            playerMP.worldObj.markBlockForUpdate(x, y, z);
	        }
	    }
	} catch (IOException e){
		e.printStackTrace();			
	}
}	

 

But it doesn't work. What's wrong in my code???

Link to comment
Share on other sites

What's wrong with your code? A LOT of things. I would suggest reading the tutorials on the SimpleNetworkWrapper that are in the tutorials section of the modder support forums. But then again, your working with a tile entity and shouldn't need custom packets.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

* blinks in surprise *

* looks at title *

* facepalms *

 

It's 1.7...

 

Why aren't you using forge1.7.x by the way? Update your forge and then continue from there maybe?

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

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.