Spaces:
Build error
Build error
File size: 7,150 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
package net.minecraft.commands.functions;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntLists;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
import net.minecraft.Util;
import net.minecraft.commands.ExecutionCommandSource;
import net.minecraft.commands.FunctionInstantiationException;
import net.minecraft.commands.execution.UnboundEntryAction;
import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.DoubleTag;
import net.minecraft.nbt.FloatTag;
import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.ShortTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
public class MacroFunction<T extends ExecutionCommandSource<T>> implements CommandFunction<T> {
private static final DecimalFormat DECIMAL_FORMAT = Util.make(new DecimalFormat("#"), p_312286_ -> {
p_312286_.setMaximumFractionDigits(15);
p_312286_.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.US));
});
private static final int MAX_CACHE_ENTRIES = 8;
private final List<String> parameters;
private final Object2ObjectLinkedOpenHashMap<List<String>, InstantiatedFunction<T>> cache = new Object2ObjectLinkedOpenHashMap<>(8, 0.25F);
private final ResourceLocation id;
private final List<MacroFunction.Entry<T>> entries;
public MacroFunction(ResourceLocation p_311437_, List<MacroFunction.Entry<T>> p_310862_, List<String> p_310686_) {
this.id = p_311437_;
this.entries = p_310862_;
this.parameters = p_310686_;
}
@Override
public ResourceLocation id() {
return this.id;
}
@Override
public InstantiatedFunction<T> instantiate(@Nullable CompoundTag p_309697_, CommandDispatcher<T> p_309980_) throws FunctionInstantiationException {
if (p_309697_ == null) {
throw new FunctionInstantiationException(Component.translatable("commands.function.error.missing_arguments", Component.translationArg(this.id())));
} else {
List<String> list = new ArrayList<>(this.parameters.size());
for (String s : this.parameters) {
Tag tag = p_309697_.get(s);
if (tag == null) {
throw new FunctionInstantiationException(
Component.translatable("commands.function.error.missing_argument", Component.translationArg(this.id()), s)
);
}
list.add(stringify(tag));
}
InstantiatedFunction<T> instantiatedfunction = this.cache.getAndMoveToLast(list);
if (instantiatedfunction != null) {
return instantiatedfunction;
} else {
if (this.cache.size() >= 8) {
this.cache.removeFirst();
}
InstantiatedFunction<T> instantiatedfunction1 = this.substituteAndParse(this.parameters, list, p_309980_);
this.cache.put(list, instantiatedfunction1);
return instantiatedfunction1;
}
}
}
private static String stringify(Tag p_313061_) {
if (p_313061_ instanceof FloatTag floattag) {
return DECIMAL_FORMAT.format((double)floattag.getAsFloat());
} else if (p_313061_ instanceof DoubleTag doubletag) {
return DECIMAL_FORMAT.format(doubletag.getAsDouble());
} else if (p_313061_ instanceof ByteTag bytetag) {
return String.valueOf(bytetag.getAsByte());
} else if (p_313061_ instanceof ShortTag shorttag) {
return String.valueOf(shorttag.getAsShort());
} else {
return p_313061_ instanceof LongTag longtag ? String.valueOf(longtag.getAsLong()) : p_313061_.getAsString();
}
}
private static void lookupValues(List<String> p_313206_, IntList p_310595_, List<String> p_310258_) {
p_310258_.clear();
p_310595_.forEach(p_312583_ -> p_310258_.add(p_313206_.get(p_312583_)));
}
private InstantiatedFunction<T> substituteAndParse(List<String> p_312865_, List<String> p_312778_, CommandDispatcher<T> p_311234_) throws FunctionInstantiationException {
List<UnboundEntryAction<T>> list = new ArrayList<>(this.entries.size());
List<String> list1 = new ArrayList<>(p_312778_.size());
for (MacroFunction.Entry<T> entry : this.entries) {
lookupValues(p_312778_, entry.parameters(), list1);
list.add(entry.instantiate(list1, p_311234_, this.id));
}
return new PlainTextFunction<>(this.id().withPath(p_312634_ -> p_312634_ + "/" + p_312865_.hashCode()), list);
}
interface Entry<T> {
IntList parameters();
UnboundEntryAction<T> instantiate(List<String> p_312452_, CommandDispatcher<T> p_313016_, ResourceLocation p_311242_) throws FunctionInstantiationException;
}
static class MacroEntry<T extends ExecutionCommandSource<T>> implements MacroFunction.Entry<T> {
private final StringTemplate template;
private final IntList parameters;
private final T compilationContext;
public MacroEntry(StringTemplate p_309563_, IntList p_312180_, T p_336169_) {
this.template = p_309563_;
this.parameters = p_312180_;
this.compilationContext = p_336169_;
}
@Override
public IntList parameters() {
return this.parameters;
}
@Override
public UnboundEntryAction<T> instantiate(List<String> p_312101_, CommandDispatcher<T> p_309379_, ResourceLocation p_312655_) throws FunctionInstantiationException {
String s = this.template.substitute(p_312101_);
try {
return CommandFunction.parseCommand(p_309379_, this.compilationContext, new StringReader(s));
} catch (CommandSyntaxException commandsyntaxexception) {
throw new FunctionInstantiationException(
Component.translatable("commands.function.error.parse", Component.translationArg(p_312655_), s, commandsyntaxexception.getMessage())
);
}
}
}
static class PlainTextEntry<T> implements MacroFunction.Entry<T> {
private final UnboundEntryAction<T> compiledAction;
public PlainTextEntry(UnboundEntryAction<T> p_309648_) {
this.compiledAction = p_309648_;
}
@Override
public IntList parameters() {
return IntLists.emptyList();
}
@Override
public UnboundEntryAction<T> instantiate(List<String> p_311533_, CommandDispatcher<T> p_311835_, ResourceLocation p_311102_) {
return this.compiledAction;
}
}
} |