Skip to content

Commit

Permalink
Rework the custom music playing system
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Jun 17, 2024
1 parent 2c9a00e commit 3f26e49
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,7 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa
Identifier id = IdentifierArgumentType.getIdentifier(context, "dimID");
CustomMapLoaderMod.unloadBattleMap(context.getSource().getServer(), id, null);
return 1;
})))
.then(CommandManager.literal("triggerMusic")
.then(CommandManager.argument("dimID", IdentifierArgumentType.identifier())
.executes(context -> {
Identifier id = IdentifierArgumentType.getIdentifier(context, "dimID");
CustomMapLoaderMod.triggerMusic(id);
return 1;
})))
.then(CommandManager.literal("skipSong")
.then(CommandManager.argument("dimID", IdentifierArgumentType.identifier())
.then(CommandManager.argument("players", EntityArgumentType.players())
.executes(context -> {
Identifier id = IdentifierArgumentType.getIdentifier(context, "dimID");
Collection<ServerPlayerEntity> players = EntityArgumentType.getPlayers(context, "players");

CustomMapLoaderMod.skipSong(id, players);
return 1;
})))));
}))));

cmd.then(CommandManager.literal("lobby")
.then(CommandManager.literal("load")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,6 @@ public static void serverTick(MinecraftServer server) {
continue;
}

if (instance.tickMusic) {
long currentTime = System.currentTimeMillis();
server.getPlayerManager().getPlayerList().forEach(player -> {
PlayerInstanceData data = instance.playerData.get(player.getUuidAsString());
if (data != null) {
if (data.isSongFinished(currentTime)) {
data.playNextSong(player, currentTime);
}
}
});
}

if (instance.finishedLoading) {
continue;
}
Expand Down Expand Up @@ -225,7 +213,11 @@ private static void battleTP(ServerPlayerEntity player, ServerWorld world, Parse
if (loadResources)
loadResourcePacks(instance.getAddon(), player);

instance.addPlayerData(player);
if (SwitchableResourcepacksMod.isSafeMusicEnabled(player) && instance.getAddon().safe_music_pack != null) {
SwitchableResourcepacksMod.setMusicPack(player, instance.getAddon().safe_music_pack);
} else {
SwitchableResourcepacksMod.setMusicPack(player, instance.getAddon().music_pack);
}

ParsedPlayerCoords playerPos = parseVec3D(rawCoords);

Expand Down Expand Up @@ -328,16 +320,6 @@ public static void unloadBattleMap(MinecraftServer server, Identifier dimID, Col
DimensionLoaderMod.unLoadDimension(server, dimID, functions);
}

public static void triggerMusic(Identifier dimID) {
if (LOADED_BATTLE_MAPS.containsKey(dimID))
LOADED_BATTLE_MAPS.get(dimID).tickMusic = true;
}

public static void skipSong(Identifier dimID, Collection<ServerPlayerEntity> players) {
if (LOADED_BATTLE_MAPS.containsKey(dimID))
players.forEach(player -> LOADED_BATTLE_MAPS.get(dimID).skipSong(player));
}

private static BlockPos parseBlockPos(String coords) {
String[] coords_split = coords.split(" ");
return new BlockPos((int) Double.parseDouble(coords_split[0]), (int) Double.parseDouble(coords_split[1]), (int) Double.parseDouble(coords_split[2]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import net.kyrptonaught.serverutils.customMapLoader.addons.BattleMapAddon;
import net.kyrptonaught.serverutils.dimensionLoader.DimensionLoaderMod;
import net.kyrptonaught.serverutils.switchableresourcepacks.SwitchableResourcepacksMod;
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.server.world.ServerWorld;
import net.minecraft.util.Identifier;

import java.util.*;
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 @@ -34,10 +35,6 @@ public class LoadedBattleMapInstance {

public boolean scheduleToRemove = false;

public boolean tickMusic = false;

public final HashMap<String, PlayerInstanceData> playerData = new HashMap<>();

public LoadedBattleMapInstance(boolean centralSpawnEnabled, MapSize selectedMapSize, BattleMapAddon battleMapAddon, Identifier dimID) {
this.centralSpawnEnabled = centralSpawnEnabled;
this.selectedMapSize = selectedMapSize;
Expand All @@ -61,22 +58,6 @@ public Identifier getDimID() {
return dimID;
}

public void addPlayerData(ServerPlayerEntity player) {
if (!playerData.containsKey(player.getUuidAsString())) {
if (SwitchableResourcepacksMod.isSafeMusicEnabled(player) && battleMapAddon.safe_music_pack != null) {
playerData.put(player.getUuidAsString(), new PlayerInstanceData().setMusic(battleMapAddon.safe_music_pack));
} else {
playerData.put(player.getUuidAsString(), new PlayerInstanceData().setMusic(battleMapAddon.music_pack));
}
}
}

public void skipSong(ServerPlayerEntity player) {
if (playerData.containsKey(player.getUuidAsString()))
playerData.get(player.getUuidAsString()).skipSong(player);
}


public ServerWorld getWorld() {
return DimensionLoaderMod.loadedWorlds.get(dimID).world.asWorld();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

public class Converter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.kyrptonaught.serverutils.mixin.switchableresourcepacks;

import net.kyrptonaught.serverutils.switchableresourcepacks.PackStatus;
import net.kyrptonaught.serverutils.switchableresourcepacks.SwitchableResourcepacksMod;
import net.kyrptonaught.serverutils.switchableresourcepacks.status.PackStatus;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.common.ResourcePackStatusC2SPacket;
import net.minecraft.server.MinecraftServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

public class MusicPack {
public Identifier packID;
public PLAY_ORDER play_order;
public PLAY_ORDER play_order = PLAY_ORDER.CHRONOLOGICAL;
public String delay;

public LinkedHashMap<Identifier, Integer> songs;

public enum PLAY_ORDER {
CHRONOLOGICAL,
RANDOM
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.kyrptonaught.serverutils.CMDHelper;
import net.kyrptonaught.serverutils.ModuleWConfig;
import net.kyrptonaught.serverutils.ServerUtilsMod;
import net.kyrptonaught.serverutils.switchableresourcepacks.status.PackStatus;
import net.kyrptonaught.serverutils.switchableresourcepacks.status.PlayerStatus;
import net.kyrptonaught.serverutils.userConfig.UserConfigStorage;
import net.minecraft.command.argument.IdentifierArgumentType;
import net.minecraft.network.packet.s2c.common.ResourcePackRemoveS2CPacket;
Expand All @@ -24,7 +27,7 @@ public class SwitchableResourcepacksMod extends ModuleWConfig<ResourcePackConfig
public static final HashMap<Identifier, ResourcePack> ResourcePacks = new HashMap<>();
public static final HashMap<Identifier, MusicPack> MusicPacks = new HashMap<>();

private static final HashMap<UUID, PackStatus> playerLoaded = new HashMap<>();
private static final HashMap<UUID, PlayerStatus> playerLoaded = new HashMap<>();

private static Collection<CommandFunction<ServerCommandSource>> RP_FAILED_FUNCTIONS, RP_LOADED_FUNCTIONS, RP_STARTED_FUNCTIONS;
private static final Identifier CUSTOMPACKID = Identifier.of("custompack", "enabled");
Expand All @@ -43,6 +46,10 @@ public void onConfigLoad(ResourcePackConfig config) {
@Override
public void onInitialize() {
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> playerLoaded.remove(handler.getPlayer().getUuid()));
ServerTickEvents.START_SERVER_TICK.register(server -> {
long currentTime = System.currentTimeMillis();
server.getPlayerManager().getPlayerList().forEach(player -> getPlayerStatus(player).tick(player, currentTime));
});
}

@Override
Expand All @@ -51,9 +58,7 @@ public ResourcePackConfig createDefaultConfig() {
}

public static boolean allPacksLoaded(ServerPlayerEntity player) {
if (!playerLoaded.containsKey(player.getUuid())) return true;

PackStatus packs = playerLoaded.get(player.getUuid());
PlayerStatus packs = getPlayerStatus(player);
for (UUID pack : packs.getPacks().keySet()) {
if (!packs.isComplete(pack))
return false;
Expand All @@ -63,9 +68,7 @@ public static boolean allPacksLoaded(ServerPlayerEntity player) {
}

public static boolean didPackFail(ServerPlayerEntity player) {
if (!playerLoaded.containsKey(player.getUuid())) return false;

PackStatus packs = playerLoaded.get(player.getUuid());
PlayerStatus packs = getPlayerStatus(player);
for (UUID pack : packs.getPacks().keySet()) {
if (packs.didFail(pack))
return true;
Expand All @@ -75,10 +78,7 @@ public static boolean didPackFail(ServerPlayerEntity player) {
}

public static void packStatusUpdate(ServerPlayerEntity player, UUID packname, PackStatus.LoadingStatus status) {
if (!playerLoaded.containsKey(player.getUuid()))
playerLoaded.put(player.getUuid(), new PackStatus());

playerLoaded.get(player.getUuid()).setPackLoadStatus(packname, status);
getPlayerStatus(player).setPackLoadStatus(packname, status);

if (allPacksLoaded(player)) {
if (RP_LOADED_FUNCTIONS == null)
Expand All @@ -94,10 +94,14 @@ public static void packStatusUpdate(ServerPlayerEntity player, UUID packname, Pa
}

private static void addPackStatus(ServerPlayerEntity player, UUID packname, boolean temp) {
getPlayerStatus(player).addPack(packname, temp);
}

public static PlayerStatus getPlayerStatus(ServerPlayerEntity player) {
if (!playerLoaded.containsKey(player.getUuid()))
playerLoaded.put(player.getUuid(), new PackStatus());
playerLoaded.put(player.getUuid(), new PlayerStatus());

playerLoaded.get(player.getUuid()).addPack(packname, temp);
return playerLoaded.get(player.getUuid());
}

public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
Expand Down Expand Up @@ -127,8 +131,7 @@ public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher)
UserConfigStorage.setValue(player, CUSTOMPACKID, String.valueOf(enabled));
UserConfigStorage.syncPlayer(player);

if (playerLoaded.containsKey(player.getUuid()))
playerLoaded.get(player.getUuid()).getPacks().remove(CUSTOMPACKUUID);
getPlayerStatus(player).getPacks().remove(CUSTOMPACKUUID);

player.sendMessage(Text.translatable(enabled ? "lem.config.custompack.enable" : "lem.config.custompack.disable"));
return 1;
Expand All @@ -147,6 +150,36 @@ public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher)
player.sendMessage(Text.translatable(enabled ? "lem.config.safemusic.enable" : "lem.config.safemusic.disable"));
return 1;
})));

dispatcher.register(CommandManager.literal("music")
.requires(source -> source.hasPermissionLevel(0))
.then(CommandManager.literal("play")
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayer();
getPlayerStatus(player).startMusic();
return 1;
}))
.then(CommandManager.literal("set")
.then(CommandManager.argument("packid", IdentifierArgumentType.identifier())
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayer();
Identifier packid = IdentifierArgumentType.getIdentifier(context, "packid");

setMusicPack(player, MusicPacks.get(packid));
return 1;
})))
.then(CommandManager.literal("skip")
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayer();
getPlayerStatus(player).skipSong(player);
return 1;
}))
.then(CommandManager.literal("stop")
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayer();
getPlayerStatus(player).stopMusic();
return 1;
})));
}

public static boolean isCustomPackEnabled(ServerPlayerEntity player) {
Expand All @@ -157,6 +190,10 @@ public static boolean isSafeMusicEnabled(ServerPlayerEntity player) {
return Boolean.parseBoolean(UserConfigStorage.getValue(player, SAFEMUSICID));
}

public static void setMusicPack(ServerPlayerEntity player, MusicPack pack) {
getPlayerStatus(player).setMusic(pack);
}

private void execute(ResourcePack pack, ServerPlayerEntity player) {
if (RP_STARTED_FUNCTIONS == null)
RP_STARTED_FUNCTIONS = CMDHelper.getFunctions(player.getServer(), ServerUtilsMod.SwitchableResourcepacksModule.getConfig().playerStartFunction);
Expand All @@ -170,7 +207,7 @@ private void execute(ResourcePack pack, ServerPlayerEntity player) {
}

UUID packUUID = UUID.nameUUIDFromBytes(pack.packID.toString().getBytes(StandardCharsets.UTF_8));
if (playerLoaded.containsKey(player.getUuid()) && playerLoaded.get(player.getUuid()).getPacks().containsKey(packUUID))
if (getPlayerStatus(player).getPacks().containsKey(packUUID))
return;

addPackStatus(player, packUUID, false);
Expand Down Expand Up @@ -201,20 +238,18 @@ public static void addPacks(List<ResourcePack> packList, ServerPlayerEntity play
}

private static void clearTempPacks(ServerPlayerEntity player) {
if (playerLoaded.containsKey(player.getUuid()))
playerLoaded.get(player.getUuid()).getPacks().entrySet().removeIf(uuidStatusEntry -> {
if (uuidStatusEntry.getValue().isTempPack()) {
player.networkHandler.sendPacket(new ResourcePackRemoveS2CPacket(Optional.of(uuidStatusEntry.getKey())));
return true;
}
return false;
});
getPlayerStatus(player).getPacks().entrySet().removeIf(uuidStatusEntry -> {
if (uuidStatusEntry.getValue().isTempPack()) {
player.networkHandler.sendPacket(new ResourcePackRemoveS2CPacket(Optional.of(uuidStatusEntry.getKey())));
return true;
}
return false;
});
}

private static boolean hasNewPacks(List<ResourcePack> packList, ServerPlayerEntity player) {
List<UUID> c = new ArrayList<>();
if (playerLoaded.containsKey(player.getUuid()))
playerLoaded.get(player.getUuid()).getPacks().forEach((uuid, status) -> c.add(uuid));
getPlayerStatus(player).getPacks().forEach((uuid, status) -> c.add(uuid));

List<UUID> n = new ArrayList<>();
for (ResourcePack pack : packList) {
Expand Down
Loading

0 comments on commit 3f26e49

Please sign in to comment.