Topic category: User side tutorials
I spent some time today rewriting and optimizing my BlockDestroyedByPlayer procedure and decided to show it in this short tutorial post :)
Note that this procedure goes into the triggers of a specific block, NOT a tool, and the spawned drops have to be set manually - in this example, Diamond Ore was chosen. Also, you will have to set the drop amount in the Block Properties to 0 to disable the drops generated by MCreator.
In order for you to understand and possibly learn from it, you will find a detailed explanation of the procedure below the screenshot, as well as a link to download this procedure from.
The block starts with checking whether the breaking player is in survival mode and is holding the proper tool (so that the items don't drop while in creative or breaking a stone with an axe for example).
This is followed up by an if block that checks whether the tool is enchanted with Silk Touch, in which case the ore will drop as a whole block and the procedure ends.
If that is not the case, the procedure will check whether the tool is instead enchanted with Fortune, and if so, will set a local variable:
It sets the number variable "level" to the Fortune enchantment level of the tool, as we will need it for calculations and because the maximum amount of bonus drops always equals the level of fortune.
Then, an if block checks whether we any bonus drops at all. This works by rolling a random number and checking whether this number falls into the percentage for one drop at the given Fortune level. The percentage is calculated the same way it is calculated in vanilla, specifically 1/(Fortune Level+2) for each possible amount of bonus drops. So for Fortune III, this would be 1/5=20% (at level three, that means 20% each for 2, 3, or 4 total drops, and the remaining 40% mean no bonus drops, so only one drop total).
Mind, in order for the procedure to calculate decimals, we have to change the 2 to a decimal number, so I wrote 2.1, which doesn't impact the math at all.
Next, I used a repeat block to get "level" down to the correct amount of bonus items to drop.
If the random block (it always rolls a random decimal number between 0 and 1) doesn't generate a fitting percentage, the local variable is reduced by 1 and the block repeats. It does this until either the random number falls in the percentage (in which case the loop is broken), or all possible bonus percentages are rolled for and missed, meaning the bonus amount will be 1.
Finally, the procedure spawns the XP orbs and then a repeat block spawns the bonus amount of drops plus the one regular drop. This last part is not included in the prior if block, and therefore is executed even if the tool is not enchanted with fortune, to assure that even an unenchanted tool can break the block and receive the default item drop.
If you have any questions or suggestions, please let me know in the replies :)
You can download this procedure in my Google Drive folder here: http://mcreator.ciri.link/