Skip to content

Commit

Permalink
Add Commands
Browse files Browse the repository at this point in the history
* Fixed formatting for default prefix issue
* Added generic "FireCommand" class
* Added commands: /dps /dps reload and /dps forceupdate
* Make createMarkerSets and update public methods
* Added a tabcompleter for the commands
  • Loading branch information
FireController1847 committed Dec 21, 2020
1 parent 5ca5656 commit c7785e4
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/firecontroller1847/dynmapps/DynmapPS.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.firecontroller1847.dynmapps;

import com.firecontroller1847.dynmapps.command.CommandDynmapPS;
import com.firecontroller1847.dynmapps.tabcompleter.TabCompleterDynmapPS;
import net.sacredlabyrinth.Phaed.PreciousStones.PreciousStones;
import net.sacredlabyrinth.Phaed.PreciousStones.field.Field;
import net.sacredlabyrinth.Phaed.PreciousStones.managers.ForceFieldManager;
import net.sacredlabyrinth.phaed.simpleclans.Clan;
import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;
import org.bukkit.World;
import org.bukkit.command.PluginCommand;
import org.dynmap.DynmapAPI;
import org.dynmap.markers.AreaMarker;
import org.dynmap.markers.MarkerAPI;
Expand All @@ -18,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

// TODO: Add reload command
public class DynmapPS extends FirePlugin {
Expand Down Expand Up @@ -62,6 +66,11 @@ public boolean onAfterConfiguration() {
}
this.addLoop("update", updateLoopDelay * 1000, this::update);

// Register commands
PluginCommand cmdDynmapps = Objects.requireNonNull(this.getCommand("dynmapps"));
cmdDynmapps.setExecutor(new CommandDynmapPS());
cmdDynmapps.setTabCompleter(new TabCompleterDynmapPS());

// We have loaded successfully
return true;
}
Expand Down Expand Up @@ -92,7 +101,7 @@ public boolean onConfigReload() {
}

// Constructs all of the marker sets
private void createMarkerSets() {
public void createMarkerSets() {
MarkerAPI markerApi = dynmapApi.getMarkerAPI();

// Loop through all layers
Expand Down Expand Up @@ -121,7 +130,7 @@ private void createMarkerSets() {

// Updates the markers and marker sets
// This all happens on a different thread than the main server thread to prevent lag
private void update() {
public void update() {
ForceFieldManager manager = preciousStones.getForceFieldManager();
if (this.getConfig().getBoolean("debug")) {
this.getLogger().info("Starting " + this.getName() + " rebuild...");
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/firecontroller1847/dynmapps/FireCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.firecontroller1847.dynmapps;

import org.bukkit.command.CommandExecutor;

/**
* An abstract command that has an instance of the plugin readily available.
*/
public abstract class FireCommand implements CommandExecutor {

// Variables
protected FirePlugin plugin;

// Constructor
public FireCommand(FirePlugin plugin) {
this.plugin = plugin;
}

// Getters
public FirePlugin getPlugin() {
return plugin;
}

// Setters
public void setPlugin(FirePlugin plugin) {
this.plugin = plugin;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.firecontroller1847.dynmapps.command;

import com.firecontroller1847.dynmapps.DynmapPS;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class CommandDynmapPS implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// Get plugin instance
DynmapPS plugin = (DynmapPS) DynmapPS.getInstance();

// Handle-Sub-Commands
if (args.length > 0) {
// Reload
if (args[0].equalsIgnoreCase("reload")) {
return DynmapPS.runCommand(new CommandReload(plugin), "dynmapps.command.reload", sender, command, label, args);
}

// ForceUpdate
if (args[0].equalsIgnoreCase("forceupdate")) {
return DynmapPS.runCommand(new CommandForceUpdate(plugin), "dynmapps.command.forceupdate", sender, command, label, args);
}
}

// If it's not the command or any sub command, return false
sender.sendMessage("/" + label + " reload");
sender.sendMessage("/" + label + " forceupdate");
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.firecontroller1847.dynmapps.command;

import com.firecontroller1847.dynmapps.DynmapPS;
import com.firecontroller1847.dynmapps.FireCommand;
import com.firecontroller1847.dynmapps.FirePlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

public class CommandForceUpdate extends FireCommand {

public CommandForceUpdate(FirePlugin plugin) {
super(plugin);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
(new Thread(() -> {
sender.sendMessage(plugin.getTranslation("forceupdate.wait"));
((DynmapPS) plugin).update();
sender.sendMessage(plugin.getTranslation("forceupdate.complete"));
})).start();

// The command always works
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.firecontroller1847.dynmapps.command;

import com.firecontroller1847.dynmapps.FireCommand;
import com.firecontroller1847.dynmapps.FirePlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

public class CommandReload extends FireCommand {

public CommandReload(FirePlugin plugin) {
super(plugin);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// Reload Plugin
plugin.reload();

// Send message
sender.sendMessage(plugin.getTranslation("config.reloaded"));

// The command always works
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.firecontroller1847.dynmapps.tabcompleter;

import com.firecontroller1847.dynmapps.DynmapPS;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.util.StringUtil;

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

public class TabCompleterDynmapPS implements TabCompleter {

// Define Primary Arguments
public static final String[][] FIRST_ARGS = {
{ "reload", "dynmapps.command.reload" },
{ "forceupdate", "dynmapps.command.forceupdate" }
};

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
// Completions
ArrayList<String> completions = new ArrayList();

// First-Level Arguments
if (args.length == 1) {
ArrayList<String> originals = new ArrayList<>();
for (String[] argument : FIRST_ARGS) {
if (DynmapPS.hasPermission(sender, argument[1])) {
originals.add(argument[0]);
}
}
StringUtil.copyPartialMatches(args[0], originals, completions);
}

// Sort Completions & Return
Collections.sort(completions);
return completions;
}

}
10 changes: 8 additions & 2 deletions src/main/resources/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@
# %clan_owner%: The name of the owner of the clan
# %clan_description%: The description of the clan
# %clan_member_count%: The amount of members in the clan
prefix: "[§DynmapPS]"
no_permission: "§cYou do not have permission to run this command!"
prefix: "[§bDynmapPS§r]"
no_permission: "§cYou do not have permission to run this command!"
config:
error: "%prefix% §cThere was an error loading the configuration!"
reloaded: "%prefix% Configuration reloaded!"
forceupdate:
wait: "%prefix% Forcing an update, this may take a couple of seconds..."
complete: "%prefix% Updated!"
37 changes: 36 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,39 @@ load: POSTWORLD
name: ${project.name}
version: ${project.version}
author: FireController#1847
website: https://www.visualfiredev.com/
website: https://www.visualfiredev.com/

# Commands
commands:
dynmapps:
description: Lists all of the available commands for the plugin
usage: /dynmapps
aliases: [dps]
permission: dynmapps.command

# Permissions
permissions:
# Stars
dynmapps.*:
description: Gives permission to everything for DynmapPS
default: op
children:
dynmapps.command.*: true
dynmapps.command.*:
description: Gives permission to all DynmapPS commands
default: op
children:
dynmapps.command: true
dynmapps.command.reload: true
dynmapps.command.forceupdate: true

# Commands
dynmapps.command:
description: Gives permission to the main command
default: true
dynmapps.command.reload:
description: Gives permission to the reload command
default: op
dynmapps.command.forceupdate:
description: Gives permission to force a map update
default: op

0 comments on commit c7785e4

Please sign in to comment.