Jump to content

Recommended Posts

Posted (edited)

Hi,

 

I'm using the following code to prevent the player breaking carrots which aren't fully-grown. It works fine on singleplayer, but not at all on multiplayer, in fact on multiplayer the event isn't being fired at all. Why could this be?

 

@SubscribeEvent
    public void onBlockBreak(BreakEvent event)
    {
        IBlockState state = event.getState();
        net.minecraft.block.Block block = state.getBlock();
        
        for (Object o : state.getProperties().entrySet())
        {
            Map.Entry e = (Map.Entry)o;
            
            if (e.getKey() instanceof PropertyInteger)
            {
                PropertyInteger prop = (PropertyInteger)e.getKey();

                if (prop.getName().equals("age"))
                {
                    int age = state.getValue(prop);
                    if (age < 7) event.setCanceled(true);
                }
            }
        }
}
Edited by AntiRix
Posted

I had 1.12.2 already set up so might as well go with that.

 

The mod isn't for the server itself, it's a client-side mod, but one which isn't working on multiplayer for some reason.

Posted (edited)

Ok, so how should I approach cancelling block break events with a client-side mod only, on multiplayer?

Edited by AntiRix
Posted (edited)

I've adjusted the code, but now it seems the block doesn't even have any properties. 

 

@SubscribeEvent
	public void onBlockBreak(PlayerInteractEvent.LeftClickBlock event)
	{
		IBlockState state = mc.theWorld.getBlockState(event.getPos());
		
		mc.thePlayer.addChatMessage(new TextComponentString("onBlockBreak"));
		for (Object o : state.getProperties().entrySet())
		{
			Map.Entry e = (Map.Entry)o;
			
			if (e.getKey() instanceof PropertyInteger)
			{
				PropertyInteger prop = (PropertyInteger)e.getKey();
				mc.thePlayer.addChatMessage(new TextComponentString(prop.getName() + ": " + state.getValue(prop)));
				if (prop.getName().equals("age"))
				{
					int age = state.getValue(prop);
					
					if (age < 7)
					{
						event.setUseBlock(Result.DENY);
						event.setUseItem(Result.DENY);
						event.setCanceled(true);
					}
					
					return;
				}
			}
		}
}

 

Edited by AntiRix
Posted (edited)

If I have the following at the beginning of the method, nothing happens.

 

if (!mc.theWorld.isRemote) return;

 

If I have the following, properties are retrieved but it still doesn't cancel the breaking, even though age < 7 is true:

 

if (mc.theWorld.isRemote) return;

 

This makes no sense considering what you just said

Edited by AntiRix
  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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