summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttributeMap.java
blob: 0412470b3d005a81d9bc14f86e04f4a978e56fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package org.bukkit.craftbukkit.attribute;

import com.google.common.base.Preconditions;
import net.minecraft.server.AttributeMapBase;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;

public class CraftAttributeMap implements Attributable {

    private final AttributeMapBase handle;

    public CraftAttributeMap(AttributeMapBase handle) {
        this.handle = handle;
    }

    @Override
    public AttributeInstance getAttribute(Attribute attribute) {
        Preconditions.checkArgument(attribute != null, "attribute");
        net.minecraft.server.AttributeInstance nms = handle.a(toMinecraft(attribute.name()));

        return (nms == null) ? null : new CraftAttributeInstance(nms, attribute);
    }

    static String toMinecraft(String bukkit) {
        int first = bukkit.indexOf('_');
        int second = bukkit.indexOf('_', first + 1);

        StringBuilder sb = new StringBuilder(bukkit.toLowerCase());

        sb.setCharAt(first, '.');
        sb.deleteCharAt(second);
        sb.setCharAt(second, bukkit.charAt(second + 1));

        return sb.toString();
    }
}