Skip to content

Commit

Permalink
Send players into config phase during addon reload
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Apr 9, 2024
1 parent 0a6199a commit 823542f
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 33 deletions.
23 changes: 23 additions & 0 deletions src/main/java/net/kyrptonaught/serverutils/CMDHelper.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package net.kyrptonaught.serverutils;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.function.CommandFunction;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Supplier;

public class CMDHelper {
Expand All @@ -20,6 +24,14 @@ public static void executeAs(PlayerEntity player, Collection<CommandFunction<Ser
}
}

public static void executeFunctionsAs(PlayerEntity player, String id) {
Collection<CommandFunction<ServerCommandSource>> functions = getFunctions(player.getServer(), id);
if (functions != null)
for (CommandFunction<ServerCommandSource> commandFunction : functions) {
player.getServer().getCommandFunctionManager().execute(commandFunction, player.getCommandSource().withLevel(2).withSilent());
}
}

public static Supplier<Text> getFeedbackLiteral(String text) {
return getFeedback(Text.literal(text));
}
Expand All @@ -31,4 +43,15 @@ public static Supplier<Text> getFeedbackTranslatable(String text) {
public static Supplier<Text> getFeedback(Text text) {
return () -> text;
}

public static Collection<CommandFunction<ServerCommandSource>> getFunctions(MinecraftServer server, String id) {
if (id.startsWith("#")) {
return server.getCommandFunctionManager().getTag(new Identifier(id.replaceAll("#", "")));
}

Optional<CommandFunction<ServerCommandSource>> function = server.getCommandFunctionManager().getFunction(new Identifier(id));
if (function.isPresent()) return Collections.singleton(function.get());

return Collections.emptySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class ServerUtilsMod implements ModInitializer {
public static Module floodgateCompatModule = registerModule("floodgatecompat", new FloodgateCompatMod());
public static PlayerJoinLocationMod playerJoinLocationMod = (PlayerJoinLocationMod) registerModule("playerjoinlocation", new PlayerJoinLocationMod());
public static Module knockbackModule = registerModule("knockback", new KnockbackMod());
public static Module customMapLoaderModule = registerModule("custommaploader", new CustomMapLoaderMod());
public static CustomMapLoaderMod CustomMapLoaderModule = (CustomMapLoaderMod) registerModule("custommaploader", new CustomMapLoaderMod());
public static Module utilModule = registerModule("util", new UtilCommandsMod());
public static Module smallInvModule = registerModule("smallinv", new SmallInvMod());
public static Module advancementMenu = registerModule("advancementmenu", new AdvancementMenuMod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa

cmd.then(CommandManager.literal("reload")
.executes(context -> {
CustomMapLoaderMod.reloadAddonFiles(context.getSource().getServer());
context.getSource().getServer().execute(() -> {
CustomMapLoaderMod.reloadAddonFiles(context.getSource().getServer());
});
return 1;
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.kyrptonaught.serverutils.customMapLoader;

import net.kyrptonaught.serverutils.AbstractConfigFile;

public class CustomMapLoaderConfig extends AbstractConfigFile {

public String reconfigFunction;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.kyrptonaught.serverutils.Module;
import net.kyrptonaught.serverutils.CMDHelper;
import net.kyrptonaught.serverutils.ModuleWConfig;
import net.kyrptonaught.serverutils.ServerUtilsMod;
import net.kyrptonaught.serverutils.chestTracker.ChestTrackerMod;
import net.kyrptonaught.serverutils.customMapLoader.addons.BaseMapAddon;
import net.kyrptonaught.serverutils.customMapLoader.addons.BattleMapAddon;
Expand All @@ -19,11 +21,12 @@
import net.kyrptonaught.serverutils.playerlockdown.PlayerLockdownMod;
import net.kyrptonaught.serverutils.switchableresourcepacks.ResourcePackConfig;
import net.kyrptonaught.serverutils.switchableresourcepacks.SwitchableResourcepacksMod;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.network.packet.s2c.config.DynamicRegistriesS2CPacket;
import net.minecraft.registry.*;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.function.CommandFunction;
import net.minecraft.server.network.ServerConfigurationNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
Expand All @@ -37,7 +40,7 @@
import java.nio.file.Path;
import java.util.*;

public class CustomMapLoaderMod extends Module {
public class CustomMapLoaderMod extends ModuleWConfig<CustomMapLoaderConfig> {

public static final HashMap<Identifier, BattleMapAddon> BATTLE_MAPS = new HashMap<>();
public static final HashMap<Identifier, LobbyMapAddon> LOBBY_MAPS = new HashMap<>();
Expand All @@ -56,17 +59,37 @@ public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher)
CustomMapLoaderCommands.registerCommands(dispatcher);
}

public static void reloadAddonFiles(MinecraftServer server){
Registry<DimensionType> dimensionTypeRegistry = server.getRegistryManager().get(RegistryKeys.DIMENSION_TYPE);
((RegistryUnfreezer) dimensionTypeRegistry).unfreeze();
@Override
public CustomMapLoaderConfig createDefaultConfig() {
return new CustomMapLoaderConfig();
}

public static void reloadAddonFiles(MinecraftServer server) {
BATTLE_MAPS.clear();
LOBBY_MAPS.clear();

Registry<DimensionType> dimensionTypeRegistry = server.getRegistryManager().get(RegistryKeys.DIMENSION_TYPE);
((RegistryUnfreezer) dimensionTypeRegistry).unfreeze();
IO.discoverAddons(server);
dimensionTypeRegistry.freeze();

Votebook.generateBookLibrary(getAllBattleMaps());

dimensionTypeRegistry.freeze();

if (server.getPlayerManager() != null) {
Collection<CommandFunction<ServerCommandSource>> RECONFIG_FUNCTION = CMDHelper.getFunctions(server, ServerUtilsMod.CustomMapLoaderModule.getConfig().reconfigFunction);
List<ServerPlayerEntity> players = server.getPlayerManager().getPlayerList();
for (int i = players.size() - 1; i >= 0; i--) {
CMDHelper.executeAs(players.get(i), RECONFIG_FUNCTION);
players.get(i).networkHandler.reconfigure();
}
}
}

public static void onEnterReconfig(ServerConfigurationNetworkHandler handler, ServerPlayerEntity player) {
CombinedDynamicRegistries<ServerDynamicRegistryType> combinedDynamicRegistries = player.getServer().getCombinedDynamicRegistries();
handler.sendPacket(new DynamicRegistriesS2CPacket(new DynamicRegistryManager.ImmutableImpl(SerializableRegistries.streamDynamicEntries(combinedDynamicRegistries)).toImmutable()));
handler.endConfiguration();
}

public static List<BattleMapAddon> getAllBattleMaps(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.function.CommandFunctionManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
Expand Down Expand Up @@ -177,8 +176,7 @@ private static void handleClick(ServerPlayerEntity player, String action, String
if (action.startsWith("command/")) {
CMDHelper.executeAs(player, cmd);
} else if (action.startsWith("function/")) {
CommandFunctionManager functionManager = player.getServer().getCommandFunctionManager();
functionManager.getFunction(new Identifier(cmd)).ifPresent(commandFunction -> functionManager.execute(commandFunction, player.getServer().getCommandSource().withLevel(2).withSilent()));
CMDHelper.executeFunctionsAs(player, cmd);
} else if (action.startsWith("openUI/")) {
if (slot.replaceOpenScreen()) replaceScreen(cmd, player);
else showScreenFor(cmd, player);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.kyrptonaught.serverutils.mixin.customMapLoader;

import net.kyrptonaught.serverutils.customMapLoader.CustomMapLoaderMod;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.play.AcknowledgeReconfigurationC2SPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerPlayNetworkHandler.class)
public abstract class ServerPlayNetworkHandlerMixin extends ServerCommonNetworkHandler {

@Shadow
public ServerPlayerEntity player;

public ServerPlayNetworkHandlerMixin(MinecraftServer server, ClientConnection connection, ConnectedClientData clientData) {
super(server, connection, clientData);
}

@Inject(method = "onAcknowledgeReconfiguration", at = @At("TAIL"))
public void grabReconfig(AcknowledgeReconfigurationC2SPacket packet, CallbackInfo ci) {
CustomMapLoaderMod.onEnterReconfig((ServerConfigurationNetworkHandler) this.connection.getPacketListener(), this.player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(ScoreboardPlayerInfo::registerScoreboardOBJs);
ServerPlayConnectionEvents.INIT.register(ScoreboardPlayerInfo::onPlayerPreConnect);
ServerPlayConnectionEvents.JOIN.register(ScoreboardPlayerInfo::onPlayerConnect);
ServerPlayConnectionEvents.DISCONNECT.register(ScoreboardPlayerInfo::onPlayerDisconnect);
}

@Override
Expand Down Expand Up @@ -91,7 +92,7 @@ public static void onPlayerPreConnect(ServerPlayNetworkHandler handler, Minecraf

public static void onPlayerConnect(ServerPlayNetworkHandler handler, PacketSender sender, MinecraftServer server) {
if (queuedPlayerData.containsKey(((ServerCommonNetworkHandlerAccessor) handler).getConnection())) {
QueuedPlayerData playerData = queuedPlayerData.remove(((ServerCommonNetworkHandlerAccessor) handler).getConnection());
QueuedPlayerData playerData = queuedPlayerData.get(((ServerCommonNetworkHandlerAccessor) handler).getConnection());
protocolObjective.setScore(handler.player, playerData.protocolVersion);
setHasLEMClient(handler.player, playerData.hasLCH);
setHasOptifine(handler.player, playerData.hasOptishit);
Expand All @@ -106,6 +107,10 @@ public static void onPlayerConnect(ServerPlayNetworkHandler handler, PacketSende
setFabricClient(handler.player, true);
}

public static void onPlayerDisconnect(ServerPlayNetworkHandler handler, MinecraftServer server){
queuedPlayerData.remove(((ServerCommonNetworkHandlerAccessor) handler).getConnection());
}

public static void checkBrand(ServerPlayerEntity player, String brand) {
if (brand.contains("forge"))
setForgeClient(player, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.command.argument.IdentifierArgumentType;
import net.minecraft.network.packet.s2c.common.ResourcePackRemoveS2CPacket;
import net.minecraft.network.packet.s2c.common.ResourcePackSendS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.function.CommandFunction;
Expand Down Expand Up @@ -79,9 +78,9 @@ public static void packStatusUpdate(ServerPlayerEntity player, UUID packname, Pa

if (allPacksLoaded(player)) {
if (RP_LOADED_FUNCTIONS == null)
RP_LOADED_FUNCTIONS = getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerCompleteFunction);
RP_LOADED_FUNCTIONS = CMDHelper.getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerCompleteFunction);
if (RP_FAILED_FUNCTIONS == null)
RP_FAILED_FUNCTIONS = getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerFailedFunction);
RP_FAILED_FUNCTIONS = CMDHelper.getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerFailedFunction);

if (didPackFail(player))
CMDHelper.executeAs(player, RP_FAILED_FUNCTIONS);
Expand All @@ -90,14 +89,6 @@ public static void packStatusUpdate(ServerPlayerEntity player, UUID packname, Pa
}
}

private static Collection<CommandFunction<ServerCommandSource>> getFunctions(MinecraftServer server, String id) {
if (id.startsWith("#")) {
return server.getCommandFunctionManager().getTag(new Identifier(id.replaceAll("#", "")));
}

return Collections.singleton(server.getCommandFunctionManager().getFunction(new Identifier(id)).get());
}

private static void addPackStatus(ServerPlayerEntity player, UUID packname, boolean temp) {
if (!playerLoaded.containsKey(player.getUuid()))
playerLoaded.put(player.getUuid(), new PackStatus());
Expand Down Expand Up @@ -146,7 +137,7 @@ public static boolean isCustomPackEnabled(ServerPlayerEntity player) {

private void execute(ResourcePackConfig.RPOption rpOption, ServerPlayerEntity player) {
if (RP_STARTED_FUNCTIONS == null)
RP_STARTED_FUNCTIONS = getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerStartFunction);
RP_STARTED_FUNCTIONS = CMDHelper.getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerStartFunction);

CMDHelper.executeAs(player, RP_STARTED_FUNCTIONS);

Expand All @@ -168,7 +159,7 @@ public static void addPacks(List<ResourcePackConfig.RPOption> packList, ServerPl
if (!hasNewPacks(packList, player)) return;

if (RP_STARTED_FUNCTIONS == null)
RP_STARTED_FUNCTIONS = getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerStartFunction);
RP_STARTED_FUNCTIONS = CMDHelper.getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerStartFunction);

CMDHelper.executeAs(player, RP_STARTED_FUNCTIONS);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package net.kyrptonaught.serverutils.welcomeMessage;

import net.kyrptonaught.serverutils.CMDHelper;
import net.kyrptonaught.serverutils.ModuleWConfig;
import net.kyrptonaught.serverutils.ServerUtilsMod;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.function.CommandFunction;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;

import java.util.HashSet;
import java.util.Optional;
import java.util.UUID;

public class WelcomeModule extends ModuleWConfig<WelcomeMessageConfig> {
Expand All @@ -20,8 +17,7 @@ public static void trySendWelcomeMessage(MinecraftServer server, ServerPlayerEnt
if (playerMsgSent.contains(player.getUuid()))
return;
WelcomeMessageConfig config = ServerUtilsMod.WelcomeMessageModule.getConfig();
Optional<CommandFunction<ServerCommandSource>> function = server.getCommandFunctionManager().getFunction(new Identifier(config.function));
function.ifPresent(commandFunction -> server.getCommandFunctionManager().execute(commandFunction, player.getCommandSource()));
CMDHelper.executeFunctionsAs(player, config.function);
playerMsgSent.add(player.getUuid());
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/serverutils.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"chesttracker.AbstractBlockStateMixin",
"cpslimiter.ServerPlayNetworkHandlerMixin",
"critBlocker.PlayerEntityMixin",
"customMapLoader.ServerPlayNetworkHandlerMixin",
"customMapLoader.SimpleRegistryMixin",
"customWorldBorder.PlayerManagerMixin",
"customWorldBorder.WorldBorderMixin",
Expand Down

0 comments on commit 823542f

Please sign in to comment.