Changed from System.nanoTime() to System.currentTimeMillis().

Reverted Citizens download.
Reverted static use of instance and using DI instead.
Fixed misspelling on permission node.
This commit is contained in:
Matt 2018-11-01 18:27:54 +00:00
parent c23c4c447c
commit 094e7c2cd0
24 changed files with 322 additions and 304 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
\.idea/
dependency-reduced-pom\.xml
citizens-cmd\.iml
target/

View File

@ -92,7 +92,7 @@
<dependency> <dependency>
<groupId>net.citizensnpcs</groupId> <groupId>net.citizensnpcs</groupId>
<artifactId>citizensapi</artifactId> <artifactId>citizensapi</artifactId>
<version>2.0.23-SNAPSHOT</version> <version>2.0.24-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -34,13 +34,11 @@ import me.mattmoreira.citizenscmd.utility.DisplayFormat;
import me.mattmoreira.citizenscmd.utility.Util; import me.mattmoreira.citizenscmd.utility.Util;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.*; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -53,8 +51,6 @@ public final class CitizensCMD extends JavaPlugin {
*/ */
private final String[] REGISTERED_LANG_FILES = {"en", "pt", "ro", "bg", "no", "ch"}; private final String[] REGISTERED_LANG_FILES = {"en", "pt", "ro", "bg", "no", "ch"};
private static CitizensCMD plugin;
private static CommandHandler commandHandler = null; private static CommandHandler commandHandler = null;
private static LangHandler lang = null; private static LangHandler lang = null;
private static DataHandler dataHandler = null; private static DataHandler dataHandler = null;
@ -70,43 +66,32 @@ public final class CitizensCMD extends JavaPlugin {
private static HashMap<String, Boolean> waitingList; private static HashMap<String, Boolean> waitingList;
public void onLoad() {
if (!hasCitizensFile()) {
info(color(TAG + "&cCitizens &7is needed for this plugin to work!"));
info(color(TAG + "&cCitizens.jar &7is not installed on the server!"));
info(color(TAG + "&cDownloading Citizens jar..."));
Util.downloadCitizens();
}
}
public void onEnable() { public void onEnable() {
if (!hasCitizens()) Util.loadCitizens(); if (!hasCitizens()) Util.disablePlugin(this);
plugin = this; commandHandler = new CommandHandler(this);
commandHandler = new CommandHandler();
commandHandler.enable(); commandHandler.enable();
checkOldConfig(); checkOldConfig(this);
new Metrics(this); new Metrics(this);
info(color(TAG + "&3Citizens&cCMD &8&o" + getDescription().getVersion() + " &8By &3Mateus Moreira &c@LichtHund")); info(color(TAG + "&3Citizens&cCMD &8&o" + getDescription().getVersion() + " &8By &3Mateus Moreira &c@LichtHund"));
permissionsManager = new PermissionsManager(); permissionsManager = new PermissionsManager(this);
dataHandler = new DataHandler(); dataHandler = new DataHandler(this);
dataHandler.initialize(); dataHandler.initialize();
cooldownHandler = new CooldownHandler(); cooldownHandler = new CooldownHandler(this);
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> cooldownHandler.initialize(), 30L); Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> cooldownHandler.initialize(), 30L);
saveDefaultConfig(); saveDefaultConfig();
registerCommands(); registerCommands();
registerEvents(); registerEvents();
registerLangs(); registerLangs(this);
setLang(getConfig().getString("lang")); setLang(getConfig().getString("lang"));
if (hasPAPI()) { if (hasPAPI()) {
@ -175,7 +160,7 @@ public final class CitizensCMD extends JavaPlugin {
} else } else
displayFormat = DisplayFormat.MEDIUM; displayFormat = DisplayFormat.MEDIUM;
if (upCheck()) { if (upCheck(this)) {
SpigotUpdater updater = new SpigotUpdater(this, 30224); SpigotUpdater updater = new SpigotUpdater(this, 30224);
try { try {
// If there's an update, tell the user that they can update // If there's an update, tell the user that they can update
@ -211,8 +196,8 @@ public final class CitizensCMD extends JavaPlugin {
} }
} }
new UpdateScheduler().runTaskTimerAsynchronously(this, 72000L, 72000L); new UpdateScheduler(this).runTaskTimerAsynchronously(this, 72000L, 72000L);
new CooldownScheduler().runTaskTimerAsynchronously(this, 36000L, 36000L); new CooldownScheduler(this).runTaskTimerAsynchronously(this, 36000L, 36000L);
} }
@Override @Override
@ -221,16 +206,6 @@ public final class CitizensCMD extends JavaPlugin {
cooldownHandler.saveToFile(); cooldownHandler.saveToFile();
} }
/**
* Checks if Citizens is installed or not on the server
*
* @return Returns true if Citizens is found and false if not
*/
private boolean hasCitizensFile() {
return Util.doesCitizensExist();
}
private boolean hasCitizens() { private boolean hasCitizens() {
return Bukkit.getPluginManager().isPluginEnabled("Citizens"); return Bukkit.getPluginManager().isPluginEnabled("Citizens");
} }
@ -249,7 +224,7 @@ public final class CitizensCMD extends JavaPlugin {
*/ */
private void registerCommands() { private void registerCommands() {
getCommand("npcmd").setExecutor(commandHandler); getCommand("npcmd").setExecutor(commandHandler);
Stream.of(new CMDHelp(), new CMDAdd(), new CMDCooldown(), new CMDList(), new CMDReload(), new CMDRemove(), new CMDEdit(), new CMDPrice(), new CMDSound()).forEach(commandHandler::register); Stream.of(new CMDHelp(this), new CMDAdd(this), new CMDCooldown(this), new CMDList(this), new CMDReload(this), new CMDRemove(this), new CMDEdit(this), new CMDPrice(this), new CMDSound()).forEach(commandHandler::register);
} }
/** /**
@ -257,8 +232,8 @@ public final class CitizensCMD extends JavaPlugin {
*/ */
private void registerEvents() { private void registerEvents() {
PluginManager pm = getServer().getPluginManager(); PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new UpdateEvent(), this); pm.registerEvents(new UpdateEvent(this), this);
pm.registerEvents(new NPCListener(), this); pm.registerEvents(new NPCListener(this), this);
} }
/** /**
@ -279,26 +254,17 @@ public final class CitizensCMD extends JavaPlugin {
return economy != null; return economy != null;
} }
/**
* Gets the plugin instance
*
* @return Returns instance of the plugin
*/
public static CitizensCMD getPlugin() {
return plugin;
}
/** /**
* Creates all the language files * Creates all the language files
*/ */
private void registerLangs() { private void registerLangs(CitizensCMD plugin) {
File langFile; File langFile;
for (String registeredLangFile : REGISTERED_LANG_FILES) { for (String registeredLangFile : REGISTERED_LANG_FILES) {
langFile = new File(CitizensCMD.getPlugin().getDataFolder(), "lang/" + registeredLangFile + ".yml"); langFile = new File(plugin.getDataFolder(), "lang/" + registeredLangFile + ".yml");
if (!langFile.exists()) if (!langFile.exists())
CitizensCMD.getPlugin().saveResource("lang/" + registeredLangFile + ".yml", false); plugin.saveResource("lang/" + registeredLangFile + ".yml", false);
} }
} }
@ -311,41 +277,41 @@ public final class CitizensCMD extends JavaPlugin {
case "en": case "en":
case "eng": case "eng":
case "english": case "english":
lang = new LangHandler("en"); lang = new LangHandler(this, "en");
break; break;
case "pt": case "pt":
case "port": case "port":
case "portuguese": case "portuguese":
lang = new LangHandler("pt"); lang = new LangHandler(this, "pt");
break; break;
case "ro": case "ro":
case "roma": case "roma":
case "romanian": case "romanian":
lang = new LangHandler("ro"); lang = new LangHandler(this, "ro");
break; break;
case "bg": case "bg":
case "bulg": case "bulg":
case "bulgarian": case "bulgarian":
lang = new LangHandler("bg"); lang = new LangHandler(this, "bg");
break; break;
case "no": case "no":
case "norw": case "norw":
case "norwegian": case "norwegian":
lang = new LangHandler("no"); lang = new LangHandler(this, "no");
break; break;
case "ch": case "ch":
case "chi": case "chi":
case "chinese": case "chinese":
lang = new LangHandler("ch"); lang = new LangHandler(this, "ch");
break; break;
default: default:
lang = new LangHandler("en"); lang = new LangHandler(this, "en");
break; break;
} }
lang.initialize(); lang.initialize();

View File

