aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/firmament/mixins/CaseInsensitiveCommandMapPatch.java
blob: 2696a6f74843ffe7cea6ef11c19e83fc2262f5f9 (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
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.mixins;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.brigadier.tree.CommandNode;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.Locale;
import java.util.Map;

@Mixin(value = CommandNode.class, remap = false)
public class CaseInsensitiveCommandMapPatch<S> {
    @WrapOperation(method = "getRelevantNodes", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"), remap = false)
    public Object modify(Map map, Object text, Operation<Object> op) {
        var original = op.call(map, text);
        if (original == null) {
            return map.get(((String) text).toLowerCase(Locale.ROOT));
        }
        return original;
    }

}