From 9064f4b1f2866165382a75bb9d207fc626cdbd87 Mon Sep 17 00:00:00 2001 From: Video Date: Sun, 20 Nov 2022 22:57:31 -0700 Subject: [PATCH] A few more changes - Cleans up /potion a tiny bit - Rewrites part of /moblimiter - Resolves FS-346 by cleaning up /setlever - Modernizes /opall a bit - Changes more components to use Adventure --- .../totalfreedommod/AntiNuke.java | 2 - .../totalfreedommod/AntiSpam.java | 20 ++--- .../totalfreedom/totalfreedommod/Muter.java | 6 +- .../command/Command_moblimiter.java | 51 +++++-------- .../command/Command_opall.java | 5 +- .../command/Command_potion.java | 74 ++++++------------- .../command/Command_setlever.java | 51 +++++-------- 7 files changed, 73 insertions(+), 136 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/AntiNuke.java b/src/main/java/me/totalfreedom/totalfreedommod/AntiNuke.java index 27c992a6..676517b1 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/AntiNuke.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/AntiNuke.java @@ -2,10 +2,8 @@ package me.totalfreedom.totalfreedommod; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; -import me.totalfreedom.totalfreedommod.util.FUtil; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/AntiSpam.java b/src/main/java/me/totalfreedom/totalfreedommod/AntiSpam.java index 3a553301..7cf69fdf 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/AntiSpam.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/AntiSpam.java @@ -2,9 +2,8 @@ package me.totalfreedom.totalfreedommod; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; -import me.totalfreedom.totalfreedommod.util.FSync; -import me.totalfreedom.totalfreedommod.util.FUtil; -import org.bukkit.ChatColor; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -70,17 +69,16 @@ public class AntiSpam extends FreedomService int time = count * minutes; playerdata.setMuted(true, time); - FSync.bcastMsg(String.format("%s has automatically been muted for %d minutes for spamming chat.", - player.getName(), - time), - ChatColor.RED); + server.broadcast(Component.text(player.getName()).append(Component.text(" has been automatically muted for ")) + .append(Component.text(time)).append(Component.text(" minutes for spamming chat.")) + .color(NamedTextColor.RED)); playerdata.resetMsgCount(); event.setCancelled(true); } else if (playerdata.incrementAndGetMsgCount() > MSG_PER_CYCLE / 2) { - FUtil.playerMsg(player, "Please refrain from spamming chat.", ChatColor.GRAY); + player.sendMessage(Component.text("Please refrain from spamming chat.", NamedTextColor.GRAY)); event.setCancelled(true); } @@ -96,7 +94,7 @@ public class AntiSpam extends FreedomService if (fPlayer.allCommandsBlocked()) { - FUtil.playerMsg(player, "Your commands have been blocked by an admin.", ChatColor.RED); + player.sendMessage(Component.text("Your commands have been blocked by an admin.", NamedTextColor.RED)); event.setCancelled(true); return; } @@ -108,7 +106,9 @@ public class AntiSpam extends FreedomService if (fPlayer.incrementAndGetMsgCount() > MSG_PER_CYCLE) { - FUtil.bcastMsg(player.getName() + " was automatically kicked for spamming commands.", ChatColor.RED); + server.broadcast(Component.text(player.getName()) + .append(Component.text(" was automatically kicked for spamming commands.")) + .color(NamedTextColor.RED)); plugin.ae.autoEject(player, "Kicked for spamming commands."); fPlayer.resetMsgCount(); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/Muter.java b/src/main/java/me/totalfreedom/totalfreedommod/Muter.java index b6b50d50..270b848c 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/Muter.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/Muter.java @@ -4,6 +4,8 @@ import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FSync; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.entity.Player; @@ -42,7 +44,7 @@ public class Muter extends FreedomService return; } - FSync.playerMsg(event.getPlayer(), ChatColor.RED + "You are muted."); + player.sendMessage(Component.text("You are muted.", NamedTextColor.RED)); event.setCancelled(true); } @@ -80,7 +82,7 @@ public class Muter extends FreedomService if (ConfigEntry.MUTED_BLOCKED_COMMANDS.getStringList().contains(cmdName)) { - player.sendMessage(ChatColor.RED + "That command is blocked while you are muted."); + player.sendMessage(Component.text("That command is blocked while you are muted.", NamedTextColor.RED)); event.setCancelled(true); return; } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_moblimiter.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_moblimiter.java index 575ccf8e..0bb2d6f8 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_moblimiter.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_moblimiter.java @@ -3,15 +3,15 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.GameRuleHandler; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.rank.Rank; +import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @CommandPermissions(level = Rank.ADMIN, source = SourceType.BOTH) -@CommandParameters(description = "Control mob limiting parameters.", usage = "/ |dragon|giant|ghast|slime>") +@CommandParameters(description = "Control mob limiting parameters.", usage = "/ | dragon | giant | ghast | slime>") public class Command_moblimiter extends FreedomCommand { - @Override public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { @@ -20,45 +20,29 @@ public class Command_moblimiter extends FreedomCommand return false; } - if (args[0].equalsIgnoreCase("on")) + switch (args[0].toLowerCase()) { - ConfigEntry.MOB_LIMITER_ENABLED.setBoolean(true); - } - else if (args[0].equalsIgnoreCase("off")) - { - ConfigEntry.MOB_LIMITER_ENABLED.setBoolean(false); - } - else if (args[0].equalsIgnoreCase("dragon")) - { - ConfigEntry.MOB_LIMITER_DISABLE_DRAGON.setBoolean(!ConfigEntry.MOB_LIMITER_DISABLE_DRAGON.getBoolean()); - } - else if (args[0].equalsIgnoreCase("giant")) - { - ConfigEntry.MOB_LIMITER_DISABLE_GIANT.setBoolean(!ConfigEntry.MOB_LIMITER_DISABLE_GIANT.getBoolean()); - } - else if (args[0].equalsIgnoreCase("slime")) - { - ConfigEntry.MOB_LIMITER_DISABLE_SLIME.setBoolean(!ConfigEntry.MOB_LIMITER_DISABLE_SLIME.getBoolean()); - } - else if (args[0].equalsIgnoreCase("ghast")) - { - ConfigEntry.MOB_LIMITER_DISABLE_GHAST.setBoolean(!ConfigEntry.MOB_LIMITER_DISABLE_GHAST.getBoolean()); - } - else - { - if (args.length < 2) + case "on" -> ConfigEntry.MOB_LIMITER_ENABLED.setBoolean(true); + case "off" -> ConfigEntry.MOB_LIMITER_ENABLED.setBoolean(false); + case "dragon" -> ConfigEntry.MOB_LIMITER_DISABLE_DRAGON.setBoolean(!ConfigEntry.MOB_LIMITER_DISABLE_DRAGON.getBoolean()); + case "giant" -> ConfigEntry.MOB_LIMITER_DISABLE_GIANT.setBoolean(!ConfigEntry.MOB_LIMITER_DISABLE_GIANT.getBoolean()); + case "slime" -> ConfigEntry.MOB_LIMITER_DISABLE_SLIME.setBoolean(!ConfigEntry.MOB_LIMITER_DISABLE_SLIME.getBoolean()); + case "ghast" -> ConfigEntry.MOB_LIMITER_DISABLE_GHAST.setBoolean(!ConfigEntry.MOB_LIMITER_DISABLE_GHAST.getBoolean()); + case "setmax" -> { - return false; - } + if (args.length < 2) + { + return false; + } - if (args[0].equalsIgnoreCase("setmax")) - { try { ConfigEntry.MOB_LIMITER_MAX.setInteger(Math.max(0, Math.min(2000, Integer.parseInt(args[1])))); } - catch (NumberFormatException ignored) + catch (Exception ex) { + msg("Invalid number: " + args[1], ChatColor.RED); + return true; } } } @@ -78,7 +62,6 @@ public class Command_moblimiter extends FreedomCommand } plugin.gr.setGameRule(GameRuleHandler.GameRule.DO_MOB_SPAWNING, !ConfigEntry.MOB_LIMITER_ENABLED.getBoolean()); - return true; } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java index 45ff1689..3cd46e01 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java @@ -10,13 +10,12 @@ import org.bukkit.entity.Player; @CommandParameters(description = "OP everyone on the server.", usage = "/") public class Command_opall extends FreedomCommand { - @Override public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { FUtil.adminAction(sender.getName(), "Opping all players on the server", false); - for (Player player : server.getOnlinePlayers()) + server.getOnlinePlayers().forEach(player -> { if (!player.isOp()) { @@ -28,7 +27,7 @@ public class Command_opall extends FreedomCommand { player.recalculatePermissions(); } - } + }); return true; } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_potion.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_potion.java index ba546a27..d8db4b9b 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_potion.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_potion.java @@ -6,7 +6,6 @@ import java.util.Collections; import java.util.List; import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.util.FUtil; -import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -21,8 +20,6 @@ import org.bukkit.potion.PotionEffectType; aliases = "effect") public class Command_potion extends FreedomCommand { - - @SuppressWarnings("deprecation") @Override public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { @@ -32,15 +29,8 @@ public class Command_potion extends FreedomCommand { if (args[0].equalsIgnoreCase("list")) { - List potionEffectTypeNames = new ArrayList<>(); - for (PotionEffectType potion_effect_type : PotionEffectType.values()) - { - if (potion_effect_type != null) - { - potionEffectTypeNames.add(potion_effect_type.getName()); - } - } - msg("Potion effect types: " + StringUtils.join(potionEffectTypeNames, ", "), ChatColor.AQUA); + msg("Potion effect types: " + FUtil.listToString(Arrays.stream(PotionEffectType.values()) + .map(PotionEffectType::getName).toList()), ChatColor.AQUA); } else if (args[0].equalsIgnoreCase("clearall")) { @@ -50,14 +40,9 @@ public class Command_potion extends FreedomCommand return true; } - FUtil.adminAction(sender.getName(), "Cleared all potion effects from all players", true); - for (Player target : server.getOnlinePlayers()) - { - for (PotionEffect potion_effect : target.getActivePotionEffects()) - { - target.removePotionEffect(potion_effect.getType()); - } - } + FUtil.adminAction(sender.getName(), "Clearing all potion effects from all players", true); + server.getOnlinePlayers().forEach(target -> target.getActivePotionEffects().forEach(effect -> + target.removePotionEffect(effect.getType()))); } } @@ -86,7 +71,7 @@ public class Command_potion extends FreedomCommand if (target == null) { - msg(FreedomCommand.PLAYER_NOT_FOUND, ChatColor.RED); + msg(PLAYER_NOT_FOUND, ChatColor.RED); return true; } @@ -95,7 +80,8 @@ public class Command_potion extends FreedomCommand target.removePotionEffect(potion_effect.getType()); } - msg("Cleared all active potion effects " + (!target.equals(playerSender) ? "from player " + target.getName() + "." : "from yourself."), ChatColor.AQUA); + msg("Cleared all active potion effects " + (!target.equals(playerSender) ? "from player " + + target.getName() + "." : "from yourself."), ChatColor.AQUA); } break; } @@ -132,44 +118,41 @@ public class Command_potion extends FreedomCommand } } - PotionEffectType potion_effect_type = PotionEffectType.getByName(args[1]); - if (potion_effect_type == null) + PotionEffectType effectType = PotionEffectType.getByName(args[1]); + if (effectType == null) { - msg("Invalid potion effect type.", ChatColor.AQUA); + msg("Invalid potion effect: " + args[1], ChatColor.AQUA); return true; } int duration; try { - duration = Integer.parseInt(args[2]); - duration = Math.min(duration, 100000); + duration = Math.min(Integer.parseInt(args[2]), 100000); } catch (NumberFormatException ex) { - msg("Invalid potion duration.", ChatColor.RED); + msg("Invalid duration: " + args[2], ChatColor.RED); return true; } int amplifier; try { - amplifier = Integer.parseInt(args[3]); - amplifier = Math.min(amplifier, 100000); + amplifier = Math.min(Integer.parseInt(args[3]), 100000); } catch (NumberFormatException ex) { - msg("Invalid potion amplifier.", ChatColor.RED); + msg("Invalid potion amplifier: " + args[3], ChatColor.RED); return true; } - PotionEffect new_effect = potion_effect_type.createEffect(duration, amplifier); - target.addPotionEffect(new_effect, true); - msg( - "Added potion effect: " + new_effect.getType().getName() - + ", Duration: " + new_effect.getDuration() - + ", Amplifier: " + new_effect.getAmplifier() - + (!target.equals(playerSender) ? " to player " + target.getName() + "." : " to yourself."), ChatColor.AQUA); + PotionEffect new_effect = effectType.createEffect(duration, amplifier); + target.addPotionEffect(new_effect); + msg("Added potion effect: " + new_effect.getType().getName() + + ", Duration: " + new_effect.getDuration() + + ", Amplifier: " + new_effect.getAmplifier() + + (!target.equals(playerSender) ? " to player " + target.getName() + "." : " to yourself."), ChatColor.AQUA); } break; } @@ -206,7 +189,7 @@ public class Command_potion extends FreedomCommand } else if (args[0].equalsIgnoreCase("add")) { - return getAllPotionTypes(); + return Arrays.stream(PotionEffectType.values()).map(PotionEffectType::getName).toList(); } } case 3 -> @@ -237,17 +220,4 @@ public class Command_potion extends FreedomCommand return Collections.emptyList(); } - - public List getAllPotionTypes() - { - List types = new ArrayList<>(); - for (PotionEffectType potionEffectType : PotionEffectType.values()) - { - if (potionEffectType != null) - { - types.add(potionEffectType.getName()); - } - } - return types; - } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_setlever.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_setlever.java index b8430169..2df3c253 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_setlever.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_setlever.java @@ -1,7 +1,6 @@ package me.totalfreedom.totalfreedommod.command; -import java.util.List; - +import java.util.Optional; import me.totalfreedom.totalfreedommod.rank.Rank; import org.bukkit.Location; import org.bukkit.Material; @@ -15,10 +14,9 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @CommandPermissions(level = Rank.NON_OP, source = SourceType.BOTH) -@CommandParameters(description = "Set the on/off state of the lever at position x, y, z in world 'worldname'.", usage = "/ ") +@CommandParameters(description = "Set the on/off state of the lever at position x, y, z in world 'worldname'.", usage = "/ ") public class Command_setlever extends FreedomCommand { - @Override public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { @@ -33,50 +31,38 @@ public class Command_setlever extends FreedomCommand x = Double.parseDouble(args[0]); y = Double.parseDouble(args[1]); z = Double.parseDouble(args[2]); + + // Shows the "Invalid coordinates" message without having to do boilerplate bullshit + if (Math.abs(x) > 29999998 || Math.abs(y) > 29999998 || Math.abs(z) > 29999998) + { + throw new IllegalArgumentException(); + } } - catch (NumberFormatException ex) + catch (IllegalArgumentException ex) { msg("Invalid coordinates."); return true; } - if (x > 29999998 || x < -29999998 || y > 29999998 || y < -29999998 || z > 29999998 || z < -29999998) + Optional optionalWorld = server.getWorlds().stream().filter(world -> + world.getName().equalsIgnoreCase(args[3])).findAny(); + + if (optionalWorld.isEmpty()) { - msg("Coordinates cannot be larger than 29999998 or smaller than -29999998 blocks."); + msg("Invalid world: " + args[3]); return true; } - World world = null; - final String needleWorldName = args[3].trim(); - final List worlds = server.getWorlds(); - for (final World testWorld : worlds) - { - if (testWorld.getName().trim().equalsIgnoreCase(needleWorldName)) - { - world = testWorld; - break; - } - } - - if (world == null) - { - msg("Invalid world name."); - return true; - } - - final Location leverLocation = new Location(world, x, y, z); - - final boolean leverOn = (args[4].trim().equalsIgnoreCase("on") || args[4].trim().equalsIgnoreCase("1")); - + final Location leverLocation = new Location(optionalWorld.get(), x, y, z); final Block targetBlock = leverLocation.getBlock(); if (targetBlock.getType() == Material.LEVER) { BlockState state = targetBlock.getState(); BlockData data = state.getBlockData(); - Switch caster = (Switch)data; + Switch caster = (Switch) data; - caster.setPowered(leverOn); + caster.setPowered(args[4].trim().equalsIgnoreCase("on") || args[4].trim().equalsIgnoreCase("1")); state.setBlockData(data); state.update(); @@ -84,8 +70,7 @@ public class Command_setlever extends FreedomCommand } else { - msg("Target block " + targetBlock + " is not a lever."); - return true; + msg("That block isn't a lever."); } return true;