Jump to content

Gaining access to player instance upon breaking a block


Dandrel

Recommended Posts

Hi

 

Does anybody know how I would go about gaining access the instance of entity player that destroyed a block?

 

My overall aim is to detect what tool was used to destroy the block in the onBlockDestroyedByPlayer

 

Thanks

Link to comment
Share on other sites

Basically, I have code in the onBlockDestroyedByPlayer that I want to run each time the block is destroyed. However, I want the code to be in an if statement for example

if("toolusedtodestroyblock" != DiamondPickaxe)

{

Run Code

}

 

So what i need is to get whatever tool is used to destroy the block. I am assuming I need access to the player class

Link to comment
Share on other sites

This is what the run code is

 

Random rnd = new Random();

int num = rnd.nextInt(4);

if(num ==0){

world.setBlock(x,y+1,z,this,0,3);

}

if(num == 1){

world.setBlock(x, y, z +1, this, 0, 3);

}

if(num ==2){

world.setBlock(x, y, z -1, this, 0, 3);

}

if(num ==3){

world.setBlock(x+1, y, z , this, 0, 3);

}

if(num ==4){

world.setBlock(x-1, y, z , this, 0, 3);

}

 

//Second block

Random rnd2 = new Random();

int num2 = rnd.nextInt(4);

while (num2==num){

num2 = rnd.nextInt(4);

}

if(num2 ==0){

world.setBlock(x,y+1,z,this,0,3);

}

if(num2 == 1){

world.setBlock(x, y, z +1, this, 0, 3);

}

if(num2 ==2){

world.setBlock(x, y, z -1, this, 0, 3);

}

if(num2 ==3){

world.setBlock(x+1, y, z , this, 0, 3);

}

if(num2 ==4){

world.setBlock(x-1, y, z , this, 0, 3);

}

Link to comment
Share on other sites

Hint: You can use [ code ] tags for short snippets of code, or paste as a gist for syntax highlighting and line numbers :)

 

Random rnd = new Random(); // Make this a private field and only initialise it once, instead.
int num = rnd.nextInt(5);

// Put the switch below in a private method, and call that instead.
switch (num) {
    case 0: world.setBlock(x, y+1, z, this, 0, 3); break;
    case 1: world.setBlock(x, y, z+1, this, 0, 3); break;
    case 2: world.setBlock(x, y, z-1, this, 0, 3); break;
    case 3: world.setBlock(x+1, y, z, this, 0, 3); break;
    case 4: world.setBlock(x-1, y, z, this, 0, 3); break;
}

num2 = rnd.nextInt(4);
if (num2 >= num) {
   ++num2;
}

// Call the private method you called above.

Random#nextInt(n)

returns an integer between 0 and n, not including n.

 

In your code, you first check if

num == 0

. Even if it is, you still check if it is

1

,

2

and so on.

 

You also instantiate not one, but two instances of Random(). You can simply instantiate the random once, perhaps in your constructor, and then use that same random continuously.

 

You have a while-loop that is not guaranteed to ever terminate. Of course, it mostly will - but it could run anywhere from 0 to infinity times, if you're unlucky. Simply decrease the number of possible outcomes of the random number, and increment the outcome if it is the same or larger than the actual number you want to exclude as an option. If you're into Big O notation, this changes the complexity of your code from O(∞) to O(1), assuming the

Random#nextInt

call is O(1) still.

 

Either way, the code above should be both more concise, more maintainable and more performant - especially if you follow the hints I have given you in the comments of the code.

 

Enjoy :)

Link to comment
Share on other sites

Thanks Elyon, my code is much more cleaner and easier to understand and works much better!

As for the removeBlockByPlayer, I have never used this method before. Is this method called just like destroyedByPlayer as in it is called once it is destroyed?

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



×
×
  • Create New...

Important Information

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