Skip to content

Commit

Permalink
Merge branch 'master' into backup-improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Dream-Master authored Sep 13, 2024
2 parents 0776349 + 6aa640b commit 5ee2cee
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 74 deletions.
15 changes: 8 additions & 7 deletions src/main/java/serverutils/ServerUtilitiesCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@
import serverutils.aurora.AuroraConfig;
import serverutils.aurora.mc.AuroraMinecraftHandler;
import serverutils.command.ServerUtilitiesCommands;
import serverutils.data.Leaderboard;
import serverutils.data.NodeEntry;
import serverutils.data.ServerUtilitiesLoadedChunkManager;
import serverutils.data.ServerUtilitiesUniverseData;
import serverutils.events.CustomPermissionPrefixesRegistryEvent;
import serverutils.events.IReloadHandler;
import serverutils.events.LeaderboardRegistryEvent;
import serverutils.events.ServerReloadEvent;
import serverutils.events.ServerUtilitiesPreInitRegistryEvent;
import serverutils.handlers.ServerUtilitiesPlayerEventHandler;
Expand Down Expand Up @@ -109,7 +107,6 @@
public class ServerUtilitiesCommon {

public static final Collection<NodeEntry> CUSTOM_PERM_PREFIX_REGISTRY = new HashSet<>();
public static final Map<ResourceLocation, Leaderboard> LEADERBOARDS = new HashMap<>();
public static final Map<String, String> KAOMOJIS = new HashMap<>();
public static final Map<String, ConfigValueProvider> CONFIG_VALUE_PROVIDERS = new HashMap<>();
public static final Map<UUID, ServerUtilitiesCommon.EditingConfig> TEMP_SERVER_CONFIG = new HashMap<>();
Expand Down Expand Up @@ -177,7 +174,6 @@ public void preInit(FMLPreInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(ServerUtilitiesWorldEventHandler.INST);
MinecraftForge.EVENT_BUS.register(ServerUtilitiesUniverseData.INST);
MinecraftForge.EVENT_BUS.register(ServerUtilitiesPermissions.INST);
MinecraftForge.EVENT_BUS.register(ServerUtilitiesLeaderboards.INST);
FMLCommonHandler.instance().bus().register(ServerUtilitiesServerEventHandler.INST);
if (AuroraConfig.enable) {
MinecraftForge.EVENT_BUS.register(AuroraMinecraftHandler.INST);
Expand All @@ -187,9 +183,7 @@ public void preInit(FMLPreInitializationEvent event) {
}

public void init(FMLInitializationEvent event) {
new LeaderboardRegistryEvent(leaderboard -> LEADERBOARDS.put(leaderboard.id, leaderboard)).post();
ServerUtilitiesPermissions.registerPermissions();

ServerUtilitiesPreInitRegistryEvent.Registry registry = new ServerUtilitiesPreInitRegistryEvent.Registry() {

@Override
Expand Down Expand Up @@ -264,7 +258,7 @@ public void onAction(ForgePlayer player, NBTTagCompound data) {
registry.registerTeamAction(ServerUtilitiesTeamGuiActions.TRANSFER_OWNERSHIP);

new ServerUtilitiesPreInitRegistryEvent(registry).post();

RELOAD_IDS.put(new ResourceLocation(ServerUtilities.MOD_ID, "internal_reload"), this::onReload);
RankConfigAPI.getHandler();

CHAT_FORMATTING_SUBSTITUTES.put("name", ForgePlayer::getDisplayName);
Expand Down Expand Up @@ -356,6 +350,13 @@ public void registerTasks() {
}
}

public boolean onReload(ServerReloadEvent event) {
if (event.getUniverse() != null) {
ServerUtilitiesLeaderboards.loadLeaderboards();
}
return true;
}

public void handleClientMessage(MessageToClient message) {}

public long getWorldTime() {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/serverutils/ServerUtilitiesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ public static class Commands {

@Config.DefaultBoolean(true)
public boolean dump_permissions;

@Config.DefaultBoolean(true)
public boolean dump_stats;
}

public static class Backups {
Expand Down
175 changes: 131 additions & 44 deletions src/main/java/serverutils/ServerUtilitiesLeaderboards.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
package serverutils;

import java.io.File;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.function.IntFunction;

import net.minecraft.stats.StatBase;
import net.minecraft.stats.StatList;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.ResourceLocation;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import serverutils.data.Leaderboard;
import serverutils.events.LeaderboardRegistryEvent;
import serverutils.lib.data.ForgePlayer;
import serverutils.lib.io.DataReader;
import serverutils.lib.math.Ticks;
import serverutils.lib.util.JsonUtils;
import serverutils.lib.util.permission.DefaultPermissionLevel;
import serverutils.lib.util.permission.PermissionAPI;

public final class ServerUtilitiesLeaderboards {

private ServerUtilitiesLeaderboards() {}

public static final ServerUtilitiesLeaderboards INST = new ServerUtilitiesLeaderboards();

@SubscribeEvent
@SuppressWarnings("unused") // used by reflection
public void registerLeaderboards(LeaderboardRegistryEvent event) {
event.register(
new Leaderboard.FromStat(
new ResourceLocation(ServerUtilities.MOD_ID, "deaths"),
StatList.deathsStat,
false,
Leaderboard.FromStat.DEFAULT));
event.register(
new Leaderboard.FromStat(
new ResourceLocation(ServerUtilities.MOD_ID, "mob_kills"),
StatList.mobKillsStat,
false,
Leaderboard.FromStat.DEFAULT));
event.register(
new Leaderboard.FromStat(
new ResourceLocation(ServerUtilities.MOD_ID, "time_played"),
StatList.minutesPlayedStat,
false,
Leaderboard.FromStat.TIME));
event.register(
new Leaderboard.FromStat(
new ResourceLocation(ServerUtilities.MOD_ID, "time_afk"),
ServerUtilitiesStats.AFK_TIME,
false,
Leaderboard.FromStat.TIME));
event.register(
new Leaderboard.FromStat(
new ResourceLocation(ServerUtilities.MOD_ID, "jumps"),
StatList.jumpStat,
false,
Leaderboard.FromStat.DEFAULT));

event.register(
public static final Map<ResourceLocation, Leaderboard> LEADERBOARDS = new HashMap<>();
private static final File STAT_LEADERBOARD_FILE = new File(
ServerUtilities.SERVER_FOLDER + "stat_leaderboards.json");
private static final String[] DEFAULT_STAT_LEADERBOARDS = { StatList.deathsStat.statId,
StatList.mobKillsStat.statId, StatList.minutesPlayedStat.statId, ServerUtilitiesStats.AFK_TIME.statId,
StatList.jumpStat.statId };

static void loadLeaderboards() {
LEADERBOARDS.clear();
registerLeaderboard(
new Leaderboard(
new ResourceLocation(ServerUtilities.MOD_ID, "deaths_per_hour"),
new ChatComponentTranslation("serverutilities.stat.dph"),
Expand All @@ -65,22 +47,21 @@ public void registerLeaderboards(LeaderboardRegistryEvent event) {
},
Comparator.comparingDouble(ServerUtilitiesLeaderboards::getDPH).reversed(),
player -> getDPH(player) >= 0D));

event.register(
registerLeaderboard(
new Leaderboard(
new ResourceLocation(ServerUtilities.MOD_ID, "time_active"),
new ChatComponentTranslation("serverutilities.stat.time_active"),
player -> Leaderboard.FromStat.TIME.apply(getActivePlayTime(player)),
Comparator.comparingLong(ServerUtilitiesLeaderboards::getActivePlayTime).reversed(),
player -> getActivePlayTime(player) != 0));
event.register(
registerLeaderboard(
new Leaderboard(
new ResourceLocation(ServerUtilities.MOD_ID, "time_afk_percent"),
new ChatComponentTranslation("serverutilities.stat.time_afk_percent"),
player -> Leaderboard.FromStat.PERCENTAGE.apply(getAfkTimeFraction(player)),
Comparator.comparingDouble(ServerUtilitiesLeaderboards::getAfkTimeFraction).reversed(),
player -> getAfkTimeFraction(player) != 0));
event.register(
registerLeaderboard(
new Leaderboard(
new ResourceLocation(ServerUtilities.MOD_ID, "last_seen"),
new ChatComponentTranslation("serverutilities.stat.last_seen"),
Expand All @@ -97,6 +78,8 @@ public void registerLeaderboards(LeaderboardRegistryEvent event) {
},
Comparator.comparingLong(ServerUtilitiesLeaderboards::getRelativeLastSeen),
player -> player.getLastTimeSeen() != 0L));
loadFromFile();
new LeaderboardRegistryEvent(ServerUtilitiesLeaderboards::registerLeaderboard).post();
}

private static int getActivePlayTime(ForgePlayer player) {
Expand Down Expand Up @@ -132,4 +115,108 @@ private static double getDPH(ForgePlayer player) {

return -1D;
}

private static JsonElement getAndSaveDefaults() {
JsonObject obj = new JsonObject();
for (String id : DEFAULT_STAT_LEADERBOARDS) {
JsonObject obj1 = new JsonObject();
obj1.addProperty("name", "");
obj1.addProperty("reverse", false);
obj.add(id, obj1);
}

JsonObject example = new JsonObject();
example.addProperty("name", "The name that appears in the gui, leave empty/remove for default");
example.addProperty(
"reverse",
"(true || false) whether lower numbers should appear highest in the leaderboard");
obj.add("example.Stat || Full list of usable stats can be dumped with /dump_stats", example);
JsonUtils.toJsonSafe(STAT_LEADERBOARD_FILE, obj);
return obj;
}

private static void loadFromFile() {
JsonElement element;
if (!STAT_LEADERBOARD_FILE.exists()) {
element = getAndSaveDefaults();
} else {
element = DataReader.get(STAT_LEADERBOARD_FILE).safeJson();
}

if (JsonUtils.isNull(element)) return;

if (element.isJsonObject()) {
for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) {
String key = entry.getKey();
if (key.startsWith("example")) continue;

StatBase stat = StatList.func_151177_a(key);
if (stat == null) {
ServerUtilities.LOGGER.warn("Couldn't find stat with id {}, skipping", key);
continue;
}

JsonElement value = entry.getValue();
if (value.isJsonObject()) {
JsonObject obj = value.getAsJsonObject();

IChatComponent name;
JsonElement nameElem = obj.getAsJsonPrimitive("name");
if (nameElem != null && !nameElem.getAsString().isEmpty()) {
name = new ChatComponentText(nameElem.getAsString());
} else {
name = getSafeName(stat);
}

boolean reverse = false;
JsonElement reverseElem = obj.getAsJsonPrimitive("reverse");
if (reverseElem != null && reverseElem.getAsBoolean()) {
reverse = true;
}

registerLeaderboard(
new Leaderboard.FromStat(
new ResourceLocation(ServerUtilities.MOD_ID, key),
name,
stat,
reverse,
getStatIntFunction(stat)));

}
}
}
}

private static IntFunction<IChatComponent> getStatIntFunction(StatBase stat) {
if (stat.type.equals(StatBase.timeStatType)) {
return Leaderboard.FromStat.TIME;
}

return Leaderboard.FromStat.DEFAULT;
}

private static IChatComponent getSafeName(StatBase stat) {
IChatComponent component = stat.statName;
String id = stat.statId;
if (component instanceof ChatComponentTranslation translation && translation.getFormatArgs().length > 0) {
if (translation.getFormatArgs()[0] instanceof ChatComponentTranslation arg) {
if (id.startsWith("stat.entityKilledBy")) {
return new ChatComponentTranslation("serverutilities.stat.killed_by", arg.getUnformattedText());
} else if (id.startsWith("stat.killEntity")) {
return new ChatComponentTranslation(
"serverutilities.stat.entities_killed",
arg.getUnformattedText());
}
}
}
return component;
}

public static void registerLeaderboard(Leaderboard leaderboard) {
LEADERBOARDS.put(leaderboard.id, leaderboard);
PermissionAPI.registerNode(
ServerUtilitiesPermissions.getLeaderboardNode(leaderboard),
DefaultPermissionLevel.ALL,
"");
}
}
24 changes: 18 additions & 6 deletions src/main/java/serverutils/ServerUtilitiesPermissions.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package serverutils;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.annotation.Nullable;

Expand All @@ -16,6 +19,7 @@
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.registry.GameData;
import serverutils.data.Leaderboard;
import serverutils.data.NodeEntry;
import serverutils.events.CustomPermissionPrefixesRegistryEvent;
import serverutils.events.RegisterRankConfigEvent;
import serverutils.events.RegisterRankConfigHandlerEvent;
Expand Down Expand Up @@ -97,7 +101,7 @@ public class ServerUtilitiesPermissions {
public static final String INFINITE_BACK_USAGE = "serverutilities.back.infinite";
public static final String CRASH_REPORTS_VIEW = "admin_panel.serverutilities.crash_reports.view";
public static final String CRASH_REPORTS_DELETE = "admin_panel.serverutilities.crash_reports.delete";
private static final String LEADERBOARD_PREFIX = "serverutilities.leaderboard.";
public static final String LEADERBOARD_PREFIX = "serverutilities.leaderboard.";
public static final String EDIT_WORLD_GAMERULES = "admin_panel.serverutilities.edit_world.gamerules";
public static final String RANK_EDIT = "serverutilities.admin_panel.ranks.view";

Expand Down Expand Up @@ -264,10 +268,6 @@ public static void registerPermissions() {
CLAIMS_ITEM_BLACKLIST.contains(item) ? DefaultPermissionLevel.OP : DefaultPermissionLevel.ALL,
"");
}

for (Leaderboard leaderboard : ServerUtilitiesCommon.LEADERBOARDS.values()) {
PermissionAPI.registerNode(getLeaderboardNode(leaderboard), DefaultPermissionLevel.ALL, "");
}
}

@SubscribeEvent
Expand Down Expand Up @@ -370,7 +370,19 @@ public static boolean hasItemUsePermission(EntityPlayer player, Item block) {
return PermissionAPI.hasPermission(player, CLAIMS_ITEM_PREFIX + '.' + formatId(block));
}

public static Collection<NodeEntry> getPrefixes() {
return ServerUtilitiesCommon.CUSTOM_PERM_PREFIX_REGISTRY;
}

public static Collection<NodeEntry> getPrefixesExcluding(String... toExclude) {
return ServerUtilitiesCommon.CUSTOM_PERM_PREFIX_REGISTRY.stream().filter(
prefix -> toExclude.length == 0 || Arrays.stream(toExclude).noneMatch(prefix.getNode()::startsWith))
.collect(Collectors.toSet());
}

public static String getLeaderboardNode(Leaderboard leaderboard) {
return LEADERBOARD_PREFIX + leaderboard.id.getResourceDomain() + "." + leaderboard.id.getResourcePath();
String domain = leaderboard.id.getResourceDomain();
String id = (domain.equals(ServerUtilities.MOD_ID) ? "" : domain + ".") + leaderboard.id.getResourcePath();
return LEADERBOARD_PREFIX + id;
}
}
Loading

0 comments on commit 5ee2cee

Please sign in to comment.