@ -29,8 +29,11 @@ import static me.mattmoreira.citizenscmd.utility.Util.npcNotSelected;
public class CMDAdd extends CommandBase { public class CMDAdd extends CommandBase {
public CMDAdd() { private CitizensCMD plugin;
public CMDAdd(CitizensCMD plugin) {
super("add", "citizenscmd.add", false, null, 2, 512); super("add", "citizenscmd.add", false, null, 2, 512);
this.plugin = plugin;
} }
/** /**
@ -42,7 +45,7 @@ public class CMDAdd extends CommandBase {
@Override @Override
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
if (npcNotSelected(player)) return; if (npcNotSelected(plugin, player)) return;
String permission = args[0]; String permission = args[0];
boolean left = false; boolean left = false;
@ -76,7 +79,7 @@ public class CMDAdd extends CommandBase {
boolean finalLeft = left; boolean finalLeft = left;
CitizensCMD.getPlugin().getDataHandler().addCommand(getSelectedNpcId(player), permission, finalString, player, finalLeft); plugin.getDataHandler().addCommand(getSelectedNpcId(player), permission, finalString, player, finalLeft);
} }
} }

View File

@ -27,22 +27,25 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
public class CMDCooldown extends CommandBase { public class CMDCooldown extends CommandBase {
public CMDCooldown() { private CitizensCMD plugin;
public CMDCooldown(CitizensCMD plugin) {
super("cooldown", "citizenscmd.cooldown", false, new String[]{"cd"}, 1, 1); super("cooldown", "citizenscmd.cooldown", false, new String[]{"cd"}, 1, 1);
this.plugin = plugin;
} }
@Override @Override
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
if (npcNotSelected(player)) return; if (npcNotSelected(plugin, player)) return;
if (notInteger(args[0])) { if (notInteger(args[0])) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_COOLDOWN)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_COOLDOWN));
return; return;
} }
CitizensCMD.getPlugin().getDataHandler().setCooldown(getSelectedNpcId(player), Integer.valueOf(args[0]), player); plugin.getDataHandler().setCooldown(getSelectedNpcId(player), Integer.valueOf(args[0]), player);
} }
} }

View File

@ -30,18 +30,21 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
public class CMDEdit extends CommandBase { public class CMDEdit extends CommandBase {
public CMDEdit() { private CitizensCMD plugin;
public CMDEdit(CitizensCMD plugin) {
super("edit", "citizenscmd.edit", false, null, 4, 512); super("edit", "citizenscmd.edit", false, null, 4, 512);
this.plugin = plugin;
} }
@Override @Override
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
if (npcNotSelected(player)) return; if (npcNotSelected(plugin, player)) return;
if (notInteger(args[2])) { if (notInteger(args[2])) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_ID_NUMBER)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
return; return;
} }
@ -57,49 +60,49 @@ public class CMDEdit extends CommandBase {
case "perm": case "perm":
if (args.length > 4) { if (args.length > 4) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_PERMISSION)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_PERMISSION));
return; return;
} }
type = EnumTypes.EditType.PERM; type = EnumTypes.EditType.PERM;
break; break;
default: default:
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_ARGUMENTS)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ARGUMENTS));
return; return;
} }
switch (args[1].toLowerCase()) { switch (args[1].toLowerCase()) {
case "left": case "left":
int leftCommandSize = CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT).size(); int leftCommandSize = plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT).size();
if (leftCommandSize == 0) { if (leftCommandSize == 0) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NO_COMMANDS)); player.sendMessage(plugin.getLang().getMessage(Path.NO_COMMANDS));
return; return;
} }
if (commandID < 1 || commandID > leftCommandSize) { if (commandID < 1 || commandID > leftCommandSize) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_ID_NUMBER)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
return; return;
} }
click = EnumTypes.ClickType.LEFT; click = EnumTypes.ClickType.LEFT;
break; break;
case "right": case "right":
int rightCommandSize = CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT).size(); int rightCommandSize = plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT).size();
if (rightCommandSize == 0) { if (rightCommandSize == 0) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NO_COMMANDS)); player.sendMessage(plugin.getLang().getMessage(Path.NO_COMMANDS));
return; return;
} }
if (commandID < 1 || commandID > rightCommandSize) { if (commandID < 1 || commandID > rightCommandSize) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_ID_NUMBER)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
return; return;
} }
click = EnumTypes.ClickType.RIGHT; click = EnumTypes.ClickType.RIGHT;
break; break;
default: default:
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_CLICK_TYPE)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_CLICK_TYPE));
return; return;
} }
@ -112,6 +115,6 @@ public class CMDEdit extends CommandBase {
else stringBuilder.append(commandsArray[i]).append(" "); else stringBuilder.append(commandsArray[i]).append(" ");
} }
CitizensCMD.getPlugin().getDataHandler().edit(npc, commandID, click, type, stringBuilder.toString().trim(), player); plugin.getDataHandler().edit(npc, commandID, click, type, stringBuilder.toString().trim(), player);
} }
} }

View File

@ -32,23 +32,26 @@ import static me.mattmoreira.citizenscmd.utility.Util.color;
*/ */
public class CMDHelp extends CommandBase { public class CMDHelp extends CommandBase {
public CMDHelp() { private CitizensCMD plugin;
public CMDHelp(CitizensCMD plugin) {
super("help", "citizenscmd.npcmd", false, null, 0, 0); super("help", "citizenscmd.npcmd", false, null, 0, 0);
this.plugin = plugin;
} }
@Override @Override
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
JSONMessage.create(color(HEADER)).send(player); JSONMessage.create(color(HEADER)).send(player);
JSONMessage.create(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_VERSION) + " &c&o" + CitizensCMD.getPlugin().getDescription().getVersion())).send(player); JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.HELP_VERSION) + " &c&o" + plugin.getDescription().getVersion())).send(player);
JSONMessage.create(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_INFO))).send(player); JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.HELP_INFO))).send(player);
JSONMessage.create(color("&3/npcmd &cadd &b<console &b| &bmessage &b| &bnone | &bpermission &b| &b> &6<command> &d[-l]")).suggestCommand("/npcmd add ").tooltip(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_ADD) + "\n" + CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oadd &b&ossentials.heal &6&oheal")).send(player); JSONMessage.create(color("&3/npcmd &cadd &b<console &b| &bmessage &b| &bnone | &bpermission &b| &b> &6<command> &d[-l]")).suggestCommand("/npcmd add ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_ADD) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oadd &b&ossentials.heal &6&oheal")).send(player);
JSONMessage.create(color("&3/npcmd &ccooldown &6<time>")).suggestCommand("/npcmd cooldown ").tooltip(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_COOLDOWN) + "\n" + CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&ocooldown &6&o15")).send(player); JSONMessage.create(color("&3/npcmd &ccooldown &6<time>")).suggestCommand("/npcmd cooldown ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_COOLDOWN) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&ocooldown &6&o15")).send(player);
JSONMessage.create(color("&3/npcmd &cprice &6<price>")).suggestCommand("/npcmd price ").tooltip(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_PRICE) + "\n" + CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oprice &6&o250")).send(player); JSONMessage.create(color("&3/npcmd &cprice &6<price>")).suggestCommand("/npcmd price ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_PRICE) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oprice &6&o250")).send(player);
JSONMessage.create(color("&3/npcmd &clist")).suggestCommand("/npcmd list").tooltip(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_LIST) + "\n&8" + CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&olist")).send(player); JSONMessage.create(color("&3/npcmd &clist")).suggestCommand("/npcmd list").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_LIST) + "\n&8" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&olist")).send(player);
JSONMessage.create(color("&3/npcmd &cedit &b<cmd | perm> &b<left | right> &6<id> &6<new command | new permission>")).suggestCommand("/npcmd edit ").tooltip(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_EDIT) + "\n" + CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oedit &b&ocmd &b&oright &6&o1 fly")).send(player); JSONMessage.create(color("&3/npcmd &cedit &b<cmd | perm> &b<left | right> &6<id> &6<new command | new permission>")).suggestCommand("/npcmd edit ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_EDIT) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oedit &b&ocmd &b&oright &6&o1 fly")).send(player);
JSONMessage.create(color("&3/npcmd &csound &b<sound> &d[volume] &d[pitch]")).suggestCommand("/npcmd sound ").tooltip(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_SOUND) + "\n" + CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&osound &b&oENTITY_VILLAGER_YES")).send(player); JSONMessage.create(color("&3/npcmd &csound &b<sound> &d[volume] &d[pitch]")).suggestCommand("/npcmd sound ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_SOUND) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&osound &b&oENTITY_VILLAGER_YES")).send(player);
JSONMessage.create(color("&3/npcmd &cremove &b<left | right> &6<id>")).suggestCommand("/npcmd remove ").tooltip(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_REMOVE) + "\n" + CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oremove &b&oright &6&o1")).send(player); JSONMessage.create(color("&3/npcmd &cremove &b<left | right> &6<id>")).suggestCommand("/npcmd remove ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_REMOVE) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oremove &b&oright &6&o1")).send(player);
JSONMessage.create(color("&3/npcmd &creload")).suggestCommand("/npcmd reload").tooltip(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_RELOAD) + "\n" + CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oreload")).send(player); JSONMessage.create(color("&3/npcmd &creload")).suggestCommand("/npcmd reload").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_RELOAD) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oreload")).send(player);
} }

View File

