diff --git a/README.md b/README.md index 74e56ec..cfbbdd0 100644 --- a/README.md +++ b/README.md @@ -39,17 +39,21 @@ See [this page](https://github.com/Patbox/PredicateAPI/blob/1.19.4/BUILTIN.md) f }, // Modifies how player name is displayed "player": { - // Toggles this feature. + // Toggles changing of player name on the list. "modify_name": false, - // Toggles modification of text on right of player name + // Toggles modification of text on right of player name. Available since 3.3.0+1.20.3 "modify_right_text": false, + // Toggles setting of different player positions on player list with a sorting index. Available since 3.6.0+1.21.2 + "modify_list_order": false, + // Toggles modification of player name visibility on player list through hidden parameter. Available since 3.6.0+1.21.2, always enabled before. + "modify_visibility": false, // Hides player name from player list. Doesn't have any effect on commands, suggestions or entity visibility! "hidden": false, // Disables this formatting, forcing it to use vanilla one. "passthrough": false, // Default format of player name "format": "%player:displayname%", - // Text displayed on right of the name + // Text displayed on right of the name. Available since 3.3.0+1.20.3 "right_text": "", // Enables sending updates when player sends a message "update_on_chat_message": false, @@ -60,10 +64,12 @@ See [this page](https://github.com/Patbox/PredicateAPI/blob/1.19.4/BUILTIN.md) f { // Requirement of style, used for applying "require": {/* PREDICATE */}, - // Applied formatting, same as one above + // (Optional) Applied formatting, same as one above. If not set defaults to next valid one. "format": "...", - // Text displayed on right of the name + // (Optional) Text displayed on right of the name. If not set defaults to next valid one. "right_text": "", + // Optional. Sorting index for player list. If not set defaults to next valid one, with 0 being default one. Available since 3.6.0+1.21.2 + "sorting_index": 0, // Optional. Disables this formatting, forcing it to use vanilla one. "passthrough": false, // Optional, hides player name from player list. Doesn't have any effect on commands, suggestions or entity visibility! diff --git a/build.gradle b/build.gradle index 6d1451b..4a865a0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version '1.6.+' + id 'fabric-loom' version '1.7.+' id 'maven-publish' id "com.modrinth.minotaur" version "2.+" id 'com.matthewprenger.cursegradle' version '1.4.0' @@ -32,8 +32,8 @@ dependencies { modCompileOnly("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}") modLocalRuntime("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}") - modImplementation include("eu.pb4:predicate-api:0.5.0+1.21") - modImplementation include("eu.pb4:placeholder-api:2.4.0+1.21") + modImplementation include("eu.pb4:predicate-api:0.6.0+1.21.2") + modImplementation include("eu.pb4:placeholder-api:2.5.0+1.21.2") modImplementation include("me.lucko:fabric-permissions-api:0.3.1") modImplementation include("eu.pb4:player-data-api:0.6.0+1.21") @@ -62,7 +62,7 @@ tasks.withType(JavaCompile).configureEach { // The Minecraft launcher currently installs Java 8 for users, so your mod probably wants to target Java 8 too // JDK 9 introduced a new way of specifying this that will make sure no newer classes or methods are used. // We'll use that if it's available, but otherwise we'll use the older option. - it.options.release = 16 + it.options.release = 21 } java { diff --git a/gradle.properties b/gradle.properties index 123aa4d..a4956e0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use -minecraft_version=1.21 -yarn_mappings=1.21+build.1 -loader_version=0.15.11 +minecraft_version=1.21.2-rc1 +yarn_mappings=1.21.2-rc1+build.1 +loader_version=0.16.7 #Fabric api -fabric_version=0.100.1+1.21 +fabric_version=0.105.4+1.21.2 # Mod Properties - mod_version = 3.5.1+1.21 + mod_version = 3.6.0+1.21.2 maven_group = eu.pb4 archives_base_name = styledplayerlist diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 48c0a02..0d18421 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/eu/pb4/styledplayerlist/MicroScheduler.java b/src/main/java/eu/pb4/styledplayerlist/MicroScheduler.java deleted file mode 100644 index 3fe7bb3..0000000 --- a/src/main/java/eu/pb4/styledplayerlist/MicroScheduler.java +++ /dev/null @@ -1,70 +0,0 @@ -package eu.pb4.styledplayerlist; - -import net.minecraft.server.MinecraftServer; -import org.apache.commons.lang3.mutable.MutableLong; - -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -public class MicroScheduler { - private final List singleTasks = new CopyOnWriteArrayList<>(); - private final List repeatingTasks = new CopyOnWriteArrayList<>(); - private static MicroScheduler INSTANCE; - private final MinecraftServer server; - private final Thread thread; - - public MicroScheduler(MinecraftServer s) { - this.server = s; - this.thread = new Thread(this::run); - this.thread.start(); - } - - private void run() { - while (!this.server.isStopped()) { - try { - singleTasks.removeIf(this::executeScheduled); - repeatingTasks.forEach(this::executeScheduled); - Thread.sleep(10); - } catch (Throwable e) { - - } - } - } - - private void executeScheduled(RepeatingTask repeatingTask) { - if (System.currentTimeMillis() >= repeatingTask.time.longValue()) { - this.server.execute(repeatingTask.runnable); - repeatingTask.time.add(repeatingTask.delay); - } - } - - private boolean executeScheduled(ScheduledTask scheduledTask) { - if (System.currentTimeMillis() >= scheduledTask.time) { - this.server.execute(scheduledTask.runnable); - return true; - } - return false; - } - - public void scheduleOnce(long delay, Runnable task) { - this.singleTasks.add(new ScheduledTask(System.currentTimeMillis() + delay, task)); - } - - public void scheduleRepeating(long delay, Runnable task) { - this.repeatingTasks.add(new RepeatingTask(delay, task)); - } - - public static MicroScheduler get(MinecraftServer server) { - if (INSTANCE == null || INSTANCE.server != server) { - INSTANCE = new MicroScheduler(server); - } - return INSTANCE; - } - - private record ScheduledTask(long time, Runnable runnable) {} - private record RepeatingTask(long delay, MutableLong time, Runnable runnable) { - RepeatingTask(long delay, Runnable runnable) { - this(delay, new MutableLong(System.currentTimeMillis() + delay), runnable); - } - } -} diff --git a/src/main/java/eu/pb4/styledplayerlist/config/Config.java b/src/main/java/eu/pb4/styledplayerlist/config/Config.java index daaef12..68bd8f7 100644 --- a/src/main/java/eu/pb4/styledplayerlist/config/Config.java +++ b/src/main/java/eu/pb4/styledplayerlist/config/Config.java @@ -2,12 +2,8 @@ import eu.pb4.placeholders.api.ParserContext; import eu.pb4.placeholders.api.PlaceholderContext; -import eu.pb4.placeholders.api.Placeholders; import eu.pb4.placeholders.api.node.TextNode; import eu.pb4.placeholders.api.parsers.NodeParser; -import eu.pb4.placeholders.api.parsers.PatternPlaceholderParser; -import eu.pb4.placeholders.api.parsers.StaticPreParser; -import eu.pb4.placeholders.api.parsers.TextParserV1; import eu.pb4.predicate.api.BuiltinPredicates; import eu.pb4.predicate.api.MinecraftPredicate; import eu.pb4.predicate.api.PredicateContext; @@ -55,7 +51,7 @@ public Config(ConfigData data) { for (ConfigData.PermissionNameFormat entry : data.playerName.permissionNameFormat) { this.permissionNameFormat.add(new PermissionNameFormat(entry.require != null ? entry.require : BuiltinPredicates.operatorLevel(5), - parseText(entry.format), parseText(entry.rightTextFormat), entry.ignoreFormatting, entry.hidePlayer != null ? entry.hidePlayer : isHiddenDefault)); + parseText(entry.format), parseText(entry.rightTextFormat), entry.index, entry.ignoreFormatting, entry.hidePlayer != null ? entry.hidePlayer : isHiddenDefault)); } } @@ -105,7 +101,19 @@ public boolean isPlayerHidden(ServerPlayerEntity player) { return this.isHiddenDefault; } + @Nullable + public Integer sortingIndex(ServerPlayerEntity player) { + var context = PredicateContext.of(player); + for (PermissionNameFormat entry : this.permissionNameFormat) { + if (entry.index != null && entry.predicate.test(context).success()) { + return entry.index; + } + } + + return null; + } + - record PermissionNameFormat(MinecraftPredicate predicate, @Nullable TextNode name, @Nullable TextNode right, boolean passthrough, boolean hidden) { + record PermissionNameFormat(MinecraftPredicate predicate, @Nullable TextNode name, @Nullable TextNode right, @Nullable Integer index, boolean passthrough, boolean hidden) { } } diff --git a/src/main/java/eu/pb4/styledplayerlist/config/data/ConfigData.java b/src/main/java/eu/pb4/styledplayerlist/config/data/ConfigData.java index 414ddc2..488f9da 100644 --- a/src/main/java/eu/pb4/styledplayerlist/config/data/ConfigData.java +++ b/src/main/java/eu/pb4/styledplayerlist/config/data/ConfigData.java @@ -35,6 +35,10 @@ public static class PlayerName { public boolean changePlayerName = false; @SerializedName("modify_right_text") public boolean changeRightText = false; + @SerializedName("modify_list_order") + public boolean modifyListOrder = false; + @SerializedName("modify_visibility") + public boolean changeVisiblity = false; @SerializedName("passthrough") public boolean ignoreFormatting = false; @SerializedName("hidden") @@ -59,6 +63,8 @@ public static class PermissionNameFormat { public String format = null; @SerializedName("right_text") public String rightTextFormat = null; + @SerializedName("sorting_index") + public Integer index = null; @SerializedName("passthrough") public boolean ignoreFormatting = false; diff --git a/src/main/java/eu/pb4/styledplayerlist/mixin/PlayerListS2CPacketEntryMixin.java b/src/main/java/eu/pb4/styledplayerlist/mixin/PlayerListS2CPacketEntryMixin.java index d141b94..a941dc2 100644 --- a/src/main/java/eu/pb4/styledplayerlist/mixin/PlayerListS2CPacketEntryMixin.java +++ b/src/main/java/eu/pb4/styledplayerlist/mixin/PlayerListS2CPacketEntryMixin.java @@ -11,6 +11,6 @@ public class PlayerListS2CPacketEntryMixin { @ModifyConstant(method = "(Lnet/minecraft/server/network/ServerPlayerEntity;)V", constant = @Constant(intValue = 1, ordinal = 0)) private static int styledPlayerList$hideRealPlayer(int constant, ServerPlayerEntity player) { - return ConfigManager.getConfig().isPlayerHidden(player) ? 0 : 1; + return ConfigManager.getConfig().configData.playerName.changeVisiblity ? (ConfigManager.getConfig().isPlayerHidden(player) ? 0 : 1) : constant; } } diff --git a/src/main/java/eu/pb4/styledplayerlist/mixin/ServerPlayNetworkManagerMixin.java b/src/main/java/eu/pb4/styledplayerlist/mixin/ServerPlayNetworkManagerMixin.java index 3a4cfda..3db9928 100644 --- a/src/main/java/eu/pb4/styledplayerlist/mixin/ServerPlayNetworkManagerMixin.java +++ b/src/main/java/eu/pb4/styledplayerlist/mixin/ServerPlayNetworkManagerMixin.java @@ -127,8 +127,21 @@ public ServerPlayNetworkManagerMixin(MinecraftServer server, ClientConnection co return; } + var list = EnumSet.noneOf(PlayerListS2CPacket.Action.class); if (ConfigManager.getConfig().configData.playerName.changePlayerName) { - PlayerListS2CPacket packet = new PlayerListS2CPacket(EnumSet.of(PlayerListS2CPacket.Action.UPDATE_DISPLAY_NAME, PlayerListS2CPacket.Action.UPDATE_LISTED), List.of(this.player)); + list.add(PlayerListS2CPacket.Action.UPDATE_DISPLAY_NAME); + } + + if (ConfigManager.getConfig().configData.playerName.modifyListOrder) { + list.add(PlayerListS2CPacket.Action.UPDATE_LIST_ORDER); + } + + if (ConfigManager.getConfig().configData.playerName.changeVisiblity) { + list.add(PlayerListS2CPacket.Action.UPDATE_LISTED); + } + + if (!list.isEmpty()) { + PlayerListS2CPacket packet = new PlayerListS2CPacket(list, List.of(this.player)); this.server.getPlayerManager().sendToAll(packet); } diff --git a/src/main/java/eu/pb4/styledplayerlist/mixin/ServerPlayerEntityMixin.java b/src/main/java/eu/pb4/styledplayerlist/mixin/ServerPlayerEntityMixin.java index e618f98..d41e1e8 100644 --- a/src/main/java/eu/pb4/styledplayerlist/mixin/ServerPlayerEntityMixin.java +++ b/src/main/java/eu/pb4/styledplayerlist/mixin/ServerPlayerEntityMixin.java @@ -20,4 +20,18 @@ public class ServerPlayerEntityMixin { } } + + @Inject(method = "getPlayerListOrder", at = @At("HEAD"), cancellable = true) + private void styledPlayerList$getPlayerListIndex(CallbackInfoReturnable cir) { + try { + if (ConfigManager.isEnabled() && ConfigManager.getConfig().configData.playerName.modifyListOrder) { + var x = ConfigManager.getConfig().sortingIndex((ServerPlayerEntity) (Object) this); + if (x != null) { + cir.setReturnValue(x); + } + } + } catch (Exception ignored) { + + } + } }