Skip to content

Commit

Permalink
0.3.18 Update lang.yml & Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
TWME-TW committed Mar 20, 2024
1 parent 9c0754e commit 78562bf
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 189 deletions.
8 changes: 5 additions & 3 deletions src/main/java/dev/twme/debugstickpro/DebugStickPro.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package dev.twme.debugstickpro;

import dev.twme.debugstickpro.blockdatautil.BlockDataSeparater;
import dev.twme.debugstickpro.commands.MainCommandTabComplete;
import dev.twme.debugstickpro.commands.MainCommands;
import dev.twme.debugstickpro.configs.ConfigFile;
import dev.twme.debugstickpro.configs.ConfigLoader;
import dev.twme.debugstickpro.configs.LangLoader;
import dev.twme.debugstickpro.display.ActionBarDisplayTask;
import dev.twme.debugstickpro.hook.CoreProtectUtil;
import dev.twme.debugstickpro.mode.freeze.FreezeBlockManager;
import dev.twme.debugstickpro.commands.MainCommands;
import dev.twme.debugstickpro.listeners.*;
import dev.twme.debugstickpro.mode.freeze.FreezeBlockManager;
import dev.twme.debugstickpro.util.Log;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
Expand All @@ -23,7 +24,7 @@ public final class DebugStickPro extends JavaPlugin {
public static final int ConfigVersion = 3;

// TODO: 如果更改此值,請確保在 lang.yml 中也更改了相應的值
public static final int LangVersion = 2;
public static final int LangVersion = 3;

@Override
public void onEnable() {
Expand Down Expand Up @@ -59,6 +60,7 @@ public void onDisable() {

private void registerCommands() {
this.getCommand("debugstickpro").setExecutor(new MainCommands());
this.getCommand("debugstickpro").setTabCompleter(new MainCommandTabComplete());
}

private void registerListeners() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dev.twme.debugstickpro.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MainCommandTabComplete implements TabCompleter {
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if (!(commandSender instanceof Player)) {
return null;
}
ArrayList<String> list = new ArrayList<>();
Player player = (Player) commandSender;
if (strings.length == 1) {
if (player.hasPermission("debugstickpro.help")) {
list.add("help");
}
if (player.hasPermission("debugstickpro.reload")) {
list.add("reload");
}
if (player.hasPermission("debugstickpro.give")) {
list.add("give");
}
if (player.hasPermission("debugstickpro.mode")) {
list.add("mode");
}
return list;
}

if (strings.length == 2) {
if (strings[0].equalsIgnoreCase("give")) {
return null;
}

if (strings[0].equalsIgnoreCase("mode")) {
if (player.hasPermission("debugstickpro.mode")) {
list.add("classic");
}
if (player.hasPermission("debugstickpro.mode.copy")) {
list.add("copy");
}
if (player.hasPermission("debugstickpro.mode.freeze")) {
list.add("freeze");
}
return list;
}
}
return list;
}
}
181 changes: 14 additions & 167 deletions src/main/java/dev/twme/debugstickpro/commands/MainCommands.java
Original file line number Diff line number Diff line change
@@ -1,197 +1,44 @@
package dev.twme.debugstickpro.commands;

import dev.twme.debugstickpro.DebugStickPro;
import dev.twme.debugstickpro.commands.subcommand.GiveCommand;
import dev.twme.debugstickpro.commands.subcommand.HelpCommand;
import dev.twme.debugstickpro.commands.subcommand.ModeCommand;
import dev.twme.debugstickpro.commands.subcommand.ReloadCommand;
import dev.twme.debugstickpro.configs.LangFile;
import dev.twme.debugstickpro.events.PlayerChangeDebugStickModeEvent;
import dev.twme.debugstickpro.playerdata.DebugStickMode;
import dev.twme.debugstickpro.playerdata.PlayerData;
import dev.twme.debugstickpro.playerdata.PlayerDataManager;
import dev.twme.debugstickpro.util.DebugStickItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class MainCommands implements CommandExecutor , TabCompleter {
public class MainCommands implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {

MiniMessage mm = MiniMessage.miniMessage();

if (!(commandSender instanceof Player)){
if (!(commandSender instanceof Player)) {
MiniMessage mm = MiniMessage.miniMessage();
Component parsed = mm.deserialize(LangFile.CommandsMessages.YouAreNotPlayer);
commandSender.sendMessage(parsed);
return true;
}

Player player = (Player) commandSender;

if (strings.length == 0 || strings[0].equalsIgnoreCase("help")){
Component parsed = mm.deserialize(LangFile.CommandsMessages.Help.Title);
player.sendMessage(parsed);
Component component = mm.deserialize(LangFile.CommandsMessages.Help.Usage);
player.sendMessage(component);
Component component1 = mm.deserialize(LangFile.CommandsMessages.Help.Description);
player.sendMessage(component1);
return true;
if (strings.length == 0 || strings[0].equalsIgnoreCase("help")) {
return HelpCommand.onHelpCommand(player, strings);
}
if (strings[0].equalsIgnoreCase("reload")){
DebugStickPro.getInstance().onReload();
Component parsed = mm.deserialize(LangFile.CommandsMessages.Reload.Success);
player.sendMessage(parsed);
return true;
if (strings[0].equalsIgnoreCase("reload")) {
return ReloadCommand.onReloadCommand(player, strings);
}
if (strings[0].equalsIgnoreCase("give")){
if (strings.length == 1) {
player.getInventory().addItem(DebugStickItem.getDebugStickItem());

Component parsed = mm.deserialize(LangFile.CommandsMessages.Give.Success.replace("%player%", player.getName()));
player.sendMessage(parsed);
if (DebugStickItem.checkPlayer(player)) {
PlayerDataManager.addPlayerToDisplayList(player.getUniqueId());
}
return true;
} else {
Player onlinePlayer = Bukkit.getPlayerExact(strings[1]);
if (onlinePlayer == null){
Component parsed = mm.deserialize(LangFile.CommandsMessages.Give.NoPlayer);
player.sendMessage(parsed);
return true;
} else {
onlinePlayer.getInventory().addItem(DebugStickItem.getDebugStickItem());
Component parsed = mm.deserialize(LangFile.CommandsMessages.Give.Success.replace("%player%", player.getName()));
player.sendMessage(parsed);
if (DebugStickItem.checkPlayer(onlinePlayer)) {
PlayerDataManager.addPlayerToDisplayList(onlinePlayer.getUniqueId());
}
return true;
}
}
if (strings[0].equalsIgnoreCase("give")) {
return GiveCommand.onGiveCommand(player, strings);
}

if (strings[0].equalsIgnoreCase("mode")) {
if (!player.hasPermission("debugstickpro.mode")) {
Component parsed = mm.deserialize(LangFile.CommandsMessages.NoPermission);
player.sendMessage(parsed);
return true;
}
if (strings.length == 1) {
Component parsed = mm.deserialize(LangFile.CommandsMessages.Mode.Usage);
player.sendMessage(parsed);
return true;
} else {
if (strings[1].equalsIgnoreCase("classic")) {
PlayerData playerData = PlayerDataManager.getPlayerData(player.getUniqueId());

if (modeChangeEventCancelled(player.getUniqueId(), playerData.getDebugStickMode(), DebugStickMode.CLASSIC)) {
return true;
}

PlayerDataManager.setPlayerData(player.getUniqueId(),playerData.setDebugStickMode(DebugStickMode.CLASSIC));
Component parsed = mm.deserialize(LangFile.CommandsMessages.Mode.SuccessSetToClassic);
player.sendMessage(parsed);
return true;
}
if (strings[1].equalsIgnoreCase("copy")) {
if (player.hasPermission("debugstickpro.mode.copy")) {
PlayerData playerData = PlayerDataManager.getPlayerData(player.getUniqueId());

if (modeChangeEventCancelled(player.getUniqueId(), playerData.getDebugStickMode(), DebugStickMode.COPY)) {
return true;
}

PlayerDataManager.setPlayerData(player.getUniqueId(),playerData.setDebugStickMode(DebugStickMode.COPY));
Component parsed = mm.deserialize(LangFile.CommandsMessages.Mode.SuccessSetToCopy);
player.sendMessage(parsed);
return true;
} else {
Component parsed = mm.deserialize(LangFile.CommandsMessages.NoPermission);
player.sendMessage(parsed);
return true;
}
}
if (strings[1].equalsIgnoreCase("freeze")) {
if (player.hasPermission("debugstickpro.mode.freeze")) {
PlayerData playerData = PlayerDataManager.getPlayerData(player.getUniqueId());

if (modeChangeEventCancelled(player.getUniqueId(), playerData.getDebugStickMode(), DebugStickMode.FREEZE)) {
return true;
}

PlayerDataManager.setPlayerData(player.getUniqueId(),PlayerDataManager.getPlayerData(player.getUniqueId()).setDebugStickMode(DebugStickMode.FREEZE));
Component parsed = mm.deserialize(LangFile.CommandsMessages.Mode.SuccessSetToFreeze);
player.sendMessage(parsed);
return true;
} else {
Component parsed = mm.deserialize(LangFile.CommandsMessages.NoPermission);
player.sendMessage(parsed);
return true;
}
}
}
return ModeCommand.onModeCommand(player, strings);
}
return false;
}

@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if (!(commandSender instanceof Player)){
return null;
}
ArrayList<String> list = new ArrayList<>();
Player player = (Player) commandSender;
if (strings.length == 1){
if (player.hasPermission("debugstickpro.help")){
list.add("help");
}
if (player.hasPermission("debugstickpro.reload")){
list.add("reload");
}
if (player.hasPermission("debugstickpro.give")){
list.add("give");
}
if (player.hasPermission("debugstickpro.mode")){
list.add("mode");
}
return list;
}

if (strings.length == 2){
if (strings[0].equalsIgnoreCase("give")){
return null;
}

if (strings[0].equalsIgnoreCase("mode")){
if (player.hasPermission("debugstickpro.mode")){
list.add("classic");
}
if (player.hasPermission("debugstickpro.mode.copy")){
list.add("copy");
}
if (player.hasPermission("debugstickpro.mode.freeze")){
list.add("freeze");
}
return list;
}
}
return list;
}

public static boolean modeChangeEventCancelled(UUID playerUUID, DebugStickMode previousMode, DebugStickMode newMode) {
PlayerChangeDebugStickModeEvent event = new PlayerChangeDebugStickModeEvent(playerUUID, previousMode, newMode);

Bukkit.getPluginManager().callEvent(event);

return event.isCancelled();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.twme.debugstickpro.commands.subcommand;

import dev.twme.debugstickpro.configs.LangFile;
import dev.twme.debugstickpro.playerdata.PlayerDataManager;
import dev.twme.debugstickpro.util.DebugStickItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class GiveCommand {
public static boolean onGiveCommand(Player player, String[] args) {
MiniMessage mm = MiniMessage.miniMessage();

if (!player.hasPermission("debugstickpro.give")) {
Component parsed = mm.deserialize(LangFile.CommandsMessages.NoPermission);
player.sendMessage(parsed);
return true;
}

if (args.length == 1) {
player.getInventory().addItem(DebugStickItem.getDebugStickItem());

Component parsed = mm.deserialize(LangFile.CommandsMessages.Give.Success.replace("%player%", player.getName()));
player.sendMessage(parsed);
if (DebugStickItem.checkPlayer(player)) {
PlayerDataManager.addPlayerToDisplayList(player.getUniqueId());
}
return true;
} else {
Player onlinePlayer = Bukkit.getPlayerExact(args[1]);
if (onlinePlayer == null) {
Component parsed = mm.deserialize(LangFile.CommandsMessages.Give.NoPlayer);
player.sendMessage(parsed);
return true;
} else {
onlinePlayer.getInventory().addItem(DebugStickItem.getDebugStickItem());
Component parsed = mm.deserialize(LangFile.CommandsMessages.Give.Success.replace("%player%", player.getName()));
player.sendMessage(parsed);
if (DebugStickItem.checkPlayer(onlinePlayer)) {
PlayerDataManager.addPlayerToDisplayList(onlinePlayer.getUniqueId());
}
return true;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.twme.debugstickpro.commands.subcommand;

import dev.twme.debugstickpro.configs.LangFile;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.entity.Player;

public class HelpCommand {
public static boolean onHelpCommand(Player player, String[] args) {
MiniMessage mm = MiniMessage.miniMessage();
if (args.length > 1) {
return false;
}
if (!player.hasPermission("debugstickpro.help")) {
Component parsed = mm.deserialize(LangFile.CommandsMessages.NoPermission);
player.sendMessage(parsed);
return true;
}
for (Component component : LangFile.CommandsMessages.Help.HelpMessage) {
player.sendMessage(component);
}
return true;
}
}
Loading

0 comments on commit 78562bf

Please sign in to comment.