| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import net.minecraft.commands.CommandResultCallback;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.Commands;
|
| 12 | import net.minecraft.commands.FunctionInstantiationException;
|
| 13 | import net.minecraft.commands.execution.ExecutionContext;
|
| 14 | import net.minecraft.commands.functions.CommandFunction;
|
| 15 | import net.minecraft.commands.functions.InstantiatedFunction;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 18 | import net.minecraft.util.profiling.Profiler;
|
| 19 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | public class ServerFunctionManager {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | private static final Identifier TICK_FUNCTION_TAG = Identifier.withDefaultNamespace("tick");
|
| 25 | private static final Identifier LOAD_FUNCTION_TAG = Identifier.withDefaultNamespace("load");
|
| 26 | private final MinecraftServer server;
|
| 27 | private List<CommandFunction<CommandSourceStack>> ticking = ImmutableList.of();
|
| 28 | private boolean postReload;
|
| 29 | private ServerFunctionLibrary library;
|
| 30 |
|
| 31 | public ServerFunctionManager(final MinecraftServer server, final ServerFunctionLibrary library) {
|
| 32 | this.server = server;
|
| 33 | this.library = library;
|
| 34 | this.postReload(library);
|
| 35 | }
|
| 36 |
|
| 37 | public CommandDispatcher<CommandSourceStack> getDispatcher() {
|
| 38 | return this.server.getCommands().getDispatcher();
|
| 39 | }
|
| 40 |
|
| 41 | public void tick() {
|
| 42 | if (this.server.tickRateManager().runsNormally()) {
|
| 43 | if (this.postReload) {
|
| 44 | this.postReload = false;
|
| 45 | Collection<CommandFunction<CommandSourceStack>> functions = this.library.getTag(LOAD_FUNCTION_TAG);
|
| 46 | this.executeTagFunctions(functions, LOAD_FUNCTION_TAG);
|
| 47 | }
|
| 48 |
|
| 49 | this.executeTagFunctions(this.ticking, TICK_FUNCTION_TAG);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | private void executeTagFunctions(final Collection<CommandFunction<CommandSourceStack>> functions, final Identifier loadFunctionTag) {
|
| 54 | Profiler.get().push(loadFunctionTag::toString);
|
| 55 |
|
| 56 | for (CommandFunction<CommandSourceStack> function : functions) {
|
| 57 | this.execute(function, this.getGameLoopSender());
|
| 58 | }
|
| 59 |
|
| 60 | Profiler.get().pop();
|
| 61 | }
|
| 62 |
|
| 63 | public void execute(final CommandFunction<CommandSourceStack> functionIn, final CommandSourceStack sender) {
|
| 64 | ProfilerFiller profiler = Profiler.get();
|
| 65 | profiler.push(() -> "function " + functionIn.id());
|
| 66 |
|
| 67 | try {
|
| 68 | InstantiatedFunction<CommandSourceStack> function = functionIn.instantiate(null, this.getDispatcher());
|
| 69 | Commands.executeCommandInContext(sender, context -> ExecutionContext.queueInitialFunctionCall(context, function, sender, CommandResultCallback.EMPTY));
|
| 70 | } catch (FunctionInstantiationException var9) {
|
| 71 | } catch (Exception e) {
|
| 72 | LOGGER.warn("Failed to execute function {}", functionIn.id(), e);
|
| 73 | } finally {
|
| 74 | profiler.pop();
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | public void replaceLibrary(final ServerFunctionLibrary library) {
|
| 79 | this.library = library;
|
| 80 | this.postReload(library);
|
| 81 | }
|
| 82 |
|
| 83 | private void postReload(final ServerFunctionLibrary library) {
|
| 84 | this.ticking = List.copyOf(library.getTag(TICK_FUNCTION_TAG));
|
| 85 | this.postReload = true;
|
| 86 | }
|
| 87 |
|
| 88 | public CommandSourceStack getGameLoopSender() {
|
| 89 | return this.server.createCommandSourceStack().withPermission(LevelBasedPermissionSet.GAMEMASTER).withSuppressedOutput();
|
| 90 | }
|
| 91 |
|
| 92 | public Optional<CommandFunction<CommandSourceStack>> get(final Identifier id) {
|
| 93 | return this.library.getFunction(id);
|
| 94 | }
|
| 95 |
|
| 96 | public List<CommandFunction<CommandSourceStack>> getTag(final Identifier id) {
|
| 97 | return this.library.getTag(id);
|
| 98 | }
|
| 99 |
|
| 100 | public Iterable<Identifier> getFunctionNames() {
|
| 101 | return this.library.getFunctions().keySet();
|
| 102 | }
|
| 103 |
|
| 104 | public Iterable<Identifier> getTagNames() {
|
| 105 | return this.library.getAvailableTags();
|
| 106 | }
|
| 107 | }
|
| 108 |
|