Jump to content

[SOLVED]TileEntity implementing IUpdatePlayerListBox only working clientside


Brickfix

Recommended Posts

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 :)

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

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

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Wait guys, I still don't don't understand... I shall send my code -- Define the function to decrease hunger local function decreaseHunger(player)     local currentHunger = player.getHunger()          -- Check if hunger is not already zero     if currentHunger > 0 then         player.setHunger(currentHunger - 1)     end end -- Register event to decrease hunger over time script.registerEvent(EntityPlayer, function(player)     while true do         -- Decrease hunger every few seconds         decreaseHunger(player)         script.sleep(1000) -- Sleep for 1 second     end end)     Any help appriecated. It's been a while since I learnt Javascript or whatever Minecraft is made with... ... ... ... Nicknotname Hungerinpeaceful xx
    • good days  i get this error: java.lang.ClassCastException: class net.minecraft.client.player.LocalPlayer cannot be cast to class net.minecraftforge.common.extensions.IForgeServerPlayer (net.minecraft.client.player.LocalPlayer is in module minecraft@1.20.4 of loader 'TRANSFORMER' @3e8b3b79; net.minecraftforge.common.extensions.IForgeServerPlayer is in module forge@49.0.26 of loader 'TRANSFORMER' @3e8b3b79)     at mercmod.blocks.classes.panel_whit_entity_inside.use(panel_whit_entity_inside.java:576) ~[main/:?] {re:classloading}     at net.minecraft.world.level.block.state.BlockBehaviour$BlockStateBase.use(BlockBehaviour.java:826) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading}     at net.minecraft.client.multiplayer.MultiPlayerGameMode.performUseItemOn(MultiPlayerGameMode.java:324) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.multiplayer.MultiPlayerGameMode.lambda$useItemOn$4(MultiPlayerGameMode.java:292) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.multiplayer.MultiPlayerGameMode.startPrediction(MultiPlayerGameMode.java:251) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.multiplayer.MultiPlayerGameMode.useItemOn(MultiPlayerGameMode.java:291) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.startUseItem(Minecraft.java:1799) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.handleKeybinds(Minecraft.java:2083) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.tick(Minecraft.java:1902) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.runTick(Minecraft.java:1216) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.run(Minecraft.java:801) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:234) ~[forge-1.20.4-49.0.26_mapped_official_1.20.4-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A} ------------------------------------------------------------------------------------------------------------------   context:  im updating the briefcase from mi mod an i need to make a gui whit this image that gonna be launched from a block and from a item soo im using kapenjoe tutorials  ---------------------------------------------------------------------------------------- The problem is that tutorial is for 1.20.1 and im using 1.20.4 and i really dont get how to call and open the gui from the block nor from the item i made this based on post i made short ago everything seems find but when right click the block to open the gui it crash the game  @Override public InteractionResult use(BlockState blkstate, Level warudo, BlockPos pos, Player pe, InteractionHand hand, BlockHitResult hitresult) {     if(warudo.isClientSide()){         BlockEntity blkentity = warudo.getBlockEntity(pos);         if(blkentity instanceof Panel_BlockEntity){             //   ↓↓↓  open the menu from the block              IForgeServerPlayer ifpe = (IForgeServerPlayer)pe; //<--- this is wrong  "class net.minecraft.client.player.LocalPlayer cannot be cast to class net.minecraftforge.common.extensions.IForgeServerPlayer"             ifpe.openMenu( (Panel_BlockEntity) blkentity, pos ); //<-- i need an example of how open a gui in 1.20.4         }     } return InteractionResult.sidedSuccess(warudo.isClientSide()); } Theres nothing highlighted in red in the code like everything where right an possible but dont works  ####################################################################################################### i need to see an example, just the piece of code for the use() method of the block whit the block entity for 1.20.4  and the same  but for the item     @Override     public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level warudo, @NotNull Player pe, @NotNull InteractionHand interactionHand)     {         if (!warudo.isClientSide())         {             ItemStack heldItem = pe.getItemInHand(interactionHand);                          if (heldItem.getCapability(ForgeCapabilities.ITEM_HANDLER).isPresent())             {                 //   ↓↓↓  open the menu from the item                 NetworkHooks.openScreen((ServerPlayer) pe, this);             }         }         return super.use(warudo, pe, interactionHand);     }   thanks for your attention           
    • It depends on the data you want to save, but generally speaking you should use some custom Player Capabilities, attach to the Player and use them to store/retrieve any kind of data you want. You can find how to make them work for 1.20.x in the documentation here: https://docs.minecraftforge.net/en/latest/datastorage/capabilities/
  • Topics

×
×
  • Create New...

Important Information

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