Skip to content

Commit

Permalink
Map loader will now wait to notify datapack until all players rps are…
Browse files Browse the repository at this point in the history
… loaded.

SRP will now notify the datapack when a player is finished loading, or if they failed.
  • Loading branch information
kyrptonaught committed Mar 6, 2024
1 parent 25f54fa commit aa948ba
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 39 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/kyrptonaught/serverutils/CMDHelper.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.kyrptonaught.serverutils;

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

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

public class CMDHelper {
Expand All @@ -11,6 +14,12 @@ public static void executeAs(PlayerEntity player, String cmd) {
player.getServer().getCommandManager().executeWithPrefix(player.getCommandSource().withLevel(2).withSilent(), cmd);
}

public static void executeAs(PlayerEntity player, Collection<CommandFunction<ServerCommandSource>> functions) {
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.chestTracker.ChestTrackerMod;
import net.kyrptonaught.serverutils.customMapLoader.addons.BattleMapAddon;
Expand Down Expand Up @@ -41,32 +42,58 @@ public class CustomMapLoaderMod extends Module {
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTING.register(IO::discoverAddons);
ServerTickEvents.START_SERVER_TICK.register(CustomMapLoaderMod::serverTick);
}

@Override
public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
CustomMapLoaderCommands.registerCommands(dispatcher);
}

public static void serverTick(MinecraftServer server) {
Iterator<Map.Entry<Identifier, LoadedBattleMapInstance>> it = LOADED_BATTLE_MAPS.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Identifier, LoadedBattleMapInstance> pair = it.next();
LoadedBattleMapInstance instance = pair.getValue();

if (instance.scheduleToRemove) {
it.remove();
continue;
}

if (instance.finishedLoading)
continue;

if (instance.runPreTriggerCondition(server))
instance.executeDatapack(server);
}
}


