Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.7.10] Why can't access variable changes from inside !this.worldObj.isRemote

Featured Replies

Posted

Hello!

 

I was just wondering why I can't access the changes done to the variable cookTime when it is inside a !this.worldObj.isRemote if statement.

If I remove the !this.worldObj.isRemote I am able to get changes done to it and all is fine.

 

What happens is that with that if statement in there, I dont get access to the code saying "this.cookTime++;", the cookTime variable only works when I am right clicking inside the gui (this is only for when I try to access it with sysout to console), when I leave the gui the variable freezes.

 

Do I need the !this.worldObj.isRemote in the code below, or is it ok if I remove it? Or does code like that have to be done ONLY on server?

 

 

I'm new to java and still learning, sorry if it is obvious, thanks.

 

TileEntityCustomFurnace


public void updateEntity() {
	boolean flag = this.burnTime > 0;
	boolean flag1 = false;

	if (this.burnTime > 0) {
		this.burnTime--;
	}

	if (!this.worldObj.isRemote) {
		if (this.burnTime == 0 && this.canSmelt()) {
			this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[1]);

			if (this.burnTime > 0) {
				flag1 = true;

				if (this.slots[1] != null) {
					this.slots[1].stackSize--;

					if (this.slots[1].stackSize == 0) {
						this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);

					}

				}
			}

		}

		if (this.isBurning() && this.canSmelt()) {
			this.cookTime++;

			if (this.cookTime == this.furnaceSpeed) {
				this.cookTime = 0;
				this.smeltItem();
				flag1 = true;
			}
		} else {
			this.cookTime = 0;
		}

		if (flag != this.burnTime > 0) {
			flag1 = true;
			BlockCustomFurnace.updateCustomFurnaceBlock(this.burnTime > 0, this.worldObj, this.xCoord, this.yCoord,
					this.zCoord);

		}
	}

	if (flag1) {
		this.markDirty();
	}

}

only do changes on important stuff on server side (!worldObj.isRemote) . if u need those values on client side sync them. (packets or Containers got an additional way of syncing when they are opened)

  • Author

Thanks for reply!

I googled syncing client and server and got receiveClientEvent and addBlockEvent.

Am I doing anything right or am I way off? I got kinda stuck now, all I need is the cookTime variable, hmm.

 

TileEntityCustomFurnace


         public int eventData;
 private int ticksSinceSync;
 private int field_145974_k;

    @Override
    public boolean receiveClientEvent(int eventID, int eventData)
    {
        if (eventID == 1)
        {
            this.eventData = eventData;
            return true;
        }
       else
        {
            return super.receiveClientEvent(eventID, eventData);
        }
    }

public void updateEntity() {
	boolean flag = this.hasPower();
	boolean flag1 = false;

	if (++this.field_145974_k % 20 * 4 == 0){
		worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.blockCustomFurnaceIdle, 1, this.cookTime);
	 }
	if(this.hasPower() && this.isMacerating()){
		this.power--;


	}

	if (!this.worldObj.isRemote) {
		if (this.hasItemPower(this.slots[1]) && this.power <= (this.maxPower - this.getItemPower(this.slots[1]))) {
			this.power += getItemPower(this.slots[1]);

			if(this.slots[1] != null){

				flag1 = true;

				this.slots[1].stackSize--;

				if(this.slots[1].stackSize == 0){
					this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);

				}
			}

		}

		if (this.hasPower() && this.canMacerate()) {
			this.cookTime++;

			//System.out.println(this.isActive);

			if (this.cookTime== this.maceratingSpeed) {
				this.cookTime = 0;

				this.macerateItem();
				flag1 = true;
			}
		} else {
			this.cookTime = 0;

		}


         	if (flag != this.cookTime > 0)
        	{
            	flag1 = true;
            		BlockCustomMacerator.updateCustomMaceratorBlock(this.cookTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
        		}


	}

	if (flag1) {
		this.markDirty();
	}

}

Vanilla has unique packets for every tile entity and is not appropriate for mod added tile entities.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • Author

Shouldn't cookTime be updated on client when I have container like this?

 


public void addCraftingToCrafters(ICrafting icrafting){
	super.addCraftingToCrafters(icrafting);
	icrafting.sendProgressBarUpdate(this, 0, this.customMacerator.cookTime);
	icrafting.sendProgressBarUpdate(this, 1, this.customMacerator.power);
}

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

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

		if(this.lastCookTime != this.customMacerator.cookTime){
			icrafting.sendProgressBarUpdate(this, 0, this.customMacerator.cookTime);
		}

		if(this.lastBurnTime != this.customMacerator.power){
			icrafting.sendProgressBarUpdate(this, 1, this.customMacerator.power);
		}

	}

	this.lastCookTime = this.customMacerator.cookTime;
	this.lastBurnTime = this.customMacerator.power;

}

@SideOnly(Side.CLIENT)
public void updateProgressBar(int slot, int newValue){
	if(slot == 0) this.customMacerator.cookTime = newValue;
	if(slot == 1) this.customMacerator.power = newValue;

}

 

  • Author

If cookTime only updates when I am inside the gui is problem still with the container?

 

TileEntity

public boolean isActive() {
	System.out.println(this.cookTime);
	return this.cookTime > 0;
}

 

BlockClass

  @Override
  @SideOnly(Side.CLIENT)
public void randomDisplayTick(World world, int x, int y, int z, Random random) {

	TileEntityCustomMacerator te = (TileEntityCustomMacerator) world.getTileEntity(x, y, z);

	if (te.isActive()) { stuff }

 

Yes, the Container packets are only send when the gui is open. And as diesieben07 said, if you need the data outside of the gui, you need custom packets instead of the Container packets.

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/

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.