Skip to content

Commit

Permalink
Add rename command, refactor locale
Browse files Browse the repository at this point in the history
  • Loading branch information
RhythmicSys committed Dec 20, 2024
1 parent 6ae4e68 commit 60a5a32
Show file tree
Hide file tree
Showing 26 changed files with 492 additions and 513 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

## Commands

| Command | Permission | Description | Usage | Aliases |
|-----------|------------------------------|------------------------------|-------------------------------------------------------------|---------|
| flyspeed | adminhax.commands.speed.fly | change your fly speed | /flyspeed [set \| get \|reset] [value \|player] <value> | fs |
| walkspeed | adminhax.commands.speed.walk | change your walk speed | /walkspeed [set \| get \|reset] [value \|player] <value> | ws |
| fly | adminhax.commands.fly | fly | /fly <player> | |
| feed | adminhax.commands.feed | fill your hunger bar | /feed <player> | |
| heal | adminhax.commands.heal | heal | /heal <player> | |
| repair | adminhax.commands.repair | repair items | /repair [hand \| hotbar \|inventory \|armor \|all] <player> | |
| godmode | adminhax.commands.godmode | toggle godmode/invincibility | /godmode [get \| toggle] <player> | |
| Command | Permission | Description | Usage | Aliases |
|----------------|------------------------------|----------------------------------------|-------------------------------------------------------------|--------------|
| flyspeed | adminhax.commands.speed.fly | change your fly speed | /flyspeed [set \| get \|reset] [value \|player] <value> | fs |
| walkspeed | adminhax.commands.speed.walk | change your walk speed | /walkspeed [set \| get \|reset] [value \|player] <value> | ws |
| fly | adminhax.commands.fly | fly | /fly <player> | |
| feed | adminhax.commands.feed | fill your hunger bar | /feed <player> | |
| heal | adminhax.commands.heal | heal | /heal <player> | |
| repair | adminhax.commands.repair | repair items | /repair [hand \| hotbar \|inventory \|armor \|all] <player> | |
| godmode | adminhax.commands.godmode | toggle godmode/invincibility | /godmode [get \| toggle] <player> | |
| broadcast | adminhax.commands.broadcast | Send a message to the whole server | /broadcast <message> | broadcastmsg |
| adminhaxreload | adminhax.reload | reloads the AdminHax config and locale | /adminhaxreload | |

## Permissions

Expand Down Expand Up @@ -42,5 +44,7 @@
| adminhax.commands.repair.all | op | repair everything in your inventory and armor slots |
| adminhax.commands.godmode | op | be invincible |
| adminhax.commands.godmode.get | op | see what your godmode status is |
| adminhax.commands.broadcast | op | broadcast a message server-wide |
| adminhax.reload | op | reload the plugin configuration |

---
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>simplexity</groupId>
<artifactId>AdminHax</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<packaging>jar</packaging>

<name>AdminHax</name>
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/simplexity/adminhax/AdminHax.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
import org.bukkit.plugin.java.JavaPlugin;
import simplexity.adminhax.commands.basic.BroadcastMsg;
import simplexity.adminhax.commands.basic.ReloadPlugin;
import simplexity.adminhax.commands.withargs.GodMode;
import simplexity.adminhax.commands.basic.RenameItem;
import simplexity.adminhax.commands.hax.Feed;
import simplexity.adminhax.commands.hax.Fly;
import simplexity.adminhax.commands.withargs.Repair;
import simplexity.adminhax.commands.speed.FlySpeed;
import simplexity.adminhax.commands.hax.Heal;
import simplexity.adminhax.commands.speed.FlySpeed;
import simplexity.adminhax.commands.speed.WalkSpeed;
import simplexity.adminhax.commands.withargs.GodMode;
import simplexity.adminhax.commands.withargs.Repair;
import simplexity.adminhax.config.ConfigHandler;
import simplexity.adminhax.config.LocaleHandler;
import simplexity.adminhax.listeners.FlyListeners;
import simplexity.adminhax.util.Permissions;