@ -32,38 +32,41 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
public class CMDList extends CommandBase { public class CMDList extends CommandBase {
public CMDList() { private CitizensCMD plugin;
super("list", "citizescmd.list", false, new String[]{"l"}, 0, 0);
public CMDList(CitizensCMD plugin) {
super("list", "citizenscmd.list", false, new String[]{"l"}, 0, 0);
this.plugin = plugin;
} }
@Override @Override
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
if (npcNotSelected(player)) return; if (npcNotSelected(plugin, player)) return;
int npc = getSelectedNpcId(player); int npc = getSelectedNpcId(player);
List<String> leftCommands = CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT) != null ? CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT) : new ArrayList<>(); List<String> leftCommands = plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT) != null ? plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT) : new ArrayList<>();
List<String> rightCommands = CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT) != null ? CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT) : new ArrayList<>(); List<String> rightCommands = plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT) != null ? plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT) : new ArrayList<>();
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
JSONMessage.create(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.LIST_COOLDOWN) + CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc))).tooltip(CitizensCMD.getPlugin().getLang().getMessage(Path.LIST_TOOLTIP)).suggestCommand("/npcmd cooldown ").send(player); JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.LIST_COOLDOWN) + plugin.getDataHandler().getNPCCooldown(npc))).tooltip(plugin.getLang().getMessage(Path.LIST_TOOLTIP)).suggestCommand("/npcmd cooldown ").send(player);
JSONMessage.create(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.LIST_PRICE) + CitizensCMD.getPlugin().getDataHandler().getPrice(npc))).tooltip(CitizensCMD.getPlugin().getLang().getMessage(Path.LIST_TOOLTIP)).suggestCommand("/npcmd price ").send(player); JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.LIST_PRICE) + plugin.getDataHandler().getPrice(npc))).tooltip(plugin.getLang().getMessage(Path.LIST_TOOLTIP)).suggestCommand("/npcmd price ").send(player);
player.sendMessage(""); player.sendMessage("");
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.LIST_COUNT_RIGHT).replace("{count}", String.valueOf(rightCommands.size()))); player.sendMessage(plugin.getLang().getMessage(Path.LIST_COUNT_RIGHT).replace("{count}", String.valueOf(rightCommands.size())));
int rightCount = 1; int rightCount = 1;
for (String command : rightCommands) { for (String command : rightCommands) {
JSONMessage.create(color("&c" + rightCount + " &7- &7" + command.replace("[", "&8[&c").replace("]", "&8]&b"))).suggestCommand("/npcmd edit cmd right " + rightCount + " ").tooltip(CitizensCMD.getPlugin().getLang().getMessage(Path.LIST_TOOLTIP)).send(player); JSONMessage.create(color("&c" + rightCount + " &7- &7" + command.replace("[", "&8[&c").replace("]", "&8]&b"))).suggestCommand("/npcmd edit cmd right " + rightCount + " ").tooltip(plugin.getLang().getMessage(Path.LIST_TOOLTIP)).send(player);
rightCount++; rightCount++;
} }
player.sendMessage(""); player.sendMessage("");
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.LIST_COUNT_LEFT).replace("{count}", String.valueOf(leftCommands.size()))); player.sendMessage(plugin.getLang().getMessage(Path.LIST_COUNT_LEFT).replace("{count}", String.valueOf(leftCommands.size())));
int leftCount = 1; int leftCount = 1;
for (String command : leftCommands) { for (String command : leftCommands) {
JSONMessage.create(color("&c" + leftCount + " &7- &7" + command.replace("[", "&8[&c").replace("]", "&8]&b"))).suggestCommand("/npcmd edit cmd left " + leftCount + " ").tooltip(CitizensCMD.getPlugin().getLang().getMessage(Path.LIST_TOOLTIP)).send(player); JSONMessage.create(color("&c" + leftCount + " &7- &7" + command.replace("[", "&8[&c").replace("]", "&8]&b"))).suggestCommand("/npcmd edit cmd left " + leftCount + " ").tooltip(plugin.getLang().getMessage(Path.LIST_TOOLTIP)).send(player);
leftCount++; leftCount++;
} }
} }

View File

@ -27,22 +27,25 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
public class CMDPrice extends CommandBase { public class CMDPrice extends CommandBase {
public CMDPrice() { private CitizensCMD plugin;
public CMDPrice(CitizensCMD plugin) {
super("price", "citizenscmd.price", false, new String[]{"p"}, 1, 1); super("price", "citizenscmd.price", false, new String[]{"p"}, 1, 1);
this.plugin = plugin;
} }
@Override @Override
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
if (npcNotSelected(player)) return; if (npcNotSelected(plugin, player)) return;
if (notDouble(args[0])) { if (notDouble(args[0])) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_PRICE)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_PRICE));
return; return;
} }
CitizensCMD.getPlugin().getDataHandler().setPrice(getSelectedNpcId(player), Double.valueOf(args[0]), player); plugin.getDataHandler().setPrice(getSelectedNpcId(player), Double.valueOf(args[0]), player);
} }
} }

View File

@ -30,74 +30,77 @@ import static me.mattmoreira.citizenscmd.utility.Util.color;
public class CMDReload extends CommandBase { public class CMDReload extends CommandBase {
public CMDReload() { private CitizensCMD plugin;
public CMDReload(CitizensCMD plugin) {
super("reload", "citizenscmd.reload", true, null, 0, 0); super("reload", "citizenscmd.reload", true, null, 0, 0);
this.plugin = plugin;
} }
@Override @Override
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
CitizensCMD.getPlugin().reloadConfig(); plugin.reloadConfig();
CitizensCMD.getPlugin().saveDefaultConfig(); plugin.saveDefaultConfig();
CitizensCMD.getPlugin().setLang(CitizensCMD.getPlugin().getConfig().getString("lang")); plugin.setLang(plugin.getConfig().getString("lang"));
if (CitizensCMD.getPlugin().getConfig().contains("cooldown-time-display")) { if (plugin.getConfig().contains("cooldown-time-display")) {
switch (CitizensCMD.getPlugin().getConfig().getString("cooldown-time-display").toLowerCase()) { switch (plugin.getConfig().getString("cooldown-time-display").toLowerCase()) {
case "short": case "short":
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.SHORT); plugin.setDisplayFormat(DisplayFormat.SHORT);
break; break;
case "medium": case "medium":
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.MEDIUM); plugin.setDisplayFormat(DisplayFormat.MEDIUM);
break; break;
case "full": case "full":
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.FULL); plugin.setDisplayFormat(DisplayFormat.FULL);
break; break;
default: default:
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.MEDIUM); plugin.setDisplayFormat(DisplayFormat.MEDIUM);
} }
} else } else
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.MEDIUM); plugin.setDisplayFormat(DisplayFormat.MEDIUM);
if (CitizensCMD.getEconomy() != null) if (CitizensCMD.getEconomy() != null)
CitizensCMD.getPlugin().setShift(CitizensCMD.getPlugin().getConfig().getBoolean("shift-confirm")); plugin.setShift(plugin.getConfig().getBoolean("shift-confirm"));
CitizensCMD.getPlugin().getDataHandler().reload(); plugin.getDataHandler().reload();
CitizensCMD.getPlugin().getCooldownHandler().reload(); plugin.getCooldownHandler().reload();
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.RELOAD)); player.sendMessage(plugin.getLang().getMessage(Path.RELOAD));
} }
@Override @Override
public void execute(CommandSender sender, String[] args) { public void execute(CommandSender sender, String[] args) {
CitizensCMD.getPlugin().reloadConfig(); plugin.reloadConfig();
CitizensCMD.getPlugin().saveConfig(); plugin.saveConfig();
CitizensCMD.getPlugin().setLang(CitizensCMD.getPlugin().getConfig().getString("lang")); plugin.setLang(plugin.getConfig().getString("lang"));
if (CitizensCMD.getPlugin().getConfig().contains("cooldonw-time-display")) { if (plugin.getConfig().contains("cooldonw-time-display")) {
switch (CitizensCMD.getPlugin().getConfig().getString("cooldonw-time-display").toLowerCase()) { switch (plugin.getConfig().getString("cooldonw-time-display").toLowerCase()) {
case "short": case "short":
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.SHORT); plugin.setDisplayFormat(DisplayFormat.SHORT);
break; break;
case "medium": case "medium":
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.MEDIUM); plugin.setDisplayFormat(DisplayFormat.MEDIUM);
break; break;
case "full": case "full":
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.FULL); plugin.setDisplayFormat(DisplayFormat.FULL);
break; break;
default: default:
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.MEDIUM); plugin.setDisplayFormat(DisplayFormat.MEDIUM);
} }
} else } else
CitizensCMD.getPlugin().setDisplayFormat(DisplayFormat.MEDIUM); plugin.setDisplayFormat(DisplayFormat.MEDIUM);
if (CitizensCMD.getEconomy() != null) if (CitizensCMD.getEconomy() != null)
CitizensCMD.getPlugin().setShift(CitizensCMD.getPlugin().getConfig().getBoolean("shift-confirm")); plugin.setShift(plugin.getConfig().getBoolean("shift-confirm"));
CitizensCMD.getPlugin().getDataHandler().reload(); plugin.getDataHandler().reload();
CitizensCMD.getPlugin().getCooldownHandler().reload(); plugin.getCooldownHandler().reload();
sender.sendMessage(color(HEADER)); sender.sendMessage(color(HEADER));
sender.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.RELOAD)); sender.sendMessage(plugin.getLang().getMessage(Path.RELOAD));
} }
} }

View File

