Jump to content

[1.12.1] Canceled PlaceEvent removes block from inventory


Pixtar

Recommended Posts

Hi all,

I'm using the following code to cancel the PlaceEvent:

@SubscribeEvent
public static void onBlockInteract( PlaceEvent event )
{
	int blockId = Block.getIdFromBlock( event.getPlacedBlock().getBlock() );
	if( blockId == 0 )
	{
		event.setCanceled( true );
	}
}

The following happens:

The block gets placed, it appears for a short time, then disappears and isn't placed at all BUT the placed block is gone from the inventory of the player AND is back after a relog of the player.

 

I want to give the player black the block during his current session, not after a relog.

 

If read the following thread/post maybe I'm encountering the same problem - but I don't know how to solve it.

 

How can I gave the player the not placed item/block back?

 

Kind regards,

Pixtar

Link to comment
Share on other sites

Hi Aarilight,

until now I haven't annotated any class or method with @SideOnly.

 

That sounds like both sides are in need of the mod, client and server?

 

So I need to divide the code into two sections, server and client, like this?

@SubscribeEvent
public static void onBlockInteract( PlaceEvent event )
{
  if( FMLCommonHandler.instance().getSide().isServer() )
  {
      int blockId = Block.getIdFromBlock( event.getPlacedBlock().getBlock() );
      if( blockId == 0 )
      {
          event.setCanceled( true );
      }
  }
  else
  {
      //What to do here?
  }
}

 

Currently I haven't much knowledge about Sides coding.

 

Kind regards,

Pixtar

Link to comment
Share on other sites

You don't need that, you already know your handler is only being called on the server, so it will only reach the top half anyway. Honestly the @SideOnly question was my only idea of what could be wrong, it prevents stuff from being called altogether by removing them from the code on the other side. If you're not using it then it's not your problem. I have no other ideas, so you'll have to wait until someone more knowledgeable responds. Sorry >.>

Link to comment
Share on other sites

Hi Aarilight,

ah okay, no problem, thanks for your help anyway.

 

I think I've detected another problem I need to solve. The canceled event is based on a server side config, which contains a list of blocks which aren't allowed to be place. So I need to follow the hints of this forge documentation and provide the blocks which needs to be blocked via an NetworkInterface to the client. Right?

 

Kind regards,

Pixtar

Link to comment
Share on other sites

Hi loordgek,

 

thanks for your hint. I've rewritten my code and added the config I'm using to define my blocked block:

@Config(modid = ExampleMod.MODID, name=ExampleMod.MODID+"/"+ExampleMod.MODID, category="config")
public class MyConfig
{
   public static int blockedBlockId = 1;
}

 

public class MyConfigWrapper
{
   public static Block blockedBlock = null;
   MyConfigWrapper()
   {
   	blockedBlock = Block.getBlockById( ExampleMod.config.blockedBlockId );
   }
}

 

public static MyConfig config = null;
public static MyConfigWrapper configWrapper = null;
@Mod.EventHandler
public void preinit( FMLPreInitializationEvent event )
{
  config = new MyConfig( );
  configWrapper = new MyConfigWrapper( );
  //How to share the blocked block for the client?
}

@SubscribeEvent
public static void onBlockInteract( PlaceEvent event )
{
  if( FMLCommonHandler.instance().getSide().isServer() )
  {
      Block destBlock = event.getPlacedBlock().getBlock();
      if( destBlock == configWrapper.blockedBlock )
      {
          event.setCanceled( true );
      }
  }
  else
  {
      //What to do here?
  }
}

At the end these changes are cosmetic I will still have the same problems: The client doesn't know about block which block should be blocked, right?

 

Kind regards,

Pixtar

Edited by Pixtar
Link to comment
Share on other sites

    public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
    {
        if (!worldIn.isRemote) return net.minecraftforge.common.ForgeHooks.onPlaceItemIntoWorld(this, playerIn, worldIn, pos, side, hitX, hitY, hitZ, hand);
        EnumActionResult enumactionresult = this.getItem().onItemUse(playerIn, worldIn, pos, hand, side, hitX, hitY, hitZ);

        if (enumactionresult == EnumActionResult.SUCCESS)
        {
            playerIn.addStat(StatList.getObjectUseStats(this.item));
        }

        return enumactionresult;
    }

this is the point in the itemStack where it gets called from,and the problem you have

 

why is the hook only called from the server side and not both ??

Link to comment
Share on other sites

Hi loordgek,

so how to cast from an Integer to an object Block dynamically without using switch case to determine the static Block?

 

I've removed the SideOnly barrier.

 

At the end I'm here to figure out exactly that question:

Quote

why is the hook only called from the server side and not both ??

 

Maybe PlaceEvent is only hooked on the server side and I need to use RightClickBlock?

 

Kind regards,

Pixtar

Link to comment
Share on other sites

3 minutes ago, Pixtar said:

so how to cast from an Integer to an object Block dynamically without using switch case to determine the static Block?

you dont

save the RegistryName to the config

 

Block.getRegistryName();

 

15 minutes ago, Pixtar said:

Maybe PlaceEvent is only hooked on the server side and I need to use RightClickBlock?

yes works tested it,  read the javadoc on it

Link to comment
Share on other sites

Hi loordgek,

 

if I'm storing e.g. Dirt via RegistryName it stores nothing in the annotated config except the variable name:

public static ResourceLocation blockedBlock = Blocks.DIRT.getRegistryName();

Result

