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/
really cool tutorial and procedure, this way it is compatible with any level of fortune
That was the goal, yes :)
Update: changed the procedure a bit to make it more compatible with really high levels of Fortune, since cheated levels (like Fortune 10 or even higher) could cause the old version to make the percentages too small and result in no bonus drops.
This is now fixed and any cheated level of Fortune will continue to work correctly. :)
for which version Mcreator ?
A few things to add and/or point out in case you were like me and didn't read all the text at first.
In order to make things drop properly, make sure that on the Properties for the block it is set to drop nothing, by setting the drop amount to 0 (as stated in the text). Alternatively you can make it drop Air.
If you are like me and making a Metal and not a Gem, you'll want to remove the Experience drops, since in Vanilla you only get experience for Smelting the metals and not when you mine them (The Experience drop is instead listed in the Smelting and Blasting Recipes). You can remove the experience drops by either setting the dropped XP to 0 or removing the procedure entirely.
I've come across a small issue. After adding this code, the Ore I applied it to was able to be mined by any Pickaxe instead of just Iron and higher like I set it for. In other words, the code causes the game to ignore the block's harvest level.
I'm new to this, so I'm not sure how to fix it, but if I do manage to fix it by myself I'll delete this reply and then put it here.
While I haven't corrected the every pickaxe issue, I did find another issue that was really easy to fix.
In the sections of the code where it says "spawn item/gem" and "spawn xp orb", due to it simply stating x y and z, it actually spawns the items and xp orbs at the bottom corner of the block.
This is fixed by simply changing said x y and z to x=0.5, y+0.5, and z+0.5, which will make the items and orbs drop from the center of the block.
There might be a better fix for it, but I managed to make it work.
I replaced "Can Item in main-hand of Event/target entity harvest provided blockstate" with "Item in main-hand of Event/target entity = Netherite Pickaxe".
I then copy and pasted the entire code and placed it after itself, only changing which Pickaxe the code checks for. So with the way I set it up, the procedure will check if the main-hand item is a Netherite Pickaxe, Diamond Pickaxe, or Iron Pickaxe, and if it isn't one of those, the block won't drop anything.
If you do this don't forget to add any applicable Pickaxes your mod adds as well. This fix, if I had to guess, unfortunately prevents other modded items from harvesting the ore.
I think what you've achieved is really cool - I was wondering how this would differ from just setting the blocks lootable?
I did the same procedure for Mcreator 1.9.1