public static void battleLoad(MinecraftServer server, Identifier addon, Identifier dimID, boolean centralSpawnEnabled, Collection<ServerPlayerEntity> players, Collection<CommandFunction<ServerCommandSource>> functions) {
BattleMapAddon config = CustomMapLoaderMod.BATTLE_MAPS.get(addon);
MapSize mapSize = CustomMapLoaderMod.autoSelectMapSize(config, server.getCurrentPlayerCount());

Path path = server.getSavePath(WorldSavePath.ROOT).resolve("dimensions").resolve(dimID.getNamespace()).resolve(dimID.getPath());
IO.unZipMap(path, config.filePath, config.getDirectoryInZip(mapSize));

DimensionLoaderMod.loadDimension(dimID, config.dimensionType_id, (server1, customDimHolder) -> {
LoadedBattleMapInstance instance = new LoadedBattleMapInstance(centralSpawnEnabled, mapSize, config, dimID);
battlePrepare(instance, players);
battleSpawn(instance, players);
DimensionLoaderMod.loadDimension(dimID, config.dimensionType_id,
(server1, customDimHolder) -> {
LoadedBattleMapInstance instance = new LoadedBattleMapInstance(centralSpawnEnabled, mapSize, config, dimID);
battlePrepare(instance, players);
battleSpawn(instance, players);

if (functions != null) {
for (CommandFunction<ServerCommandSource> commandFunction : functions) {
server.getCommandFunctionManager().execute(commandFunction, server.getCommandSource().withLevel(2).withSilent());
}
}
LOADED_BATTLE_MAPS.put(dimID, instance);
});
instance.setPreTriggerDatapackCondition(server2 -> {
for (ServerPlayerEntity player : players) {
if (!SwitchableResourcepacksMod.allPacksLoaded(player))
return false;
}
return true;
});
instance.setDatapackFunctions(functions);

LOADED_BATTLE_MAPS.put(dimID, instance);
});

server.getPlayerManager().broadcast(Text.literal("Loading map: ").append(config.getNameText()), false);
MessageSender.sendGameMessageWMentions(Text.literal("Loading map: ").append(config.getNameText()));
Expand Down Expand Up @@ -117,11 +144,11 @@ public static void battleTp(Identifier dimID, boolean initialSpawn, Collection<S

if (initialSpawn) {
for (ServerPlayerEntity player : players) {
battleTP(player, instance.getWorld(), centerPos, instance.getNextInitialSpawn(), instance.getAddon().required_packs,true, true);
battleTP(player, instance.getWorld(), centerPos, instance.getNextInitialSpawn(), instance.getAddon().required_packs, true, true);
}
} else {
for (ServerPlayerEntity player : players) {
battleTP(player, instance.getWorld(), centerPos, instance.getUnusedRandomSpawn(), instance.getAddon().required_packs, true,false);
battleTP(player, instance.getWorld(), centerPos, instance.getUnusedRandomSpawn(), instance.getAddon().required_packs, true, false);
}
}
}
Expand Down Expand Up @@ -212,7 +239,8 @@ public static void unloadLobbyMap(MinecraftServer server, Identifier dimID, Coll
}

public static void unloadBattleMap(MinecraftServer server, Identifier dimID, Collection<CommandFunction<ServerCommandSource>> functions) {
LOADED_BATTLE_MAPS.remove(dimID);
if (LOADED_BATTLE_MAPS.containsKey(dimID))
LOADED_BATTLE_MAPS.get(dimID).scheduleToRemove = true;

DimensionLoaderMod.unLoadDimension(server, dimID, functions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import net.kyrptonaught.serverutils.customMapLoader.addons.BattleMapAddon;
import net.kyrptonaught.serverutils.dimensionLoader.DimensionLoaderMod;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.function.CommandFunction;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;

public class LoadedBattleMapInstance {

Expand All @@ -23,6 +28,13 @@ public class LoadedBattleMapInstance {

public List<String> unusedRandomSpawns;

private Function<MinecraftServer, Boolean> preTriggerDatapackCondition;
private Collection<CommandFunction<ServerCommandSource>> datapackFunctions;

public boolean finishedLoading = false;

public boolean scheduleToRemove = false;

public LoadedBattleMapInstance(boolean centralSpawnEnabled, MapSize selectedMapSize, BattleMapAddon battleMapAddon, Identifier dimID) {
this.centralSpawnEnabled = centralSpawnEnabled;
this.selectedMapSize = selectedMapSize;
Expand Down Expand Up @@ -69,4 +81,33 @@ public String getUnusedRandomSpawn() {

return unusedRandomSpawns.remove(getWorld().random.nextInt(unusedRandomSpawns.size()));
}

public void setPreTriggerDatapackCondition(Function<MinecraftServer, Boolean> execute) {
this.preTriggerDatapackCondition = execute;
}

public boolean runPreTriggerCondition(MinecraftServer server) {
if (preTriggerDatapackCondition == null)
return true;

if (preTriggerDatapackCondition.apply(server)) {
preTriggerDatapackCondition = null;
return true;
}

return false;
}

public void setDatapackFunctions(Collection<CommandFunction<ServerCommandSource>> functions) {
this.datapackFunctions = functions;
}

public void executeDatapack(MinecraftServer server) {
if (datapackFunctions != null)
for (CommandFunction<ServerCommandSource> commandFunction : datapackFunctions) {
server.getCommandFunctionManager().execute(commandFunction, server.getCommandSource().withLevel(2).withSilent());
}
datapackFunctions = null;
finishedLoading = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@ public class CustomDimHolder {
public RuntimeWorldHandle world;
private boolean scheduleDelete = false;

public CustomDimHolder(Identifier dimID, Identifier copyFromID, Collection<CommandFunction<ServerCommandSource>> functions) {
public CustomDimHolder(Identifier dimID, Identifier copyFromID) {
this.dimID = dimID;
this.copyFromID = copyFromID;
setFunctions(functions);
}

public CustomDimHolder(Identifier dimID, Identifier copyFromID, BiConsumer<MinecraftServer, CustomDimHolder> functions) {
this.dimID = dimID;
this.copyFromID = copyFromID;
setFunctions(functions);
}

public void scheduleToDelete() {
Expand Down Expand Up @@ -55,21 +48,23 @@ public void register(RuntimeWorldHandle handle) {
this.world = handle;
}

public void setFunctions(BiConsumer<MinecraftServer, CustomDimHolder> execute) {
public CustomDimHolder setCompleteTask(BiConsumer<MinecraftServer, CustomDimHolder> execute) {
this.completionTask = execute;
return this;
}

public void setFunctions(Collection<CommandFunction<ServerCommandSource>> functions) {
setFunctions((server, customDimHolder) -> {
public CustomDimHolder setCompleteTask(Collection<CommandFunction<ServerCommandSource>> functions) {
setCompleteTask((server, customDimHolder) -> {
if (functions != null) {
for (CommandFunction<ServerCommandSource> commandFunction : functions) {
server.getCommandFunctionManager().execute(commandFunction, server.getCommandSource().withLevel(2).withSilent());
}
}
});
return this;
}

public void executeFunctions(MinecraftServer server) {
public void executeComplete(MinecraftServer server) {
if (completionTask != null) completionTask.accept(server, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher)
}

public static void loadDimension(Identifier id, Identifier dimID, BiConsumer<MinecraftServer, CustomDimHolder> onComplete) {
loadedWorlds.put(id, new CustomDimHolder(id, dimID, onComplete));
loadedWorlds.put(id, new CustomDimHolder(id, dimID).setCompleteTask(onComplete));
}

public static Text loadDimension(MinecraftServer server, Identifier id, Identifier dimID, Collection<CommandFunction<ServerCommandSource>> functions) {
Expand All @@ -65,7 +65,7 @@ public static Text loadDimension(MinecraftServer server, Identifier id, Identifi
return Text.literal("Failed creating temp directory");
}

loadedWorlds.put(id, new CustomDimHolder(id, dimID, functions));
loadedWorlds.put(id, new CustomDimHolder(id, dimID).setCompleteTask(functions));
return Text.literal("Preparing Dimension");
}

Expand All @@ -74,7 +74,7 @@ public static Text unLoadDimension(MinecraftServer server, Identifier id, Collec
if (holder == null)
return Text.literal("Dimension not found");

holder.setFunctions(functions);
holder.setCompleteTask(functions);
holder.scheduleToDelete();
return Text.literal("Unloading Dimension");
}
Expand Down Expand Up @@ -109,13 +109,12 @@ public static void serverTickWorldAdd(MinecraftServer server) {

if (holder.scheduledDelete()) {
if (holder.deleteFinished(fantasy)) {
holder.executeFunctions(server);
holder.executeComplete(server);
CustomWorldBorderMod.onDimensionUnload(holder.world.asWorld());
DatapackInteractables.unloadWorld(holder.world.getRegistryKey());
it.remove();
}
} else if (!holder.wasRegistered()) {
//I don't really understand how mc registry key/entry shit works, but this does somehow work
Registry<DimensionType> registry = server.getRegistryManager().get(RegistryKeys.DIMENSION_TYPE);
RegistryEntry<DimensionType> entry = registry.getEntry(registry.getKey(registry.get(holder.copyFromID)).get()).get();

Expand All @@ -125,7 +124,7 @@ public static void serverTickWorldAdd(MinecraftServer server) {
.setGenerator(new VoidChunkGenerator(server.getRegistryManager().get(RegistryKeys.BIOME).entryOf(BiomeKeys.THE_VOID)));

holder.register(fantasy.openTemporaryWorld(holder.dimID, worldConfig));
holder.executeFunctions(server);
holder.executeComplete(server);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ public ResourcePackStatusMixin(MinecraftServer server, ClientConnection connecti
@Override
public void onResourcePackStatus(ResourcePackStatusC2SPacket packet) {
super.onResourcePackStatus(packet);
System.out.println(this.player.getNameForScoreboard() + " " + packet.id() + " " + packet.status());
switch (packet.status()) {
case ACCEPTED ->
SwitchableResourcepacksMod.packStatusUpdate(this.player, packet.id(), PackStatus.LoadingStatus.STARTED);
case SUCCESSFULLY_LOADED ->
SwitchableResourcepacksMod.packStatusUpdate(this.player, packet.id(), PackStatus.LoadingStatus.FINISHED);
case DECLINED, FAILED_DOWNLOAD, INVALID_URL, FAILED_RELOAD ->
SwitchableResourcepacksMod.packStatusUpdate(this.player, packet.id(), PackStatus.LoadingStatus.FAILED);
case DISCARDED ->
SwitchableResourcepacksMod.packStatusUpdate(this.player, packet.id(), PackStatus.LoadingStatus.REMOVED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public Map<UUID, Status> getPacks() {
return packs;
}

public boolean isComplete(UUID pack) {
return packs.get(pack).getLoadingStatus() == LoadingStatus.FINISHED || packs.get(pack).getLoadingStatus() == LoadingStatus.FAILED;
}

public boolean didFail(UUID pack) {
return packs.get(pack).getLoadingStatus() == LoadingStatus.FAILED;
}

public static class Status {
private boolean tempPack;
private LoadingStatus loadingStatus;
Expand All @@ -45,7 +53,6 @@ public enum LoadingStatus {
PENDING,
STARTED,
FAILED,
FINISHED,
REMOVED
FINISHED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.List;

public class ResourcePackConfig extends AbstractConfigFile {
public String playerCompleteFunction;
public String playerFailedFunction;

List<RPOption> packs = new ArrayList<>();

public static class RPOption {
Expand Down
Loading

0 comments on commit aa948ba

Please sign in to comment.