blockedBlock {
}

 

Let's talk about which versions we are using, currently I'm working with forge-1.12.1-14.22.0.2467-mdk

 

Could it be a sync bug in forge? I'm asking, because I've contact to another mod developer and he's saying he's having the same issue. :-/

 

Kind regards,

Pixtar

Edited by Pixtar
Link to comment
Share on other sites

Hi loordgek,

 

well that worked for me:

public static String blockedBlock = Blocks.DIRT.getRegistryName().toString();

Results in:

S:blockedBlock=minecraft:dirt

 

Like I mentioned before about the cast .. how to cast it backwards? Currently I would do the comparison like the following, because I don't know how to cast from String to Block backwards:

if ( event.getPlacedBlock().getBlock().getRegistryName().toString().equals( config.blockedBlock ) )

The only way I found myself is this one ?!

if ( event.getPlacedBlock().getBlock() == Block.REGISTRY.getObject(new ResourceLocation( ExampleMod.config.blockedBlock ) ) )

 

Kind regards,

Pixtar

Edited by Pixtar
Link to comment
Share on other sites

1 hour ago, Pixtar said:

because Blocks.PODZOL isn't defined.

Of course it's not. For the same reason Blocks.PINK_WOOL isn't defined: it's a metadata state variant of the dirt block. You need to also read/supply a metadata value or IBlockState definition.

Edited by Draco18s

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.

Link to comment
Share on other sites

Hi Draco18s,

thanks for the advice of the metadata state. I will keep that in mind.


Anyway - the entire thread is heading the wrong track. My initial problem of the sync client / server is still available.

5 hours ago, Pixtar said:

The block gets placed, it appears for a short time, then disappears and isn't placed at all BUT the placed block is gone from the inventory of the player AND is back after a relog of the player.

Pixtar

Link to comment
Share on other sites

6 hours ago, Aarilight said:

Your handler is only running on the server, not the client, you can tell because the block was never placed, your client just thought it was.

 

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.

Link to comment
Share on other sites

Hi Draco18s,

so I've cleaned my gradle, created a complete clean server, removed all mods at the client side, compiled the BlockPlaceEventTest loordgek posted, inserted it at the server and the client side and tried it again.

I encounter still the same problem - the inventory of the client won't be updated, ONLY if the user collects a block of the same denied type the belt receives an update and the blocks are back.

 

Yep - I understand the circumstance that the client thinks the block is/was placed.

 

Main question: At which place do I need to modify what to inform the client that the block wasn't placed successfully if the code of loordgek isn't doing that?

Side question: How does the client notice. that the blocks weren't placed after collecting a new block of the same denied type?

Pixtar

Edited by Pixtar
Link to comment
Share on other sites

1 hour ago, Pixtar said:

Hi Draco18s,

compiled the BlockPlaceEventTest loordgek posted, inserted it at the server and the client side and tried it again.

You put the code from a test into your code? He posted that test to demonstrate how BlockPlaceEvent works. This code won't do anything for you as is.

 

6 hours ago, Pixtar said:

Maybe PlaceEvent is only hooked on the server side and I need to use RightClickBlock?

In the handler which started this entire problem, did you try using RightClickBlock instead of PlaceEvent?

 

I went ahead and just tested in game, and yes, PlaceEvent only seems to occur on the server. RightClickBlock can be cancelled based on the ItemStack in your hand, though. Here is working code that only cancels Podzol:

 

	@SubscribeEvent
	public static void onBlockPlace(RightClickBlock event) {
		ItemStack block = event.getEntityPlayer().getHeldItemMainhand();
		if (ItemStack.areItemStacksEqual(block, new ItemStack(Blocks.DIRT, block.getCount(), 2))) {
			event.setCanceled(true);
		}
	}

 

To cancel based on a list of blocked blocks, the easiest way would be to store them as ItemStacks, since that supports holding both the block type and the metadata, and just loop through the list in this event handler. To check equality, make sure the count of the stack you're checking is the same as the held stack count. WILDCARD_VALUE does not work for count. If one of them matches, cancel the event.

Link to comment
Share on other sites

Hi Aarilight,

7 hours ago, Aarilight said:

You put the code from a test into your code? He posted that test to demonstrate how BlockPlaceEvent works. This code won't do anything for you as is.

well, that might be, that he posted that code only for a demo - nevertheless the code is doing the same like mine; canceling the event.

 

7 hours ago, Aarilight said:

In the handler which started this entire problem, did you try using RightClickBlock instead of PlaceEvent?

Yep - I tried that one too. Thank you for your time to create and test your posted snippet.

 

I'm sorry to say that, but for my version of forge (1.12.1-14.22.0.2452) it does nothing else than the other codes - furthermore it doesn't only block Podzol it blocks the entire dirt group. Block gets placed, forge tidies up the block, it disappears and it's gone from the inventory.

 

Have a nice day,

Pixtar

Link to comment
Share on other sites

Hi all again,

 

it's driving me insane. xD

It's only working flawless if both ; client and server are having the same config file. >:(

 

Now I changed the config file at the server from dirt to stone and I'm running into the same problem - again.

Block isn't placed, Block is temporary gone from the hotbar slot. I can throw/drop [Q] the "empty" slot and it drops the gone stone block.

SOO the client knows about the not placed block, but it doesn't refresh the hotbar slot.

 

How can I refresh/update the hotbar slot?

 

Kind regards,

Pixtar

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.



×
×
  • Create New...

Important Information

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