Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Problem sending message to player
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 1
TehNoiseBomb

Problem sending message to player

By TehNoiseBomb, December 23, 2014 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

TehNoiseBomb    1

TehNoiseBomb

TehNoiseBomb    1

  • Tree Puncher
  • TehNoiseBomb
  • Members
  • 1
  • 7 posts
Posted December 23, 2014

I've run into a problem while developing my mod where I want to send a chat message to the player when a block receives a redstone pulse. When testing the mod, I get an error:

java.lang.NullPointerException: Exception while updating neighbours

. My code:

 

 

@Override
public void onNeighborBlockChange(World w, int x, int y, int z, Block neighbor){
     if (!w.isRemote){
          if(w.isBlockIndirectlyGettingPowered(x, y, z)){
               w.getClosestPlayer((double)x, (double)y, (double)z, 0)
                    .addChatMessage(
                         new ChatComponentText("test message"));}
     }
}

 

 

The error occurs on the .addChatMessage() line.

  • Quote

Share this post


Link to post
Share on other sites

shieldbug1    70

shieldbug1

shieldbug1    70

  • Diamond Finder
  • shieldbug1
  • Forge Modder
  • 70
  • 404 posts
Posted December 23, 2014

You're trying to get the nearest player to the block in a radius of 0. You need to make sure that player is not null before trying to do anything because World#getClosestPlayer(double,double,double,int) can return null.

  • Quote

Share this post


Link to post
Share on other sites

TehNoiseBomb    1

TehNoiseBomb

TehNoiseBomb    1

  • Tree Puncher
  • TehNoiseBomb
  • Members
  • 1
  • 7 posts
Posted December 23, 2014

OK, so I added in the check to see if the player is null or not and changed the getClosestPlayer to 5 and it works now.

 

 

@Override
public void onNeighborBlockChange(World w, int x, int y, int z, Block neighbor){
     if (!w.isRemote){
          [bold]EntityPlayer player = w.getClosestPlayer((double)x, (double)y, (double)z, 5);
          if(player!=null){[/bold]
               if(w.isBlockIndirectlyGettingPowered(x, y, z)){
                    player.addChatMessage(
                         new ChatComponentText("test message"));
            }
               }
          }
}

 

 

However for some reason the chat message is firing twice. Any ideas?

  • Quote

Share this post


Link to post
Share on other sites

coolboy4531    66

coolboy4531

coolboy4531    66

  • Dragon Slayer
  • coolboy4531
  • Members
  • 66
  • 584 posts
Posted December 23, 2014

I'm pretty sure it is being fired twice on the server.

  • Quote

Share this post


Link to post
Share on other sites

TehNoiseBomb    1

TehNoiseBomb

TehNoiseBomb    1

  • Tree Puncher
  • TehNoiseBomb
  • Members
  • 1
  • 7 posts
Posted December 23, 2014

Yea that's what I'm thinking. I tried adding in @SideOnly(Side.CLIENT) before the function but that didn't change anything.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2090

Draco18s

Draco18s    2090

  • Reality Controller
  • Draco18s
  • Members
  • 2090
  • 14008 posts
Posted December 23, 2014

Yea that's what I'm thinking. I tried adding in @SideOnly(Side.CLIENT) before the function but that didn't change anything.

 

That's not what @SideOnly is for.

  • Quote

Share this post


Link to post
Share on other sites

TehNoiseBomb    1

TehNoiseBomb

TehNoiseBomb    1

  • Tree Puncher
  • TehNoiseBomb
  • Members
  • 1
  • 7 posts
Posted December 23, 2014

I figured that out but the problem persists. Is there any way to have the message be sent to the player without it being duplicated?

  • Quote

Share this post


Link to post
Share on other sites

shieldbug1    70

shieldbug1

shieldbug1    70

  • Diamond Finder
  • shieldbug1
  • Forge Modder
  • 70
  • 404 posts
Posted December 24, 2014

Wrap the code in

if(!world.isRemote)

  • Quote

Share this post


Link to post
Share on other sites

TehNoiseBomb    1

TehNoiseBomb