public final class AdminHax extends JavaPlugin {

Expand All @@ -24,18 +26,21 @@ public final class AdminHax extends JavaPlugin {
@Override
public void onEnable() {
instance = this;
LocaleHandler.getInstance().loadLocale();
LocaleHandler.getInstance().reloadLocale();
this.saveDefaultConfig();
getConfig().options().copyDefaults(true);
saveConfig();
ConfigHandler.getInstance().reloadConfigValues();
this.getCommand("flyspeed").setExecutor(new FlySpeed(Util.FLY_SPEED_PERMISSION, Util.FLY_SPEED_OTHER_PERMISSION, 0.1f));
this.getCommand("walkspeed").setExecutor(new WalkSpeed(Util.WALK_SPEED_PERMISSION, Util.WALK_SPEED_OTHER_PERMISSION, 0.2f));
this.getCommand("fly").setExecutor(new Fly(Util.FLY_PERMISSION, Util.FLY_OTHER_PERMISSION));
this.getCommand("feed").setExecutor(new Feed(Util.FEED_PERMISSION, Util.FEED_OTHER_PERMISSION));
this.getCommand("heal").setExecutor(new Heal(Util.HEAL_PERMISSION, Util.HEAL_OTHER_PERMISSION));
this.getCommand("repair").setExecutor(new Repair(Util.REPAIR_HAND_PERMISSION, "hand"));
this.getCommand("godmode").setExecutor(new GodMode(Util.GODMODE_PERMISSION, "toggle"));
this.getCommand("flyspeed").setExecutor(new FlySpeed(Permissions.FLY_SPEED_PERMISSION, Permissions.FLY_SPEED_OTHER_PERMISSION, 0.1f));
this.getCommand("walkspeed").setExecutor(new WalkSpeed(Permissions.WALK_SPEED_PERMISSION, Permissions.WALK_SPEED_OTHER_PERMISSION, 0.2f));
this.getCommand("fly").setExecutor(new Fly(Permissions.FLY_PERMISSION, Permissions.FLY_OTHER_PERMISSION));
this.getCommand("feed").setExecutor(new Feed(Permissions.FEED_PERMISSION, Permissions.FEED_OTHER_PERMISSION));
this.getCommand("heal").setExecutor(new Heal(Permissions.HEAL_PERMISSION, Permissions.HEAL_OTHER_PERMISSION));
this.getCommand("repair").setExecutor(new Repair(Permissions.REPAIR_HAND_PERMISSION, "hand"));
this.getCommand("godmode").setExecutor(new GodMode(Permissions.GODMODE_PERMISSION, "toggle"));
this.getCommand("broadcastmsg").setExecutor(new BroadcastMsg());
this.getCommand("adminhaxreload").setExecutor(new ReloadPlugin());
this.getCommand("rename").setExecutor(new RenameItem());
this.getServer().getPluginManager().registerEvents(new FlyListeners(), this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import simplexity.adminhax.AdminHax;
import simplexity.adminhax.config.LocaleHandler;
import simplexity.adminhax.config.Message;

public class BroadcastMsg implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
if (args.length == 0) {
sender.sendRichMessage(LocaleHandler.getInstance().getInvalidCommand());
sender.sendRichMessage(Message.ERROR_INVALID_COMMAND.getMessage());
return false;
}
StringBuilder msg = new StringBuilder();
for (String arg : args) {
msg.append(arg).append(" ");
}
String message = msg.toString().trim();
message = LocaleHandler.getInstance().getBroadcastServerPrefix() + message;
message = Message.BROADCAST_SERVER_PREFIX.getMessage() + message;
AdminHax.getInstance().getServer().sendMessage(AdminHax.getMiniMessage().deserialize(message));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import org.jetbrains.annotations.NotNull;
import simplexity.adminhax.config.ConfigHandler;
import simplexity.adminhax.config.LocaleHandler;
import simplexity.adminhax.config.Message;

public class ReloadPlugin implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
ConfigHandler.getInstance().reloadConfigValues();
LocaleHandler.getInstance().loadLocale();
sender.sendRichMessage(LocaleHandler.getInstance().getConfigReloaded());
LocaleHandler.getInstance().reloadLocale();
sender.sendRichMessage(Message.PLUGIN_MESSAGES_CONFIG_RELOADED.getMessage());
return true;
}
}
67 changes: 67 additions & 0 deletions src/main/java/simplexity/adminhax/commands/basic/RenameItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package simplexity.adminhax.commands.basic;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import simplexity.adminhax.AdminHax;
import simplexity.adminhax.config.ConfigHandler;
import simplexity.adminhax.config.Message;
import simplexity.adminhax.util.Permissions;
import simplexity.adminhax.util.RenamePermission;

public class RenameItem implements CommandExecutor {

private final MiniMessage miniMessage = AdminHax.getMiniMessage();

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendRichMessage(Message.ERROR_MUST_BE_PLAYER.getMessage());
return false;
}
String renameString = String.join(" ", args);
renameString = renameString.trim();
String strippedString = miniMessage.stripTags(renameString);
if ( strippedString.length() > ConfigHandler.getInstance().getMaxRenameCharacters() && !sender.hasPermission(Permissions.BYPASS_RENAME_LENGTH)) {
player.sendRichMessage(Message.ERROR_NAME_TOO_LONG.getMessage());
return false;
}
ItemStack heldItem = player.getInventory().getItemInMainHand();
if (heldItem.isEmpty() || heldItem.getType().isEmpty()) {
player.sendRichMessage(Message.ERROR_MUST_HOLD_ITEM.getMessage());
return false;
}
Component newName = parsedName(player, renameString);
setItemName(newName, heldItem);
player.sendRichMessage(Message.RENAME_SUCCESSFUL.getMessage(),
Placeholder.unparsed("item", heldItem.translationKey()),
Placeholder.component("name", newName));
return true;
}
//Stolen from https://github.com/YouHaveTrouble/JustChat @YouHaveTrouble - I'm just gonna continue using this over and over
private Component parsedName(Player player, String name) {
TagResolver.Builder tagResolver = TagResolver.builder();
for (RenamePermission permission : RenamePermission.values()) {
if (player.hasPermission(permission.getPermission())) {
tagResolver.resolver(permission.getTagResolver());
}
}
MiniMessage nameParser = MiniMessage.builder().tags(tagResolver.build()).build();
return nameParser.deserialize(name).decoration(TextDecoration.ITALIC, false);
}

private void setItemName(Component name, ItemStack itemStack) {
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(name);
itemStack.setItemMeta(itemMeta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.jetbrains.annotations.NotNull;
import simplexity.adminhax.Util;
import simplexity.adminhax.config.LocaleHandler;
import simplexity.adminhax.util.Util;
import simplexity.adminhax.config.Message;

public abstract class AbstractHaxCommand implements CommandExecutor {

Expand All @@ -24,7 +24,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
if (args.length > 0) {
Player player = Util.getPlayerFromArgs(adminPermission, sender, args);
if (player == null) {
Util.sendUserMessage(sender, LocaleHandler.getInstance().getInvalidPlayer());
Util.sendUserMessage(sender, Message.ERROR_INVALID_PLAYER.getMessage());
return false;
}
runLogic(player, sender, args, true);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/simplexity/adminhax/commands/hax/Feed.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import simplexity.adminhax.Util;
import simplexity.adminhax.config.LocaleHandler;
import simplexity.adminhax.util.Util;
import simplexity.adminhax.config.Message;

public class Feed extends AbstractHaxCommand {
public Feed(Permission basicPermission, Permission adminPermission) {
Expand All @@ -21,12 +21,12 @@ public void runLogic(Player player, CommandSender sender, String[] args, boolean

@Override
public void sendSelfMessage(Player player) {
Util.sendUserMessage(player, LocaleHandler.getInstance().getFeedSelf());
Util.sendUserMessage(player, Message.FEED_SELF.getMessage());
}

@Override
public void sendOtherMessage(CommandSender sender, Player player) {
Util.sendUserMessage(sender, LocaleHandler.getInstance().getFeedOther(), null, player);
Util.sendUserMessage(sender, Message.FEED_OTHER.getMessage(), null, player);
}

}
14 changes: 7 additions & 7 deletions src/main/java/simplexity/adminhax/commands/hax/Fly.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.bukkit.permissions.Permission;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import simplexity.adminhax.Util;
import simplexity.adminhax.config.LocaleHandler;
import simplexity.adminhax.util.Util;
import simplexity.adminhax.config.Message;

public class Fly extends AbstractHaxCommand {

Expand All @@ -26,22 +26,22 @@ public void runLogic(Player player, CommandSender sender, String[] args, boolean
@Override
public void sendSelfMessage(Player player) {
String value = getValueString(player);
Util.sendUserMessage(player, LocaleHandler.getInstance().getFlySetOwn(), value);
Util.sendUserMessage(player, Message.FLY_SET_OWN.getMessage(), value);
}

@Override
public void sendOtherMessage(CommandSender sender, Player player) {
String value = getValueString(player);
Util.sendUserMessage(player, LocaleHandler.getInstance().getFlySetOwn(), value);
Util.sendUserMessage(sender, LocaleHandler.getInstance().getFlySetOther(), value, player);
Util.sendUserMessage(player, Message.FLY_SET_OWN.getMessage(), value);
Util.sendUserMessage(sender, Message.FLY_SET_OTHER.getMessage(), value, player);
}

private String getValueString(Player player){
String value;
if (isFlyEnabled(player)) {
value = LocaleHandler.getInstance().getEnabled();
value = Message.INSERT_ENABLED.getMessage();
} else {
value = LocaleHandler.getInstance().getDisabled();
value = Message.INSERT_DISABLED.getMessage();
}
return value;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/simplexity/adminhax/commands/hax/Heal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import simplexity.adminhax.Util;
import simplexity.adminhax.config.LocaleHandler;
import simplexity.adminhax.util.Util;
import simplexity.adminhax.config.Message;

public class Heal extends AbstractHaxCommand {

Expand All @@ -22,12 +22,12 @@ public void runLogic(Player player, CommandSender sender, String[] args, boolean

@Override
public void sendSelfMessage(Player player) {
Util.sendUserMessage(player, LocaleHandler.getInstance().getHealSelf());
Util.sendUserMessage(player, Message.HEAL_SELF.getMessage());
}

@Override
public void sendOtherMessage(CommandSender sender, Player player) {
Util.sendUserMessage(sender, LocaleHandler.getInstance().getHealOther(), null, player);
Util.sendUserMessage(sender, Message.HEAL_OTHER.getMessage(), null, player);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.bukkit.permissions.Permission;
import org.jetbrains.annotations.NotNull;
import simplexity.adminhax.AdminHax;
import simplexity.adminhax.Util;
import simplexity.adminhax.util.Util;
import simplexity.adminhax.config.ConfigHandler;
import simplexity.adminhax.config.LocaleHandler;
import simplexity.adminhax.config.Message;

import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -65,23 +65,23 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

public void setSpeedLogic(CommandSender sender, Player player, String[] args, Float speed) {
if (speed == null) {
Util.sendUserMessage(sender, LocaleHandler.getInstance().getInvalidNumber());
Util.sendUserMessage(sender, Message.ERROR_INVALID_NUMBER.getMessage());
return;
}
setByOther = LocaleHandler.getInstance().getSpeedSetByOther();
setOther = LocaleHandler.getInstance().getSpeedSetOther();
setOwn = LocaleHandler.getInstance().getSpeedSetOwn();
setByOther = Message.SPEED_SET_BY_OTHER.getMessage();
setOther = Message.SPEED_SET_OTHER.getMessage();
setOwn = Message.SPEED_SET_OWN.getMessage();
}

public void resetSpeedLogic(CommandSender sender, Player player, String[] args) {
resetByOther = LocaleHandler.getInstance().getSpeedResetByOther();
resetOther = LocaleHandler.getInstance().getSpeedResetOther();
resetOwn = LocaleHandler.getInstance().getSpeedResetOwn();
resetByOther = Message.SPEED_RESET_BY_OTHER.getMessage();
resetOther = Message.SPEED_RESET_OTHER.getMessage();
resetOwn = Message.SPEED_RESET_OWN.getMessage();
}

public void getSpeedLogic(CommandSender sender, Player player, String[] args) {
getOther = LocaleHandler.getInstance().getSpeedGetOther();
getOwn = LocaleHandler.getInstance().getSpeedGetOwn();
getOther = Message.SPEED_GET_OTHER.getMessage();
getOwn = Message.SPEED_GET_OWN.getMessage();
}

private void setSenderHasBasicPermission(CommandSender sender) {
Expand Down Expand Up @@ -170,15 +170,15 @@ public void sendOwnMessage(String speedtype, Player player, String speed, String
}

private void sendInvalidCommandMessage(CommandSender sender) {
Util.sendUserMessage(sender, LocaleHandler.getInstance().getInvalidCommand());
Util.sendUserMessage(sender, Message.ERROR_INVALID_COMMAND.getMessage());
}

private void sendInvalidPlayerMessage(CommandSender sender) {
Util.sendUserMessage(sender, LocaleHandler.getInstance().getInvalidPlayer());
Util.sendUserMessage(sender, Message.ERROR_INVALID_PLAYER.getMessage());
}

public void sendOutOfRangeMessage(CommandSender sender) {
sender.sendRichMessage(LocaleHandler.getInstance().getNotInRange(),
sender.sendRichMessage(Message.ERROR_NOT_IN_RANGE.getMessage(),
Placeholder.parsed("min", String.valueOf(ConfigHandler.getInstance().getMinFlySpeed())),
Placeholder.parsed("max", String.valueOf(ConfigHandler.getInstance().getMaxFlySpeed())));
}
Expand Down
Loading

0 comments on commit 60a5a32

Please sign in to comment.