From bdbcc504940799d9c0ead08f4c36a3d9ed2c282b Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Thu, 12 Dec 2019 15:24:43 +0800 Subject: [PATCH] Code cleanup Former-commit-id: 10726ad0830d0dc44a52ff66fc4aa3b539f5fb77 Former-commit-id: c6e9b1113f3d0dc605c20d272c3f22a99067777e --- deploy.sh | 2 +- .../Command/SubCommands/SubCommand_Debug.java | 2 +- .../Command/SubCommands/SubCommand_Price.java | 3 +- .../SubCommands/SubCommand_SetOwner.java | 2 +- .../maxgamer/quickshop/Database/Database.java | 1 + .../quickshop/Database/MySQLCore.java | 2 ++ .../quickshop/Database/SQLiteCore.java | 5 +++- .../quickshop/Listeners/PlayerListener.java | 12 ++++---- .../quickshop/Listeners/WorldListener.java | 3 -- .../java/org/maxgamer/quickshop/Metrics.java | 12 +++++++- .../sk89q/worldedit/util/net/HttpRequest.java | 4 +-- .../org/maxgamer/quickshop/QuickShop.java | 10 +++---- .../quickshop/Shop/ContainerShop.java | 2 -- .../quickshop/Shop/RealDisplayItem.java | 12 ++++---- .../maxgamer/quickshop/Shop/ShopLoader.java | 4 ++- .../maxgamer/quickshop/Shop/ShopManager.java | 4 +-- .../org/maxgamer/quickshop/Util/ItemNMS.java | 2 ++ .../org/maxgamer/quickshop/Util/MsgUtil.java | 6 ++-- .../org/maxgamer/quickshop/Util/Util.java | 29 ++++++++++++------- .../quickshop/Watcher/OngoingFeeWatcher.java | 3 +- 20 files changed, 74 insertions(+), 46 deletions(-) diff --git a/deploy.sh b/deploy.sh index 8dc6bd1af..06f931f78 100644 --- a/deploy.sh +++ b/deploy.sh @@ -8,7 +8,7 @@ if [ ! -f $filename ]; then exit 1 fi -read JAR <$filename +read -r JAR <$filename xToken=$1 curl -X POST -H "X-Api-Token: $xToken" -F 'metadata={"changelog":"Auto upload by TravisCI, see update details on spigotmc.org","gameVersions":[7330,7105],"releaseType":"release"}' -F "file=@./devbukkit/$JAR" -s "https://dev.bukkit.org/api/projects/320536/upload-file" >fileid.json cat fileid.json diff --git a/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Debug.java b/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Debug.java index e884a0d27..6f0be72b3 100644 --- a/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Debug.java +++ b/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Debug.java @@ -79,7 +79,7 @@ public class SubCommand_Debug implements CommandProcesser { public void printHandlerList(@NotNull CommandSender sender, String event) { try { final Class clazz = Class.forName(event); - final Method method = clazz.getMethod("getHandlerList", (Class[]) new Class[0]); + final Method method = clazz.getMethod("getHandlerList"); final Object[] obj = new Object[0]; final HandlerList list = (HandlerList) method.invoke(null, obj); diff --git a/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Price.java b/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Price.java index 1c5f1bb40..9f26e944f 100644 --- a/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Price.java +++ b/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_Price.java @@ -14,6 +14,7 @@ import org.maxgamer.quickshop.Util.Util; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.logging.Level; public class SubCommand_Price implements CommandProcesser { @@ -131,7 +132,7 @@ public class SubCommand_Price implements CommandProcesser { sender.sendMessage( MsgUtil.getMessage("fee-charged-for-price-change", sender, plugin.getEconomy().format(fee))); try { - plugin.getEconomy().deposit(plugin.getServer().getOfflinePlayer(plugin.getConfig().getString("tax-account")) + plugin.getEconomy().deposit(plugin.getServer().getOfflinePlayer(Objects.requireNonNull(plugin.getConfig().getString("tax-account"))) .getUniqueId(), fee); } catch (Exception e) { e.getMessage(); diff --git a/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_SetOwner.java b/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_SetOwner.java index c8f33e28e..4f1ac2eee 100644 --- a/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_SetOwner.java +++ b/src/main/java/org/maxgamer/quickshop/Command/SubCommands/SubCommand_SetOwner.java @@ -61,7 +61,7 @@ public class SubCommand_SetOwner implements CommandProcesser { shop.setOwner(p.getUniqueId()); //shop.setSignText(); shop.update(); - sender.sendMessage(MsgUtil.getMessage("command.new-owner", sender, shopOwner == null ? "" : shopOwner)); + sender.sendMessage(MsgUtil.getMessage("command.new-owner", sender, shopOwner)); return; } diff --git a/src/main/java/org/maxgamer/quickshop/Database/Database.java b/src/main/java/org/maxgamer/quickshop/Database/Database.java index e7f44a3ea..4603298b2 100644 --- a/src/main/java/org/maxgamer/quickshop/Database/Database.java +++ b/src/main/java/org/maxgamer/quickshop/Database/Database.java @@ -199,6 +199,7 @@ public class Database { * * @return the database core object, that this database runs on. */ + @NotNull public DatabaseCore getCore() { return core; } diff --git a/src/main/java/org/maxgamer/quickshop/Database/MySQLCore.java b/src/main/java/org/maxgamer/quickshop/Database/MySQLCore.java index beb56e6d2..baec1c66f 100644 --- a/src/main/java/org/maxgamer/quickshop/Database/MySQLCore.java +++ b/src/main/java/org/maxgamer/quickshop/Database/MySQLCore.java @@ -1,6 +1,7 @@ package org.maxgamer.quickshop.Database; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.sql.Connection; import java.sql.DriverManager; @@ -74,6 +75,7 @@ public class MySQLCore implements DatabaseCore { * * @return The database connection */ + @Nullable @Override public Connection getConnection() { for (int i = 0; i < MAX_CONNECTIONS; i++) { diff --git a/src/main/java/org/maxgamer/quickshop/Database/SQLiteCore.java b/src/main/java/org/maxgamer/quickshop/Database/SQLiteCore.java index 90cb11bb7..3265a3d8d 100644 --- a/src/main/java/org/maxgamer/quickshop/Database/SQLiteCore.java +++ b/src/main/java/org/maxgamer/quickshop/Database/SQLiteCore.java @@ -1,6 +1,7 @@ package org.maxgamer.quickshop.Database; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; @@ -9,6 +10,7 @@ import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.LinkedList; +import java.util.Objects; public class SQLiteCore implements DatabaseCore { private final File dbFile; @@ -34,7 +36,7 @@ public class SQLiteCore implements DatabaseCore { } synchronized (dbFile) { try { - PreparedStatement ps = bs.prepareStatement(getConnection()); + PreparedStatement ps = bs.prepareStatement(Objects.requireNonNull(getConnection())); ps.execute(); ps.close(); } catch (SQLException e) { @@ -60,6 +62,7 @@ public class SQLiteCore implements DatabaseCore { * * @return The database connection */ + @Nullable @Override public Connection getConnection() { try { diff --git a/src/main/java/org/maxgamer/quickshop/Listeners/PlayerListener.java b/src/main/java/org/maxgamer/quickshop/Listeners/PlayerListener.java index 3aa0a5723..ece30f325 100644 --- a/src/main/java/org/maxgamer/quickshop/Listeners/PlayerListener.java +++ b/src/main/java/org/maxgamer/quickshop/Listeners/PlayerListener.java @@ -28,6 +28,7 @@ import org.maxgamer.quickshop.Util.MsgUtil; import org.maxgamer.quickshop.Util.Util; import java.util.HashMap; +import java.util.Objects; import java.util.UUID; //import com.griefcraft.lwc.LWC; @@ -54,8 +55,8 @@ public class PlayerListener implements Listener { block = e.getClickedBlock(); } - if (plugin.getShopManager().getShop(block.getLocation()) != null && (plugin.getShopManager().getShop(block - .getLocation()).getOwner().equals(e.getPlayer().getUniqueId()) || e.getPlayer().isOp())) { + if (plugin.getShopManager().getShop(Objects.requireNonNull(block).getLocation()) != null && (Objects.requireNonNull(plugin.getShopManager().getShop(block + .getLocation())).getOwner().equals(e.getPlayer().getUniqueId()) || e.getPlayer().isOp())) { if (plugin.getConfig().getBoolean("shop.sneak-to-control") && !e.getPlayer().isSneaking()) { return; } @@ -65,8 +66,8 @@ public class PlayerListener implements Listener { } MsgUtil.sendControlPanelInfo(e.getPlayer(), - plugin.getShopManager().getShop(block.getLocation())); - plugin.getShopManager().getShop(block.getLocation()).setSignText(); + Objects.requireNonNull(plugin.getShopManager().getShop(block.getLocation()))); + Objects.requireNonNull(plugin.getShopManager().getShop(block.getLocation())).setSignText(); } } @@ -221,7 +222,7 @@ public class PlayerListener implements Listener { plugin.getShopManager().getActions().put(p.getUniqueId(), info); p.sendMessage( - MsgUtil.getMessage("how-much-to-trade-for", p, Util.getItemStackName(e.getItem()))); + MsgUtil.getMessage("how-much-to-trade-for", p, Util.getItemStackName(Objects.requireNonNull(e.getItem())))); } } @@ -235,6 +236,7 @@ public class PlayerListener implements Listener { Inventory inventory = e.getInventory(); // FIXME: 24/11/2019 inventory cannot be null + //noinspection ConstantConditions if (inventory == null) { return; } diff --git a/src/main/java/org/maxgamer/quickshop/Listeners/WorldListener.java b/src/main/java/org/maxgamer/quickshop/Listeners/WorldListener.java index aa016d5cb..98fb30e48 100644 --- a/src/main/java/org/maxgamer/quickshop/Listeners/WorldListener.java +++ b/src/main/java/org/maxgamer/quickshop/Listeners/WorldListener.java @@ -77,9 +77,6 @@ public class WorldListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onWorldUnload(WorldUnloadEvent e) { // FIXME: 24/11/2019 It's not necessary but ok. - if (e.isCancelled()) { - return; - } // This is a workaround, because I don't get parsed chunk events when a // world unloads, I think... // So manually tell all of these shops they're unloaded. diff --git a/src/main/java/org/maxgamer/quickshop/Metrics.java b/src/main/java/org/maxgamer/quickshop/Metrics.java index 83ecaea16..5ff267934 100644 --- a/src/main/java/org/maxgamer/quickshop/Metrics.java +++ b/src/main/java/org/maxgamer/quickshop/Metrics.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.Nullable; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -47,6 +48,7 @@ public class Metrics { * @return The gzipped String. * @throws IOException If the compression failed. */ + @Nullable private static byte[] compress(final String str) throws IOException { if (str == null) { return null; @@ -162,7 +164,7 @@ public class Metrics { connection.addRequestProperty("Accept", "application/json"); connection.addRequestProperty("Connection", "close"); connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request - connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length)); + connection.addRequestProperty("Content-Length", String.valueOf(Objects.requireNonNull(compressedData).length)); connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION); connection.setConnectTimeout(300000); @@ -326,6 +328,7 @@ public class Metrics { this.callable = callable; } + @Nullable @Override protected JSONObject getChartData() throws Exception { JSONObject data = new JSONObject(); @@ -375,6 +378,7 @@ public class Metrics { this.callable = callable; } + @Nullable @Override protected JSONObject getChartData() throws Exception { JSONObject data = new JSONObject(); @@ -423,6 +427,7 @@ public class Metrics { protected abstract JSONObject getChartData() throws Exception; + @Nullable private JSONObject getRequestJsonObject() { JSONObject chart = new JSONObject(); chart.put("chartId", chartId); @@ -462,6 +467,7 @@ public class Metrics { this.callable = callable; } + @Nullable @Override public JSONObject getChartData() throws Exception { JSONObject data = new JSONObject(); @@ -511,6 +517,7 @@ public class Metrics { this.callable = callable; } + @Nullable @Override protected JSONObject getChartData() throws Exception { JSONObject data = new JSONObject(); @@ -556,6 +563,7 @@ public class Metrics { this.callable = callable; } + @Nullable @Override protected JSONObject getChartData() throws Exception { JSONObject data = new JSONObject(); @@ -594,6 +602,7 @@ public class Metrics { this.callable = callable; } + @Nullable @Override protected JSONObject getChartData() throws Exception { JSONObject data = new JSONObject(); @@ -625,6 +634,7 @@ public class Metrics { this.callable = callable; } + @Nullable @Override protected JSONObject getChartData() throws Exception { JSONObject data = new JSONObject(); diff --git a/src/main/java/org/maxgamer/quickshop/NonQuickShopStuffs/com/sk89q/worldedit/util/net/HttpRequest.java b/src/main/java/org/maxgamer/quickshop/NonQuickShopStuffs/com/sk89q/worldedit/util/net/HttpRequest.java index 624e1c898..e1bf7ac04 100644 --- a/src/main/java/org/maxgamer/quickshop/NonQuickShopStuffs/com/sk89q/worldedit/util/net/HttpRequest.java +++ b/src/main/java/org/maxgamer/quickshop/NonQuickShopStuffs/com/sk89q/worldedit/util/net/HttpRequest.java @@ -206,7 +206,7 @@ public class HttpRequest implements Closeable { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - int b = 0; + int b; while ((b = inputStream.read()) != -1) { bos.write(b); } @@ -260,7 +260,7 @@ public class HttpRequest implements Closeable { bis = new BufferedInputStream(inputStream); byte[] data = new byte[READ_BUFFER_SIZE]; - int len = 0; + int len; while ((len = bis.read(data, 0, READ_BUFFER_SIZE)) >= 0) { out.write(data, 0, len); readBytes += len; diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index c8a8f9267..62db92bd9 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -326,7 +326,7 @@ public class QuickShop extends JavaPlugin { } Util.debugLog("Unloading all shops..."); try { - this.getShopManager().getLoadedShops().forEach(Shop::onUnload); + Objects.requireNonNull(this.getShopManager().getLoadedShops()).forEach(Shop::onUnload); } catch (Throwable th) { //ignore, we didn't care that } @@ -437,7 +437,7 @@ public class QuickShop extends JavaPlugin { if (limitCfg != null) { this.limit = limitCfg.getBoolean("use", false); limitCfg = limitCfg.getConfigurationSection("ranks"); - for (String key : limitCfg.getKeys(true)) { + for (String key : Objects.requireNonNull(limitCfg).getKeys(true)) { limits.put(key, limitCfg.getInt(key)); } } @@ -577,7 +577,7 @@ public class QuickShop extends JavaPlugin { try { ConfigurationSection dbCfg = getConfig().getConfigurationSection("database"); DatabaseCore dbCore; - if (dbCfg.getBoolean("mysql")) { + if (Objects.requireNonNull(dbCfg).getBoolean("mysql")) { // MySQL database - Required database be created first. dbPrefix = dbCfg.getString("prefix"); if (dbPrefix == null || "none".equals(dbPrefix)) { @@ -589,7 +589,7 @@ public class QuickShop extends JavaPlugin { String port = dbCfg.getString("port"); String database = dbCfg.getString("database"); boolean useSSL = dbCfg.getBoolean("usessl"); - dbCore = new MySQLCore(host, user, pass, database, port, useSSL); + dbCore = new MySQLCore(Objects.requireNonNull(host), Objects.requireNonNull(user), Objects.requireNonNull(pass), Objects.requireNonNull(database), Objects.requireNonNull(port), useSSL); } else { // SQLite database - Doing this handles file creation dbCore = new SQLiteCore(new File(this.getDataFolder(), "shops.db")); @@ -1080,7 +1080,7 @@ public class QuickShop extends JavaPlugin { File file = new File(getDataFolder(), "example.config.yml"); file.delete(); try { - Files.copy(getResource("config.yml"), file.toPath()); + Files.copy(Objects.requireNonNull(getResource("config.yml")), file.toPath()); } catch (IOException ioe) { getLogger().warning("Error on spawning the example config file: " + ioe.getMessage()); } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index cb429d415..0921e6d9b 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -2,7 +2,6 @@ package org.maxgamer.quickshop.Shop; import com.lishid.openinv.OpenInv; import lombok.EqualsAndHashCode; -import lombok.ToString; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -32,7 +31,6 @@ import java.util.logging.Level; * ChestShop core */ @EqualsAndHashCode -@ToString public class ContainerShop implements Shop { private DisplayItem displayItem; @EqualsAndHashCode.Exclude diff --git a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java index 23ebc932a..8cf25359f 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java @@ -14,6 +14,8 @@ import org.maxgamer.quickshop.Event.ShopDisplayItemSpawnEvent; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Util.Util; +import java.util.Objects; + @ToString public class RealDisplayItem implements DisplayItem { @@ -45,7 +47,7 @@ public class RealDisplayItem implements DisplayItem { } //return !this.item.getLocation().equals(getDisplayLocation()); /* We give 0.6 block to allow item drop on the chest, not floating on the air. */ - if (!this.item.getLocation().getWorld().equals(getDisplayLocation().getWorld())) { + if (!Objects.requireNonNull(this.item.getLocation().getWorld()).equals(Objects.requireNonNull(getDisplayLocation()).getWorld())) { return true; } return this.item.getLocation().distance(getDisplayLocation()) > 0.6; @@ -69,15 +71,15 @@ public class RealDisplayItem implements DisplayItem { @Override public void fixDisplayMoved() { - for (Entity entity : this.shop.getLocation().getWorld().getEntities()) { + for (Entity entity : Objects.requireNonNull(this.shop.getLocation().getWorld()).getEntities()) { if (!(entity instanceof Item)) { continue; } Item eItem = (Item) entity; - if (eItem.getUniqueId().equals(this.item.getUniqueId())) { + if (eItem.getUniqueId().equals(Objects.requireNonNull(this.item).getUniqueId())) { Util.debugLog("Fixing moved Item displayItem " + eItem.getUniqueId() + " at " + eItem .getLocation()); - eItem.teleport(getDisplayLocation()); + eItem.teleport(Objects.requireNonNull(getDisplayLocation())); return; } } @@ -175,7 +177,7 @@ public class RealDisplayItem implements DisplayItem { Util.debugLog(trace.getClassName() + "#" + trace.getMethodName() + "#" + trace.getLineNumber()); } } - if (!Util.isDisplayAllowBlock(getDisplayLocation().getBlock().getType())) { + if (!Util.isDisplayAllowBlock(Objects.requireNonNull(getDisplayLocation()).getBlock().getType())) { Util.debugLog("Can't spawn the displayItem because there is not an AIR block above the shopblock."); return; } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopLoader.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopLoader.java index 3f88c705c..5782c4b8f 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopLoader.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopLoader.java @@ -19,6 +19,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.UUID; import java.util.logging.Logger; @@ -131,7 +132,7 @@ public class ShopLoader { if(backupedDatabaseInDeleteProcess){ plugin.getDatabaseHelper().removeShop(shop.getLocation().getBlockX(), shop - .getLocation().getBlockY(), shop.getLocation().getBlockZ(), shop.getLocation().getWorld() + .getLocation().getBlockY(), shop.getLocation().getBlockZ(), Objects.requireNonNull(shop.getLocation().getWorld()) .getName()); } @@ -172,6 +173,7 @@ public class ShopLoader { return sum / m.length; } + @SuppressWarnings("ConstantConditions") private boolean shopNullCheck(@Nullable Shop shop) { if (shop == null) { Util.debugLog("Shop Object is null"); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java index 84aca24fd..39e5f9a75 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java @@ -306,7 +306,7 @@ public class ShopManager { if (shop.isDoubleShop()) { Shop nextTo = shop.getAttachedShop(); - if (nextTo.getPrice() > shop.getPrice()) { + if (Objects.requireNonNull(nextTo).getPrice() > shop.getPrice()) { // The one next to it must always be a // buying shop. p.sendMessage(MsgUtil.getMessage("buying-more-than-selling", p)); @@ -458,7 +458,7 @@ public class ShopManager { // even if the shop is unlimited, the config option pay-unlimited-shop-owners is set to true, // the unlimited shop owner should have enough money. if (plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) { - amount = Math.min(amount, (int) ownerCanAfford); + amount = Math.min(amount, ownerCanAfford); } } if (amount < 1) { // typed 'all' but the auto set amount is 0 diff --git a/src/main/java/org/maxgamer/quickshop/Util/ItemNMS.java b/src/main/java/org/maxgamer/quickshop/Util/ItemNMS.java index 060bedfa7..23132765e 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/ItemNMS.java +++ b/src/main/java/org/maxgamer/quickshop/Util/ItemNMS.java @@ -5,6 +5,7 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.maxgamer.quickshop.QuickShop; import java.lang.reflect.Method; @@ -42,6 +43,7 @@ public abstract class ItemNMS { * @return The json for ItemStack. * @throws Throwable throws */ + @Nullable public static String saveJsonfromNMS(@NotNull ItemStack bStack) throws Throwable { if (bStack.getType() == Material.AIR) { return null; diff --git a/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java b/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java index 8d8a603a3..ae822dd79 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java +++ b/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java @@ -213,7 +213,7 @@ public class MsgUtil { String filled = fillArgs(raw, args); if (player != null) { if (plugin.getPlaceHolderAPI() != null && plugin.getPlaceHolderAPI().isEnabled()) { - filled = PlaceholderAPI.setPlaceholders((OfflinePlayer) player, filled); + filled = PlaceholderAPI.setPlaceholders(player, filled); Util.debugLog("Processed message " + filled + " by PlaceHolderAPI."); } } @@ -503,7 +503,7 @@ public class MsgUtil { } public static @NotNull String getSubString(@NotNull String text, @NotNull String left, @NotNull String right) { - String result = ""; + String result; int zLen; if (left.isEmpty()) { zLen = 0; @@ -530,7 +530,7 @@ public class MsgUtil { * @param shop Target shop */ public static void sendControlPanelInfo(@NotNull CommandSender sender, @NotNull Shop shop) { - if ((sender instanceof Player && !((Player) sender).isOp()) && !QuickShop.getPermissionManager().hasPermission(sender, "quickshop.use")) { + if ((sender instanceof Player && !sender.isOp()) && !QuickShop.getPermissionManager().hasPermission(sender, "quickshop.use")) { return; } if (plugin.getConfig().getBoolean("sneak-to-control")) { diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java index b712b61c1..14b2aafcc 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java @@ -238,7 +238,7 @@ public class Util { */ private static boolean equalsBlockStateLocation(@NotNull Location b1, @NotNull Location b2) { return (b1.getBlockX() == b2.getBlockX()) && (b1.getBlockY() == b2.getBlockY()) && (b1.getBlockZ() == b2 - .getBlockZ()) && (b1.getWorld().getName().equals(b2.getWorld().getName())); + .getBlockZ()) && (Objects.requireNonNull(b1.getWorld()).getName().equals(Objects.requireNonNull(b2.getWorld()).getName())); } /** @@ -287,6 +287,7 @@ public class Util { * @param b The block which is attached * @return The block the sign is attached to */ + @Nullable public static Block getAttached(@NotNull Block b) { if (b.getBlockData() instanceof Directional) { Directional directional = (Directional) b.getBlockData(); @@ -309,7 +310,7 @@ public class Util { } public static String getItemStackName(@NotNull ItemStack itemStack) { - if (itemStack.hasItemMeta() && itemStack.getItemMeta().hasDisplayName()) { + if (itemStack.hasItemMeta() && Objects.requireNonNull(itemStack.getItemMeta()).hasDisplayName()) { return itemStack.getItemMeta().getDisplayName(); } return MsgUtil.getItemi18n(itemStack.getType().name()); @@ -321,6 +322,7 @@ public class Util { * @param itemStack Target ItemStack * @return LocalName or NULL */ + @Nullable public static String getLocalizedName(@NotNull ItemStack itemStack) { ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta == null) { @@ -349,6 +351,7 @@ public class Util { * @param iStack itemstack * @return potion data, readable */ + @Nullable public static String getPotiondata(@NotNull ItemStack iStack) { if ((iStack.getType() != Material.POTION) && (iStack.getType() != Material.LINGERING_POTION) && (iStack .getType() != Material.SPLASH_POTION)) { @@ -361,9 +364,9 @@ public class Util { PotionMeta pMeta = (PotionMeta) iStack.getItemMeta(); //if (pMeta.getBasePotionData().getType() != null) { if (!(pMeta.getBasePotionData().isUpgraded())) { - pEffects.add(ChatColor.BLUE + MsgUtil.getPotioni18n(pMeta.getBasePotionData().getType().getEffectType())); + pEffects.add(ChatColor.BLUE + MsgUtil.getPotioni18n(Objects.requireNonNull(pMeta.getBasePotionData().getType().getEffectType()))); } else { - pEffects.add(ChatColor.BLUE + MsgUtil.getPotioni18n(pMeta.getBasePotionData().getType().getEffectType()) + " II"); + pEffects.add(ChatColor.BLUE + MsgUtil.getPotioni18n(Objects.requireNonNull(pMeta.getBasePotionData().getType().getEffectType())) + " II"); } //} @@ -403,6 +406,7 @@ public class Util { * @param b The chest to check. * @return the block which is also a chest and connected to b. */ + @Nullable public static Block getSecondHalf(@NotNull Block b) { if (!isDoubleChest(b)) { Util.debugLog("Target block not a DoubleChest, ignored."); @@ -416,11 +420,11 @@ public class Util { InventoryHolder right = doubleChest.getRightSide(); Chest leftC = (Chest) left; Chest rightC = (Chest) right; - if (equalsBlockStateLocation(oneSideOfChest.getLocation(), rightC.getLocation())) { + if (equalsBlockStateLocation(oneSideOfChest.getLocation(), Objects.requireNonNull(rightC).getLocation())) { Util.debugLog("The left side of the chest was found."); - return leftC.getBlock(); + return Objects.requireNonNull(leftC).getBlock(); } - if (equalsBlockStateLocation(oneSideOfChest.getLocation(), leftC.getLocation())) { + if (equalsBlockStateLocation(oneSideOfChest.getLocation(), Objects.requireNonNull(leftC).getLocation())) { Util.debugLog("The right side of the chest was found."); return rightC.getBlock(); } @@ -440,6 +444,7 @@ public class Util { * @return the block which is also a chest and connected to b. * @deprecated */ + @Nullable @Deprecated public static Block getSecondHalf_old(@NotNull Block b) { // if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) @@ -471,7 +476,7 @@ public class Util { Iterator iterator = plugin.getShopManager().getShopIterator(); while (iterator.hasNext()) { Shop shop = iterator.next(); - if (shop.getLocation().getWorld().getName().equals(worldName)) { + if (Objects.requireNonNull(shop.getLocation().getWorld()).getName().equals(worldName)) { cost++; } } @@ -576,6 +581,7 @@ public class Util { * @param filePath Target file * @return Byte array */ + @Nullable public static byte[] inputStream2ByteArray(@NotNull String filePath) { try { InputStream in = new FileInputStream(filePath); @@ -594,6 +600,7 @@ public class Util { * @param inputStream Target stream * @return Byte array */ + @Nullable public static byte[] inputStream2ByteArray(@NotNull InputStream inputStream) { try { byte[] data = toByteArray(inputStream); @@ -676,10 +683,10 @@ public class Util { if(!stack.hasItemMeta()){ return false; } - if(!stack.getItemMeta().hasLore()){ + if(!Objects.requireNonNull(stack.getItemMeta()).hasLore()){ return false; } - for (String lore:stack.getItemMeta().getLore()) { + for (String lore: Objects.requireNonNull(stack.getItemMeta().getLore())) { if(plugin.getConfig().getStringList("shop.blacklist-lores").contains(lore)){ return true; } @@ -1083,7 +1090,7 @@ public class Util { */ public static Material getSignMaterial() { - Material signMaterial = Material.matchMaterial(plugin.getConfig().getString("shop.sign-material")); + Material signMaterial = Material.matchMaterial(Objects.requireNonNull(plugin.getConfig().getString("shop.sign-material"))); if (signMaterial != null) { return signMaterial; } diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/OngoingFeeWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/OngoingFeeWatcher.java index 450e066f7..8497f4be8 100644 --- a/src/main/java/org/maxgamer/quickshop/Watcher/OngoingFeeWatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Watcher/OngoingFeeWatcher.java @@ -8,6 +8,7 @@ import org.maxgamer.quickshop.Shop.Shop; import org.maxgamer.quickshop.Util.MsgUtil; import org.maxgamer.quickshop.Util.Util; +import java.util.Objects; import java.util.UUID; /** @@ -64,6 +65,6 @@ public class OngoingFeeWatcher extends BukkitRunnable { */ public void removeShop(@NotNull Shop shop) { Bukkit.getScheduler().runTask(plugin, (@NotNull Runnable) shop::delete); - MsgUtil.send(shop.getOwner(), MsgUtil.getMessageOfflinePlayer("shop-removed-cause-ongoing-fee", Bukkit.getOfflinePlayer(shop.getOwner()), "World:" + shop.getLocation().getWorld().getName() + " X:" + shop.getLocation().getBlockX() + " Y:" + shop.getLocation().getBlockY() + " Z:" + shop.getLocation().getBlockZ()), shop.isUnlimited()); + MsgUtil.send(shop.getOwner(), MsgUtil.getMessageOfflinePlayer("shop-removed-cause-ongoing-fee", Bukkit.getOfflinePlayer(shop.getOwner()), "World:" + Objects.requireNonNull(shop.getLocation().getWorld()).getName() + " X:" + shop.getLocation().getBlockX() + " Y:" + shop.getLocation().getBlockY() + " Z:" + shop.getLocation().getBlockZ()), shop.isUnlimited()); } } \ No newline at end of file