From f2bbf6f6dce0340f45fbc30591faff418d4c4bf6 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Thu, 27 Jan 2022 00:36:00 -0600 Subject: [PATCH] Gamerule stuff --- .../dev/plex/listener/impl/ChatListener.java | 6 +- src/main/java/dev/plex/player/PlexPlayer.java | 10 ++ .../dev/plex/services/ServiceManager.java | 4 +- .../plex/services/impl/GameRuleService.java | 47 ++++++++++ .../plex/services/impl/GameruleService.java | 92 ------------------- 5 files changed, 62 insertions(+), 97 deletions(-) create mode 100644 src/main/java/dev/plex/services/impl/GameRuleService.java delete mode 100644 src/main/java/dev/plex/services/impl/GameruleService.java diff --git a/src/main/java/dev/plex/listener/impl/ChatListener.java b/src/main/java/dev/plex/listener/impl/ChatListener.java index 95e26f1..3ced307 100644 --- a/src/main/java/dev/plex/listener/impl/ChatListener.java +++ b/src/main/java/dev/plex/listener/impl/ChatListener.java @@ -17,15 +17,15 @@ public class ChatListener extends PlexListener PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId()); if (!plexPlayer.getPrefix().isEmpty()) { - event.setFormat(String.format("%s %s §7» %s", plexPlayer.getPrefix(), ChatColor.RESET + plexPlayer.getName(), event.getMessage())); + event.setFormat(String.format("%s %s §7» %s", plexPlayer.getPrefix(), ChatColor.RESET + plexPlayer.displayName(), event.getMessage())); } else if (Plex.get().getRankManager().isAdmin(plexPlayer)) { - event.setFormat(String.format("%s %s §7» %s", plexPlayer.getRankFromString().getPrefix(), ChatColor.RESET + plexPlayer.getName(), event.getMessage())); + event.setFormat(String.format("%s %s §7» %s", plexPlayer.getRankFromString().getPrefix(), ChatColor.RESET + plexPlayer.displayName(), event.getMessage())); } else { - event.setFormat(String.format("%s §7» %s", ChatColor.RESET + plexPlayer.getName(), event.getMessage())); + event.setFormat(String.format("%s §7» %s", ChatColor.RESET + plexPlayer.displayName(), event.getMessage())); } } } diff --git a/src/main/java/dev/plex/player/PlexPlayer.java b/src/main/java/dev/plex/player/PlexPlayer.java index 5e69302..566417b 100644 --- a/src/main/java/dev/plex/player/PlexPlayer.java +++ b/src/main/java/dev/plex/player/PlexPlayer.java @@ -8,12 +8,16 @@ import dev.plex.rank.enums.Rank; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; +import net.kyori.adventure.text.serializer.ComponentSerializer; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; @Getter @Setter @@ -30,6 +34,7 @@ public class PlexPlayer @Indexed private String name; + private Player player; private String loginMSG; private String prefix; @@ -53,6 +58,7 @@ public class PlexPlayer this.id = uuid.substring(0, 8); this.name = ""; + this.player = Bukkit.getPlayer(name); this.loginMSG = ""; this.prefix = ""; @@ -66,6 +72,10 @@ public class PlexPlayer this.rank = ""; } + public String displayName() { + return PlainTextComponentSerializer.plainText().serialize(player.displayName()); + } + public Rank getRankFromString() { OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(uuid)); diff --git a/src/main/java/dev/plex/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index 42922ce..bd0f37b 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -3,7 +3,7 @@ package dev.plex.services; import com.google.common.collect.Lists; import dev.plex.Plex; import dev.plex.services.impl.BanService; -import dev.plex.services.impl.GameruleService; +import dev.plex.services.impl.GameRuleService; import org.bukkit.Bukkit; import java.util.List; @@ -13,7 +13,7 @@ public class ServiceManager { public ServiceManager() { registerService(new BanService()); - registerService(new GameruleService()); + registerService(new GameRuleService()); } public void startServices() { diff --git a/src/main/java/dev/plex/services/impl/GameRuleService.java b/src/main/java/dev/plex/services/impl/GameRuleService.java new file mode 100644 index 0000000..224bef8 --- /dev/null +++ b/src/main/java/dev/plex/services/impl/GameRuleService.java @@ -0,0 +1,47 @@ +package dev.plex.services.impl; + +import dev.plex.services.AbstractService; +import dev.plex.util.PlexLog; +import org.bukkit.Bukkit; +import org.bukkit.GameRule; +import org.bukkit.World; + +public class GameRuleService extends AbstractService +{ + public GameRuleService() + { + super(false, true); + } + + @Override + public void run() + { + for (World world : Bukkit.getWorlds()) + { + commitGameRules(world); + PlexLog.log("Set gamerules for world: " + world); + } + } + + 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.ANNOUNCE_ADVANCEMENTS, false); + world.setGameRule(GameRule.SHOW_DEATH_MESSAGES, false); + world.setGameRule(GameRule.SEND_COMMAND_FEEDBACK, false); + } + + @Override + public int repeatInSeconds() + { + return 0; + } +} diff --git a/src/main/java/dev/plex/services/impl/GameruleService.java b/src/main/java/dev/plex/services/impl/GameruleService.java deleted file mode 100644 index aa47898..0000000 --- a/src/main/java/dev/plex/services/impl/GameruleService.java +++ /dev/null @@ -1,92 +0,0 @@ -package dev.plex.services.impl; - -import dev.plex.Plex; -import dev.plex.services.AbstractService; -import dev.plex.util.PlexLog; -import org.bukkit.Bukkit; -import org.bukkit.World; - -import java.util.EnumMap; -import java.util.List; -import java.util.Map; - -public class GameruleService extends AbstractService { - private final Map rules = new EnumMap<>(GameRule.class); - - public GameruleService() { - super(false, false); - } - - public void setGameRule(GameRule gameRule, boolean value) { - setGameRule(gameRule, value, true); - } - - public void setGameRule(GameRule gameRule, boolean value, boolean doCommit) { - rules.put(gameRule, value); - if (doCommit) { - commitGameRules(); - } - } - - @SuppressWarnings("deprecation") - public void commitGameRules() { - List worlds = Bukkit.getWorlds(); - for (Map.Entry gameRuleEntry : rules.entrySet()) { - String gameRuleName = gameRuleEntry.getKey().getGameRuleName(); - String gameRuleValue = gameRuleEntry.getValue().toString(); - - for (World world : worlds) { - world.setGameRuleValue(gameRuleName, gameRuleValue); - if (gameRuleEntry.getKey() == GameRule.DO_DAYLIGHT_CYCLE && !gameRuleEntry.getValue()) { - long time = world.getTime(); - time -= time % 24000; - world.setTime(time + 24000 + 6000); - } - } - } - } - - @Override - public void run() { - for (GameRule gameRule : GameRule.values()) { - rules.put(gameRule, gameRule.getDefaultValue()); - PlexLog.log(gameRule.toString()); - } - } - - @Override - public int repeatInSeconds() { - return 0; - } - - public enum GameRule { - DO_FIRE_TICK("doFireTick", true), - MOB_GRIEFING("mobGriefing", true), - KEEP_INVENTORY("keepInventory", true), - DO_MOB_SPAWNING("doMobSpawning", true), - DO_MOB_LOOT("doMobLoot", true), - DO_TILE_DROPS("doTileDrops", true), - COMMAND_BLOCK_OUTPUT("commandBlockOutput", true), - NATURAL_REGENERATION("naturalRegeneration", true), - DO_DAYLIGHT_CYCLE("doDaylightCycle", true), - ANNOUNCE_ADVANCEMENTS("announceAdvancements", true), - SHOW_DEATH_MESSAGES("showDeathMessages", true), - SEND_COMMAND_FEEDBACK("sendCommandFeedback", true); - - private final String gameRuleName; - private final boolean defaultValue; - - GameRule(String gameRuleName, boolean defaultValue) { - this.gameRuleName = gameRuleName; - this.defaultValue = defaultValue; - } - - public String getGameRuleName() { - return gameRuleName; - } - - public boolean getDefaultValue() { - return defaultValue; - } - } -}