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
This commit is contained in:
Video 2022-11-20 22:57:31 -07:00
parent 2698cbf46d
commit 9064f4b1f2
7 changed files with 73 additions and 136 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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;
}

View File

@ -3,6 +3,7 @@ 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;
@ -11,7 +12,6 @@ import org.bukkit.entity.Player;
@CommandParameters(description = "Control mob limiting parameters.", usage = "/<command> <on | off | setmax <count> | 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
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" ->
{
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;
}
}

View File

@ -10,13 +10,12 @@ import org.bukkit.entity.Player;
@CommandParameters(description = "OP everyone on the server.", usage = "/<command>")
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;
}

View File

@ -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<String> 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,41 +118,38 @@ 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()
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);
@ -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<String> getAllPotionTypes()
{
List<String> types = new ArrayList<>();
for (PotionEffectType potionEffectType : PotionEffectType.values())
{
if (potionEffectType != null)
{
types.add(potionEffectType.getName());
}
}
return types;
}
}

View File

@ -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;
@ -18,7 +17,6 @@ import org.bukkit.entity.Player;
@CommandParameters(description = "Set the on/off state of the lever at position x, y, z in world 'worldname'.", usage = "/<command> <x> <y> <z> <worldname> <on | off>")
public class Command_setlever extends FreedomCommand
{
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
@ -33,41 +31,29 @@ 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<World> 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<World> 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)
@ -76,7 +62,7 @@ public class Command_setlever extends FreedomCommand
BlockData data = state.getBlockData();
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;