TehNoiseBomb    1

  • Tree Puncher
  • TehNoiseBomb
  • Members
  • 1
  • 7 posts
Posted December 24, 2014

Wrap the code in

if(!world.isRemote)

 

I have that. As a refresher:

 

 

@Override
public void onNeighborBlockChange(World w, int x, int y, int z, Block neighbor){
     if (!w.isRemote){
          EntityPlayer player = w.getClosestPlayer((double)x, (double)y, (double)z, 5);
          if(player!=null){
               if(w.isBlockIndirectlyGettingPowered(x, y, z)){
                    player.addChatComponentMessage(new ChatComponentText("test message"));
               }
          }
     }
}

 

  • Quote

Share this post


Link to post
Share on other sites

TehNoiseBomb    1

TehNoiseBomb

TehNoiseBomb    1

  • Tree Puncher
  • TehNoiseBomb
  • Members
  • 1
  • 7 posts
Posted December 24, 2014

I have decided to go a different path with what I was trying to achieve with this. Instead of giving the player the information through a chat message, I am having the player place a sign above my mod block that automatically loads the sign with the information I want to display.

 

If you are interested in how I achieved this my code is below (sorry, I'm not formatting it this time tongue.gif):

 

@Override
    public void onNeighborChange(IBlockAccess w, int x, int y, int z, int tileX, int tileY, int tileZ){
	if(w.getBlock(x, tileY, z)==Blocks.standing_sign||w.getBlock(x, tileY, z)==Blocks.wall_sign){
		this.sign = (TileEntitySign)w.getTileEntity(x, y+1, z);
		signText();
	}
    }
//This is in it's own method because I call it from elsewhere to have the sign be updated when the information changes.
private void signText(){ 
	this.sign.signText[0]="line0";
	this.sign.signText[1]="line1";
	this.sign.signText[2]="line3";
}

@Override
public boolean canPlaceBlockAt(World w, int x, int y, int z)
    {
	if(w.getBlock(x, y+1, z).isAir(w, x, y+1, z)){
		return w.getBlock(x, y, z).isReplaceable(w, z, y, z);
	}
	else return false;
    }
[/spoiler]

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Rohman
      [1.12.2 Build 2847] Server Crash on startup

      By Rohman · Posted 29 minutes ago

      Here you go. Thanks for having a look. debug(1).log
    • diesieben07
      Server Cannot Start

      By diesieben07 · Posted 50 minutes ago

      You installed a client-only mod (Controllable) on the server. Mods have the ability to indicate their client-only-ness to Forge and it will not load the mod on a server. This mod has not done so, please report this bug to the mod author.
    • diesieben07
      [1.12.2] How do i make it so my sword renders in my mobs hand?.

      By diesieben07 · Posted 56 minutes ago

      Why would you replace them?! This is a simple method override and you are calling super. There is no need to replace anything. Again: Please learn Java basics before making a mod.
    • diesieben07
      [1.12.2 Build 2847] Server Crash on startup

      By diesieben07 · Posted 59 minutes ago

      Post the debug.log file.
    • diesieben07
      Need your help pls

      By diesieben07 · Posted 1 hour ago

      1.8.9 is no longer supported on this forum due to it's age. Update to a modern version of Minecraft to receive support.
  • Topics

    • Rohman
      2
      [1.12.2 Build 2847] Server Crash on startup

      By Rohman
      Started 1 hour ago

    • The_Unkown675
      1
      Server Cannot Start

      By The_Unkown675
      Started 4 hours ago

    • J0WAY
      17
      [1.12.2] How do i make it so my sword renders in my mobs hand?.

      By J0WAY
      Started Thursday at 09:10 PM

    • _jiriik_
      1
      Need your help pls

      By _jiriik_
      Started 2 hours ago

    • Kuaka
      0
      My Forge 1.12 and 1.12.2 Keep Crashing when i start it up

      By Kuaka
      Started 1 hour ago

  • Who's Online (See full list)

    • Ugdhar
    • Gnaux
    • plugsmustard
    • vaartis
    • HussNuss
    • AkosM
    • Ollie_Bear
    • Kuaka
    • AdieCraft
    • cookiedragon234
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Problem sending message to player
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community