Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author

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

  • Author

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);

}

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 :)

  • Author

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?

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.