Jump to content

[1.12] Preventing block breaking on a server


AntiRix

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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