@ -28,18 +28,21 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
public class CMDRemove extends CommandBase { public class CMDRemove extends CommandBase {
public CMDRemove() { private CitizensCMD plugin;
public CMDRemove(CitizensCMD plugin) {
super("remove", "citizenscmd.remove", false, new String[]{"delete", "del", "rem"}, 2, 2); super("remove", "citizenscmd.remove", false, new String[]{"delete", "del", "rem"}, 2, 2);
this.plugin = plugin;
} }
@Override @Override
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
if (npcNotSelected(player)) return; if (npcNotSelected(plugin, player)) return;
if (notInteger(args[1])) { if (notInteger(args[1])) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_ID_NUMBER)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
return; return;
} }
@ -49,40 +52,40 @@ public class CMDRemove extends CommandBase {
switch (args[0]) { switch (args[0]) {
case "left": case "left":
int leftCommandSize = CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT).size(); int leftCommandSize = plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT).size();
if (leftCommandSize == 0) { if (leftCommandSize == 0) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NO_COMMANDS)); player.sendMessage(plugin.getLang().getMessage(Path.NO_COMMANDS));
return; return;
} }
if (commandID < 1 || commandID > leftCommandSize) { if (commandID < 1 || commandID > leftCommandSize) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_ID_NUMBER)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
return; return;
} }
click = EnumTypes.ClickType.LEFT; click = EnumTypes.ClickType.LEFT;
break; break;
case "right": case "right":
int rightCommandSize = CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT).size(); int rightCommandSize = plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT).size();
if (rightCommandSize == 0) { if (rightCommandSize == 0) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NO_COMMANDS)); player.sendMessage(plugin.getLang().getMessage(Path.NO_COMMANDS));
return; return;
} }
if (commandID < 0 || commandID > rightCommandSize) { if (commandID < 0 || commandID > rightCommandSize) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_ID_NUMBER)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
return; return;
} }
click = EnumTypes.ClickType.RIGHT; click = EnumTypes.ClickType.RIGHT;
break; break;
default: default:
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_CLICK_TYPE)); player.sendMessage(plugin.getLang().getMessage(Path.INVALID_CLICK_TYPE));
return; return;
} }
CitizensCMD.getPlugin().getDataHandler().removeCommand(npc, commandID, click, player); plugin.getDataHandler().removeCommand(npc, commandID, click, player);
} }
} }

View File

@ -18,9 +18,7 @@
package me.mattmoreira.citizenscmd.commands; package me.mattmoreira.citizenscmd.commands;
import me.mattmoreira.citizenscmd.CitizensCMD;
import me.mattmoreira.citizenscmd.commands.base.CommandBase; import me.mattmoreira.citizenscmd.commands.base.CommandBase;
import me.mattmoreira.citizenscmd.utility.Path;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -34,12 +32,12 @@ public class CMDSound extends CommandBase {
public void execute(Player player, String[] args) { public void execute(Player player, String[] args) {
if (npcNotSelected(player)) return; //if (npcNotSelected(player)) return;
int npc = getSelectedNpcId(player); int npc = getSelectedNpcId(player);
if (args.length == 0) { if (args.length == 0) {
CitizensCMD.getPlugin().getDataHandler().removeSound(npc, player); //CitizensCMD.getPlugin().getDataHandler().removeSound(npc, player);
return; return;
} }
@ -60,11 +58,11 @@ public class CMDSound extends CommandBase {
sound = Sound.valueOf(soundString); sound = Sound.valueOf(soundString);
} catch (Exception e) { } catch (Exception e) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_SOUND)); //player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.INVALID_SOUND));
return; return;
} }
CitizensCMD.getPlugin().getDataHandler().setSound(npc, sound, volume, pitch, player); //CitizensCMD.getPlugin().getDataHandler().setSound(npc, sound, volume, pitch, player);
} }

View File

