Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/doc creation #3

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,32 @@
import me.ninjamandalorian.ImplodusTravel.object.Station;
import net.md_5.bungee.api.ChatColor;

/** Admin Command Class
* @author NinjaMandalorian
*/
public class ImplodusTravelAdminCommand implements CommandExecutor, TabCompleter{

/** Constructor that adds command to plugin */
public ImplodusTravelAdminCommand() {
ImplodusTravel plugin = ImplodusTravel.getInstance();
plugin.getCommand("implodustraveladmin").setExecutor(this);
}

/** Admin command switch case */
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

// Returns if no permission
if (!sender.hasPermission("implodustravel.admin")) {
sender.sendMessage(ChatColor.RED + "You do not have permission to run this command.");
return true;
}

// Returns if no arguments
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Invalid command.");
return true;
}

// Command switch
switch (args[0]) {
case "list":
listStations(sender);
Expand All @@ -55,89 +61,130 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return true;
}

/** Show station info to user
* @param sender - user who requested info
* @param args - arguments
*/
private void viewStation(CommandSender sender, String[] args) {
// Get station from 1st argument
Station station = strToStation(args.length > 0 ? args[0] : null);
if (station == null) {
sender.sendMessage("Please specify a station UUID.");
return;
}
// Sends info to player
String stationString = "&aStation: &e" + station.getDisplayName() + "&a | Owner: &e" + station.getOwner().getName() + "&a | Banner: &e" + station.getStationLocation().toString();
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', stationString));
}

