Added first commands

This commit is contained in:
BuildTools committed 2020-12-11 16:19:37 +00:00
1 parent 879293237f
commit 44eef2ff30
12 files changed
+311

No files matched your search

@@ -0,0 +1,27 @@
package me.william278.huskhomes2.Commands;
import me.william278.huskhomes2.messageManager;
import me.william278.huskhomes2.settingHandler;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class delHomeCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player p = (Player) sender;
if (args.length == 1) {
String homeName = args[0];
settingHandler.deleteHome(p, homeName);
} else {
messageManager.sendMessage(p, "error_invalid_syntax", command.getUsage());
}
return true;
}
return false;
}
}
@@ -0,0 +1,32 @@
package me.william278.huskhomes2.Commands;
import me.william278.huskhomes2.*;
import me.william278.huskhomes2.Objects.Home;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class homeCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player p = (Player) sender;
if (args.length == 1) {
String homeName = args[0];
if (dataManager.homeExists(p, homeName)) {
Home home = dataManager.getHome(p.getName(), homeName);
teleportManager.queueTimedTeleport(p, home);
} else {
messageManager.sendMessage(p, "error_home_invalid");
}
} else {
homeListHandler.displayPlayerHomeList(p, 1);
}
return true;
}
return false;
}
}
@@ -0,0 +1,31 @@
package me.william278.huskhomes2.Commands;
import me.william278.huskhomes2.homeListHandler;
import me.william278.huskhomes2.messageManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class homeListCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player p = (Player) sender;
int pageNo = 1;
if (args.length == 1) {
try {
pageNo = Integer.parseInt(args[0]);
} catch (Exception e) {
messageManager.sendMessage(p, "error_invalid_syntax", command.getUsage());
return true;
}
}
homeListHandler.displayPlayerHomeList(p, pageNo);
return true;
}
return false;
}
}
@@ -0,0 +1,27 @@
package me.william278.huskhomes2.Commands;
import me.william278.huskhomes2.messageManager;
import me.william278.huskhomes2.settingHandler;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class setHomeCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player p = (Player) sender;
if (args.length == 1) {
String homeName = args[0];
settingHandler.setHome(p.getLocation(), p, homeName);
} else {
messageManager.sendMessage(p, "error_invalid_syntax", command.getUsage());
}
return true;
}
return false;
}
}
@@ -1,5 +1,9 @@
package me.william278.huskhomes2;
import me.william278.huskhomes2.Commands.delHomeCommand;
import me.william278.huskhomes2.Commands.homeCommand;
import me.william278.huskhomes2.Commands.homeListCommand;
import me.william278.huskhomes2.Commands.setHomeCommand;
import me.william278.huskhomes2.Events.onPlayerJoin;
import me.william278.huskhomes2.Objects.Settings;
import org.bukkit.plugin.java.JavaPlugin;
@@ -20,6 +24,13 @@ public final class Main extends JavaPlugin {
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", new pluginMessageHandler());
}
private static void registerCommands(Main plugin) {
plugin.getCommand("home").setExecutor(new homeCommand());
plugin.getCommand("sethome").setExecutor(new setHomeCommand());
plugin.getCommand("delhome").setExecutor(new delHomeCommand());
plugin.getCommand("homelist").setExecutor(new homeListCommand());
}
private static void registerEvents(Main plugin) {
plugin.getServer().getPluginManager().registerEvents(new onPlayerJoin(), plugin);
}
@@ -43,6 +54,9 @@ public final class Main extends JavaPlugin {
setupBungeeChannels(this);
}
// Register commands
registerCommands(this);
// Register events
registerEvents(this);
@@ -19,6 +19,14 @@ public class SetHomeConditions {
conditionsNotMetReason = "error_set_home_name_taken";
return;
}
if (homeName.length() > 16) {
conditionsNotMetReason = "error_set_home_invalid_length";
return;
}
if (!homeName.matches("[A-Za-z0-9_\\-]+")) {
conditionsNotMetReason = "error_set_home_invalid_characters";
return;
}
conditionsMet = true;
}
@@ -13,6 +13,14 @@ public class SetWarpConditions {
conditionsNotMetReason = "error_set_warp_name_taken";
return;
}
if (warpName.length() > 16) {
conditionsNotMetReason = "error_set_warp_invalid_length";
return;
}
if (!warpName.matches("[A-Za-z0-9_\\-]+")) {
conditionsNotMetReason = "error_set_warp_invalid_characters";
return;
}
conditionsMet = true;
}
@@ -19,6 +19,7 @@ public class Settings {
int teleportWarmupTime;
boolean doBungee;
boolean doSpawnCommand;
private void setSettings(FileConfiguration configFile) {
this.language = configFile.getString("language");
@@ -27,6 +28,8 @@ public class Settings {
this.storageType = configFile.getString("data_storage_option.storage_type");
this.doSpawnCommand = configFile.getBoolean("spawn_command.enabled");
this.playerTable = configFile.getString("data_storage_options.table_names.player_data");
this.locationsTable = configFile.getString("data_storage_options.table_names.location_data");
this.homesTable = configFile.getString("data_storage_options.table_names.home_data");
@@ -0,0 +1,144 @@
package me.william278.huskhomes2;
import me.william278.huskhomes2.Objects.Home;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.entity.Player;
import java.util.ArrayList;
public class homeListHandler {
private static TextComponent pageButton(String buttonText, ChatColor color, String command, String hoverMessage) {
TextComponent button = new TextComponent(buttonText);
button.setColor(color);
button.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, (command)));
button.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(hoverMessage).color(ChatColor.GRAY).italic(true).create())));
return button;
}
private static TextComponent divider() {
TextComponent divider = new TextComponent(" • ");
divider.setColor(ChatColor.GRAY);
return divider;
}
private static TextComponent clickablePrivateHome(Home home) {
TextComponent clickableHome = new TextComponent("[" + home.getName() + "]");
clickableHome.setColor(ChatColor.ITALIC);
clickableHome.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, ("/edithome " + home.getName())));
clickableHome.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(home.getOwnerUsername()).color(ChatColor.GREEN).italic(false).append(": ").append(home.getDescription()).color(net.md_5.bungee.api.ChatColor.GRAY).italic(false).create())));
return clickableHome;
}
private static TextComponent clickablePublicHome(Home home) {
TextComponent clickableHome = new TextComponent("[" + home.getName() + "]");
clickableHome.setColor(ChatColor.ITALIC);
clickableHome.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, ("/phome " + home.getOwnerUsername() + "." + home.getName())));
clickableHome.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(home.getDescription()).color(net.md_5.bungee.api.ChatColor.GRAY).italic(false).create())));
return clickableHome;
}
private static void displayPageButtons(Player p, int pageNumber, int homeListSize, int homeUpperBound) {
ComponentBuilder pageButtons = new ComponentBuilder();
TextComponent nextPageButton = pageButton("[Next Page →]", net.md_5.bungee.api.ChatColor.GREEN, "/homelist " + (pageNumber + 1), "Click to view next 10");
TextComponent previousPageButton = pageButton("[← Last Page]", net.md_5.bungee.api.ChatColor.RED, "/homelist " + (pageNumber - 1), "Click to view previous 10");
TextComponent divider = new TextComponent(" • ");
divider.setColor(net.md_5.bungee.api.ChatColor.GRAY);
if (pageNumber != 1) {
pageButtons.append(previousPageButton);
if (homeListSize > homeUpperBound) {
pageButtons.append(divider);
pageButtons.append(nextPageButton);
}
p.sendMessage("");
} else {
if (homeListSize > homeUpperBound) {
pageButtons.append(nextPageButton);
p.sendMessage("");
}
}
p.spigot().sendMessage(pageButtons.create());
}
public static void displayPlayerHomeList(Player player, int pageNumber) {
ComponentBuilder homeList = new ComponentBuilder();
ArrayList<Home> homes = dataManager.getPlayerHomes(player.getName());
if (homes == null || homes.isEmpty()) {
messageManager.sendMessage(player, "error_no_homes_set");
return;
}
int homeLowerBound = (pageNumber - 1) * 10;
int homeUpperBound = pageNumber * 10;
if (homeUpperBound > homes.size()) {
homeUpperBound = homes.size();
}
player.sendMessage("");
messageManager.sendMessage(player, "private_home_list_page_top", player.getName(), Integer.toString(homeLowerBound + 1), Integer.toString(homeUpperBound), Integer.toString(homes.size()));
for (int i = homeLowerBound; i < homeUpperBound; i++) {
try {
Home home = homes.get(i);
if (i != homeLowerBound) {
homeList.append(divider());
}
homeList.append(clickablePrivateHome(home));
} catch (IndexOutOfBoundsException e) {
if (i == homeLowerBound) {
messageManager.sendMessage(player, "home_list_page_empty");
}
i = homeUpperBound;
}
}
player.spigot().sendMessage(homeList.create());
// Display page buttons
displayPageButtons(player, pageNumber, homes.size(), homeUpperBound);
}
public static void displayPublicHomeList(Player player, int pageNumber) {
ComponentBuilder homeList = new ComponentBuilder();
ArrayList<Home> homes = dataManager.getPublicHomes();
if (homes == null || homes.isEmpty()) {
messageManager.sendMessage(player, "error_no_public_homes_set");
return;
}
int homeLowerBound = (pageNumber - 1) * 10;
int homeUpperBound = pageNumber * 10;
if (homeUpperBound > homes.size()) {
homeUpperBound = homes.size();
}
player.sendMessage("");
messageManager.sendMessage(player, "public_home_list_page_top", Integer.toString(homeLowerBound + 1), Integer.toString(homeUpperBound), Integer.toString(homes.size()));
// List homes
for (int i = homeLowerBound; i < homeUpperBound; i++) {
try {
Home home = homes.get(i);
if (i != homeLowerBound) {
homeList.append(divider());
}
homeList.append(clickablePublicHome(home));
} catch (IndexOutOfBoundsException e) {
if (i == homeLowerBound) {
messageManager.sendMessage(player, "home_list_page_empty");
}
i = homeUpperBound;
}
}
player.spigot().sendMessage(homeList.create());
// Display page buttons
displayPageButtons(player, pageNumber, homes.size(), homeUpperBound);
}
}
+2
View File
@@ -38,6 +38,8 @@ home_list_page_empty: '§7(There are no homes to list on this page)'
error_public_home_invalid: '§4Error: §cCould not find a public home owned by %1% called %2%.'
error_set_home_invalid_characters: '§4Error: §cHome names must be alphanumeric (A-Z,0-9)'
error_set_home_invalid_length: '§4Error: §cHome names can only contain up to 16 characters!'
error_set_warp_invalid_characters: '§4Error: §cWarp names must be alphanumeric (A-Z,0-9)'
error_set_warp_invalid_length: '§4Error: §cWarp names can only contain up to 16 characters!'
error_set_home_maximum_homes: '§4Error: §cYou can only set a maximum of %1% homes!'
error_insufficient_funds: '§4Error: §cYou do not have enough money! (Cost: %1%)'
set_home_spent_money: '§7Successfully spent %1% to set an additional home!'
+2
View File
@@ -67,3 +67,5 @@ set_warp_success: '§aSuccessfully set a new warp called §l%1%'
error_set_warp_name_taken: '§4Error: §cThere is already a warp set with that name.'
warp_deleted: '§aSuccessfully deleted §l%1%§r§a warp.'
error_warp_invalid: '§4Error: §cThere is no warp set with the name %1%'
error_set_warp_invalid_characters: '§4Error: §cWarp names must be alphanumeric (A-Z,0-9)'
error_set_warp_invalid_length: '§4Error: §cWarp names can only contain up to 16 characters!'
+13
View File
@@ -5,3 +5,16 @@ api-version: 1.16
authors: [William278]
description: A powerful, intuitive and flexible teleportation suite
website: timetobattle.net
commands:
home:
usage: /home <home>
description: Return to a home
sethome:
usage: /sethome <home>
description: Set a home
delhome:
usage: /delhome <home>
description: Delete a home
homelist:
usage: /homelist [page no]
description: View a list of homes