@ -40,8 +40,13 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
*/ */
public class CommandHandler implements CommandExecutor, TabCompleter, IHandler { public class CommandHandler implements CommandExecutor, TabCompleter, IHandler {
private CitizensCMD plugin;
private List<CommandBase> commands; private List<CommandBase> commands;
public CommandHandler(CitizensCMD plugin) {
this.plugin = plugin;
}
@Override @Override
public void enable() { public void enable() {
commands = new ArrayList<>(); commands = new ArrayList<>();
@ -78,13 +83,13 @@ public class CommandHandler implements CommandExecutor, TabCompleter, IHandler {
if (!command.allowConsole() && !(sender instanceof Player)) { if (!command.allowConsole() && !(sender instanceof Player)) {
sender.sendMessage(color(HEADER)); sender.sendMessage(color(HEADER));
sender.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.CONSOLE_NOT_ALLOWED)); sender.sendMessage(plugin.getLang().getMessage(Path.CONSOLE_NOT_ALLOWED));
return true; return true;
} }
if (!sender.hasPermission(command.getPermission())) { if (!sender.hasPermission(command.getPermission())) {
sender.sendMessage(color(HEADER)); sender.sendMessage(color(HEADER));
sender.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NO_PERMISSION)); sender.sendMessage(plugin.getLang().getMessage(Path.NO_PERMISSION));
return true; return true;
} }
@ -94,7 +99,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter, IHandler {
|| (command.getMaximumArguments() != -1 || (command.getMaximumArguments() != -1
&& command.getMaximumArguments() < args.length)) { && command.getMaximumArguments() < args.length)) {
sender.sendMessage(color(HEADER)); sender.sendMessage(color(HEADER));
sender.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.WRONG_USAGE)); sender.sendMessage(plugin.getLang().getMessage(Path.WRONG_USAGE));
return true; return true;
} }
@ -110,7 +115,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter, IHandler {
} }
} }
sender.sendMessage(color(HEADER)); sender.sendMessage(color(HEADER));
sender.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.WRONG_USAGE)); sender.sendMessage(plugin.getLang().getMessage(Path.WRONG_USAGE));
return true; return true;
} }
@ -202,7 +207,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter, IHandler {
*/ */
private List<String> getCommandNames(String subCMD, String[] args, int arg, Player player) { private List<String> getCommandNames(String subCMD, String[] args, int arg, Player player) {
List<String> commandNames = new ArrayList<>(); List<String> commandNames = new ArrayList<>();
String[][] argsComplete = getTabCompleteArgs(subCMD, player); String[][] argsComplete = getTabCompleteArgs(plugin, subCMD, player);
if (!args[arg - 1].equals("")) { if (!args[arg - 1].equals("")) {
for (String commandName : argsComplete[arg - 1]) { for (String commandName : argsComplete[arg - 1]) {

View File

@ -31,6 +31,7 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
public class CooldownHandler { public class CooldownHandler {
private CitizensCMD plugin;
private static File cooldownsFile; private static File cooldownsFile;
private static File dir; private static File dir;
@ -38,11 +39,15 @@ public class CooldownHandler {
private static HashMap<String, Long> cooldownData; private static HashMap<String, Long> cooldownData;
public CooldownHandler(CitizensCMD plugin) {
this.plugin = plugin;
}
/** /**
* Createst the basic of the class and starts the HashMap * Createst the basic of the class and starts the HashMap
*/ */
public void initialize() { public void initialize() {
File pluginFolder = CitizensCMD.getPlugin().getDataFolder(); File pluginFolder = plugin.getDataFolder();
dir = new File(pluginFolder + "/data"); dir = new File(pluginFolder + "/data");
cooldownsFile = new File(dir.getPath(), "cooldowns.yml"); cooldownsFile = new File(dir.getPath(), "cooldowns.yml");
cooldownsConfigurator = new YamlConfiguration(); cooldownsConfigurator = new YamlConfiguration();
@ -78,7 +83,7 @@ public class CooldownHandler {
if (!cooldownsConfigurator.contains("cooldown-data")) return; if (!cooldownsConfigurator.contains("cooldown-data")) return;
HashMap<String, Integer> cachedDataFromSaves = CitizensCMD.getPlugin().getDataHandler().getCachedCooldownByID(); HashMap<String, Integer> cachedDataFromSaves = plugin.getDataHandler().getCachedCooldownByID();
for (String parent : cooldownsConfigurator.getConfigurationSection("cooldown-data").getKeys(false)) { for (String parent : cooldownsConfigurator.getConfigurationSection("cooldown-data").getKeys(false)) {
for (String child : cooldownsConfigurator.getConfigurationSection("cooldown-data." + parent).getKeys(false)) { for (String child : cooldownsConfigurator.getConfigurationSection("cooldown-data." + parent).getKeys(false)) {
@ -136,7 +141,7 @@ public class CooldownHandler {
* @return returns in seconds the time left * @return returns in seconds the time left
*/ */
public long getTimeLeft(int npc, String uuid) { public long getTimeLeft(int npc, String uuid) {
return CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc) - getSecondsDifference(cooldownData.get("cooldown-data.npc-" + npc + "." + uuid)); return plugin.getDataHandler().getNPCCooldown(npc) - getSecondsDifference(cooldownData.get("cooldown-data.npc-" + npc + "." + uuid));
} }
/** /**
@ -148,10 +153,10 @@ public class CooldownHandler {
*/ */
public boolean onCooldown(int npc, String uuid) { public boolean onCooldown(int npc, String uuid) {
if (cooldownData.containsKey("cooldown-data.npc-" + npc + "." + uuid)) { if (cooldownData.containsKey("cooldown-data.npc-" + npc + "." + uuid)) {
if (CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc) == -1) if (plugin.getDataHandler().getNPCCooldown(npc) == -1)
return true; return true;
else else
return getSecondsDifference(cooldownData.get("cooldown-data.npc-" + npc + "." + uuid)) < CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc); return getSecondsDifference(cooldownData.get("cooldown-data.npc-" + npc + "." + uuid)) < plugin.getDataHandler().getNPCCooldown(npc);
} }
return false; return false;
} }

View File

@ -37,6 +37,7 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
public class DataHandler { public class DataHandler {
private CitizensCMD plugin;
private static File savesFile; private static File savesFile;
private static File dir; private static File dir;
@ -44,11 +45,15 @@ public class DataHandler {
private HashMap<String, Object> data; private HashMap<String, Object> data;
public DataHandler(CitizensCMD plugin) {
this.plugin = plugin;
}
/** /**
* Creates file and folder * Creates file and folder
*/ */
public void initialize() { public void initialize() {
File pluginFolder = CitizensCMD.getPlugin().getDataFolder(); File pluginFolder = plugin.getDataFolder();
dir = new File(pluginFolder + "/data"); dir = new File(pluginFolder + "/data");
savesFile = new File(dir.getPath(), "saves.yml"); savesFile = new File(dir.getPath(), "saves.yml");
dataConfigurator = new YamlConfiguration(); dataConfigurator = new YamlConfiguration();
@ -139,8 +144,8 @@ public class DataHandler {
List<String> commandListLeft = data.containsKey("npc-data.npc-" + npc + ".left-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".left-click-commands") : new ArrayList<>(); List<String> commandListLeft = data.containsKey("npc-data.npc-" + npc + ".left-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".left-click-commands") : new ArrayList<>();
if (!data.containsKey("npc-data.npc-" + npc + ".cooldown")) { if (!data.containsKey("npc-data.npc-" + npc + ".cooldown")) {
data.put("npc-data.npc-" + npc + ".cooldown", getDefaultCooldown()); data.put("npc-data.npc-" + npc + ".cooldown", getDefaultCooldown(plugin));
dataConfigurator.set("npc-data.npc-" + npc + ".cooldown", getDefaultCooldown()); dataConfigurator.set("npc-data.npc-" + npc + ".cooldown", getDefaultCooldown(plugin));
} }
if (left) commandListLeft.add("[" + permission + "] " + command); if (left) commandListLeft.add("[" + permission + "] " + command);
@ -169,12 +174,12 @@ public class DataHandler {
} }
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NPC_ADDED)); player.sendMessage(plugin.getLang().getMessage(Path.NPC_ADDED));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NPC_ADD_FAIL)); player.sendMessage(plugin.getLang().getMessage(Path.NPC_ADD_FAIL));
} }
}).start(); }).start();
} }
@ -197,12 +202,12 @@ public class DataHandler {
data.replace("npc-data.npc-" + npc + ".cooldown", cooldown); data.replace("npc-data.npc-" + npc + ".cooldown", cooldown);
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NPC_COOLDOWN_SET)); player.sendMessage(plugin.getLang().getMessage(Path.NPC_COOLDOWN_SET));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NPC_COOLDOWN_SET_ERROR)); player.sendMessage(plugin.getLang().getMessage(Path.NPC_COOLDOWN_SET_ERROR));
} }
}).start(); }).start();
} }
@ -225,7 +230,7 @@ public class DataHandler {
data.replace("npc-data.npc-" + npc + ".price", price); data.replace("npc-data.npc-" + npc + ".price", price);
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NPC_PRICE_SET)); player.sendMessage(plugin.getLang().getMessage(Path.NPC_PRICE_SET));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
@ -263,7 +268,7 @@ public class DataHandler {
data.put("npc-data.npc-" + npc + ".sound", soundProperties); data.put("npc-data.npc-" + npc + ".sound", soundProperties);
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.SOUND_ADDED)); player.sendMessage(plugin.getLang().getMessage(Path.SOUND_ADDED));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
@ -363,7 +368,7 @@ public class DataHandler {
dataConfigurator.set("npc-data.npc-" + npc + "." + click.toString().toLowerCase() + "-click-commands", commands); dataConfigurator.set("npc-data.npc-" + npc + "." + click.toString().toLowerCase() + "-click-commands", commands);
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.REMOVED_COMMAND)); player.sendMessage(plugin.getLang().getMessage(Path.REMOVED_COMMAND));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
@ -384,7 +389,7 @@ public class DataHandler {
dataConfigurator.set("npc-data.npc-" + npc + ".sound", soundProperties); dataConfigurator.set("npc-data.npc-" + npc + ".sound", soundProperties);
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.SOUND_REMOVED)); player.sendMessage(plugin.getLang().getMessage(Path.SOUND_REMOVED));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
@ -432,7 +437,7 @@ public class DataHandler {
dataConfigurator.set("npc-data.npc-" + npc + "." + click.toString().toLowerCase() + "-click-commands", commandsData); dataConfigurator.set("npc-data.npc-" + npc + "." + click.toString().toLowerCase() + "-click-commands", commandsData);
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.EDITED_COMMAND).replace("{type}", typeText)); player.sendMessage(plugin.getLang().getMessage(Path.EDITED_COMMAND).replace("{type}", typeText));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
@ -455,8 +460,8 @@ public class DataHandler {
if (dataConfigurator.contains("npc-data.npc-" + npc)) if (dataConfigurator.contains("npc-data.npc-" + npc))
dataConfigurator.set("npc-data.npc-" + npc, null); dataConfigurator.set("npc-data.npc-" + npc, null);
clearCache(); /*clearCache();
cacheData(); cacheData();*/
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {

View File

@ -32,11 +32,13 @@ import static me.mattmoreira.citizenscmd.utility.Util.*;
public class LangHandler { public class LangHandler {
private CitizensCMD plugin;
private String lang; private String lang;
private HashMap<String, String> messages; private HashMap<String, String> messages;
public LangHandler(String lang) { public LangHandler(CitizensCMD plugin, String lang) {
this.plugin = plugin;
this.lang = lang; this.lang = lang;
} }
@ -81,10 +83,10 @@ public class LangHandler {
FileConfiguration langConf; FileConfiguration langConf;
try { try {
langFile = new File(CitizensCMD.getPlugin().getDataFolder(), "lang/" + lang + ".yml"); langFile = new File(plugin.getDataFolder(), "lang/" + lang + ".yml");
langConf = new YamlConfiguration(); langConf = new YamlConfiguration();
if (!langFile.exists()) CitizensCMD.getPlugin().saveResource("lang/" + lang + ".yml", false); if (!langFile.exists()) plugin.saveResource("lang/" + lang + ".yml", false);
langConf.load(langFile); langConf.load(langFile);

View File

@ -47,8 +47,11 @@ import static me.mattmoreira.citizenscmd.utility.Util.color;
public class NPCListener implements Listener { public class NPCListener implements Listener {
public NPCListener() { private CitizensCMD plugin;
Bukkit.getMessenger().registerOutgoingPluginChannel(CitizensCMD.getPlugin(), "BungeeCord");
public NPCListener(CitizensCMD plugin) {
this.plugin = plugin;
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@ -58,55 +61,55 @@ public class NPCListener implements Listener {
if (!player.hasPermission("citizenscmd.use")) return; if (!player.hasPermission("citizenscmd.use")) return;
if (!CitizensCMD.getPlugin().getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) { if (!plugin.getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) {
if (!player.hasPermission("citizenscmd.bypass")) { if (!player.hasPermission("citizenscmd.bypass")) {
if (CitizensCMD.getPlugin().getCooldownHandler().onCooldown(npc, player.getUniqueId().toString())) { if (plugin.getCooldownHandler().onCooldown(npc, player.getUniqueId().toString())) {
String cooldownMessage; String cooldownMessage;
if (CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc) == -1) if (plugin.getDataHandler().getNPCCooldown(npc) == -1)
cooldownMessage = CitizensCMD.getPlugin().getLang().getMessage(Path.ONE_TIME_CLICK); cooldownMessage = plugin.getLang().getMessage(Path.ONE_TIME_CLICK);
else else
cooldownMessage = CitizensCMD.getPlugin().getLang().getMessage(Path.ON_COOLDOWN); cooldownMessage = plugin.getLang().getMessage(Path.ON_COOLDOWN);
player.sendMessage(cooldownMessage.replace("{time}", getFormattedTime(CitizensCMD.getPlugin().getCooldownHandler().getTimeLeft(npc, player.getUniqueId().toString()), CitizensCMD.getPlugin().getDisplayFormat()))); player.sendMessage(cooldownMessage.replace("{time}", getFormattedTime(plugin, plugin.getCooldownHandler().getTimeLeft(npc, player.getUniqueId().toString()), plugin.getDisplayFormat())));
return; return;
} }
} }
if (CitizensCMD.getPlugin().getDataHandler().hasSound(npc)) { if (plugin.getDataHandler().hasSound(npc)) {
List<String> soundProperties = CitizensCMD.getPlugin().getDataHandler().getNPCSound(npc); List<String> soundProperties = plugin.getDataHandler().getNPCSound(npc);
player.playSound(player.getLocation(), Sound.valueOf(soundProperties.get(0).toUpperCase()), Float.parseFloat(soundProperties.get(1)), Float.parseFloat(soundProperties.get(2))); player.playSound(player.getLocation(), Sound.valueOf(soundProperties.get(0).toUpperCase()), Float.parseFloat(soundProperties.get(1)), Float.parseFloat(soundProperties.get(2)));
} }
if (CitizensCMD.getPlugin().getDataHandler().hasNoCommands(npc, EnumTypes.ClickType.RIGHT)) return; if (plugin.getDataHandler().hasNoCommands(npc, EnumTypes.ClickType.RIGHT)) return;
} }
double price = CitizensCMD.getPlugin().getDataHandler().getPrice(npc); double price = plugin.getDataHandler().getPrice(npc);
if (price > 0.0) { if (price > 0.0) {
if (CitizensCMD.getEconomy() != null) { if (CitizensCMD.getEconomy() != null) {
if (!CitizensCMD.getPlugin().getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) { if (!plugin.getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) {
String messageConfirm = CitizensCMD.getPlugin().getLang().getMessage(Path.PAY_CONFIRM); String messageConfirm = plugin.getLang().getMessage(Path.PAY_CONFIRM);
if (!CitizensCMD.getPlugin().shouldShift()) if (!plugin.shouldShift())
messageConfirm = messageConfirm.replace("{shift}", ""); messageConfirm = messageConfirm.replace("{shift}", "");
else else
messageConfirm = messageConfirm.replace("{shift}", "Shift "); messageConfirm = messageConfirm.replace("{shift}", "Shift ");
messageConfirm = messageConfirm.replace("{price}", String.valueOf(price)); messageConfirm = messageConfirm.replace("{price}", String.valueOf(price));
player.sendMessage(messageConfirm); player.sendMessage(messageConfirm);
CitizensCMD.getPlugin().getWaitingList().put(player.getUniqueId().toString() + "." + npc, true); plugin.getWaitingList().put(player.getUniqueId().toString() + "." + npc, true);
new ConfirmScheduler(player, npc).runTaskLaterAsynchronously(CitizensCMD.getPlugin(), 300L); new ConfirmScheduler(plugin, player, npc).runTaskLaterAsynchronously(plugin, 300L);
return; return;
} }
if (CitizensCMD.getPlugin().shouldShift() && !player.isSneaking()) return; if (plugin.shouldShift() && !player.isSneaking()) return;
if (CitizensCMD.getEconomy().getBalance(player) < price) { if (CitizensCMD.getEconomy().getBalance(player) < price) {
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.PAY_NO_MONEY)); player.sendMessage(plugin.getLang().getMessage(Path.PAY_NO_MONEY));
return; return;
} }
CitizensCMD.getPlugin().getWaitingList().remove(player.getUniqueId().toString() + "." + npc); plugin.getWaitingList().remove(player.getUniqueId().toString() + "." + npc);
CitizensCMD.getEconomy().withdrawPlayer(player, price); CitizensCMD.getEconomy().withdrawPlayer(player, price);
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.PAY_COMPLETED).replace("{price}", String.valueOf(price))); player.sendMessage(plugin.getLang().getMessage(Path.PAY_COMPLETED).replace("{price}", String.valueOf(price)));
} }
} }
@ -114,20 +117,18 @@ public class NPCListener implements Listener {
List<String> permissions = new ArrayList<>(); List<String> permissions = new ArrayList<>();
List<String> commands = new ArrayList<>(); List<String> commands = new ArrayList<>();
for (String list : CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT)) { for (String list : plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT)) {
Pattern pattern = Pattern.compile("\\[([^]]*)] ([^]]*)"); Pattern pattern = Pattern.compile("\\[([^]]*)] ([^]]*)");
Matcher matcher = pattern.matcher(list); Matcher matcher = pattern.matcher(list);
if (matcher.find()) { if (matcher.find()) {
permissions.add(matcher.group(1)); permissions.add(matcher.group(1));
String command = matcher.group(2); String command = matcher.group(2);
if (command.contains("%p%")) if (command.contains("%p%")) command = command.replace("%p%", player.getName());
command = command.replace("%p%", player.getName()); if (command.contains("%player%")) command = command.replace("%player%", player.getName());
if (command.contains("%player%")) if (plugin.papiEnabled())
command = command.replace("%player%", player.getName());
if (CitizensCMD.getPlugin().papiEnabled())
commands.add(PlaceholderAPI.setPlaceholders((OfflinePlayer) player, command)); commands.add(PlaceholderAPI.setPlaceholders((OfflinePlayer) player, command));
else else commands.add(command);
commands.add(command);
} }
} }
@ -137,7 +138,7 @@ public class NPCListener implements Listener {
switch (permissions.get(i).toLowerCase()) { switch (permissions.get(i).toLowerCase()) {
case "console": case "console":
CitizensCMD.getPlugin().getServer().dispatchCommand(CitizensCMD.getPlugin().getServer().getConsoleSender(), commands.get(i)); plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), commands.get(i));
break; break;
case "none": case "none":
@ -151,7 +152,7 @@ public class NPCListener implements Listener {
case "message": case "message":
String finalMessage; String finalMessage;
if (commands.get(i).contains("{display}")) { if (commands.get(i).contains("{display}")) {
String tmpStr = commands.get(i).replace("{display}", CitizensCMD.getPlugin().getLang().getMessage(Path.MESSAGE_DISPLAY)); String tmpStr = commands.get(i).replace("{display}", plugin.getLang().getMessage(Path.MESSAGE_DISPLAY));
finalMessage = tmpStr.replace("{name}", event.getNPC().getFullName()); finalMessage = tmpStr.replace("{name}", event.getNPC().getFullName());
} else } else
finalMessage = commands.get(i); finalMessage = commands.get(i);
@ -159,14 +160,15 @@ public class NPCListener implements Listener {
break; break;
default: default:
CitizensCMD.getPlugin().getPermissionsManager().setPermission(player, permissions.get(i)); plugin.getPermissionsManager().setPermission(player, permissions.get(i));
player.chat("/" + commands.get(i)); player.chat("/" + commands.get(i));
CitizensCMD.getPlugin().getPermissionsManager().unsetPermission(player, permissions.get(i)); plugin.getPermissionsManager().unsetPermission(player, permissions.get(i));
} }
} }
if (!player.hasPermission("citizenscmd.bypass") || CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc) != 0) if (!player.hasPermission("citizenscmd.bypass") || plugin.getDataHandler().getNPCCooldown(npc) != 0) {
CitizensCMD.getPlugin().getCooldownHandler().addInteraction(npc, player.getUniqueId().toString(), System.nanoTime()); plugin.getCooldownHandler().addInteraction(npc, player.getUniqueId().toString(), System.currentTimeMillis());
}
} }
@ -177,49 +179,49 @@ public class NPCListener implements Listener {
if (!player.hasPermission("citizenscmd.use")) return; if (!player.hasPermission("citizenscmd.use")) return;
if (!CitizensCMD.getPlugin().getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) { if (!plugin.getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) {
if (!player.hasPermission("citizenscmd.bypass")) { if (!player.hasPermission("citizenscmd.bypass")) {
if (CitizensCMD.getPlugin().getCooldownHandler().onCooldown(npc, player.getUniqueId().toString())) { if (plugin.getCooldownHandler().onCooldown(npc, player.getUniqueId().toString())) {
String cooldownMessage; String cooldownMessage;
if (CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc) == -1) if (plugin.getDataHandler().getNPCCooldown(npc) == -1)
cooldownMessage = CitizensCMD.getPlugin().getLang().getMessage(Path.ONE_TIME_CLICK); cooldownMessage = plugin.getLang().getMessage(Path.ONE_TIME_CLICK);
else else
cooldownMessage = CitizensCMD.getPlugin().getLang().getMessage(Path.ON_COOLDOWN); cooldownMessage = plugin.getLang().getMessage(Path.ON_COOLDOWN);
player.sendMessage(cooldownMessage.replace("{time}", getFormattedTime(CitizensCMD.getPlugin().getCooldownHandler().getTimeLeft(npc, player.getUniqueId().toString()), CitizensCMD.getPlugin().getDisplayFormat()))); player.sendMessage(cooldownMessage.replace("{time}", getFormattedTime(plugin, plugin.getCooldownHandler().getTimeLeft(npc, player.getUniqueId().toString()), plugin.getDisplayFormat())));
return; return;
} }
} }
if (CitizensCMD.getPlugin().getDataHandler().hasSound(npc)) { if (plugin.getDataHandler().hasSound(npc)) {
List<String> soundProperties = CitizensCMD.getPlugin().getDataHandler().getNPCSound(npc); List<String> soundProperties = plugin.getDataHandler().getNPCSound(npc);
player.playSound(player.getLocation(), Sound.valueOf(soundProperties.get(0)), Float.parseFloat(soundProperties.get(1)), Float.parseFloat(soundProperties.get(2))); player.playSound(player.getLocation(), Sound.valueOf(soundProperties.get(0)), Float.parseFloat(soundProperties.get(1)), Float.parseFloat(soundProperties.get(2)));
} }
if (CitizensCMD.getPlugin().getDataHandler().hasNoCommands(npc, EnumTypes.ClickType.LEFT)) return; if (plugin.getDataHandler().hasNoCommands(npc, EnumTypes.ClickType.LEFT)) return;
} }
double price = CitizensCMD.getPlugin().getDataHandler().getPrice(npc); double price = plugin.getDataHandler().getPrice(npc);
if (price > 0.0) { if (price > 0.0) {
if (CitizensCMD.getEconomy() != null) { if (CitizensCMD.getEconomy() != null) {
if (!CitizensCMD.getPlugin().getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) { if (!plugin.getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) {
String messageConfirm = CitizensCMD.getPlugin().getLang().getMessage(Path.PAY_CONFIRM); String messageConfirm = plugin.getLang().getMessage(Path.PAY_CONFIRM);
if (!CitizensCMD.getPlugin().shouldShift()) if (!plugin.shouldShift())
messageConfirm = messageConfirm.replace("{shift}", ""); messageConfirm = messageConfirm.replace("{shift}", "");
else else
messageConfirm = messageConfirm.replace("{shift}", "Shift "); messageConfirm = messageConfirm.replace("{shift}", "Shift ");
messageConfirm = messageConfirm.replace("{price}", String.valueOf(price)); messageConfirm = messageConfirm.replace("{price}", String.valueOf(price));
player.sendMessage(messageConfirm); player.sendMessage(messageConfirm);
CitizensCMD.getPlugin().getWaitingList().put(player.getUniqueId().toString() + "." + npc, true); plugin.getWaitingList().put(player.getUniqueId().toString() + "." + npc, true);
new ConfirmScheduler(player, npc).runTaskLaterAsynchronously(CitizensCMD.getPlugin(), 300L); new ConfirmScheduler(plugin, player, npc).runTaskLaterAsynchronously(plugin, 300L);
return; return;
} }
if (CitizensCMD.getPlugin().shouldShift() && !player.isSneaking()) return; if (plugin.shouldShift() && !player.isSneaking()) return;
CitizensCMD.getPlugin().getWaitingList().remove(player.getUniqueId().toString() + "." + npc); plugin.getWaitingList().remove(player.getUniqueId().toString() + "." + npc);
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.PAY_CANCELED)); player.sendMessage(plugin.getLang().getMessage(Path.PAY_CANCELED));
} }
} }
@ -227,7 +229,7 @@ public class NPCListener implements Listener {
List<String> permissions = new ArrayList<>(); List<String> permissions = new ArrayList<>();
List<String> commands = new ArrayList<>(); List<String> commands = new ArrayList<>();
for (String list : CitizensCMD.getPlugin().getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT)) { for (String list : plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.LEFT)) {
Pattern pattern = Pattern.compile("\\[([^]]*)] ([^]]*)"); Pattern pattern = Pattern.compile("\\[([^]]*)] ([^]]*)");
Matcher matcher = pattern.matcher(list); Matcher matcher = pattern.matcher(list);
if (matcher.find()) { if (matcher.find()) {
@ -237,7 +239,7 @@ public class NPCListener implements Listener {
command = command.replace("%p%", player.getName()); command = command.replace("%p%", player.getName());
if (command.contains("%player%")) if (command.contains("%player%"))
command = command.replace("%player%", player.getName()); command = command.replace("%player%", player.getName());
if (CitizensCMD.getPlugin().papiEnabled()) if (plugin.papiEnabled())
commands.add(PlaceholderAPI.setPlaceholders((OfflinePlayer) player, command)); commands.add(PlaceholderAPI.setPlaceholders((OfflinePlayer) player, command));
else else
commands.add(command); commands.add(command);
@ -250,7 +252,7 @@ public class NPCListener implements Listener {
switch (permissions.get(i).toLowerCase()) { switch (permissions.get(i).toLowerCase()) {
case "console": case "console":
CitizensCMD.getPlugin().getServer().dispatchCommand(CitizensCMD.getPlugin().getServer().getConsoleSender(), commands.get(i)); plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), commands.get(i));
break; break;
case "none": case "none":
@ -264,7 +266,7 @@ public class NPCListener implements Listener {
case "message": case "message":
String finalMessage; String finalMessage;
if (commands.get(i).contains("{display}")) { if (commands.get(i).contains("{display}")) {
String tmpStr = commands.get(i).replace("{display}", CitizensCMD.getPlugin().getLang().getMessage(Path.MESSAGE_DISPLAY)); String tmpStr = commands.get(i).replace("{display}", plugin.getLang().getMessage(Path.MESSAGE_DISPLAY));
finalMessage = tmpStr.replace("{name}", event.getNPC().getFullName()); finalMessage = tmpStr.replace("{name}", event.getNPC().getFullName());
} else } else
finalMessage = commands.get(i); finalMessage = commands.get(i);
@ -272,19 +274,20 @@ public class NPCListener implements Listener {
break; break;
default: default:
CitizensCMD.getPlugin().getPermissionsManager().setPermission(player, permissions.get(i)); plugin.getPermissionsManager().setPermission(player, permissions.get(i));
player.chat("/" + commands.get(i)); player.chat("/" + commands.get(i));
CitizensCMD.getPlugin().getPermissionsManager().unsetPermission(player, permissions.get(i)); plugin.getPermissionsManager().unsetPermission(player, permissions.get(i));
} }
} }
if (!player.hasPermission("citizenscmd.bypass") || CitizensCMD.getPlugin().getDataHandler().getNPCCooldown(npc) != 0) if (!player.hasPermission("citizenscmd.bypass") || plugin.getDataHandler().getNPCCooldown(npc) != 0) {
CitizensCMD.getPlugin().getCooldownHandler().addInteraction(npc, player.getUniqueId().toString(), System.nanoTime()); plugin.getCooldownHandler().addInteraction(npc, player.getUniqueId().toString(), System.currentTimeMillis());
}
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onRemoveNPC(NPCRemoveEvent event) { public void onRemoveNPC(NPCRemoveEvent event) {
CitizensCMD.getPlugin().getDataHandler().removeNPCData(event.getNPC().getId()); plugin.getDataHandler().removeNPCData(event.getNPC().getId());
} }
/** /**
@ -302,7 +305,7 @@ public class NPCListener implements Listener {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
player.sendPluginMessage(CitizensCMD.getPlugin(), "BungeeCord", byteArrayOutputStream.toByteArray()); player.sendPluginMessage(plugin, "BungeeCord", byteArrayOutputStream.toByteArray());
} }
} }

View File

@ -31,12 +31,18 @@ import static me.mattmoreira.citizenscmd.utility.Util.color;
public class UpdateEvent implements Listener { public class UpdateEvent implements Listener {
private CitizensCMD plugin;
public UpdateEvent(CitizensCMD plugin) {
this.plugin = plugin;
}
@EventHandler (priority = EventPriority.NORMAL) @EventHandler (priority = EventPriority.NORMAL)
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
if (CitizensCMD.getPlugin().getUpdateStatus() && event.getPlayer().hasPermission("citizenscmd.update")) { if (plugin.getUpdateStatus() && event.getPlayer().hasPermission("citizenscmd.update")) {
JSONMessage.create(color(HEADER)).send(event.getPlayer()); JSONMessage.create(color(HEADER)).send(event.getPlayer());
JSONMessage.create(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.NEW_VERSION) + CitizensCMD.getPlugin().getNewVersion())).send(event.getPlayer()); JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.NEW_VERSION) + plugin.getNewVersion())).send(event.getPlayer());
JSONMessage.create(color(CitizensCMD.getPlugin().getLang().getUncoloredMessage(Path.DOWNLOAD_AT) + " spigotmc.org/resources/citizens-CMD.30224/")).openURL("https://spigotmc.org/resources/citizens-CMD.30224/").send(event.getPlayer()); JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.DOWNLOAD_AT) + " spigotmc.org/resources/citizens-CMD.30224/")).openURL("https://spigotmc.org/resources/citizens-CMD.30224/").send(event.getPlayer());
} }
} }

View File

@ -29,8 +29,10 @@ import java.util.UUID;
public class PermissionsManager { public class PermissionsManager {
private HashMap<UUID, PermissionAttachment> permissionsData; private HashMap<UUID, PermissionAttachment> permissionsData;
private CitizensCMD plugin;
public PermissionsManager() { public PermissionsManager(CitizensCMD plugin) {
this.plugin = plugin;
permissionsData = new HashMap<>(); permissionsData = new HashMap<>();
} }
@ -41,7 +43,7 @@ public class PermissionsManager {
* @param permission The permission node * @param permission The permission node
*/ */
public void setPermission(Player player, String permission) { public void setPermission(Player player, String permission) {
PermissionAttachment permissionAttachment = player.addAttachment(CitizensCMD.getPlugin()); PermissionAttachment permissionAttachment = player.addAttachment(plugin);
permissionsData.put(player.getUniqueId(), permissionAttachment); permissionsData.put(player.getUniqueId(), permissionAttachment);
PermissionAttachment permissionAttachment1 = permissionsData.get(player.getUniqueId()); PermissionAttachment permissionAttachment1 = permissionsData.get(player.getUniqueId());
permissionAttachment1.setPermission(permission, true); permissionAttachment1.setPermission(permission, true);

View File

@ -27,10 +27,12 @@ public class ConfirmScheduler extends BukkitRunnable {
private Player player; private Player player;
private int npc; private int npc;
private CitizensCMD plugin;
public ConfirmScheduler(Player player, int npc) { public ConfirmScheduler(CitizensCMD plugin, Player player, int npc) {
this.player = player; this.player = player;
this.npc = npc; this.npc = npc;
this.plugin = plugin;
} }
/** /**
@ -38,9 +40,9 @@ public class ConfirmScheduler extends BukkitRunnable {
*/ */
@Override @Override
public void run() { public void run() {
if (CitizensCMD.getPlugin().getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) { if (plugin.getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) {
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.PAY_CANCELED)); player.sendMessage(plugin.getLang().getMessage(Path.PAY_CANCELED));
CitizensCMD.getPlugin().getWaitingList().remove(player.getUniqueId().toString() + "." + npc); plugin.getWaitingList().remove(player.getUniqueId().toString() + "." + npc);
} }
} }

View File

@ -23,12 +23,18 @@ import org.bukkit.scheduler.BukkitRunnable;
public class CooldownScheduler extends BukkitRunnable { public class CooldownScheduler extends BukkitRunnable {
private CitizensCMD plugin;
public CooldownScheduler(CitizensCMD plugin) {
this.plugin = plugin;
}
/** /**
* Saves the cached cooldowns on file every 30 minutes * Saves the cached cooldowns on file every 30 minutes
*/ */
@Override @Override
public void run() { public void run() {
CitizensCMD.getPlugin().getCooldownHandler().saveToFile(); plugin.getCooldownHandler().saveToFile();
} }
} }

View File

@ -24,16 +24,22 @@ import org.bukkit.scheduler.BukkitRunnable;
public class UpdateScheduler extends BukkitRunnable { public class UpdateScheduler extends BukkitRunnable {
private CitizensCMD plugin;
public UpdateScheduler(CitizensCMD plugin) {
this.plugin = plugin;
}
/** /**
* Checks for updates every hour and tells the player on join * Checks for updates every hour and tells the player on join
*/ */
@Override @Override
public void run() { public void run() {
SpigotUpdater updater = new SpigotUpdater(CitizensCMD.getPlugin(), 30224); SpigotUpdater updater = new SpigotUpdater(plugin, 30224);
try { try {
if (updater.checkForUpdates()) { if (updater.checkForUpdates()) {
CitizensCMD.getPlugin().setUpdateStatus(true); plugin.setUpdateStatus(true);
CitizensCMD.getPlugin().setNewVersion(updater.getLatestVersion()); plugin.setNewVersion(updater.getLatestVersion());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -47,13 +47,13 @@ public class TimeUtil {
* @param seconds The time in seconds to be converted * @param seconds The time in seconds to be converted
* @return String with the time like "2d 2h 2m 2s" * @return String with the time like "2d 2h 2m 2s"
*/ */
public static String getFormattedTime(long seconds, DisplayFormat format) { public static String getFormattedTime(CitizensCMD plugin, long seconds, DisplayFormat format) {
String messagesString[] = new String[4]; String messagesString[] = new String[4];
messagesString[0] = CitizensCMD.getPlugin().getLang().getMessage(Path.SECONDS); messagesString[0] = plugin.getLang().getMessage(Path.SECONDS);
messagesString[1] = CitizensCMD.getPlugin().getLang().getMessage(Path.MINUTES); messagesString[1] = plugin.getLang().getMessage(Path.MINUTES);
messagesString[2] = CitizensCMD.getPlugin().getLang().getMessage(Path.HOURS); messagesString[2] = plugin.getLang().getMessage(Path.HOURS);
messagesString[3] = CitizensCMD.getPlugin().getLang().getMessage(Path.DAYS); messagesString[3] = plugin.getLang().getMessage(Path.DAYS);
String shorts[] = new String[4]; String shorts[] = new String[4];
String mediums[] = new String[4]; String mediums[] = new String[4];

View File

@ -27,14 +27,9 @@ import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class Util { public class Util {
@ -90,11 +85,11 @@ public class Util {
* @param player The player to check if it has any NPC selected or not * @param player The player to check if it has any NPC selected or not
* @return Returns true if has an NPC selected and false if not * @return Returns true if has an NPC selected and false if not
*/ */
public static boolean npcNotSelected(Player player) { public static boolean npcNotSelected(CitizensCMD plugin, Player player) {
if (CitizensAPI.getDefaultNPCSelector().getSelected(player) != null) return false; if (CitizensAPI.getDefaultNPCSelector().getSelected(player) != null) return false;
player.sendMessage(color(HEADER)); player.sendMessage(color(HEADER));
player.sendMessage(CitizensCMD.getPlugin().getLang().getMessage(Path.NO_NPC)); player.sendMessage(plugin.getLang().getMessage(Path.NO_NPC));
return true; return true;
} }
@ -123,8 +118,8 @@ public class Util {
* *
* @return Returns true if CheckUpdates is true on the config and false if not * @return Returns true if CheckUpdates is true on the config and false if not
*/ */
public static boolean upCheck() { public static boolean upCheck(CitizensCMD plugin) {
return CitizensCMD.getPlugin().getConfig().getBoolean("check-updates"); return plugin.getConfig().getBoolean("check-updates");
} }
/** /**
@ -151,8 +146,8 @@ public class Util {
* *
* @return returns the seconds from the config * @return returns the seconds from the config
*/ */
public static int getDefaultCooldown() { public static int getDefaultCooldown(CitizensCMD plugin) {
return CitizensCMD.getPlugin().getConfig().getInt("default-cooldown"); return plugin.getConfig().getInt("default-cooldown");
} }
/** /**
@ -160,7 +155,7 @@ public class Util {
* *
* @return Returns 2d string array with arguments for tab completion * @return Returns 2d string array with arguments for tab completion
*/ */
public static String[][] getTabCompleteArgs(String subCMD, Player player) { public static String[][] getTabCompleteArgs(CitizensCMD plugin, String subCMD, Player player) {
String[][] argComplete = new String[5][]; String[][] argComplete = new String[5][];
switch (subCMD.toLowerCase()) { switch (subCMD.toLowerCase()) {
@ -169,14 +164,14 @@ public class Util {
break; break;
case "remove": case "remove":
argComplete[0] = new String[]{"left", "right"}; argComplete[0] = new String[]{"left", "right"};
argComplete[1] = CitizensCMD.getPlugin().getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.LEFT); argComplete[1] = plugin.getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.LEFT);
argComplete[2] = CitizensCMD.getPlugin().getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.RIGHT); argComplete[2] = plugin.getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.RIGHT);
break; break;
case "edit": case "edit":
argComplete[0] = new String[]{"perm", "cmd"}; argComplete[0] = new String[]{"perm", "cmd"};
argComplete[1] = new String[]{"left", "right"}; argComplete[1] = new String[]{"left", "right"};
argComplete[2] = CitizensCMD.getPlugin().getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.LEFT); argComplete[2] = plugin.getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.LEFT);
argComplete[3] = CitizensCMD.getPlugin().getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.RIGHT); argComplete[3] = plugin.getDataHandler().getCompleteCommandsNumbers(getSelectedNpcId(player), EnumTypes.ClickType.RIGHT);
argComplete[4] = new String[]{"console", "none", "permission", "server", "message"}; argComplete[4] = new String[]{"console", "none", "permission", "server", "message"};
break; break;
@ -205,13 +200,13 @@ public class Util {
* @return returns the difference in seconds * @return returns the difference in seconds
*/ */
public static long getSecondsDifference(long storedTime) { public static long getSecondsDifference(long storedTime) {
return TimeUnit.SECONDS.convert((System.nanoTime() - storedTime), TimeUnit.NANOSECONDS); return TimeUnit.SECONDS.convert((System.currentTimeMillis() - storedTime), TimeUnit.MILLISECONDS);
} }
/** /**
* Checks for old config and renames it * Checks for old config and renames it
*/ */
public static void checkOldConfig() { public static void checkOldConfig(CitizensCMD plugin) {
File configFile; File configFile;
File configFileNew; File configFileNew;
FileConfiguration configConf; FileConfiguration configConf;
@ -224,8 +219,8 @@ public class Util {
} }
try { try {
configFile = new File(CitizensCMD.getPlugin().getDataFolder(), "config.yml"); configFile = new File(plugin.getDataFolder(), "config.yml");
configFileNew = new File(CitizensCMD.getPlugin().getDataFolder(), "config_old.yml"); configFileNew = new File(plugin.getDataFolder(), "config_old.yml");
configConf = new YamlConfiguration(); configConf = new YamlConfiguration();
if (configFile.exists()) { if (configFile.exists()) {
@ -252,29 +247,14 @@ public class Util {
} }
} }
public static void downloadCitizens() { public static void disablePlugin(CitizensCMD plugin) {
try { info(color(TAG + "&cCitizens &7is needed for this plugin to work!"));
Files.copy(new URL("http://ci.citizensnpcs.co/job/Citizens2/lastSuccessfulBuild/artifact/dist/target/citizens-2.0.24-SNAPSHOT.jar").openStream(), new File(Bukkit.getServer().getUpdateFolderFile().getParentFile(), "Citizens.jar").toPath()); info(color(TAG + "&cCitizens.jar &7is not installed on the server!"));
} catch (IOException e) { info(color(TAG + "&cDisabling CitizensCMD..."));
e.printStackTrace(); Bukkit.getServer().getPluginManager().disablePlugin(plugin);
}
}
public static void loadCitizens() {
File pluginFolder = Bukkit.getServer().getUpdateFolderFile().getParentFile();
Plugin loadedPlugin;
try {
loadedPlugin = Bukkit.getPluginManager().loadPlugin(new File(pluginFolder, "Citizens.jar"));
loadedPlugin.onLoad();
Bukkit.getPluginManager().enablePlugin(loadedPlugin);
} catch (InvalidPluginException | InvalidDescriptionException e) {
e.printStackTrace();
}
} }
public static boolean doesCitizensExist() { public static boolean doesCitizensExist() {
File pluginFolder = Bukkit.getServer().getUpdateFolderFile().getParentFile(); return Bukkit.getPluginManager().isPluginEnabled("Citizens");
File citizens = new File(pluginFolder, "Citizens.jar");
return citizens.exists();
} }
} }