Jump to content

Modifying onBlockActivated of an Existing Block to open a GUI


Recommended Posts

Posted

I want to modify the vanilla note block so that when a player right clicks it, a GUI opens (rather than tuning it.) However, I have not been successful.

 

Main class with the initiation methods:

 

  Reveal hidden contents

 

 

Client and Common proxies are empty.

 

GUI class:

 

  Reveal hidden contents

 

 

GUI Handler:

 

  Reveal hidden contents

 

 

Class that overrides onBlockActivated for the note block.

 

  Reveal hidden contents

 

 

I followed these two tutorials:

http://www.minecraftforge.net/wiki/Basic_GUI

http://jabelarminecraft.blogspot.com/p/minecraft-modding-block-with-simple-gui.html (the GUI class was copied from this tutorial.)

http://jabelarminecraft.blogspot.com/p/minecraft-modding-containers.html (didn't copy any code, just read for information.)

 

I first tried following the second tutorial fully, but the interface didn't open. I thought that since the note block is a block that extends BlockContainer, perhaps I need to make a GuiHandler and use openGui() instead to open the interface. This didn't work either. I think it may be because I haven't properly made an instance of the block in  my main class. I attempted to with

    public static final BlockNote BlockNote = new BlockNote();
    public static final NoteBlock NoteBlock = new NoteBlock();

, first for my own class and then also with the vanilla class.

 

Also, if I used EntityPlayer.openGui rather than playerIn.openGui (I'm not even sure what the difference is), I would get an error saying that I can't make a static reference to a non-static method. Lastly, I had no idea what to put for the integer arguments, so I just put in 10.

 

Where did I go wrong? How can I fix this? I'm new to Java and this is my first mod, so excuse any blatant faults with the code.

Posted
  On 12/26/2015 at 12:35 AM, diesieben07 said:

Dude, just use PlayerInteractEvent.

 

So, something like this?

    public void openGUI(PlayerInteractEvent event) {
    	if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK){
    		EntityPlayer.openGui();
    	}
    }

 

I'm still not sure which arguments to put into openGui. Also, how do I specify which block the player has to interact with to trigger openGui?

Posted
  On 12/26/2015 at 12:56 AM, diesieben07 said:

You get the Block from the world and coordinates given in the event and check if it's the one you want.

 

I've figured out how to get the block, but now I need to figure out how to check if it's the right block. I don't know if getBlock returns an ID or a name and how it will be formatted, so I've made some prints to help me figure this out and just see if what I have so far works. However, when I right click any block, nothing is printed to the console.

    public void openGUI(PlayerInteractEvent event) {
    	if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK){
    		World world = event.world;
    		Block block = world.getBlockState(event.pos).getBlock();
    		//EntityPlayer.openGui(NBGUI.instance, GUI_ID, world, event.pos.getX(), event.pos.getY(), event.pos.getZ());
    		//DEBUG
    		System.out.println("Position:" +event.pos);
    		System.out.println("Block:" +block);
    	}
    }

Posted

Ah, no I did not put @SubscribeEvent. I also never made an event handler. I guess I should look that up now.

 

Also, still not sure about openGui. Are the integers supposed to be the block position? I put in the event position (which I'm assuming is the position of the block rather than of the player when the event happens), but I get an error saying "Cannot make a static reference to the non-static method openGui(Object, int, World, int, int, int) from the type EntityPlayer."

 

Edit: Do I make this event in the client or common event handler?

Posted
  On 12/26/2015 at 2:42 AM, diesieben07 said:

You can pass in whatever numbers you want. Your IGuiHandler is responsible for interpreting them correctly. As for the error message, it is pretty obvious if you know basic Java, which you should.

 

Okay, I've figured it out. However, for some reason, my my debug prints occur twice in the console:

 

[22:26:35] [Client thread/INFO] [sTDOUT]: [com.mark.nbgui.EventHandlerCommon:openGUI:23]: POS: BlockPos{x=-256, y=64, z=119}
[22:26:35] [Client thread/INFO] [sTDOUT]: [com.mark.nbgui.EventHandlerCommon:openGUI:24]: BLOCK: Block{minecraft:noteblock}
Block{minecraft:noteblock}
[22:26:35] [server thread/INFO]: Saving and pausing game...
[22:26:35] [server thread/INFO]: Saving chunks for level 'New World'/Overworld
[22:26:35] [server thread/INFO]: Saving chunks for level 'New World'/Nether
[22:26:35] [server thread/INFO]: Saving chunks for level 'New World'/The End
[22:26:35] [server thread/INFO] [sTDOUT]: [com.mark.nbgui.EventHandlerCommon:openGUI:23]: POS: BlockPos{x=-256, y=64, z=119}
[22:26:35] [server thread/INFO] [sTDOUT]: [com.mark.nbgui.EventHandlerCommon:openGUI:24]: BLOCK: Block{minecraft:noteblock}
Block{minecraft:noteblock}

 

Here is my code:

@SubscribeEvent
    public void openGUI(PlayerInteractEvent event) {
    	if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK){
    		World world = event.world;
    		Block block = world.getBlockState(event.pos).getBlock();
    		String blocktype = block.toString();
    		EntityPlayer player = event.entityPlayer;
    		if (blocktype.equals("Block{minecraft:noteblock}")) {
    			player.openGui(NBGUI.instance, GUI.GUI_ID, world, event.pos.getX(), event.pos.getY(), event.pos.getZ());
    		}
    		//DEBUG
    		System.out.println("POS: " +event.pos);
    		System.out.println("BLOCK: " +block);
    		System.out.print(blocktype);  		
    	}
    }
}

 

Is this normal/something I can ignore, or is it a sign that something is wrong (presumably with my event handler)?

Posted
  On 12/26/2015 at 6:30 AM, Markk said:

Okay, I've figured it out. However, for some reason, my my debug prints occur twice in the console:

If you look carefully, you'll notice that it's being printed once from the client thread and once from the server thread.

 

  Quote

...
                Block block = world.getBlockState(event.pos).getBlock();
    		String blocktype = block.toString();
    		EntityPlayer player = event.entityPlayer;
    		if (blocktype.equals("Block{minecraft:noteblock}")) {
...

There is almost never a good reason to use the output of

toString

for anything but log/exception messages.

 

In this case, you should compare

block

directly to the Note Block instance (

Blocks.noteblock

) using the equality (

==

) operator. If they're the equal (i.e. the same object), the block is a Note Block.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
  On 12/26/2015 at 6:50 AM, Choonster said:

  Quote

Okay, I've figured it out. However, for some reason, my my debug prints occur twice in the console:

If you look carefully, you'll notice that it's being printed once from the client thread and once from the server thread.

 

  Quote

...
                Block block = world.getBlockState(event.pos).getBlock();
    		String blocktype = block.toString();
    		EntityPlayer player = event.entityPlayer;
    		if (blocktype.equals("Block{minecraft:noteblock}")) {
...

There is almost never a good reason to use the output of

toString

for anything but log/exception messages.

 

In this case, you should compare

block

directly to the Note Block instance (

Blocks.noteblock

) using the equality (

==

) operator. If they're the equal (i.e. the same object), the block is a Note Block.

 

Ah okay, I can't believe I didn't notice it was server and client outputs together. I thought the doubling had something to do with the note block tuning up twice in a laggy manner when I right click it. I don't know why that happens or if it will be in issue later (because I plan to remove the original right click functionality.)

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



×
×
  • Create New...

Important Information

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