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.

[SOLVED]TileEntity implementing IUpdatePlayerListBox only working clientside

Featured Replies

Posted

I have created a block that shall serve as a kind of totem pole that keeps bad monsters away. It basically works flawlessly, killing all skeletons around it as it is supposed to be and so on. But there is one weird thing happening every time I end minecraft and open the world again after relaunching minecraft:

Basically nothing happens. The tileentity also summons lightning, and for a time I had a weird semilightning appearing everytime I reloaded the world. I got rid of it checking if the world was remote, as summoning entities and so forth are only working it the world is not remote. So, as I said, when reloading the world after closing minecraft and relaunching it, the tile entity stops to work. Or at least whatever it does, it does not seem to have an effect anymore. I tested it in a various amount of ways, printing out a lot of different things in the console. Additionally please note that when placing the block, it all works, and when quitting the world to the title and than opening it again it works as well without a problem. It only does not work when restarting the game itself.

 

Here is my code of the Block class

package com.brickfix.totemmod.blocks;

import java.util.Random;

import com.brickfix.totemmod.TotemBlockLogic;
import com.brickfix.totemmod.lib.Constants;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.command.ICommandSender;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityCommandBlock;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class TotemBlockTest extends BlockContainer
{
public static final String name = "totem_block";
public boolean isCharged;

public TotemBlockTest() 
{
	super(Material.rock);


	this.setCreativeTab(CreativeTabs.tabBlock);
	this.setHardness(5.0F);
	//this.setTickRandomly(true);

	GameRegistry.registerBlock(this, name);
	setUnlocalizedName(Constants.MODID + "_" + name);

}

public String getName()
{
	return this.name;
}

public int getRenderType()
    {
        return 3;
    }

@Override
public TileEntity createNewTileEntity(World worldIn, int meta) 
{
	return new TileEntityTotemBlock();
}

}

 

Here is the TileEntity

package com.brickfix.totemmod.blocks;

import java.util.Random;

import net.minecraft.command.CommandBase;
import net.minecraft.command.NumberInvalidException;
import net.minecraft.entity.Entity;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.item.EntityFireworkRocket;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.server.gui.IUpdatePlayerListBox;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import com.brickfix.totemmod.TotemBlockLogic;
import com.brickfix.totemmod.TotemMod;
import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;

public class TileEntityTotemBlock extends TileEntity implements IUpdatePlayerListBox
{
public int cooldown;
public boolean isCharged;

private final TotemBlockLogic totemLogic = new TotemBlockLogic()
{
	@Override
	public String getCommand()
	{
		return "/kill @e[type=Skeleton,r=5]";
	}

	@Override
	public BlockPos getPosition() 
	{
		return TileEntityTotemBlock.this.pos;
	}

	@Override
	public Vec3 getPositionVector() 
	{
		return new Vec3((double)TileEntityTotemBlock.this.pos.getX() + 0.5D, (double)TileEntityTotemBlock.this.pos.getY() + 0.5D, (double)TileEntityTotemBlock.this.pos.getZ() + 0.5D);
	}

	@Override
	public World getEntityWorld() 
	{
		return TileEntityTotemBlock.this.worldObj;
	}

};

public int getCooldown()
{
	return 100;
}

public TotemBlockLogic getTotemLogic()
{
	return this.totemLogic;
}

public void writeToNBT(NBTTagCompound compound)
{
	super.writeToNBT(compound);
	compound.setInteger("Cooldown", this.cooldown);
	compound.setBoolean("Charged", this.isCharged);

}
public void readFromNBT(NBTTagCompound compound)
{
	super.readFromNBT(compound);
	this.cooldown = compound.getInteger("Cooldown");
	this.isCharged = compound.getBoolean("Charged");
}

static
{
	addMapping(TileEntityTotemBlock.class, "TotemBlock");
}


public void update()
{
	if (this.worldObj.isRemote) return;

	this.isCharged = isCharged();

	if (this.cooldown==0)
	{
		int i=0;

		if (this.isCharged)
		{
			i = totemLogic.trigger(this.worldObj);
		}
		if (i!=0)
		{
			this.cooldown = this.getCooldown();
			Item rm = Items.fireworks;

			NBTTagCompound tag0 = new NBTTagCompound();
			NBTTagList explosions = new NBTTagList();
			NBTTagCompound tag1 = new NBTTagCompound();
			NBTTagCompound tag2 = new NBTTagCompound();

			int[] array = new int[1];
			array[0] = 65536*230;

			tag2.setIntArray("Colors", array);
			tag2.setBoolean("Flicker", true);

			explosions.appendTag(tag2);

			tag1.setTag("Explosions", explosions);
			tag1.setByte("Flight", (byte) 0);

			tag0.setTag("Fireworks", tag1);


			ItemStack modifier = new ItemStack(rm);

			modifier.setTagCompound(tag0);

			Entity effect = new EntityFireworkRocket(this.worldObj, pos.getX()+0.5D, pos.getY()+0.5D, pos.getZ()+0.5D, modifier);

			this.worldObj.spawnEntityInWorld(effect);
		}	
	}
	else if (this.cooldown > 0)
	{
		this.cooldown--;
	}

}

public boolean isCharged()
{
	boolean flag = this.isCharged;
	boolean flag2 = false;
	BlockPos pos = this.pos;
	if ((this.worldObj.getBlockState(pos.up()) == Blocks.obsidian.getDefaultState()) && (this.worldObj.getBlockState(pos.down()) == Blocks.obsidian.getDefaultState()))
	{
		flag2 = true;
	}

	if (flag==false && flag2==true)
	{
		this.lightningStrikes(worldObj, pos);
	}

	return flag2;
}

protected void lightningStrikes(World world, BlockPos pos)
{

	double dx = (double)pos.getX();
	double dy = (double)pos.getY();
	double dz = (double)pos.getZ();

	String sx = String.valueOf(pos.getX());
	String sy = String.valueOf(pos.getY());
	String sz = String.valueOf(pos.getZ());

	try 
	{
		dx = CommandBase.func_175761_b(dx, sx, true);
		dy = CommandBase.func_175761_b(dy, sy, false);
		dz = CommandBase.func_175761_b(dz, sz, true);
	} 
	catch (NumberInvalidException e) 
	{
		e.printStackTrace();
		return;
	}
        
        
        world.addWeatherEffect(new EntityLightningBolt(world, dx, dy, dz));
}

}

 

