From 61b88c8d9493566ca70b6c0e201128991a811089 Mon Sep 17 00:00:00 2001 From: mfnalex <1122571+mfnalex@users.noreply.github.com> Date: Wed, 24 Jun 2020 20:53:10 +0200 Subject: [PATCH] cleanup --- .../ChestSort/ChestSortChestSortCommand.java | 3 +- .../jeff_media/ChestSort/ChestSortEvent.java | 3 +- .../ChestSort/ChestSortInvSortCommand.java | 3 +- .../ChestSort/ChestSortListener.java | 4 +- .../ChestSort/ChestSortOrganizer.java | 41 ++++++++----------- .../ChestSort/ChestSortPlayerSetting.java | 2 +- .../jeff_media/ChestSort/ChestSortPlugin.java | 16 ++++---- .../ChestSort/ChestSortSettingsGUI.java | 2 +- .../ChestSort/ChestSortTabCompleter.java | 3 +- .../ChestSort/hooks/MinepacksHook.java | 5 +-- .../ChestSort/utils/CategoryLinePair.java | 2 +- .../utils/TypeMatchPositionPair.java | 2 +- 12 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortChestSortCommand.java b/src/main/java/de/jeff_media/ChestSort/ChestSortChestSortCommand.java index 634688c..ce79971 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortChestSortCommand.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortChestSortCommand.java @@ -6,6 +6,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import net.md_5.bungee.api.ChatColor; +import org.jetbrains.annotations.NotNull; public class ChestSortChestSortCommand implements CommandExecutor { @@ -16,7 +17,7 @@ public class ChestSortChestSortCommand implements CommandExecutor { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean onCommand(@NotNull CommandSender sender, Command command, @NotNull String label, String[] args) { // This command toggles automatic chest sorting for the player that runs the command if (!command.getName().equalsIgnoreCase("chestsort")) { diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortEvent.java b/src/main/java/de/jeff_media/ChestSort/ChestSortEvent.java index 773a4f8..43780ca 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortEvent.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortEvent.java @@ -6,6 +6,7 @@ import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public class ChestSortEvent extends Event implements Cancellable { @@ -35,7 +36,7 @@ public class ChestSortEvent extends Event implements Cancellable { return p; } - public HandlerList getHandlers() { + public @NotNull HandlerList getHandlers() { return HANDLERS; } diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortInvSortCommand.java b/src/main/java/de/jeff_media/ChestSort/ChestSortInvSortCommand.java index 5ecb8f9..6da8607 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortInvSortCommand.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortInvSortCommand.java @@ -4,6 +4,7 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; public class ChestSortInvSortCommand implements CommandExecutor { @@ -14,7 +15,7 @@ public class ChestSortInvSortCommand implements CommandExecutor { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean onCommand(@NotNull CommandSender sender, Command command, @NotNull String label, String[] args) { // This command toggles automatic chest sorting for the player that runs the command if (!command.getName().equalsIgnoreCase("invsort")) { diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java b/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java index 6d06e04..283e2b6 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java @@ -79,8 +79,8 @@ public class ChestSortListener implements Listener { @EventHandler public void onPlayerInventoryClose(InventoryCloseEvent event) { - if (event.getInventory() == null) return; if (event.getInventory().getHolder() == null) return; + // Might be obsolete, because its @NotNull in 1.15, but who knows if thats for 1.8 if (event.getInventory().getType() == null) return; if (event.getInventory().getType() != InventoryType.CRAFTING) return; // Weird! Returns CRAFTING instead of PLAYER @@ -413,11 +413,9 @@ public class ChestSortListener implements Listener { if (event.getSlotType() == SlotType.QUICKBAR) { plugin.organizer.sortInventory(p.getInventory(), 0, 8); plugin.organizer.updateInventoryView(event); - return; } else if (event.getSlotType() == SlotType.CONTAINER) { plugin.organizer.sortInventory(p.getInventory(), 9, 35); plugin.organizer.updateInventoryView(event); - return; } } } diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortOrganizer.java b/src/main/java/de/jeff_media/ChestSort/ChestSortOrganizer.java index 6fa2c50..70aaa32 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortOrganizer.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortOrganizer.java @@ -2,14 +2,7 @@ package de.jeff_media.ChestSort; import java.io.File; import java.io.FileNotFoundException; -import java.io.FilenameFilter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; +import java.util.*; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -152,11 +145,11 @@ public class ChestSortOrganizer { plugin.getLogger().info("Sticky set to true in " + file.getName()); } } else { - if (currentLine != null) { + //if (currentLine != null) { lines.add(new TypeMatchPositionPair(currentLine, currentLineNumber, appendLineNumber)); if (plugin.debug) plugin.getLogger().info("Added typeMatch to category file: " + currentLine); - } + //} } } currentLineNumber++; @@ -471,9 +464,6 @@ public class ChestSortOrganizer { } } - // We no longer need the original array that includes all the null-stacks - items = null; - // We need the new list as array. So why did'nt we take an array from the // beginning? // Because I did not bother to count the number of non-null items beforehand. @@ -482,7 +472,7 @@ public class ChestSortOrganizer { ItemStack[] nonNullItems = nonNullItemsList.toArray(new ItemStack[0]); // Sort the array with ItemStacks according to each ItemStacks' sortable String - Arrays.sort(nonNullItems, (s1, s2) -> (getSortableString(s1).compareTo(getSortableString(s2)))); + Arrays.sort(nonNullItems, Comparator.comparing(this::getSortableString)); // Now, we put everything back in a temporary inventory to combine ItemStacks // even when using strict slot sorting @@ -515,14 +505,14 @@ public class ChestSortOrganizer { // our temporary inventory was already sorted. Feel free to make a pull request // to // save your server half a nanosecond :) - if (item != null) - { + //if (item != null) + //{ while(unsortableSlots.contains(currentSlot) && currentSlot < endSlot) { currentSlot++; } inv.setItem(currentSlot, item); - } + //} currentSlot++; } @@ -632,24 +622,25 @@ public class ChestSortOrganizer { boolean destinationIsShulkerBox = destination.getType().name().equalsIgnoreCase("SHULKER_BOX"); Inventory temp = Bukkit.createInventory(null, maxInventorySize); for(int i = playerInvStartSlot;i<=playerInvEndSlot;i++) { - if(source.getItem(i)==null) continue; + ItemStack currentItem = source.getItem(i); + if(currentItem==null) continue; // This prevents Minepacks from being put into Minepacks /*if(plugin.hookMinepacks && plugin.listener.minepacksHook.isMinepacksBackpack(destination) - && plugin.listener.minepacksHook.isMinepacksBackpack(source.getItem(i))) continue;*/ + && plugin.listener.minepacksHook.isMinepacksBackpack(currentItem)) continue;*/ // This prevents Minepacks from being moved at all - if(plugin.hookMinepacks && plugin.listener.minepacksHook.isMinepacksBackpack(source.getItem(i))) continue; + if(plugin.hookMinepacks && plugin.listener.minepacksHook.isMinepacksBackpack(currentItem)) continue; if(plugin.hookInventoryPages - && plugin.organizer.inventoryPagesHook.isButton(source.getItem(i), i, source)) continue; + && plugin.organizer.inventoryPagesHook.isButton(currentItem, i, source)) continue; - if(destinationIsShulkerBox && source.getItem(i).getType().name().endsWith("SHULKER_BOX")) continue; + if(destinationIsShulkerBox && currentItem.getType().name().endsWith("SHULKER_BOX")) continue; - if(isOversizedStack(source.getItem(i))) continue; + if(isOversizedStack(currentItem)) continue; - if(onlyMatchingStuff && !doesInventoryContain(destination,source.getItem(i).getType())) continue; + if(onlyMatchingStuff && !doesInventoryContain(destination,currentItem.getType())) continue; - temp.addItem(source.getItem(i)); + temp.addItem(currentItem); source.clear(i); } stuffInventoryIntoAnother(temp,destination,source,false); diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortPlayerSetting.java b/src/main/java/de/jeff_media/ChestSort/ChestSortPlayerSetting.java index 77f0426..a2c752b 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortPlayerSetting.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortPlayerSetting.java @@ -25,7 +25,7 @@ public class ChestSortPlayerSetting { boolean hasSeenMessage = false; // Do we have to save these settings? - boolean changed = false; + boolean changed; DoubleClickType currentDoubleClick = DoubleClickType.NONE; diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortPlugin.java b/src/main/java/de/jeff_media/ChestSort/ChestSortPlugin.java index d12e3cc..a64e32a 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortPlugin.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortPlugin.java @@ -31,7 +31,6 @@ package de.jeff_media.ChestSort; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; -import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; @@ -147,7 +146,6 @@ public class ChestSortPlugin extends JavaPlugin { showOldConfigWarning(); ChestSortConfigUpdater configUpdater = new ChestSortConfigUpdater(this); configUpdater.updateConfig(); - configUpdater = null; usingMatchingConfig = true; //createConfig(); } @@ -237,15 +235,15 @@ public class ChestSortPlugin extends JavaPlugin { } private String getCategoryList() { - String list = ""; + StringBuilder list = new StringBuilder(); ChestSortCategory[] categories = organizer.categories.toArray(new ChestSortCategory[0]); Arrays.sort(categories); for(ChestSortCategory category : categories) { - list = list + category.name + " ("; - list = list + category.typeMatches.length + "), "; + list.append(category.name).append(" ("); + list.append(category.typeMatches.length).append("), "); } - list = list.substring(0, list.length()-2); - return list; + list = new StringBuilder(list.substring(0, list.length() - 2)); + return list.toString(); } @@ -560,8 +558,8 @@ public class ChestSortPlugin extends JavaPlugin { playerConfig.addDefault("leftClick", getConfig().getBoolean("additional-hotkeys.left-click")); playerConfig.addDefault("rightClick", getConfig().getBoolean("additional-hotkeys.right-click")); - boolean activeForThisPlayer = false; - boolean invActiveForThisPlayer = false; + boolean activeForThisPlayer; + boolean invActiveForThisPlayer; boolean middleClick, shiftClick, doubleClick, shiftRightClick, leftClick, rightClick; boolean changed = false; diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortSettingsGUI.java b/src/main/java/de/jeff_media/ChestSort/ChestSortSettingsGUI.java index 7655f26..0a713fd 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortSettingsGUI.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortSettingsGUI.java @@ -36,7 +36,7 @@ public class ChestSortSettingsGUI implements Listener { } ItemStack getItem(boolean active, Hotkey hotkey) { - ItemStack is = null; + ItemStack is; String suffix; if(active) { diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortTabCompleter.java b/src/main/java/de/jeff_media/ChestSort/ChestSortTabCompleter.java index d0d4f3d..b0bd567 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortTabCompleter.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortTabCompleter.java @@ -6,6 +6,7 @@ import java.util.List; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; +import org.jetbrains.annotations.NotNull; public class ChestSortTabCompleter implements TabCompleter { @@ -24,7 +25,7 @@ public class ChestSortTabCompleter implements TabCompleter { return list; } - public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) { String entered = ""; if(args.length>0) { diff --git a/src/main/java/de/jeff_media/ChestSort/hooks/MinepacksHook.java b/src/main/java/de/jeff_media/ChestSort/hooks/MinepacksHook.java index da74a48..6de22b1 100644 --- a/src/main/java/de/jeff_media/ChestSort/hooks/MinepacksHook.java +++ b/src/main/java/de/jeff_media/ChestSort/hooks/MinepacksHook.java @@ -27,9 +27,8 @@ public class MinepacksHook { if(minepacks == null) return false; try { - if(minepacks.getClass().getMethod("isBackpackItem", ItemStack.class) != null) { - if(minepacks.isBackpackItem(item)) return true; - } + minepacks.getClass().getMethod("isBackpackItem", ItemStack.class); + if(minepacks.isBackpackItem(item)) return true; } catch (NoSuchMethodException | SecurityException e) { plugin.getLogger().warning("You are using a version of Minepacks that is too old and does not implement every API method needed by ChestSort. Minepacks hook will be disabled."); minepacks = null; diff --git a/src/main/java/de/jeff_media/ChestSort/utils/CategoryLinePair.java b/src/main/java/de/jeff_media/ChestSort/utils/CategoryLinePair.java index c0fa55f..6e89a9c 100644 --- a/src/main/java/de/jeff_media/ChestSort/utils/CategoryLinePair.java +++ b/src/main/java/de/jeff_media/ChestSort/utils/CategoryLinePair.java @@ -4,7 +4,7 @@ package de.jeff_media.ChestSort.utils; public class CategoryLinePair { final String categoryName; final String formattedPosition; - boolean sticky = false; + boolean sticky; final short position; public CategoryLinePair(String categoryName,short position) { diff --git a/src/main/java/de/jeff_media/ChestSort/utils/TypeMatchPositionPair.java b/src/main/java/de/jeff_media/ChestSort/utils/TypeMatchPositionPair.java index 2a400b9..238a05f 100644 --- a/src/main/java/de/jeff_media/ChestSort/utils/TypeMatchPositionPair.java +++ b/src/main/java/de/jeff_media/ChestSort/utils/TypeMatchPositionPair.java @@ -4,7 +4,7 @@ public class TypeMatchPositionPair { final String typeMatch; final String formattedPosition; - boolean sticky=false; + boolean sticky; public String getTypeMatch() { return typeMatch;