mojira.dev
MC-126256

Loot Tables: "multiply_total" has broken %.

I noticed the attributes which modify the health aren't the same.
For example, in a "give" command, we use an attribute to modify total health to -90%.
For this, I use "Operation:2".
Command:
/give @p minecraft:diamond_sword 1 0 {AttributeModifiers:[

{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-0.9,Operation:2,UUIDLeast:107814,UUIDMost:884287,Slot:"mainhand"}

]}

Default world. No other commands. When I hold this sword, my hearts are reduced from 10 to 1, which makes sense. (10-90%=1)

Now to the problem part:
With loot tables we create the same diamond sword with -90% health attribute.
For this we create a new loot table. (DOWNLOAD the .zip and place into your world in /data)
However instead of "Operation:2", it uses the "multiply_total", and we add the amount -0.9.

Ingame, when we hold the sword the health is supposed to be 1 heart. (10-90%=1)
HOWEVER, it shows 2 hearts.

PROBLEM: It shows 2 hearts, bcs the code likes for some reason to change your amout you enter to a really close one. Example here: I entered "-0.9" and when the loot was generated, the item had an amout of "-0.899999..." (Look at the SCREENSHOT here.)

This was a test, basicly the generated items wont take the numbers entered 1:1, eventhough it is clearly defined in the loot table. The true, generated number is always a really close one to the entered number, which can mess things up.

Pool:

{
    "pools": [
        {
            "rolls": 1,
            "entries": [
                {
                    "type": "item",
                    "weight": 1,
                    "name": "minecraft:diamond_sword",
                    "functions": [
                        {
                            "function": "set_attributes",
                            "modifiers": [
                                {
                                    "attribute": "generic.maxHealth",
                                    "operation": "multiply_total",
                                    "amount": -0.9,
                                    "name": "Health",
                                    "slot": "mainhand"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
{
    "pools": [
        {
            "rolls": 1,
            "entries": [
                {
                    "type": "item",
                    "weight": 1,
                    "name": "minecraft:diamond_sword",
                    "functions": [
                        {
                            "function": "set_attributes",
                            "modifiers": [
                                {
                                    "attribute": "generic.maxHealth",
                                    "operation": "multiply_total",
                                    "amount": -0.9,
                                    "name": "Health",
                                    "slot": "mainhand"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

I hope I explained it somehow clear enough to understand.

Attachments

Comments 5

This is a general problem with floating-point numbers and isn't a Minecraft-specific bug. See: https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems

@Skylinerw: Thank you for your reply, I understand now why this is caused.

However I dont understand one thing: This happened to me in 1.12.2, however on previous versions like 1.11/10 it was working fine.
I will check later again when I have time and provide screenshots.

Alright. I figured the "weird numbers" only happen when I use "multiply_total": -0.9 would generate me -0.899999...
(In loot tables for example.)

However "Operation:2", like in NBT will give me -0.9 when I enter -0.9.
(In a command for example.)

Both must use different code or calculation. Why do we have 2 different types anyway? This is ridiculous..

Aha, I see the issue: the loot table function makes use of single-precision floating-point, while the NBT uses double-precision. While it won't solve issues from the use of floating-point numbers (such as 0.9 becoming 0.8999), I can reopen and restructure this report to tackle the issue of mixed single & double precision. Or a separate report can be created if you'd prefer.

Hey Skylinerw, thanks again for your reply!
This was a bit hard for me to explain the issue, but I am glad you understand. Up to you if you want to re-open it, or make a new post.
(This post might already provide some information and examples.)
However I see you understand more in this topic, so a new report might as well be a good idea.

I also would like to suggest that both shouldn't be written in a different way, but instead be the same in the command/loot table file.
("Operation:2" is in a command and "Operation": "multiply_total" is in the loot tables.)

While at it, maybe change so the loot table matches the same writing and makes use of same code as the command one.

Eric

(Unassigned)

Unconfirmed

command, command_block, item

Minecraft 1.12.2

Retrieved