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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • If you're seeking an unparalleled solution to recover lost or stolen cryptocurrency, let me introduce you to GearHead Engineers Solutions. Their exceptional team of cybersecurity experts doesn't just restore your funds; they restore your peace of mind. With a blend of cutting-edge technology and unparalleled expertise, GearHead Engineers swiftly navigates the intricate web of the digital underworld to reclaim what's rightfully yours. In your moment of distress, they become your steadfast allies, guiding you through the intricate process of recovery with transparency, trustworthiness, and unwavering professionalism. Their team of seasoned web developers and cyber specialists possesses the acumen to dissect the most sophisticated schemes, leaving no stone unturned in their quest for justice. They don't just stop at recovering your assets; they go the extra mile to identify and track down the perpetrators, ensuring they face the consequences of their deceitful actions. What sets  GearHead Engineers apart is not just their technical prowess, but their unwavering commitment to their clients. From the moment you reach out to them, you're met with compassion, understanding, and a resolute determination to right the wrongs inflicted upon you. It's not just about reclaiming lost funds; it's about restoring faith in the digital landscape and empowering individuals to reclaim control over their financial futures. If you find yourself ensnared in the clutches of cybercrime, don't despair. Reach out to GearHead Engineers and let them weave their magic. With their expertise by your side, you can turn the tide against adversity and emerge stronger than ever before. In the realm of cybersecurity, GearHead Engineers reigns supreme. Don't just take my word for it—experience their unparalleled excellence for yourself. Your journey to recovery starts here.
    • Ok so this specific code freezes the game on world creation. This is what gets me so confused, i get that it might not be the best thing, but is it really so generation heavy?
    • Wizard web recovery has exhibited unparalleled strength in the realm of recovery. They stand out as the premier team to collaborate with if you encounter withdrawal difficulties from the platform where you’ve invested. Recently, I engaged with them to recover over a million dollars trapped in an investment platform I’d been involved with for months. I furnished their team with every detail of the investment, including accounts, names, and wallet addresses to which I sent the funds. This decision proved to be the best I’ve made, especially after realizing the company had scammed me.   Wizard web recovery ensures exemplary service delivery and ensures the perpetrators face justice. They employ advanced techniques to ensure you regain access to your funds. Understandably, many individuals who have fallen victim to investment scams may still regret engaging in online services again due to the trauma of being scammed. However, I implore you to take action. Seek assistance from Wizard Web Recovery today and witness their remarkable capabilities. I am grateful that I resisted their enticements, and despite the time it took me to discover Wizard web recovery, they ultimately fulfilled my primary objective. Without wizard web recovery intervention, I would have remained despondent and perplexed indefinitely.
    • I've tested the same code on three different envionrments (Desktop win10, desktop Linux and Laptop Linux) and it kinda blows up all the same. Gonna try this code and see if i can tune it
  • Topics

×
×
  • Create New...

Important Information

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