Command Handler

This commit is contained in:
night0721 2022-07-07 23:22:56 +01:00
parent 8b17d36069
commit 21975d62b1
15 changed files with 262 additions and 149 deletions

View file

@ -44,21 +44,8 @@ public final class Main extends JavaPlugin implements Listener {
public SideBarManager getSideBarManager() { return sideBarManager; }
@Override
public void onEnable() {
// Plugin startup logic
System.out.println("PREPARING TO DESTROY HYPIXEL");
getCommand("test").setExecutor(new TestCommand());
getCommand("vanish").setExecutor(new VanishCommand());
getCommand("armor").setExecutor(new ArmorCommand());
getCommand("armor").setTabCompleter(new ArmorTab());
getCommand("gun").setExecutor(new GunCommand());
getCommand("msg").setExecutor(new MessageCommand());
getCommand("menu").setExecutor(new MenuCommand());
getCommand("rank").setExecutor(new RankCommand(this));
getCommand("hologram").setExecutor(new HologramCommand());
getCommand("craft").setExecutor(new CraftCommand());
getCommand("anvil").setExecutor(new AnvilCommand());
getCommand("enchant").setExecutor(new EnchantingCommand());
new VanishCommand();new TestCommand();new GunCommand();new AnvilCommand();new ArmorCommand();new MenuCommand();new RankCommand(this);
new MessageCommand();new HologramCommand();new CraftCommand();new EnchantingCommand();
bossbar = Bukkit.createBossBar(
ChatColor.GOLD + "Kuudra",
BarColor.RED,
@ -105,7 +92,7 @@ public final class Main extends JavaPlugin implements Listener {
public void onJoin(PlayerJoinEvent e) {
e.setJoinMessage(rankManager.getRank(e.getPlayer().getUniqueId()).getDisplay() + " " + e.getPlayer().getName() + ChatColor.WHITE + " joined the server!");
e.getPlayer().sendTitle(ChatColor.RED +"Welcome to Operation Valkyrie!", ChatColor.GREEN + "LOL", 20, 100, 20);
e.getPlayer().sendTitle(ChatColor.RED +"Welcome to Apache!", ChatColor.GREEN + "LOL", 20, 100, 20);
e.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§1NOT ENOUGH MANNER"));
bossbar.addPlayer(e.getPlayer());
@ -127,8 +114,8 @@ public final class Main extends JavaPlugin implements Listener {
@EventHandler
public void onPing(ServerListPingEvent e) {
e.setMaxPlayers(8964);
String s = centerText("Operation Valkyrie\n", 45);
String s2 = centerText("Support 1.18 & 1.8.9",45);
String s = centerText("Apache\n", 45);
String s2 = centerText("Support 1.18 & 1.8.9",15);
e.setMotd(ChatColor.AQUA.toString() + ChatColor.BOLD + s + ChatColor.GOLD + ChatColor.BOLD + s2);
try {
e.setServerIcon(Bukkit.loadServerIcon(new File("nuke.png")));

View file

@ -1,21 +1,33 @@
package com.night.nullvalkyrie.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
public class AnvilCommand implements CommandExecutor {
import java.util.List;
public class AnvilCommand extends Command {
public AnvilCommand() {
super(
"anvil",
new String[]{"av"},
"Open anvil",
""
);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
Player player = (Player) sender;
player.openInventory(Bukkit.createInventory(player, InventoryType.ANVIL));
}
}
return false;
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -2,17 +2,29 @@ package com.night.nullvalkyrie.commands;
import org.bukkit.Bukkit;
import org.bukkit.Material;
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.LeatherArmorMeta;
import org.bukkit.util.StringUtil;
public class ArmorCommand implements CommandExecutor {
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArmorCommand extends Command {
public ArmorCommand() {
super(
"armor",
new String[]{},
"Give you a set of armor",
""
);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
Player player = (Player) sender;
ItemStack helmet = new ItemStack(Material.LEATHER_HELMET);
@ -47,8 +59,19 @@ public class ArmorCommand implements CommandExecutor {
boot.setItemMeta(legdata);
player.getInventory().addItem(boot);
}
}
return false;
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
if(args.length == 1) {
return StringUtil.copyPartialMatches(args[0], Arrays.asList("angeles", "widow"), new ArrayList<>());
} else if(args.length == 2) {
List<String> names = new ArrayList<>();
for(Player player: Bukkit.getOnlinePlayers()) {
names.add(player.getName());
}
return StringUtil.copyPartialMatches(args[1], names, new ArrayList<>());
}
return new ArrayList<>();
}
}

View file

@ -1,32 +0,0 @@
package com.night.nullvalkyrie.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArmorTab implements TabCompleter {
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if(args.length == 1) {
return StringUtil.copyPartialMatches(args[0], Arrays.asList("angeles", "widow"), new ArrayList<>());
} else if(args.length == 2) {
List<String> names = new ArrayList<>();
for(Player player: Bukkit.getOnlinePlayers()) {
names.add(player.getName());
}
return StringUtil.copyPartialMatches(args[1], names, new ArrayList<>());
}
return new ArrayList<>();
}
}

View file

@ -0,0 +1,46 @@
package com.night.nullvalkyrie.commands;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
public abstract class Command extends BukkitCommand {
public Command(String command, String[] aliases, String description, String permission) {
super(command);
this.setAliases(Arrays.asList(aliases));
this.setDescription(description);
this.setPermission(permission);
this.setPermission(ChatColor.RED + "You must have" + permission + "in order to use this command");
try {
Field field = Bukkit.getServer().getClass().getDeclaredField("commandMap");
field.setAccessible(true);
CommandMap map = (CommandMap) field.get(Bukkit.getServer());
map.register(command, this);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
onCommand(sender, args);
return false;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
return onTabComplete(sender, args);
}
public abstract void onCommand(CommandSender sender, String[] args);
public abstract List<String> onTabComplete(CommandSender sender, String[] args);
}

View file

@ -1,19 +1,32 @@
package com.night.nullvalkyrie.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class CraftCommand implements CommandExecutor {
import java.util.List;
public class CraftCommand extends Command {
public CraftCommand() {
super(
"craft",
new String[]{"ct", "crafting", "craftingtable"},
"Open crafting table",
""
);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
Player player = (Player) sender;
player.openWorkbench(null, true);
}
}
return false;
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -1,21 +1,30 @@
package com.night.nullvalkyrie.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
public class EnchantingCommand extends Command {
public EnchantingCommand() {
super(
"enchant",
new String[]{"et", "enchanting", "enchantingtable"},
"Open enchanting table",
""
);
}
public class EnchantingCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
Player player = (Player) sender;
player.openEnchanting(null, true);
}
}
return false;
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -2,19 +2,29 @@ package com.night.nullvalkyrie.commands;
import org.bukkit.ChatColor;
import org.bukkit.Material;
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.bukkit.inventory.meta.LeatherArmorMeta;
import java.util.Arrays;
import java.util.List;
public class GunCommand extends Command {
public GunCommand() {
super(
"gun",
new String[]{},
"Give you a gun",
""
);
}
public class GunCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
Player player = (Player) sender;
ItemStack hoe = new ItemStack(Material.DIAMOND_HOE);
ItemMeta hoedata = hoe.getItemMeta();
@ -23,6 +33,10 @@ public class GunCommand implements CommandExecutor {
hoedata.setLore(Arrays.asList(ChatColor.GOLD + "Shoot Snowball!"));
hoe.setItemMeta(hoedata);
player.getInventory().addItem(hoe);
return false;
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -2,16 +2,24 @@ package com.night.nullvalkyrie.commands;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
public class HologramCommand implements CommandExecutor {
import java.util.List;
public class HologramCommand extends Command {
public HologramCommand() {
super(
"hologram",
new String[]{},
"Spawn a hologram",
""
);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
Player player = (Player) sender;
String[] ar = new String[]{
@ -30,7 +38,10 @@ public class HologramCommand implements CommandExecutor {
stand.setCustomName(line);
}
}
}
return false;
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -3,8 +3,6 @@ package com.night.nullvalkyrie.commands;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
@ -12,12 +10,22 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays;
import java.util.List;
public class MenuCommand implements CommandExecutor {
public class MenuCommand extends Command {
public static Inventory inv = Bukkit.createInventory(null, 45, ChatColor.DARK_BLUE.toString() + ChatColor.BOLD + "Valkyrie Menu");
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public MenuCommand() {
super(
"menu",
new String[]{"m"},
"Open the menu",
""
);
}
@Override
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
Player player = (Player) sender;
inv = Bukkit.createInventory(player,45, ChatColor.DARK_BLUE.toString() + ChatColor.BOLD + "Valkyrie Menu");
@ -58,6 +66,10 @@ public class MenuCommand implements CommandExecutor {
player.openInventory(inv);
}
return false;
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -2,15 +2,23 @@ package com.night.nullvalkyrie.commands;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class MessageCommand implements CommandExecutor {
import java.util.List;
public class MessageCommand extends Command {
public MessageCommand() {
super(
"message",
new String[]{"msg"},
"Send message to someone",
""
);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
Player player = (Player) sender;
if (args.length >= 2) {
@ -29,7 +37,10 @@ public class MessageCommand implements CommandExecutor {
player.sendMessage(ChatColor.RED + "Invalid parameter, use /msg <Player> <Message>");
}
}
}
return false;
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -7,26 +7,34 @@ import com.night.nullvalkyrie.RankSys.Rank;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
//92.0.69.141:25565
public class RankCommand implements CommandExecutor {
public class RankCommand extends Command {
private Main main;
public RankCommand(Main main) {
super(
"rank",
new String[]{},
"Set rank of players",
""
);
this.main = main;
}
private Cache<UUID, Long> cooldown = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).build();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
Player player = (Player) sender;
if(!cooldown.asMap().containsKey(player.getUniqueId())) {if(player.isOp()) {
if(!cooldown.asMap().containsKey(player.getUniqueId())) {
if(player.isOp()) {
if(args.length == 2) {
if(Bukkit.getOfflinePlayer(args[0]).hasPlayedBefore()) {
OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]);
@ -37,8 +45,9 @@ public class RankCommand implements CommandExecutor {
if(target.isOnline()) {
target.getPlayer().sendMessage(ChatColor.GREEN + player.getName() + " set your rank to " + rank.getDisplay());
}
return true;
return;
}
}
player.sendMessage(ChatColor.RED + "Invalid Rank, please specify a valid rank, ROOKIE, SPECIAL, ADMIN, OWNER");
} else {
@ -56,6 +65,10 @@ public class RankCommand implements CommandExecutor {
player.sendMessage(ChatColor.RED + "You are on a " + TimeUnit.MILLISECONDS.toSeconds(distance) + " seconds cooldown to use the command again");
}
}
return false;
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -1,22 +1,34 @@
package com.night.nullvalkyrie.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class TestCommand implements CommandExecutor {
import java.util.List;
public class TestCommand extends Command {
public TestCommand() {
super(
"test",
new String[]{},
"Test",
""
);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
if(sender instanceof Player) {
if(args.length == 1) {
if(args[0].equalsIgnoreCase("hello")) {
Player player = ((Player) sender);
Command.broadcastCommandMessage(player,player.getAddress().getHostString());
Player player = (Player) sender;
player.sendMessage(player.getAddress().getHostString());
}
}
}
return false;
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -2,8 +2,6 @@ package com.night.nullvalkyrie.commands;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -11,10 +9,18 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class VanishCommand implements CommandExecutor {
public class VanishCommand extends Command {
private List<UUID> vanished = new ArrayList<>();
public VanishCommand() {
super(
"vanish",
new String[]{},
"Turn yourself into invisible",
""
);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public void onCommand(CommandSender sender, String[] args) {
Player player = (Player) sender;
if(sender instanceof Player) {
if(vanished.contains(player.getUniqueId())) {
@ -31,8 +37,10 @@ public class VanishCommand implements CommandExecutor {
player.sendMessage(ChatColor.GREEN + "You are now vanished");
}
}
}
return false;
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -5,29 +5,3 @@ api-version: 1.18
authors: [ NightKaly, Leocthl ]
description: Null Valkyrie
website: https://github.com/night0721/nullvalkyrie
commands:
test:
description: "Test"
vanish:
description: "Turn yourself into invisible"
armor:
description: "Give you a set of armor"
gun:
description: "Give you a gun"
msg:
description: "Send message to someone"
menu:
description: "Open the menu"
rank:
description: "Set rank of players"
hologram:
description: "Spawn a hologram"
craft:
aliases: ["ct", "crafting", "craftingtable"]
description: "Open crafting table"
anvil:
aliases: [ "av", "anvil" ]
description: "Open anvil"
enchant:
aliases: [ "et", "enchanting", "enchantingtable"]
description: "Open enchanting table"