File size: 2,431 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package net.minecraft.commands.functions;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.commands.ExecutionCommandSource;
import net.minecraft.commands.execution.UnboundEntryAction;
import net.minecraft.resources.ResourceLocation;

class FunctionBuilder<T extends ExecutionCommandSource<T>> {
    @Nullable
    private List<UnboundEntryAction<T>> plainEntries = new ArrayList<>();
    @Nullable
    private List<MacroFunction.Entry<T>> macroEntries;
    private final List<String> macroArguments = new ArrayList<>();

    public void addCommand(UnboundEntryAction<T> p_309592_) {
        if (this.macroEntries != null) {
            this.macroEntries.add(new MacroFunction.PlainTextEntry<>(p_309592_));
        } else {
            this.plainEntries.add(p_309592_);
        }
    }

    private int getArgumentIndex(String p_312711_) {
        int i = this.macroArguments.indexOf(p_312711_);
        if (i == -1) {
            i = this.macroArguments.size();
            this.macroArguments.add(p_312711_);
        }

        return i;
    }

    private IntList convertToIndices(List<String> p_311467_) {
        IntArrayList intarraylist = new IntArrayList(p_311467_.size());

        for (String s : p_311467_) {
            intarraylist.add(this.getArgumentIndex(s));
        }

        return intarraylist;
    }

    public void addMacro(String p_312905_, int p_310777_, T p_328106_) {
        StringTemplate stringtemplate = StringTemplate.fromString(p_312905_, p_310777_);
        if (this.plainEntries != null) {
            this.macroEntries = new ArrayList<>(this.plainEntries.size() + 1);

            for (UnboundEntryAction<T> unboundentryaction : this.plainEntries) {
                this.macroEntries.add(new MacroFunction.PlainTextEntry<>(unboundentryaction));
            }

            this.plainEntries = null;
        }

        this.macroEntries.add(new MacroFunction.MacroEntry<>(stringtemplate, this.convertToIndices(stringtemplate.variables()), p_328106_));
    }

    public CommandFunction<T> build(ResourceLocation p_311383_) {
        return (CommandFunction<T>)(this.macroEntries != null
            ? new MacroFunction<>(p_311383_, this.macroEntries, this.macroArguments)
            : new PlainTextFunction<>(p_311383_, this.plainEntries));
    }
}