Retain cross-version compatibility & cleanup (#374)

This commit is contained in:
ArtelGG
2018-12-17 22:58:55 -05:00
committed by Trent Hensler
parent df624afedc
commit 88e0351826
25 changed files with 160 additions and 238 deletions
@@ -51,14 +51,14 @@ import java.util.UUID;
import java.util.logging.Level;
public class PlayerVaults extends JavaPlugin {
private static PlayerVaults instance;
public static boolean DEBUG = false;
private static PlayerVaults instance;
private final HashMap<String, SignSetInfo> setSign = new HashMap<>();
// Player name - VaultViewInfo
private final HashMap<String, VaultViewInfo> inVault = new HashMap<>();
// VaultViewInfo - Inventory
private final HashMap<String, Inventory> openInventories = new HashMap<>();
private final Set<Material> blockedMats = new HashSet<>();
private Economy economy = null;
private boolean useVault = false;
private YamlConfiguration signs;
@@ -68,7 +68,24 @@ public class PlayerVaults extends JavaPlugin {
private File backupsFolder = null;
private File uuidData;
private File vaultData;
private final Set<Material> blockedMats = new HashSet<>();
private String _versionString;
public static PlayerVaults getInstance() {
return instance;
}
public static void debug(String s, long start) {
long elapsed = System.currentTimeMillis() - start;
if (DEBUG || elapsed > 4) {
Bukkit.getLogger().log(Level.INFO, "At {0}. Time since start: {1}ms", new Object[]{s, (elapsed)});
}
}
public static void debug(String s) {
if (DEBUG) {
Bukkit.getLogger().log(Level.INFO, s);
}
}
@Override
public void onEnable() {
@@ -144,7 +161,7 @@ public class PlayerVaults extends JavaPlugin {
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("pvreload")) {
reloadConfig();
loadConfig(); // To update blocked materials.
@@ -362,20 +379,19 @@ public class PlayerVaults extends JavaPlugin {
return blockedMats.contains(mat);
}
public static PlayerVaults getInstance() {
return instance;
}
public static void debug(String s, long start) {
long elapsed = System.currentTimeMillis() - start;
if (DEBUG || elapsed > 4) {
Bukkit.getLogger().log(Level.INFO, "At {0}. Time since start: {1}ms", new Object[]{s, (elapsed)});
}
}
public static void debug(String s) {
if (DEBUG) {
Bukkit.getLogger().log(Level.INFO, s);
/**
* Tries to grab the server version as a string.
*
* @return Version as raw string
*/
public String getVersion() {
if (_versionString == null) {
if (Bukkit.getServer() == null) {
return null;
}
final String name = Bukkit.getServer().getClass().getPackage().getName();
_versionString = name.substring(name.lastIndexOf(46) + 1) + ".";
}
return _versionString;
}
}
@@ -26,7 +26,7 @@ public class ConvertCommand implements CommandExecutor {
sender.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
} else {
if (args.length == 0) {
sender.sendMessage(Lang.TITLE + "/pvconvert <all | plugin name>");
sender.sendMessage(Lang.TITLE + "/" + label + " <all | plugin name>");
} else {
String name = args[0];
final List<Converter> applicableConverters = new ArrayList<>();
@@ -39,7 +39,6 @@ public class ConvertCommand implements CommandExecutor {
}
}
}
if (applicableConverters.size() <= 0) {
sender.sendMessage(Lang.TITLE.toString() + Lang.CONVERT_PLUGIN_NOT_FOUND);
} else {
@@ -50,21 +49,18 @@ public class ConvertCommand implements CommandExecutor {
public void run() {
int converted = 0;
VaultOperations.setLocked(true);
for (Converter converter : applicableConverters) {
if (converter.canConvert()) {
converted += converter.run(sender);
}
}
VaultOperations.setLocked(false);
sender.sendMessage(Lang.TITLE + Lang.CONVERT_COMPLETE.toString().replace("%converted", converted + ""));
}
}, 5); // This comment is to annoy evilmidget38
}, 5);
}
}
}
return true;
}
}
}
@@ -18,7 +18,6 @@ public class DeleteCommand implements CommandExecutor {
sender.sendMessage(Lang.TITLE + Lang.LOCKED.toString());
return true;
}
switch (args.length) {
case 1:
if (sender instanceof Player) {
@@ -40,15 +39,13 @@ public class DeleteCommand implements CommandExecutor {
sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_OTHER_VAULT_ALL.toString().replaceAll("%p", target));
break;
}
VaultOperations.deleteOtherVault(sender, target, args[1]);
break;
default:
sender.sendMessage(Lang.TITLE + "/pvdel <number>");
sender.sendMessage(Lang.TITLE + "/pvdel <player> <number>");
sender.sendMessage(Lang.TITLE + "/pvdel <player> all");
sender.sendMessage(Lang.TITLE + "/" + label + " <number>");
sender.sendMessage(Lang.TITLE + "/" + label + " <player> <number>");
sender.sendMessage(Lang.TITLE + "/" + label + " <player> all");
}
return true;
}
}
@@ -19,7 +19,7 @@ public class SignCommand implements CommandExecutor {
i = Integer.parseInt(args[0]);
} catch (NumberFormatException nfe) {
sender.sendMessage(Lang.TITLE.toString() + Lang.MUST_BE_NUMBER);
sender.sendMessage(Lang.TITLE.toString() + "Usage: /" + label + " <owner> <#>");
sender.sendMessage(Lang.TITLE.toString() + "Usage: /" + label + " [owner] <#>");
return true;
}
PlayerVaults.getInstance().getSetSign().put(sender.getName(), new SignSetInfo(i));
@@ -30,7 +30,7 @@ public class SignCommand implements CommandExecutor {
i = Integer.parseInt(args[1]);
} catch (NumberFormatException nfe) {
sender.sendMessage(Lang.TITLE.toString() + Lang.MUST_BE_NUMBER);
sender.sendMessage(Lang.TITLE.toString() + "Usage: /" + label + " <owner> <#>");
sender.sendMessage(Lang.TITLE.toString() + "Usage: /" + label + " [owner] <#>");
return true;
}
PlayerVaults.getInstance().getSetSign().put(sender.getName(), new SignSetInfo(args[0].toLowerCase(), i));
@@ -22,8 +22,8 @@ package com.drtshock.playervaults.commands;
*/
public class SignSetInfo {
private String owner;
private final int number;
private String owner;
private boolean self = false;
/**
@@ -22,7 +22,6 @@ import com.drtshock.playervaults.vaultmanagement.VaultManager;
import com.drtshock.playervaults.vaultmanagement.VaultViewInfo;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -37,8 +36,6 @@ import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@SuppressWarnings("unused")
public class Listeners implements Listener {
public final PlayerVaults plugin;
@@ -89,10 +86,7 @@ public class Listeners implements Listener {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onClose(InventoryCloseEvent event) {
HumanEntity he = event.getPlayer();
if (he instanceof Player) {
saveVault((Player) he, event.getInventory());
}
saveVault((Player) event.getPlayer(), event.getInventory());
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
@@ -33,11 +33,11 @@ public class SignListener implements Listener {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
Block block = event.getClickedBlock();
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (PlayerVaults.getInstance().getInVault().containsKey(player.getUniqueId().toString())) {
Block block = event.getClickedBlock();
// Different inventories that we don't want the player to open.
if (block.getType() == Material.CHEST || block.getType() == Material.TRAPPED_CHEST || block.getType() == Material.ENDER_CHEST || block.getType() == Material.FURNACE || block.getType() == Material.BREWING_STAND || block.getType() == Material.ENCHANTING_TABLE || block.getType() == Material.BEACON) {
if (isInvalidBlock(block.getType())) {
event.setCancelled(true);
}
}
@@ -49,8 +49,8 @@ public class SignListener implements Listener {
PlayerVaults.getInstance().getSetSign().remove(player.getName());
event.setCancelled(true);
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (event.getClickedBlock().getType() == Material.WALL_SIGN || event.getClickedBlock().getType() == Material.SIGN) {
Sign s = (Sign) event.getClickedBlock().getState();
if (isValidSign(block.getType())) {
Sign s = (Sign) block.getState();
Location l = s.getLocation();
String world = l.getWorld().getName();
int x = l.getBlockX();
@@ -72,10 +72,9 @@ public class SignListener implements Listener {
}
return;
}
Block b = event.getClickedBlock();
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (b.getType() == Material.WALL_SIGN || b.getType() == Material.SIGN) {
Location l = b.getLocation();
if (isValidSign(block.getType())) {
Location l = block.getLocation();
String world = l.getWorld().getName();
int x = l.getBlockX();
int y = l.getBlockY();
@@ -154,4 +153,28 @@ public class SignListener implements Listener {
plugin.saveSigns();
}
}
}
private boolean isValidSign(Material material) {
if (PlayerVaults.getInstance().getVersion().contains("v1_13")) {
PlayerVaults.debug("[PlayerVaults] [Debug/SignListener] Sign material checked for >= 1.13");
return material == Material.SIGN || material == Material.WALL_SIGN;
}
PlayerVaults.debug("[PlayerVaults] [Debug/SignListener] Sign material checked for < 1.13");
return material == Material.valueOf("SIGN_POST") || material == Material.WALL_SIGN;
}
private boolean isInvalidBlock(Material material) {
if (PlayerVaults.getInstance().getVersion().contains("v1_13")) {
PlayerVaults.debug("[PlayerVaults] [Debug/SignListener] Block material checked for >= 1.13");
return material == Material.CHEST || material == Material.TRAPPED_CHEST
|| material == Material.ENDER_CHEST || material == Material.FURNACE
|| material == Material.BREWING_STAND || material == Material.ENCHANTING_TABLE
|| material == Material.BEACON;
}
PlayerVaults.debug("[PlayerVaults] [Debug/SignListener] Block material checked for < 1.13");
return material == Material.CHEST || material == Material.TRAPPED_CHEST
|| material == Material.ENDER_CHEST || material == Material.FURNACE
|| material == Material.valueOf("BURNING_FURNACE") || material == Material.BREWING_STAND
|| material == Material.valueOf("ENCHANTMENT_TABLE") || material == Material.BEACON;
}
}
@@ -1,19 +1,3 @@
/*
* Copyright (C) 2013 drtshock
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.translations;
import org.bukkit.ChatColor;
@@ -52,9 +36,9 @@ public enum Lang {
HELP("help", "/pv <number>"),
BLOCKED_ITEM("blocked-item", "&6%m &cis blocked from vaults");
private static YamlConfiguration LANG;
private final String path;
private final String def;
private static YamlConfiguration LANG;
/**
* Lang enum constructor.
@@ -1,19 +1,3 @@
/*
* Copyright (C) 2013 drtshock
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.vaultmanagement;
import com.drtshock.playervaults.PlayerVaults;
@@ -1,19 +1,3 @@
/*
* Copyright (C) 2013 drtshock
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.vaultmanagement;
import org.bukkit.Bukkit;
@@ -27,13 +27,21 @@ import java.util.logging.Level;
public class UUIDVaultManager {
private static UUIDVaultManager instance;
private final File directory = PlayerVaults.getInstance().getUuidData();
private final Map<String, YamlConfiguration> cachedVaultFiles = new ConcurrentHashMap<>();
public UUIDVaultManager() {
instance = this;
}
private final File directory = PlayerVaults.getInstance().getUuidData();
private final Map<String, YamlConfiguration> cachedVaultFiles = new ConcurrentHashMap<>();
/**
* Get the instance of this class.
*
* @return - instance of this class.
*/
public static UUIDVaultManager getInstance() {
return instance;
}
/**
* Saves the inventory to the specified player and vault number.
@@ -251,9 +259,7 @@ public class UUIDVaultManager {
}
public void removeCachedPlayerVaultFile(String holder) {
if (cachedVaultFiles.containsKey(holder)) {
cachedVaultFiles.remove(holder);
}
cachedVaultFiles.remove(holder);
}
/**
@@ -318,13 +324,4 @@ public class UUIDVaultManager {
PlayerVaults.getInstance().getLogger().log(Level.SEVERE, "Failed to save vault file for: " + holder, e);
}
}
/**
* Get the instance of this class.
*
* @return - instance of this class.
*/
public static UUIDVaultManager getInstance() {
return instance;
}
}
@@ -1,19 +1,3 @@
/*
* Copyright (C) 2013 drtshock
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.vaultmanagement;
import org.bukkit.inventory.Inventory;
@@ -45,6 +29,11 @@ public class VaultHolder implements InventoryHolder {
return vaultNumber;
}
@Override
public Inventory getInventory() {
return inventory;
}
/**
* Sets the inventory this vault holder holds
*
@@ -53,9 +42,4 @@ public class VaultHolder implements InventoryHolder {
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
@Override
public Inventory getInventory() {
return inventory;
}
}
@@ -23,15 +23,23 @@ import java.util.logging.Level;
public class VaultManager {
private static VaultManager instance;
private static final String VAULTKEY = "vault%d";
private static VaultManager instance;
private final File directory = PlayerVaults.getInstance().getVaultData();
private final Map<String, YamlConfiguration> cachedVaultFiles = new ConcurrentHashMap<>();
public VaultManager() {
instance = this;
}
private final File directory = PlayerVaults.getInstance().getVaultData();
private final Map<String, YamlConfiguration> cachedVaultFiles = new ConcurrentHashMap<>();
/**
* Get the instance of this class.
*
* @return - instance of this class.
*/
public static VaultManager getInstance() {
return instance;
}
/**
* Saves the inventory to the specified player and vault number.
@@ -342,13 +350,4 @@ public class VaultManager {
PlayerVaults.getInstance().getLogger().log(Level.SEVERE, "Failed to save vault file for: " + holder, e);
}
}
/**
* Get the instance of this class.
*
* @return - instance of this class.
*/
public static VaultManager getInstance() {
return instance;
}
}
@@ -1,19 +1,3 @@
/*
* Copyright (C) 2013 drtshock
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.vaultmanagement;
import com.drtshock.playervaults.PlayerVaults;
@@ -72,11 +56,11 @@ public class VaultOperations {
* @return Whether or not they have permission.
*/
public static boolean checkPerms(CommandSender sender, int number) {
if (sender.hasPermission("playervaults.amount." + String.valueOf(number))) {
if (sender.hasPermission("playervaults.amount." + number)) {
return true;
}
for (int x = number; x <= 99; x++) {
if (sender.hasPermission("playervaults.amount." + String.valueOf(x))) {
if (sender.hasPermission("playervaults.amount." + x)) {
return true;
}
}
@@ -319,7 +303,7 @@ public class VaultOperations {
if (sender.hasPermission("playervaults.delete.all")) {
VaultManager.getInstance().deleteAllVaults(holder);
PlayerVaults.getInstance().getLogger().info(String.format("%s deleted ALL vaults belonging to %s", sender.getName(), holder.toString()));
PlayerVaults.getInstance().getLogger().info(String.format("%s deleted ALL vaults belonging to %s", sender.getName(), holder));
} else {
sender.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
}
@@ -1,19 +1,3 @@
/*
* Copyright (C) 2013 drtshock
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.vaultmanagement;
/**