From 1333f06dad17fe2bcf1182a63e6e1fe52d4ed497 Mon Sep 17 00:00:00 2001 From: Taah Date: Fri, 25 Feb 2022 01:54:11 -0800 Subject: [PATCH] - Add the ability to customize game rules per world in config - Add gamerules argument to /debug --- build.gradle | 46 +++++----- .../java/dev/plex/command/PlexCommand.java | 11 +++ .../java/dev/plex/command/impl/DebugCMD.java | 17 +++- .../plex/services/impl/GameRuleService.java | 27 ++---- src/main/java/dev/plex/util/PlexUtils.java | 91 +++++++++++-------- src/main/resources/config.yml | 3 + 6 files changed, 117 insertions(+), 78 deletions(-) diff --git a/build.gradle b/build.gradle index 8996e6f..4b10107 100644 --- a/build.gradle +++ b/build.gradle @@ -1,22 +1,22 @@ plugins { - id 'java' - id 'maven-publish' - id 'net.minecrell.plugin-yml.bukkit' version '0.5.1' + id "java" + id "maven-publish" + id "net.minecrell.plugin-yml.bukkit" version "0.5.1" id "com.github.johnrengelman.shadow" version "7.1.2" } repositories { mavenLocal() maven { - url = uri('https://papermc.io/repo/repository/maven-public/') + url = uri("https://papermc.io/repo/repository/maven-public/") } maven { - url = uri('https://repository.apache.org/content/repositories/snapshots/') + url = uri("https://repository.apache.org/content/repositories/snapshots/") } maven { - url = uri('https://repo.maven.apache.org/maven2/') + url = uri("https://repo.maven.apache.org/maven2/") } maven { @@ -27,30 +27,32 @@ repositories { } dependencies { - library 'org.projectlombok:lombok:1.18.22' - annotationProcessor 'org.projectlombok:lombok:1.18.22' - library 'org.json:json:20211205' - library 'commons-io:commons-io:2.11.0' - library 'dev.morphia.morphia:morphia-core:2.2.3' - library 'redis.clients:jedis:4.1.1' - library 'org.mariadb.jdbc:mariadb-java-client:3.0.3' - library 'org.apache.httpcomponents:httpclient:4.5.13' - compileOnly 'io.papermc.paper:paper-api:1.18.1-R0.1-SNAPSHOT' - implementation 'org.bstats:bstats-base:3.0.0' - implementation 'org.bstats:bstats-bukkit:3.0.0' + library "org.projectlombok:lombok:1.18.22" + annotationProcessor "org.projectlombok:lombok:1.18.22" + + library "org.json:json:20211205" + library "commons-io:commons-io:2.11.0" + library "dev.morphia.morphia:morphia-core:2.2.3" + library "redis.clients:jedis:4.1.1" + library "org.mariadb.jdbc:mariadb-java-client:3.0.3" + library "org.apache.httpcomponents:httpclient:4.5.13" + library "org.apache.commons:commons-lang3:3.12.0" + compileOnly "io.papermc.paper:paper-api:1.18.1-R0.1-SNAPSHOT" + implementation "org.bstats:bstats-base:3.0.0" + implementation "org.bstats:bstats-bukkit:3.0.0" implementation("net.kyori:adventure-text-minimessage:4.2.0-SNAPSHOT") { exclude group: "net.kyori", module: "adventure-api" exclude group: "org.jetbrains", module: "annotations" } } -group = 'dev.plex' -version = '0.6-SNAPSHOT' -description = 'Plex' +group = "dev.plex" +version = "0.6-SNAPSHOT" +description = "Plex" shadowJar { archiveClassifier.set("") - relocate 'org.bstats', 'dev.plex' + relocate "org.bstats", "dev.plex" } bukkit { @@ -100,7 +102,7 @@ publishing { } tasks.withType(JavaCompile) { - options.encoding = 'UTF-8' + options.encoding = "UTF-8" } tasks { diff --git a/src/main/java/dev/plex/command/PlexCommand.java b/src/main/java/dev/plex/command/PlexCommand.java index e0a081d..ba29da4 100644 --- a/src/main/java/dev/plex/command/PlexCommand.java +++ b/src/main/java/dev/plex/command/PlexCommand.java @@ -508,6 +508,17 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC return LegacyComponentSerializer.legacyAmpersand().deserialize(s); } + /** + * Converts a string to a mini message kyori component + * + * @param s The string to convert + * @return A Kyori component + */ + protected Component mmString(String s) + { + return MiniMessage.miniMessage().parse(s); + } + public Rank getLevel() { return level; diff --git a/src/main/java/dev/plex/command/impl/DebugCMD.java b/src/main/java/dev/plex/command/impl/DebugCMD.java index 1da9fcc..8971d76 100644 --- a/src/main/java/dev/plex/command/impl/DebugCMD.java +++ b/src/main/java/dev/plex/command/impl/DebugCMD.java @@ -5,16 +5,20 @@ import dev.plex.command.PlexCommand; import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandPermissions; import dev.plex.rank.enums.Rank; +import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; -import java.util.List; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -@CommandParameters(name = "debug", description = "Debug command", usage = "/ redis-reset [player]") +import java.util.List; + +@CommandParameters(name = "debug", description = "Debug command", usage = "/ [player]") @CommandPermissions(level = Rank.EXECUTIVE, permission = "plex.debug") public class DebugCMD extends PlexCommand { @@ -35,6 +39,15 @@ public class DebugCMD extends PlexCommand } return componentFromString("Couldn't find player in Redis punishments."); } + if (args[0].equalsIgnoreCase("gamerules")) + { + for (World world : Bukkit.getWorlds()) + { + PlexUtils.commitGameRules(world); + PlexLog.debug("Set gamerules for world: " + world.getName()); + } + return mmString("Re-applied game all the game rules!"); + } return null; } diff --git a/src/main/java/dev/plex/services/impl/GameRuleService.java b/src/main/java/dev/plex/services/impl/GameRuleService.java index 7306473..96af258 100644 --- a/src/main/java/dev/plex/services/impl/GameRuleService.java +++ b/src/main/java/dev/plex/services/impl/GameRuleService.java @@ -1,11 +1,18 @@ package dev.plex.services.impl; +import dev.plex.Plex; import dev.plex.services.AbstractService; import dev.plex.util.PlexLog; +import dev.plex.util.PlexUtils; +import org.apache.commons.lang.BooleanUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.apache.commons.lang3.EnumUtils; import org.bukkit.Bukkit; import org.bukkit.GameRule; import org.bukkit.World; +import java.util.Locale; + public class GameRuleService extends AbstractService { public GameRuleService() @@ -18,28 +25,12 @@ public class GameRuleService extends AbstractService { for (World world : Bukkit.getWorlds()) { - commitGameRules(world); + PlexUtils.commitGameRules(world); PlexLog.debug("Set gamerules for world: " + world.getName()); } } - private void commitGameRules(World world) - { - world.setGameRule(GameRule.DO_MOB_SPAWNING, false); - world.setGameRule(GameRule.DO_FIRE_TICK, false); - world.setGameRule(GameRule.KEEP_INVENTORY, true); - world.setGameRule(GameRule.DO_MOB_LOOT, false); - world.setGameRule(GameRule.MOB_GRIEFING, false); - world.setGameRule(GameRule.DO_TILE_DROPS, false); - world.setGameRule(GameRule.COMMAND_BLOCK_OUTPUT, false); - world.setGameRule(GameRule.NATURAL_REGENERATION, true); - world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false); - world.setGameRule(GameRule.DO_WEATHER_CYCLE, false); - world.setGameRule(GameRule.ANNOUNCE_ADVANCEMENTS, false); - world.setGameRule(GameRule.SHOW_DEATH_MESSAGES, false); - world.setGameRule(GameRule.SEND_COMMAND_FEEDBACK, false); - world.setGameRule(GameRule.DO_WEATHER_CYCLE, false); - } + @Override public int repeatInSeconds() diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 2106fa4..7bff552 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -4,29 +4,11 @@ import dev.plex.Plex; import dev.plex.PlexBase; import dev.plex.config.Config; import dev.plex.storage.StorageType; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; -import java.util.stream.Collectors; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; +import org.apache.commons.lang.math.NumberUtils; +import org.bukkit.*; import org.bukkit.command.Command; import org.bukkit.command.PluginCommandYamlParser; import org.bukkit.entity.Player; @@ -35,6 +17,16 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.sql.SQLException; +import java.util.*; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; + public class PlexUtils extends PlexBase { public static Map CHAT_COLOR_NAMES; @@ -68,20 +60,17 @@ public class PlexUtils extends PlexBase if (Plex.get().getStorageType() == StorageType.MARIADB) { PlexLog.log("Successfully enabled MySQL!"); - } - else if (Plex.get().getStorageType() == StorageType.SQLITE) + } else if (Plex.get().getStorageType() == StorageType.SQLITE) { PlexLog.log("Successfully enabled SQLite!"); } try { Plex.get().getSqlConnection().getCon().close(); - } - catch (SQLException ignored) + } catch (SQLException ignored) { } - } - else if (Plex.get().getMongoConnection().getDatastore() != null) + } else if (Plex.get().getMongoConnection().getDatastore() != null) { PlexLog.log("Successfully enabled MongoDB!"); } @@ -146,7 +135,7 @@ public class PlexUtils extends PlexBase public static String messageString(String entry, Object... objects) { - String f = plugin.messages.getString(entry); + String f = plugin.messages.getString(entry); if (f == null) { throw new NullPointerException(); @@ -164,12 +153,10 @@ public class PlexUtils extends PlexBase if (config.getString(path) == null) { color = def; - } - else if (ChatColor.getByChar(config.getString(path)) == null) + } else if (ChatColor.getByChar(config.getString(path)) == null) { color = def; - } - else + } else { color = ChatColor.getByChar(config.getString(path)); } @@ -196,6 +183,39 @@ public class PlexUtils extends PlexBase } } + public static void commitGameRules(World world) + { + for (String s : Plex.get().config.getStringList("worlds." + world.getName().toLowerCase(Locale.ROOT) + ".gameRules")) + { + String gameRule = s.split(";")[0]; + T value = (T) s.split(";")[1]; + GameRule rule = (GameRule) GameRule.getByName(gameRule); + if (rule != null && check(value).getClass().equals(rule.getType())) + { + world.setGameRule(rule, value); + PlexLog.debug("Setting game rule " + gameRule + " for world " + world.getName() + " with value " + value); + } else + { + PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value)); + } + } + } + + public static Object check(T value) + { + + if (value.toString().equalsIgnoreCase("true") || value.toString().equalsIgnoreCase("false")) + { + return Boolean.parseBoolean(value.toString()); + } + + if (NumberUtils.isNumber(value.toString())) + { + return Integer.parseInt(value.toString()); + } + return value; + } + public static List getPlayerNameList() { return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); @@ -216,7 +236,7 @@ public class PlexUtils extends PlexBase try { URL u = new URL(url); - HttpURLConnection connection = (HttpURLConnection)u.openConnection(); + HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; @@ -228,8 +248,7 @@ public class PlexUtils extends PlexBase in.close(); connection.disconnect(); return new JSONParser().parse(content.toString()); - } - catch (IOException | ParseException ex) + } catch (IOException | ParseException ex) { return null; } @@ -238,13 +257,13 @@ public class PlexUtils extends PlexBase public static UUID getFromName(String name) { JSONObject profile; - profile = (JSONObject)simpleGET("https://api.ashcon.app/mojang/v2/user/" + name); + profile = (JSONObject) simpleGET("https://api.ashcon.app/mojang/v2/user/" + name); if (profile == null) { PlexLog.error("Profile from Ashcon API returned null!"); return null; } - String uuidString = (String)profile.get("uuid"); + String uuidString = (String) profile.get("uuid"); return UUID.fromString(uuidString); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 82b59a8..c4e49a1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -48,6 +48,9 @@ worlds: name: "Flatlands" permission: "plex.world.flatlands" noEdit: "&cYou can't edit this world!" + gameRules: + - "doWeatherCycle;false" + - "doDaylightCycle;false" parameters: grass_block: 1 dirt: 32