diff --git a/.gitignore b/.gitignore index 1595c43..9a2f0db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ -/target/classes/ -/target/generated-sources/ -/target/maven-archiver/ -/target/maven-status/ -/target/test-classes/ -/target/*-snapshot.jar +#/target/classes/ +#/target/generated-sources/ +#/target/maven-archiver/ +#/target/maven-status/ +#/target/test-classes/ +#/target/*-snapshot.jar +/target +/out \ No newline at end of file diff --git a/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceCommandManager.java b/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceCommandManager.java index 857936a..e2518a3 100644 --- a/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceCommandManager.java +++ b/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceCommandManager.java @@ -8,7 +8,10 @@ import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.function.Consumer; import static com.github.timkalkus.autoreplace.AutoReplaceMain.*; @@ -23,112 +26,112 @@ public class AutoReplaceCommandManager implements CommandExecutor, TabCompleter private final List commandsPlayer; private final List commandsConsole; - protected AutoReplaceCommandManager(AutoReplaceMain plugin){ + protected AutoReplaceCommandManager(AutoReplaceMain plugin) { this.plugin = plugin; // build command tree // ar on|off|default - List selfPermissions = Arrays.asList(plugin.arItemOwn,plugin.arToolOwn); + List selfPermissions = Arrays.asList(plugin.arItemOwn, plugin.arToolOwn); List selfToolPermission = Collections.singletonList(plugin.arToolOwn); List selfItemPermission = Collections.singletonList(plugin.arItemOwn); - List otherPermissions = Arrays.asList(plugin.arItemAll,plugin.arToolAll); + List otherPermissions = Arrays.asList(plugin.arItemAll, plugin.arToolAll); List otherToolPermission = Collections.singletonList(plugin.arToolAll); List otherItemPermission = Collections.singletonList(plugin.arItemAll); // on|off|default for self - CommandElement selfOn = new CommandElement(ENABLE,selfPermissions,false,null,new CommandExecutorHelper(null,null,null, ENABLE),this::executeCommandExecutorHelper); - CommandElement selfOff = new CommandElement(DISABLE,selfPermissions,false,null,new CommandExecutorHelper(null,null,null, DISABLE),this::executeCommandExecutorHelper); - CommandElement selfDefault = new CommandElement(DEFAULT,selfPermissions,false,null,new CommandExecutorHelper(null,null,null,DEFAULT),this::executeCommandExecutorHelper); - List selfOnOffDefault = Arrays.asList(selfOn,selfOff,selfDefault); + CommandElement selfOn = new CommandElement(ENABLE, selfPermissions, false, null, new CommandExecutorHelper(null, null, null, ENABLE), this::executeCommandExecutorHelper); + CommandElement selfOff = new CommandElement(DISABLE, selfPermissions, false, null, new CommandExecutorHelper(null, null, null, DISABLE), this::executeCommandExecutorHelper); + CommandElement selfDefault = new CommandElement(DEFAULT, selfPermissions, false, null, new CommandExecutorHelper(null, null, null, DEFAULT), this::executeCommandExecutorHelper); + List selfOnOffDefault = Arrays.asList(selfOn, selfOff, selfDefault); // tool|item for self - CommandElement selfTool = new CommandElement(TOOL,selfToolPermission,false,selfOnOffDefault,new CommandExecutorHelper(null,null,TOOL,null),this::executeCommandExecutorHelper); - CommandElement selfItem = new CommandElement(ITEM,selfItemPermission,false,selfOnOffDefault,new CommandExecutorHelper(null,null,ITEM,null),this::executeCommandExecutorHelper); + CommandElement selfTool = new CommandElement(TOOL, selfToolPermission, false, selfOnOffDefault, new CommandExecutorHelper(null, null, TOOL, null), this::executeCommandExecutorHelper); + CommandElement selfItem = new CommandElement(ITEM, selfItemPermission, false, selfOnOffDefault, new CommandExecutorHelper(null, null, ITEM, null), this::executeCommandExecutorHelper); // on|off|default for other - CommandElement otherOn = new CommandElement(ENABLE,otherPermissions,false,null,new CommandExecutorHelper(null,null,null, ENABLE),this::executeCommandExecutorHelper); - CommandElement otherOff = new CommandElement(DISABLE,otherPermissions,false,null,new CommandExecutorHelper(null,null,null, DISABLE),this::executeCommandExecutorHelper); - CommandElement otherDefault = new CommandElement(DEFAULT,otherPermissions,false,null,new CommandExecutorHelper(null,null,null,DEFAULT),this::executeCommandExecutorHelper); - List otherOnOffDefault = Arrays.asList(otherOn,otherOff,otherDefault); - List allOnOff = Arrays.asList(otherOn,otherOff); + CommandElement otherOn = new CommandElement(ENABLE, otherPermissions, false, null, new CommandExecutorHelper(null, null, null, ENABLE), this::executeCommandExecutorHelper); + CommandElement otherOff = new CommandElement(DISABLE, otherPermissions, false, null, new CommandExecutorHelper(null, null, null, DISABLE), this::executeCommandExecutorHelper); + CommandElement otherDefault = new CommandElement(DEFAULT, otherPermissions, false, null, new CommandExecutorHelper(null, null, null, DEFAULT), this::executeCommandExecutorHelper); + List otherOnOffDefault = Arrays.asList(otherOn, otherOff, otherDefault); + List allOnOff = Arrays.asList(otherOn, otherOff); // tool|item for other - CommandElement otherTool = new CommandElement(TOOL,otherToolPermission,false,otherOnOffDefault,new CommandExecutorHelper(null,null,TOOL,null),this::executeCommandExecutorHelper); - CommandElement otherItem = new CommandElement(ITEM,otherItemPermission,false,otherOnOffDefault,new CommandExecutorHelper(null,null,ITEM,null),this::executeCommandExecutorHelper); - List otherToolItemOnOffDefault = Arrays.asList(otherTool,otherItem,otherOn,otherOff,otherDefault); + CommandElement otherTool = new CommandElement(TOOL, otherToolPermission, false, otherOnOffDefault, new CommandExecutorHelper(null, null, TOOL, null), this::executeCommandExecutorHelper); + CommandElement otherItem = new CommandElement(ITEM, otherItemPermission, false, otherOnOffDefault, new CommandExecutorHelper(null, null, ITEM, null), this::executeCommandExecutorHelper); + List otherToolItemOnOffDefault = Arrays.asList(otherTool, otherItem, otherOn, otherOff, otherDefault); // player - CommandElement otherPlayer = new CommandElement(PLAYER_PLACEHOLDER,otherPermissions,false,otherToolItemOnOffDefault,new CommandExecutorHelper(null,PLAYER_PLACEHOLDER,null,null),this::executeCommandExecutorHelper); + CommandElement otherPlayer = new CommandElement(PLAYER_PLACEHOLDER, otherPermissions, false, otherToolItemOnOffDefault, new CommandExecutorHelper(null, PLAYER_PLACEHOLDER, null, null), this::executeCommandExecutorHelper); // tool|item for all - CommandElement allTool = new CommandElement(TOOL,otherToolPermission,false,allOnOff,new CommandExecutorHelper(null,null,TOOL,null),this::executeCommandExecutorHelper); - CommandElement allItem = new CommandElement(ITEM,otherItemPermission,false,allOnOff,new CommandExecutorHelper(null,null,ITEM,null),this::executeCommandExecutorHelper); - List allToolItemOnOff = Arrays.asList(allTool,allItem,otherOn,otherOff); + CommandElement allTool = new CommandElement(TOOL, otherToolPermission, false, allOnOff, new CommandExecutorHelper(null, null, TOOL, null), this::executeCommandExecutorHelper); + CommandElement allItem = new CommandElement(ITEM, otherItemPermission, false, allOnOff, new CommandExecutorHelper(null, null, ITEM, null), this::executeCommandExecutorHelper); + List allToolItemOnOff = Arrays.asList(allTool, allItem, otherOn, otherOff); // all - CommandElement allPlayer = new CommandElement(ALL_PLACEHOLDER,otherPermissions,false,allToolItemOnOff,new CommandExecutorHelper(null,ALL_PLACEHOLDER,null,null),this::executeCommandExecutorHelper); + CommandElement allPlayer = new CommandElement(ALL_PLACEHOLDER, otherPermissions, false, allToolItemOnOff, new CommandExecutorHelper(null, ALL_PLACEHOLDER, null, null), this::executeCommandExecutorHelper); // save/reload List savePermissions = Collections.singletonList(plugin.arSave); List reloadPermissions = Collections.singletonList(plugin.arReload); - CommandElement save = new CommandElement(SAVE,savePermissions,false,null,new CommandExecutorHelper(SAVE),this::executeCommandExecutorHelper); - CommandElement reload = new CommandElement(RELOAD,reloadPermissions,false,null,new CommandExecutorHelper(RELOAD),this::executeCommandExecutorHelper); - commandsPlayer = Arrays.asList(selfOn,selfOff,selfDefault,selfTool,selfItem,otherPlayer,allPlayer,save,reload); - commandsConsole = Arrays.asList(otherOn,otherOff,allTool,allItem,otherPlayer,allPlayer,save,reload); + CommandElement save = new CommandElement(SAVE, savePermissions, false, null, new CommandExecutorHelper(SAVE), this::executeCommandExecutorHelper); + CommandElement reload = new CommandElement(RELOAD, reloadPermissions, false, null, new CommandExecutorHelper(RELOAD), this::executeCommandExecutorHelper); + commandsPlayer = Arrays.asList(selfOn, selfOff, selfDefault, selfTool, selfItem, otherPlayer, allPlayer, save, reload); + commandsConsole = Arrays.asList(otherOn, otherOff, allTool, allItem, otherPlayer, allPlayer, save, reload); } - private void executeCommandExecutorHelper(CommandExecutorHelper ceh){ + private void executeCommandExecutorHelper(CommandExecutorHelper ceh) { LOG.fine(ceh.player.getName() + " executed 'autoreplace' command with: " + "target " + ceh.target + ", " + TOOL + "|" + ITEM + " " + ceh.toolItem + ", " + ENABLE + "|" + DISABLE + "|" + DEFAULT + "" + ceh.onOffDefault + ", save|reload " + ceh.saveReload); - if (ceh.saveReload!=null){ - if (ceh.saveReload.equals(SAVE)){ + if (ceh.saveReload != null) { + if (ceh.saveReload.equals(SAVE)) { plugin.saveConfigFile(); return; } - if (ceh.saveReload.equals(RELOAD)){ + if (ceh.saveReload.equals(RELOAD)) { plugin.reloadConfigFile(); return; } } - if (ALL_PLACEHOLDER.equals(ceh.target) || (!(ceh.player instanceof Player) && ceh.target==null)){ + if (ALL_PLACEHOLDER.equals(ceh.target) || (!(ceh.player instanceof Player) && ceh.target == null)) { // @all - boolean boolBoth = ceh.toolItem==null; + boolean boolBoth = ceh.toolItem == null; boolean boolItem = (boolBoth || ITEM.equals(ceh.toolItem)) && ceh.player.hasPermission(plugin.arItemAll); boolean boolTool = (boolBoth || TOOL.equals(ceh.toolItem)) && ceh.player.hasPermission(plugin.arToolAll); - if (ceh.onOffDefault==null){ + if (ceh.onOffDefault == null) { // display current default value - if (boolItem){ - ceh.player.sendMessage("default " + ITEM + " replacement is " + (plugin.getItemEnabledByDefault()?"enabled":"disabled")); + if (boolItem) { + ceh.player.sendMessage("default " + ITEM + " replacement is " + (plugin.getItemEnabledByDefault() ? "enabled" : "disabled")); } - if (boolTool){ - ceh.player.sendMessage("default " + TOOL + " replacement is " + (plugin.getToolEnabledByDefault()?"enabled":"disabled")); + if (boolTool) { + ceh.player.sendMessage("default " + TOOL + " replacement is " + (plugin.getToolEnabledByDefault() ? "enabled" : "disabled")); } } else { // set current default value - if (boolItem){ + if (boolItem) { plugin.setItemEnabledByDefault(ENABLE.equals(ceh.onOffDefault)); } - if (boolTool){ + if (boolTool) { plugin.setToolEnabledByDefault(ENABLE.equals(ceh.onOffDefault)); } } return; } // for the player himself - if (ceh.target==null) {// && ceh.player instanceof Player){ + if (ceh.target == null) {// && ceh.player instanceof Player){ Player player = (Player) ceh.player; - boolean boolBoth = ceh.toolItem==null; + boolean boolBoth = ceh.toolItem == null; boolean boolItem = (boolBoth || ITEM.equals(ceh.toolItem)) && ceh.player.hasPermission(plugin.arItemOwn); boolean boolTool = (boolBoth || TOOL.equals(ceh.toolItem)) && ceh.player.hasPermission(plugin.arToolOwn); - if (ceh.onOffDefault==null){ + if (ceh.onOffDefault == null) { // display current default value - if (boolItem){ - ceh.player.sendMessage("your " + ITEM + " replacement is " + (plugin.getPlayerItemEnabled(player)?"enabled":"disabled")); + if (boolItem) { + ceh.player.sendMessage("your " + ITEM + " replacement is " + (plugin.getPlayerItemEnabled(player) ? "enabled" : "disabled")); } - if (boolTool){ - ceh.player.sendMessage("your " + TOOL + " replacement is " + (plugin.getPlayerToolEnabled(player)?"enabled":"disabled")); + if (boolTool) { + ceh.player.sendMessage("your " + TOOL + " replacement is " + (plugin.getPlayerToolEnabled(player) ? "enabled" : "disabled")); } } else { // set current default value - if (boolItem){ + if (boolItem) { if (ceh.onOffDefault.equals(DEFAULT)) { plugin.setPlayerItem(player); } else { plugin.setPlayerItem(player, ENABLE.equals(ceh.onOffDefault)); } } - if (boolTool){ + if (boolTool) { if (ceh.onOffDefault.equals(DEFAULT)) { plugin.setPlayerTool(player); } else { @@ -139,28 +142,28 @@ private void executeCommandExecutorHelper(CommandExecutorHelper ceh){ return; } // for a specific player Player player = Bukkit.getPlayer(ceh.target); - if (player!=null){ - boolean boolBoth = ceh.toolItem==null; + if (player != null) { + boolean boolBoth = ceh.toolItem == null; boolean boolItem = (boolBoth || ITEM.equals(ceh.toolItem)) && ceh.player.hasPermission(plugin.arItemAll); boolean boolTool = (boolBoth || TOOL.equals(ceh.toolItem)) && ceh.player.hasPermission(plugin.arToolAll); - if (ceh.onOffDefault==null){ + if (ceh.onOffDefault == null) { // display current default value - if (boolItem){ - ceh.player.sendMessage(player.getDisplayName() + "'s " + ITEM + " replacement is " + (plugin.getPlayerItemEnabled(player)?"enabled":"disabled")); + if (boolItem) { + ceh.player.sendMessage(player.getDisplayName() + "'s " + ITEM + " replacement is " + (plugin.getPlayerItemEnabled(player) ? "enabled" : "disabled")); } - if (boolTool){ - ceh.player.sendMessage(player.getDisplayName() + "'s " + TOOL + " replacement is " + (plugin.getPlayerToolEnabled(player)?"enabled":"disabled")); + if (boolTool) { + ceh.player.sendMessage(player.getDisplayName() + "'s " + TOOL + " replacement is " + (plugin.getPlayerToolEnabled(player) ? "enabled" : "disabled")); } } else { // set current default value - if (boolItem){ + if (boolItem) { if (ceh.onOffDefault.equals(DEFAULT)) { plugin.setPlayerItem(player); } else { plugin.setPlayerItem(player, ENABLE.equals(ceh.onOffDefault)); } } - if (boolTool){ + if (boolTool) { if (ceh.onOffDefault.equals(DEFAULT)) { plugin.setPlayerTool(player); } else { @@ -179,13 +182,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - private boolean executeCommand(CommandSender sender, String[] args){ - if (args.length==0){ + private boolean executeCommand(CommandSender sender, String[] args) { + if (args.length == 0) { return false; } - for (CommandElement commandElement: (sender instanceof Player)?commandsPlayer:commandsConsole){ + for (CommandElement commandElement : (sender instanceof Player) ? commandsPlayer : commandsConsole) { if (commandElement.executeCommand(sender, new ArrayList<>(Arrays.asList(args)), - new CommandExecutorHelper(sender,null,null,null))){ + new CommandExecutorHelper(sender, null, null, null))) { return true; } } @@ -195,56 +198,56 @@ private boolean executeCommand(CommandSender sender, String[] args){ @Override public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { List resultList = new ArrayList<>(); - for (CommandElement commandElement: (sender instanceof Player)?commandsPlayer:commandsConsole){ + for (CommandElement commandElement : (sender instanceof Player) ? commandsPlayer : commandsConsole) { resultList.addAll(commandElement.autoCompleteCommand(sender, new ArrayList<>(Arrays.asList(args)))); } return resultList; } - private void showUsage(CommandSender sender, String label){ + private void showUsage(CommandSender sender, String label) { // "/ [|@all] [tool|item] [enable|disable|default]" //label = "autoreplace"; - if (sender instanceof Player){ + if (sender instanceof Player) { // Different settings for the player itself if (sender.hasPermission(plugin.arToolOwn) && sender.hasPermission(plugin.arItemOwn)) { - sender.sendMessage(co2+"/" + label + " (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets both your tool and item settings at once."); - sender.sendMessage(co2+"/" + label + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets either your tool or item setting."); + sender.sendMessage(co2 + "/" + label + " (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets both your tool and item settings at once."); + sender.sendMessage(co2 + "/" + label + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets either your tool or item setting."); }//only item.own permission if (!sender.hasPermission(plugin.arToolOwn) && sender.hasPermission(plugin.arItemOwn)) { - sender.sendMessage(co2+"/" + label + " [" + ITEM + "] (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets the item setting."); + sender.sendMessage(co2 + "/" + label + " [" + ITEM + "] (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets the item setting."); }//only tool.own permission if (sender.hasPermission(plugin.arToolOwn) && !sender.hasPermission(plugin.arItemOwn)) { - sender.sendMessage(co2+"/" + label + " [" + TOOL + "] (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets the tool setting."); + sender.sendMessage(co2 + "/" + label + " [" + TOOL + "] (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets the tool setting."); } // Different settings for the .all permission - if (sender.hasPermission(plugin.arItemAll) && sender.hasPermission(plugin.arToolAll)){ - sender.sendMessage(co2+"/" + label + " (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets both tool and item settings at once for specified player."); - sender.sendMessage(co2+"/" + label + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets either tool or item setting of specified player."); - sender.sendMessage(co2+"/" + label + " " + ALL_PLACEHOLDER + " (" + ENABLE + "|" + DISABLE + ")"+co0+"\n - "+co1+"sets both tool and item default settings."); - sender.sendMessage(co2+"/" + label + " " + ALL_PLACEHOLDER + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + ")"+co0+"\n - "+co1+"sets either tool or item default setting."); + if (sender.hasPermission(plugin.arItemAll) && sender.hasPermission(plugin.arToolAll)) { + sender.sendMessage(co2 + "/" + label + " (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets both tool and item settings at once for specified player."); + sender.sendMessage(co2 + "/" + label + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets either tool or item setting of specified player."); + sender.sendMessage(co2 + "/" + label + " " + ALL_PLACEHOLDER + " (" + ENABLE + "|" + DISABLE + ")" + co0 + "\n - " + co1 + "sets both tool and item default settings."); + sender.sendMessage(co2 + "/" + label + " " + ALL_PLACEHOLDER + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + ")" + co0 + "\n - " + co1 + "sets either tool or item default setting."); }// only item.all permission - if (sender.hasPermission(plugin.arItemAll) && !sender.hasPermission(plugin.arToolAll)){ - sender.sendMessage(co2+"/" + label + " [" + ITEM + "] (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets the item setting for specified player."); - sender.sendMessage(co2+"/" + label + " " + ALL_PLACEHOLDER + " [" + ITEM + "] (" + ENABLE + "|" + DISABLE + ")"+co0+"\n - "+co1+"sets item default setting."); + if (sender.hasPermission(plugin.arItemAll) && !sender.hasPermission(plugin.arToolAll)) { + sender.sendMessage(co2 + "/" + label + " [" + ITEM + "] (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets the item setting for specified player."); + sender.sendMessage(co2 + "/" + label + " " + ALL_PLACEHOLDER + " [" + ITEM + "] (" + ENABLE + "|" + DISABLE + ")" + co0 + "\n - " + co1 + "sets item default setting."); }// only tool.all permission - if (!sender.hasPermission(plugin.arItemAll) && sender.hasPermission(plugin.arToolAll)){ - sender.sendMessage(co2+"/" + label + " [" + TOOL + "] (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets the tool setting for specified player."); - sender.sendMessage(co2+"/" + label + " " + ALL_PLACEHOLDER + " [" + TOOL + "] (" + ENABLE + "|" + DISABLE + ")"+co0+"\n - "+co1+"sets tool default setting."); + if (!sender.hasPermission(plugin.arItemAll) && sender.hasPermission(plugin.arToolAll)) { + sender.sendMessage(co2 + "/" + label + " [" + TOOL + "] (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets the tool setting for specified player."); + sender.sendMessage(co2 + "/" + label + " " + ALL_PLACEHOLDER + " [" + TOOL + "] (" + ENABLE + "|" + DISABLE + ")" + co0 + "\n - " + co1 + "sets tool default setting."); } - if (sender.hasPermission(plugin.arReload)){ - sender.sendMessage(co2+"/" + label + " reload"+co0+"\n - "+co1+"load config file."); + if (sender.hasPermission(plugin.arReload)) { + sender.sendMessage(co2 + "/" + label + " reload" + co0 + "\n - " + co1 + "load config file."); } - if (sender.hasPermission(plugin.arSave)){ - sender.sendMessage(co2+"/" + label + " save"+co0+"\n - "+co1+"saves current settings to config file."); + if (sender.hasPermission(plugin.arSave)) { + sender.sendMessage(co2 + "/" + label + " save" + co0 + "\n - " + co1 + "saves current settings to config file."); } } else { //console or command block - sender.sendMessage(co2+"/" + label + " (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets both tool and item settings at once for specified player."); - sender.sendMessage(co2+"/" + label + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")"+co0+"\n - "+co1+"sets either tool or item setting of specified player."); - sender.sendMessage(co2+"/" + label + " " + ALL_PLACEHOLDER + " (" + ENABLE + "|" + DISABLE + ")"+co0+"\n - "+co1+"sets both tool and item default settings."); - sender.sendMessage(co2+"/" + label + " " + ALL_PLACEHOLDER + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + ")"+co0+"\n - "+co1+"sets either tool or item default setting."); - sender.sendMessage(co2+"/" + label + " save"+co0+"\n - "+co1+"saves current settings to config file."); - sender.sendMessage(co2+"/" + label + " reload"+co0+"\n - "+co1+"load config file."); + sender.sendMessage(co2 + "/" + label + " (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets both tool and item settings at once for specified player."); + sender.sendMessage(co2 + "/" + label + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + "|" + DEFAULT + ")" + co0 + "\n - " + co1 + "sets either tool or item setting of specified player."); + sender.sendMessage(co2 + "/" + label + " " + ALL_PLACEHOLDER + " (" + ENABLE + "|" + DISABLE + ")" + co0 + "\n - " + co1 + "sets both tool and item default settings."); + sender.sendMessage(co2 + "/" + label + " " + ALL_PLACEHOLDER + " (" + TOOL + "|" + ITEM + ") (" + ENABLE + "|" + DISABLE + ")" + co0 + "\n - " + co1 + "sets either tool or item default setting."); + sender.sendMessage(co2 + "/" + label + " save" + co0 + "\n - " + co1 + "saves current settings to config file."); + sender.sendMessage(co2 + "/" + label + " reload" + co0 + "\n - " + co1 + "load config file."); } } @@ -260,14 +263,15 @@ protected class CommandElement { /** * Initializes a new CommandElement. - * @param command defines String of command, can also be {@code } placeholder - * @param permissions List of which permissions are necessary - * @param allPermissionsNeeded whether you need at least one or all listed permissions - * @param children List of follow-up commands + * + * @param command defines String of command, can also be {@code } placeholder + * @param permissions List of which permissions are necessary + * @param allPermissionsNeeded whether you need at least one or all listed permissions + * @param children List of follow-up commands * @param commandExecutorHelper helper-object to write command information into - * @param targetMethod method to execute + * @param targetMethod method to execute */ - protected CommandElement(String command, List permissions, boolean allPermissionsNeeded, List children, CommandExecutorHelper commandExecutorHelper, Consumer targetMethod){ + protected CommandElement(String command, List permissions, boolean allPermissionsNeeded, List children, CommandExecutorHelper commandExecutorHelper, Consumer targetMethod) { this.command = command; this.children = children; this.permissions = permissions; @@ -276,14 +280,14 @@ protected CommandElement(String command, List permissions, boolean allPe this.targetMethod = targetMethod; } - protected List autoCompleteCommand(CommandSender sender, List args){ - if (args.isEmpty() || !isValid(args.get(0)) || !hasNeededPermissions(sender)){ + protected List autoCompleteCommand(CommandSender sender, List args) { + if (args.isEmpty() || !isValid(args.get(0)) || !hasNeededPermissions(sender)) { return new ArrayList<>(); } List result = new ArrayList<>(); - if (args.size()==1){ - if (command.equals(PLAYER_PLACEHOLDER)){ - for (Player player:Bukkit.matchPlayer(args.get(0))){ + if (args.size() == 1) { + if (command.equals(PLAYER_PLACEHOLDER)) { + for (Player player : Bukkit.matchPlayer(args.get(0))) { result.add(player.getDisplayName()); } } else { @@ -291,31 +295,31 @@ protected List autoCompleteCommand(CommandSender sender, List ar } return result; } - if (equalsCommand(args.get(0)) && args.size()>1 && children!=null && children.size()!=0){ + if (equalsCommand(args.get(0)) && args.size() > 1 && children != null && children.size() != 0) { args.remove(0); - for (CommandElement child:children){ + for (CommandElement child : children) { result.addAll(child.autoCompleteCommand(sender, new ArrayList<>(args))); } } return result; } - protected boolean executeCommand(CommandSender sender, List args, CommandExecutorHelper commandExecutorHelper){ - if (args.isEmpty() || !equalsCommand(args.get(0)) || !hasNeededPermissions(sender)){ + protected boolean executeCommand(CommandSender sender, List args, CommandExecutorHelper commandExecutorHelper) { + if (args.isEmpty() || !equalsCommand(args.get(0)) || !hasNeededPermissions(sender)) { return false; } - if (command.equals(PLAYER_PLACEHOLDER)){ - commandExecutorHelper.overwrite(new CommandExecutorHelper(null,args.get(0),null,null)); + if (command.equals(PLAYER_PLACEHOLDER)) { + commandExecutorHelper.overwrite(new CommandExecutorHelper(null, args.get(0), null, null)); } else { commandExecutorHelper.overwrite(this.commandExecutorHelper); } - if (args.size()==1){ + if (args.size() == 1) { targetMethod.accept(commandExecutorHelper); return true; } args.remove(0); - if (children!=null) { + if (children != null) { for (CommandElement child : children) { if (child.executeCommand(sender, new ArrayList<>(args), commandExecutorHelper)) { return true; @@ -326,20 +330,20 @@ protected boolean executeCommand(CommandSender sender, List args, Comman } - private boolean hasNeededPermissions(CommandSender sender){ - if (permissions.isEmpty()){ + private boolean hasNeededPermissions(CommandSender sender) { + if (permissions.isEmpty()) { return true; } if (allPermissionsNeeded) { for (String perm : permissions) { - if (!sender.hasPermission(perm)){ + if (!sender.hasPermission(perm)) { return false; } } return true; } else { for (String perm : permissions) { - if (sender.hasPermission(perm)){ + if (sender.hasPermission(perm)) { return true; } } @@ -347,16 +351,16 @@ private boolean hasNeededPermissions(CommandSender sender){ } } - private boolean isValid(String input){ // - if (command.equals(PLAYER_PLACEHOLDER)){ + private boolean isValid(String input) { // + if (command.equals(PLAYER_PLACEHOLDER)) { return !Bukkit.matchPlayer(input).isEmpty(); } return command.startsWith(input); } - private boolean equalsCommand(String input){ - if (command.equals(PLAYER_PLACEHOLDER)){ - return Bukkit.getPlayer(input)!=null; + private boolean equalsCommand(String input) { + if (command.equals(PLAYER_PLACEHOLDER)) { + return Bukkit.getPlayer(input) != null; } return command.equals(input); } @@ -369,34 +373,35 @@ private class CommandExecutorHelper { //helper-class to String onOffDefault = null; // on, off, default String saveReload = null; - CommandExecutorHelper(CommandSender player, String target, String toolItem, String onOffDefault){ + CommandExecutorHelper(CommandSender player, String target, String toolItem, String onOffDefault) { this.player = player; this.target = target; this.toolItem = toolItem; this.onOffDefault = onOffDefault; } - CommandExecutorHelper(String saveReload){ + + CommandExecutorHelper(String saveReload) { this.saveReload = saveReload; } - protected void overwrite(CommandExecutorHelper commandExecutorHelper){ - if (commandExecutorHelper ==null){ + protected void overwrite(CommandExecutorHelper commandExecutorHelper) { + if (commandExecutorHelper == null) { return; } - if (commandExecutorHelper.player!=null){ + if (commandExecutorHelper.player != null) { player = commandExecutorHelper.player; } - if (commandExecutorHelper.target!=null){ + if (commandExecutorHelper.target != null) { target = commandExecutorHelper.target; } - if (commandExecutorHelper.toolItem!=null){ + if (commandExecutorHelper.toolItem != null) { toolItem = commandExecutorHelper.toolItem; } - if (commandExecutorHelper.onOffDefault!=null){ + if (commandExecutorHelper.onOffDefault != null) { onOffDefault = commandExecutorHelper.onOffDefault; } - if (commandExecutorHelper.saveReload!=null){ + if (commandExecutorHelper.saveReload != null) { saveReload = commandExecutorHelper.saveReload; } } diff --git a/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceListener.java b/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceListener.java index a73a1da..f88655f 100644 --- a/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceListener.java +++ b/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceListener.java @@ -16,17 +16,17 @@ import java.util.List; import java.util.Objects; -public class AutoReplaceListener implements Listener{ +public class AutoReplaceListener implements Listener { private final AutoReplaceMain plugin; private static final String privateKey = "AutoReplaceKey"; - public AutoReplaceListener(AutoReplaceMain plugin){ + public AutoReplaceListener(AutoReplaceMain plugin) { this.plugin = plugin; } @EventHandler - public void itemDamaged(PlayerItemDamageEvent event){ + public void itemDamaged(PlayerItemDamageEvent event) { if (!event.getItem().hasItemMeta()) return; if (!(event.getItem().getItemMeta() instanceof Damageable)) @@ -36,7 +36,7 @@ public void itemDamaged(PlayerItemDamageEvent event){ ItemStack itemClone = event.getItem().clone(); Damageable tool = (Damageable) event.getItem().getItemMeta(); int itemSlot = getItemSlot(event); - if (event.getItem().getType().getMaxDurability()-tool.getDamage()<5) { + if (event.getItem().getType().getMaxDurability() - tool.getDamage() < 5) { BukkitRunnable delayEvent = new ToolDelayEvent(event, itemClone, itemSlot); delayEvent.runTask(plugin); } @@ -46,9 +46,9 @@ public void itemDamaged(PlayerItemDamageEvent event){ public void itemPlaced(PlayerInteractEvent event) { if (!event.hasItem()) return; // ignore all event-calls without an item - if (Objects.requireNonNull(event.getItem()).getMaxStackSize()==1) + if (Objects.requireNonNull(event.getItem()).getMaxStackSize() == 1) return; // ignore when tool, bucket or other non-stackable item - if (event.getItem().getAmount()>1) + if (event.getItem().getAmount() > 1) return; // ignore if initital stack size is bigger than 1 if (!plugin.getPlayerItemEnabled(event.getPlayer())) return; @@ -58,23 +58,22 @@ public void itemPlaced(PlayerInteractEvent event) { } @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) - { + public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - if(player.hasPermission(plugin.arToolForce)) { + if (player.hasPermission(plugin.arToolForce)) { forceTool(player); } - if(player.hasPermission(plugin.arItemForce)){ + if (player.hasPermission(plugin.arItemForce)) { forceItem(player); } } private void forceItem(Player player) { - if (player.hasPermission(plugin.arItemActivatedFalse)){ + if (player.hasPermission(plugin.arItemActivatedFalse)) { plugin.setPlayerItem(player, false); return; } - if (player.hasPermission(plugin.arItemActivatedTrue)){ + if (player.hasPermission(plugin.arItemActivatedTrue)) { plugin.setPlayerItem(player, true); return; } @@ -82,11 +81,11 @@ private void forceItem(Player player) { } private void forceTool(Player player) { - if (player.hasPermission(plugin.arToolActivatedFalse)){ + if (player.hasPermission(plugin.arToolActivatedFalse)) { plugin.setPlayerTool(player, false); return; } - if (player.hasPermission(plugin.arToolActivatedTrue)){ + if (player.hasPermission(plugin.arToolActivatedTrue)) { plugin.setPlayerTool(player, true); return; } @@ -94,8 +93,7 @@ private void forceTool(Player player) { } - - public static void markItem(ItemStack item){ + public static void markItem(ItemStack item) { ItemMeta imeta = item.getItemMeta(); List lore; assert imeta != null; @@ -109,7 +107,7 @@ public static void markItem(ItemStack item){ item.setItemMeta(imeta); } - public static void unmarkItem(ItemStack item){ + public static void unmarkItem(ItemStack item) { ItemMeta imeta = item.getItemMeta(); List lore; assert imeta != null; @@ -123,14 +121,14 @@ public static void unmarkItem(ItemStack item){ item.setItemMeta(imeta); } - private int getItemSlot(PlayerItemDamageEvent event){ + private int getItemSlot(PlayerItemDamageEvent event) { ItemStack item = event.getItem(); markItem(item); event.getPlayer().updateInventory(); ItemStack[] inv = event.getPlayer().getInventory().getContents(); int itemSlot = -1; - for (int i = 0; i playerItemSetting= new HashMap<>(); - protected Map playerToolSetting= new HashMap<>(); + protected Map playerItemSetting = new HashMap<>(); + protected Map playerToolSetting = new HashMap<>(); protected static final String PLAYER_PLACEHOLDER = ""; protected static final String ALL_PLACEHOLDER = "all"; @@ -66,27 +66,31 @@ public void onEnable() { } } - private void checkVersion(){ - Bukkit.getScheduler().runTaskAsynchronously(this, () -> { + private void checkVersion() { + Bukkit.getScheduler().runTaskAsynchronously(this, () -> { try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + "90887").openStream(); Scanner scanner = new Scanner(inputStream)) { if (scanner.hasNext()) { String onlineVersion = scanner.next(); String localVersion = this.getDescription().getVersion(); - int versionDifference = compareVersion(localVersion,onlineVersion); - if (versionDifference==0){ - LOG.fine("AutoReplace v" + localVersion + " is up to date."); return; + int versionDifference = compareVersion(localVersion, onlineVersion); + if (versionDifference == 0) { + LOG.fine("AutoReplace v" + localVersion + " is up to date."); + return; } - if (versionDifference==1){ + if (versionDifference == 1) { LOG.warning("AutoReplace v" + localVersion + " is majorly out of date. " + - "Please update to v" + onlineVersion); return; + "Please update to v" + onlineVersion); + return; } - if (versionDifference==2){ + if (versionDifference == 2) { LOG.info("AutoReplace v" + localVersion + " is out of date. " + - "Consider upgrading to v" + onlineVersion); return; + "Consider upgrading to v" + onlineVersion); + return; } - if (versionDifference>=3){ + if (versionDifference >= 3) { LOG.info("AutoReplace v" + localVersion + " is slightly out of date. " + - "Consider upgrading to v" + onlineVersion); return; + "Consider upgrading to v" + onlineVersion); + return; } LOG.info("AutoReplace v" + localVersion + " is newer than the latest publicly available version (" + onlineVersion + "). Unless you are working yourself on a new version this may be an error."); @@ -99,40 +103,39 @@ private void checkVersion(){ /** * Returns 0 when versions are identical, 1 when first version number is different, 2 when second is different, ... - * + *

* Result will be negative, when local version is more recent than onlineVersion. With the same version and a * single snapshot-tag the return value will be (-)1. * - * @param localVersion e.g. 0.2.4-snapshot + * @param localVersion e.g. 0.2.4-snapshot * @param onlineVersion e.g. 0.3.1 * @return ..., -2, -1, 0, 1, 2, 3, ... */ - private int compareVersion(String localVersion, String onlineVersion){ + private int compareVersion(String localVersion, String onlineVersion) { String local = extractVersionNumber(localVersion); String online = extractVersionNumber(onlineVersion); - int snapshotDiff = (localVersion.equals(local)? 0:1) + (onlineVersion.equals(online)? 0:-1); + int snapshotDiff = (localVersion.equals(local) ? 0 : 1) + (onlineVersion.equals(online) ? 0 : -1); int[] localArray = Arrays.stream(local.split("\\.")).mapToInt(Integer::parseInt).toArray(); int[] onlineArray = Arrays.stream(online.split("\\.")).mapToInt(Integer::parseInt).toArray(); - int maxLength=Math.max(localArray.length,onlineArray.length); - for (int i=0;i=localArray.length){ - return i+1; + int maxLength = Math.max(localArray.length, onlineArray.length); + for (int i = 0; i < maxLength; i++) { + if (i >= localArray.length) { + return i + 1; } - if (i>=onlineArray.length){ - return -(i+1); + if (i >= onlineArray.length) { + return -(i + 1); } - if (localArray[i]!=onlineArray[i]){ - return localArray[i]>onlineArray[i]?-(i+1):i+1; + if (localArray[i] != onlineArray[i]) { + return localArray[i] > onlineArray[i] ? -(i + 1) : i + 1; } } - return snapshotDiff*maxLength; + return snapshotDiff * maxLength; } - private String extractVersionNumber(String version){ + private String extractVersionNumber(String version) { Pattern pattern = Pattern.compile("\\d+[.\\d+]+"); Matcher matcher = pattern.matcher(version); - if (matcher.find()) - { + if (matcher.find()) { return matcher.group(0); } return ""; @@ -144,35 +147,35 @@ public void onDisable() { } public void saveConfigFile() { - config.set(toolSettingName,toolEnabledByDefault); - config.set(itemSettingName,itemEnabledByDefault); + config.set(toolSettingName, toolEnabledByDefault); + config.set(itemSettingName, itemEnabledByDefault); config.createSection(toolPlayerMap, getToolHashMap()); config.createSection(itemPlayerMap, getItemHashMap()); saveConfig(); } - private HashMap getToolHashMap(){ - HashMap returnMap = new HashMap<>(); - playerToolSetting.forEach((uuid, bool) -> returnMap.put(uuid.toString(),bool)); + private HashMap getToolHashMap() { + HashMap returnMap = new HashMap<>(); + playerToolSetting.forEach((uuid, bool) -> returnMap.put(uuid.toString(), bool)); return returnMap; } - private void setToolHashMap(Map inputMap){ - Map playerToolSetting = new HashMap<>(); - inputMap.forEach((string,bool)->playerToolSetting.put(UUID.fromString(string),(Boolean) bool)); - this.playerToolSetting=playerToolSetting; + private void setToolHashMap(Map inputMap) { + Map playerToolSetting = new HashMap<>(); + inputMap.forEach((string, bool) -> playerToolSetting.put(UUID.fromString(string), (Boolean) bool)); + this.playerToolSetting = playerToolSetting; } - private HashMap getItemHashMap(){ - HashMap returnMap = new HashMap<>(); - playerItemSetting.forEach((uuid, bool) -> returnMap.put(uuid.toString(),bool)); + private HashMap getItemHashMap() { + HashMap returnMap = new HashMap<>(); + playerItemSetting.forEach((uuid, bool) -> returnMap.put(uuid.toString(), bool)); return returnMap; } - private void setItemHashMap(Map inputMap){ - Map playerItemSetting = new HashMap<>(); - inputMap.forEach((string,bool)->playerItemSetting.put(UUID.fromString(string),(Boolean) bool)); - this.playerItemSetting=playerItemSetting; + private void setItemHashMap(Map inputMap) { + Map playerItemSetting = new HashMap<>(); + inputMap.forEach((string, bool) -> playerItemSetting.put(UUID.fromString(string), (Boolean) bool)); + this.playerItemSetting = playerItemSetting; } public void reloadConfigFile() { @@ -180,21 +183,21 @@ public void reloadConfigFile() { reloadConfig(); config = getConfig(); boolean missingParameters = false; - if (!config.contains(toolSettingName)){ - config.addDefault(toolSettingName,true); + if (!config.contains(toolSettingName)) { + config.addDefault(toolSettingName, true); missingParameters = true; } - if (!config.contains(itemSettingName)){ - config.addDefault(itemSettingName,true); + if (!config.contains(itemSettingName)) { + config.addDefault(itemSettingName, true); missingParameters = true; } - if (!config.isConfigurationSection(toolPlayerMap)){ - config.createSection(toolPlayerMap,playerToolSetting); + if (!config.isConfigurationSection(toolPlayerMap)) { + config.createSection(toolPlayerMap, playerToolSetting); //config.addDefault(toolPlayerMap,playerToolSetting); missingParameters = true; } - if (!config.isConfigurationSection(itemPlayerMap)){ - config.createSection(itemPlayerMap,playerItemSetting); + if (!config.isConfigurationSection(itemPlayerMap)) { + config.createSection(itemPlayerMap, playerItemSetting); missingParameters = true; } config.options().copyDefaults(true); @@ -202,66 +205,66 @@ public void reloadConfigFile() { itemEnabledByDefault = config.getBoolean(itemSettingName); setToolHashMap(Objects.requireNonNull(config.getConfigurationSection(toolPlayerMap)).getValues(false)); setItemHashMap(Objects.requireNonNull(config.getConfigurationSection(itemPlayerMap)).getValues(false)); - if (missingParameters){ + if (missingParameters) { saveConfigFile(); } } - public void setPlayerTool(Player player, boolean asEnabled){ - playerToolSetting.put(player.getUniqueId(),asEnabled); + public void setPlayerTool(Player player, boolean asEnabled) { + playerToolSetting.put(player.getUniqueId(), asEnabled); } - public void setPlayerTool(Player player){ + public void setPlayerTool(Player player) { playerToolSetting.remove(player.getUniqueId()); } - public void setPlayerItem(Player player, boolean asEnabled){ - playerItemSetting.put(player.getUniqueId(),asEnabled); + public void setPlayerItem(Player player, boolean asEnabled) { + playerItemSetting.put(player.getUniqueId(), asEnabled); } - public void setPlayerItem(Player player){ + public void setPlayerItem(Player player) { playerItemSetting.remove(player.getUniqueId()); } - public boolean getPlayerToolEnabled(Player player){ - if(player.hasPermission(arToolForce)){ - if (player.hasPermission(arToolActivatedFalse)){ + public boolean getPlayerToolEnabled(Player player) { + if (player.hasPermission(arToolForce)) { + if (player.hasPermission(arToolActivatedFalse)) { return false; } - if (player.hasPermission(arToolActivatedTrue)){ + if (player.hasPermission(arToolActivatedTrue)) { return true; } } UUID uuid = player.getUniqueId(); - if (playerToolSetting.containsKey(uuid)){ + if (playerToolSetting.containsKey(uuid)) { return playerToolSetting.get(uuid); } - if (player.hasPermission(arToolActivatedFalse)){ + if (player.hasPermission(arToolActivatedFalse)) { return false; } - if (player.hasPermission(arToolActivatedTrue)){ + if (player.hasPermission(arToolActivatedTrue)) { return true; } return toolEnabledByDefault; } - public boolean getPlayerItemEnabled(Player player){ - if(player.hasPermission(arItemForce)){ - if (player.hasPermission(arItemActivatedFalse)){ + public boolean getPlayerItemEnabled(Player player) { + if (player.hasPermission(arItemForce)) { + if (player.hasPermission(arItemActivatedFalse)) { return false; } - if (player.hasPermission(arItemActivatedTrue)){ + if (player.hasPermission(arItemActivatedTrue)) { return true; } } UUID uuid = player.getUniqueId(); - if (playerItemSetting.containsKey(uuid)){ + if (playerItemSetting.containsKey(uuid)) { return playerItemSetting.get(uuid); } - if (player.hasPermission(arItemActivatedFalse)){ + if (player.hasPermission(arItemActivatedFalse)) { return false; } - if (player.hasPermission(arItemActivatedTrue)){ + if (player.hasPermission(arItemActivatedTrue)) { return true; } return itemEnabledByDefault; @@ -271,7 +274,7 @@ public void setToolEnabledByDefault(boolean toolEnabledByDefault) { this.toolEnabledByDefault = toolEnabledByDefault; } - public boolean getToolEnabledByDefault(){ + public boolean getToolEnabledByDefault() { return toolEnabledByDefault; } @@ -279,7 +282,7 @@ public void setItemEnabledByDefault(boolean itemEnabledByDefault) { this.itemEnabledByDefault = itemEnabledByDefault; } - public boolean getItemEnabledByDefault(){ + public boolean getItemEnabledByDefault() { return itemEnabledByDefault; } diff --git a/src/main/java/com/github/timkalkus/autoreplace/ReplaceHelper.java b/src/main/java/com/github/timkalkus/autoreplace/ReplaceHelper.java index 9d35794..7f12097 100644 --- a/src/main/java/com/github/timkalkus/autoreplace/ReplaceHelper.java +++ b/src/main/java/com/github/timkalkus/autoreplace/ReplaceHelper.java @@ -18,7 +18,7 @@ public class ReplaceHelper { private int shulkerBoxLocation = -1; private int replacementItemSlot = -1; - public ReplaceHelper(Player player, ItemStack item, int itemSlot){ + public ReplaceHelper(Player player, ItemStack item, int itemSlot) { this.player = player; this.inventory = player.getInventory(); this.item = item; @@ -35,16 +35,15 @@ public ReplaceHelper(Player player, ItemStack item, int itemSlot){ public void replace() { findReplacement(); - if (replacementItemSlot==-1) { + if (replacementItemSlot == -1) { return; } - if (shulkerBox!=null) { - player.getInventory().setItem(itemSlot,shulkerBox.getInventory().getItem(replacementItemSlot)); + if (shulkerBox != null) { + player.getInventory().setItem(itemSlot, shulkerBox.getInventory().getItem(replacementItemSlot)); shulkerBox.getInventory().clear(replacementItemSlot); - player.getInventory().setItem(shulkerBoxLocation,shulkerBox.getUpdatedShulkerItem()); - } - else { - player.getInventory().setItem(itemSlot,player.getInventory().getItem(replacementItemSlot)); + player.getInventory().setItem(shulkerBoxLocation, shulkerBox.getUpdatedShulkerItem()); + } else { + player.getInventory().setItem(itemSlot, player.getInventory().getItem(replacementItemSlot)); player.getInventory().clear(replacementItemSlot); } player.updateInventory(); @@ -52,38 +51,36 @@ public void replace() { public void swapTool() { findReplacement(); - if (replacementItemSlot==-1) { + if (replacementItemSlot == -1) { saveTool(); return; } ItemStack replacementItem; - if (shulkerBox!=null) { + if (shulkerBox != null) { replacementItem = shulkerBox.getInventory().getItem(replacementItemSlot); - shulkerBox.getInventory().setItem(replacementItemSlot,item); - player.getInventory().setItem(itemSlot,replacementItem); - player.getInventory().setItem(shulkerBoxLocation,shulkerBox.getUpdatedShulkerItem()); - } - else { + shulkerBox.getInventory().setItem(replacementItemSlot, item); + player.getInventory().setItem(itemSlot, replacementItem); + player.getInventory().setItem(shulkerBoxLocation, shulkerBox.getUpdatedShulkerItem()); + } else { replacementItem = player.getInventory().getItem(replacementItemSlot); - player.getInventory().setItem(replacementItemSlot,item); - player.getInventory().setItem(itemSlot,replacementItem); // here nullpoint exception occures for armour slots + player.getInventory().setItem(replacementItemSlot, item); + player.getInventory().setItem(itemSlot, replacementItem); // here nullpoint exception occures for armour slots } - player.playSound(player.getEyeLocation(), Sound.ENTITY_ITEM_BREAK,1.0F,1.0F); + player.playSound(player.getEyeLocation(), Sound.ENTITY_ITEM_BREAK, 1.0F, 1.0F); player.updateInventory(); } public void saveTool() { int saveSlot = inventory.firstEmpty(); - if (saveSlot!=-1) { - inventory.setItem(saveSlot,item); - inventory.setItem(itemSlot,new ItemStack(Material.AIR)); - } - else { // only works for in-hand items + if (saveSlot != -1) { + inventory.setItem(saveSlot, item); + inventory.setItem(itemSlot, new ItemStack(Material.AIR)); + } else { // only works for in-hand items ItemStack offHand = player.getInventory().getItemInOffHand(); player.getInventory().setItemInOffHand(inventory.getItem(itemSlot)); player.getInventory().setItemInMainHand(offHand); } - player.playSound(player.getEyeLocation(), Sound.ENTITY_ITEM_BREAK,1.0F,1.0F); + player.playSound(player.getEyeLocation(), Sound.ENTITY_ITEM_BREAK, 1.0F, 1.0F); player.updateInventory(); } @@ -91,11 +88,11 @@ private void findReplacement() { // Searches inventory for first possible replacement ItemStack[] invContent = inventory.getContents(); // Search first in shulker boxes - for (int i=0;i