/** Show station list to user
* @param sender - user who requested list
*/
private void listStations(CommandSender sender) {
Collection<Station> stations = Station.getStations();
Collection<Station> stations = Station.getStations(); // Get stations
sender.sendMessage("Name - Owner - ID");
for (Station station : stations) {
for (Station station : stations) { // Send all stations and their information
sender.sendMessage(ChatColor.GREEN + station.getDisplayName() + " - " + station.getOwner().getName() + " - " + station.getIdString());
}
}

/** Remotely delete station object
* <p>Does not delete block from world.
* @param sender - user who requested deletion
* @param args - arguments
*/
private void deleteStation(CommandSender sender, String[] args) {
Station station = strToStation(args.length > 0 ? args[0] : null);
// Get station from 1st argument
Station station = strToStation(args.length > 0 ? args[0] : null);
if (station == null) {
sender.sendMessage("Please specify a station UUID.");
return;
}

// Delete & confirm to player
station.delete();
sender.sendMessage(ChatColor.GREEN + "Station deleted.");
}

/** Teleport user to station banner
* @param sender - user who requested teleport
* @param args - arguments
*/
private void teleportStation(CommandSender sender, String[] args) {
// Return if not a player (console)
if (!(sender instanceof Player player)) {
sender.sendMessage("You must be a player to use this command.");
return;
}

// Get station from 1st argument
Station station = strToStation(args.length > 0 ? args[0] : null);
if (station == null) {
sender.sendMessage("Please specify a station UUID.");
return;
}

// Teleport & notify player
player.teleport(station.getStationLocation());
player.sendMessage(ChatColor.GREEN + "Teleported to " + station.getDisplayName());
}

/** Converts string to station
* @param string - input string
* @return Station or null
*/
private Station strToStation(String string) {
UUID id;
try {
id = UUID.fromString(string);
id = UUID.fromString(string); // Attempts string to UUID
} catch (Exception e) {
return null;
return null; // Returns null if error
}
return Station.getStation(id);
return Station.getStation(id); // Else returns station list result (may be null)
}

/** Tab Completion for admin commands */
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (!sender.hasPermission("implodustravel.admin")) return Collections.emptyList();
if (!sender.hasPermission("implodustravel.admin")) return Collections.emptyList(); // Gives no results if not an admin

// Swithc case for no. of args
switch (args.length) {
case 0:
case 1:
return startOnly(Arrays.asList("list", "view"), args[0]);
return startOnly(Arrays.asList("list", "view", "delete", "teleport"), args[0]);
case 2:
if (args[0].equalsIgnoreCase("view")) {
ArrayList<String> returnList = new ArrayList<>();
for (Station station : Station.getStations()) returnList.add(station.getIdString());
return startOnly(returnList, args[1]);
switch (args[0]) {
case "view":
case "delete":
case "teleport":
ArrayList<String> returnList = new ArrayList<>();
for (Station station : Station.getStations()) returnList.add(station.getIdString()); // Adds strings of all station's ids
return startOnly(returnList, args[1]);
}
}
return Collections.emptyList();
return Collections.emptyList(); // If suits no other case, gets empty
}

/** Culls list to those beginning with a certain string
* @param options - Available options
* @param input - Beginning
* @return list of options beginning with string
*/
private static List<String> startOnly(List<String> options, String input) {
ArrayList<String> returnList = new ArrayList<>();
input = input.toLowerCase();
input = input.toLowerCase(); // Converts input to lowercase
for (String string : options) {
// Adds to return list if matches
if (string.toLowerCase().startsWith(input)) returnList.add(string);
}

return returnList;
}

/**
* Removes first argument of array
* @param args - Array of arguments
* @return Array arguments without the 1st argument
*/
private String[] remFirst(String[] args) {
return Arrays.copyOfRange(args, 1, args.length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,34 @@
import net.md_5.bungee.api.ChatColor;
import net.milkbowl.vault.economy.Economy;

/** Travel Command
* @author NinjaMandalorian
*/
public class ImplodusTravelCommand implements CommandExecutor, TabCompleter {

/** Constructor that adds command to plugin */
public ImplodusTravelCommand() {
ImplodusTravel plugin = ImplodusTravel.getInstance();
plugin.getCommand("implodustravel").setExecutor(this);
}

/** Admin command switch case */
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

// Switch case for 1st arg
switch (args[0].toLowerCase()) {
case "buystation":
if (sender instanceof Player plr) {
// Runs if player & has perm
if (!plr.hasPermission("implodustravel.station.buy")) return true;
buyStationCommand(plr, Arrays.copyOfRange(args, 1, args.length));
} else {
// Warns if console
sender.sendMessage("Not possible for console.");
}
return true;
case "admintoken":
// TODO delete
if (sender instanceof Player plr) {
if (!plr.hasPermission("implodustravel.admin")) return true;
plr.getInventory().addItem(ItemGenerator.getDiscoveryTokenItem(new Station(UUID.randomUUID(), "TEST_STATION", plr, null, null)));
Expand All @@ -50,31 +58,37 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return true;
}


return true;
}

/** Buy banner station
* @param player - Player using command
* @param args - Args
*/
private void buyStationCommand(Player player, String[] args) {
ItemStack item = player.getInventory().getItemInMainHand();
if (item.getType().name().endsWith("BANNER")) {
ItemStack item = player.getInventory().getItemInMainHand(); // Gets stack in player hand
if (item.getType().name().endsWith("BANNER")) { // Must be a banner
Economy econ = ImplodusTravel.getEcon();
double balance = econ.getBalance(player);
final double STATION_COST = Settings.getDefaultStationCost();
if (balance < STATION_COST) {
if (balance < STATION_COST) { // Returns if balance is less than cost
player.sendMessage(ChatColor.RED + "You need " + econ.format(STATION_COST) + " to do this.");
return;
}
econ.withdrawPlayer(player, STATION_COST);
PersistentDataController.giveItemTag(item);
econ.withdrawPlayer(player, STATION_COST); // Subtracts from player account
PersistentDataController.giveItemTag(item); // Adds custom tag
// Creates meta
ItemMeta meta = item.getItemMeta();
meta.setLore(Arrays.asList(ChatColor.GREEN + "Place this to create a new station."));
item.setItemMeta(meta);
// Notifies player
player.sendMessage(ChatColor.GREEN + "Your banner is now a station. Place it to create the station.");
} else {
player.sendMessage(ChatColor.RED + "You must use a banner.");
}
}

/** Tab completer for command */
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 1) {
Expand All @@ -83,10 +97,16 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
return Collections.emptyList();
}

/** Culls list to those beginning with a certain string
* @param options - Available options
* @param input - Beginning
* @return list of options beginning with string
*/
private static List<String> startOnly(List<String> options, String input) {
ArrayList<String> returnList = new ArrayList<>();
input = input.toLowerCase();
input = input.toLowerCase(); // Converts input to lowercase
for (String string : options) {
// Adds to return list if matches
if (string.toLowerCase().startsWith(input)) returnList.add(string);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,85 @@
import me.ninjamandalorian.ImplodusTravel.ImplodusTravel;
import me.ninjamandalorian.ImplodusTravel.object.Station;

/** Controller for plugin's persistent data
* @author NinjaMandalorian
*/
public class PersistentDataController {

private static NamespacedKey customKey = new NamespacedKey(ImplodusTravel.getInstance(), "customcheck");
private static NamespacedKey tokenKey = new NamespacedKey(ImplodusTravel.getInstance(), "stationCode");
private static NamespacedKey customKey = new NamespacedKey(ImplodusTravel.getInstance(), "customcheck"); // Key for generic custom check
private static NamespacedKey tokenKey = new NamespacedKey(ImplodusTravel.getInstance(), "stationCode"); // Key for station map's code

/** Checks if a stack is a station item
* @param item - Item to check
* @return Boolean of if is a station item
*/
public static boolean isStationItem(ItemStack item) {
if (item.getItemMeta().getPersistentDataContainer().has(customKey, PersistentDataType.INTEGER)) return true;
return false;
}

/** Checks if a block is a station block
* @param block - Block to check
* @return Boolean of if is station block
*/
public static boolean isStationBlock(Block block) {
if (block.getState() instanceof Banner banner) {
if (banner.getPersistentDataContainer().has(customKey, PersistentDataType.INTEGER)) return true;
}
return false;
}

/** Get UUID from item (Map)
* @param item - Item to get checked
* @return UUID or null
*/
public static UUID getTokenUUID(ItemStack item) {
if (item == null || item.getItemMeta() == null) return null;
if (item == null || item.getItemMeta() == null) return null; // Returns null if no ItemMeta
PersistentDataContainer pdc = item.getItemMeta().getPersistentDataContainer();
if (!pdc.has(tokenKey, PersistentDataType.STRING)) {
return null;
return null; // If doesn't have key, returns null
}
try {
UUID stationUUID = UUID.fromString(pdc.get(tokenKey, PersistentDataType.STRING));
return stationUUID;
return stationUUID; // Returns UUID construction
} catch (IllegalArgumentException ex) {
return null;
return null; // If UUID fails, returns null.
}
}

/** Gives custom tag to a block
* @param block - Block to give tag
*/
public static void giveCustomTag(Block block) {
BlockState state = block.getState();
if (state instanceof PersistentDataHolder holder) {
holder.getPersistentDataContainer().set(customKey, PersistentDataType.INTEGER, 1);
if (state instanceof PersistentDataHolder holder) { // If can be a holder
holder.getPersistentDataContainer().set(customKey, PersistentDataType.INTEGER, 1); // Sets key to value of 1
}
state.update();
state.update(); // Updates
}

/** Gives custom tag to an item
* @param item - Item to give tag
*/
public static void giveItemTag(ItemStack item) {
giveItemTag(item, 2);
giveItemTag(item, 2); // Gives tag of 2 (generic item no.) to item
}

/** Gives custom tag to an item, with a given number
* @param item - Item to give tag
* @param number - Number to assign as tag
*/
public static void giveItemTag(ItemStack item, Integer number) {
ItemMeta meta = item.getItemMeta();
PersistentDataContainer pdc = meta.getPersistentDataContainer();
pdc.set(customKey, PersistentDataType.INTEGER, number);
item.setItemMeta(meta);
}

/** Gives token tag of a station to a specified item
* @param item - Item to add tag to
* @param station - Station to give tag of
*/
public static void giveTokenTag(ItemStack item, Station station) {
ItemMeta meta = item.getItemMeta();
PersistentDataContainer pdc = meta.getPersistentDataContainer();
Expand Down
Loading