And here is my TotemLogic class

package com.brickfix.totemmod;

import net.minecraft.command.ICommandSender;
import net.minecraft.command.CommandResultStats.Type;
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.BlockPos;
import net.minecraft.util.IChatComponent;
import net.minecraft.world.World;

public abstract class TotemBlockLogic implements ICommandSender
{

public int trigger(World worldIn) 
{
	MinecraftServer minecraftserver = MinecraftServer.getServer();
	int i = 0;

        if (minecraftserver != null && minecraftserver.func_175578_N())
        {

        	try
        	{
        		i = minecraftserver.getCommandManager().executeCommand(this, getCommand());
        		//System.out.println(getCommand());
        		return i;
        	}
        	catch(Throwable t)
        	{
        		System.out.println("could not execute command");
        		return 0;
        	}
        }
	return i;

}

public abstract String getCommand();

@Override
public void addChatMessage(IChatComponent message) 
{
	//We do not want to see a chat message
}

@Override
public String getName() 
{
	// TODO Auto-generated method stub
	return "totem";
}

@Override
public Entity getCommandSenderEntity() 
{
	// This is no entity and not important
	return null;
}

@Override
public boolean sendCommandFeedback() 
{
	//We do not need this
	return false;
}

@Override
public void setCommandStat(Type type, int amount) 
{
	// We do not need this		
}

@Override
public IChatComponent getDisplayName() 
{
	return null;
}

@Override
public boolean canUseCommand(int permLevel, String commandName) 
{
	return true;
}



}

 

It would be awesome if you could help me out with this :)

Hi

 

Without looking at your code too hard, my first guess is that it's because you haven't implemented onDataPacket() and getDescriptionPacket().  If you're doing everything server-side, I would have thought it's not necessary, but I'm not 100% sure.

 

See here for an explanation

http://greyminecraftcoder.blogspot.com.au/2015/01/tileentity.html

 

Otherwise - are you sure that server side update stops getting called?  Show an example of the test code and the console output that leads you to that conclusion?

 

-TGG

 

 

 

  • Author

So it seems that I have not implemented the onDataPacket() and getDescriptionPacket() methods, I added these in and the problem persists.

I am really sure that it runs the update() only client side, I added

 

if (this.worldObj.isRemot)
{
    System.out.println("Updated ClientSide");
    return;
}
else
{
    System.out.println("Updated ServerSide");
    //Here comes the rest of my code
}

 

Turns out that when I place the block, both messages pop up in the console. Closing and reloading the world still keeps both messages showing up. Ending the minecraft game and restarting it only leads to the display of the "Updated ClientSide" message.

 

I copied over the onDataPacket() and getDescriptionPacket() methods into my code, and the problem is still there. Maybe I set them up incorrectly?

@Override
public Packet getDescriptionPacket() 
{
	NBTTagCompound nbtTagCompound = new NBTTagCompound();
	writeToNBT(nbtTagCompound);
	int metadata = getBlockMetadata();
	return new S35PacketUpdateTileEntity(this.pos, metadata, nbtTagCompound);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) 
{
	readFromNBT(pkt.getNbtCompound());
}

 

I have basically read through everything in you blog, it is explaining really good and already helped me out several times.

Thanks for taking the time :)

  • Author

Ok so here are all my classes:

 

Main mod class:

http://pastebin.com/CUzCNWF0

 

Common Proxy:

http://pastebin.com/ncaUgkpx

 

The Block class:

http://pastebin.com/Q2EFxbX5

 

The TileEntity:

http://pastebin.com/cgGQXdxT

 

And the Logic class:

http://pastebin.com/bmW1yBnW

 

I left out the client proxy as it is doing nothing ATM as well as my constants class as it is pretty obvious what it is doing.

 

Thanks again for taking the time and looking over my problem :)

 

  • Author

Yes I commented them out for a later test, but I ran it with both not commented out:

After I set the block, poth printed out.

After exiting minecraft and reloading the world only the client part printed.

  • Author

Its like my classes are bewitched or something. I even compiled it all and tested it with my actual minecraft game and the problems still occured exactly as described above.

I will update my forge version, maybe this is causing it ...

 

EDIT:

I recompiled forge and suddenly all problems have disappeared. Thanks a lot anyway, I have learned a lot of other things about tile entities during the process :D

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.