diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e924b4d..f1784c2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,15 +16,15 @@ jobs: steps: - name: Checkout Git repo - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 - name: Restore Gradle cache - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: ~/.gradle/caches - key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }} + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} restore-keys: | ${{ runner.os }}-gradle- @@ -35,24 +35,73 @@ jobs: java-version: 21 - name: Validate Gradle Wrapper - uses: gradle/wrapper-validation-action@v1 + uses: gradle/actions/wrapper-validation@v3 - name: Build with Gradle run: | chmod +x gradlew ./gradlew build --stacktrace - - name: Install rename - run: sudo apt-get install -y rename - -# This is only needed when using shadow jar -# - name: Rename file -# run: | -# cd ./build/libs/ -# rename -f 's/-all//;' '' * - - name: Archive plugin jars on GitHub - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AutomaticInventory path: build/libs + overwrite: 'true' + + release: + name: Release + needs: build + runs-on: ubuntu-latest + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + + permissions: + contents: write # Required to create/delete tags and releases + + steps: + - name: Checkout Git repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Git user + run: | + git config user.name "github-actions" + git config user.email "github-actions@github.com" + + - name: Get short commit SHA + id: vars + run: echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" + + - name: Delete previous release (if exists) + continue-on-error: true + run: | + gh release delete latest --yes + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Delete previous tag (if exists) + continue-on-error: true + run: | + git tag -d latest || true + git push origin :refs/tags/latest || true + + - name: Create new tag + run: | + git tag latest + git push origin latest + + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: AutomaticInventory + path: artifacts + + - name: Create GitHub Release + run: | + ls artifacts # optional: see what's there + gh release create latest artifacts/* \ + --title "Latest Build (${{ steps.vars.outputs.sha_short }})" \ + --notes "Automated build from commit ${{ steps.vars.outputs.sha_short }}" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.vscode/settings.json b/.vscode/settings.json index e0f15db..0be1c0c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "java.configuration.updateBuildConfiguration": "automatic" + "java.configuration.updateBuildConfiguration": "automatic", + "java.compile.nullAnalysis.mode": "automatic" } \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 5a63115..9c07a8c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,10 +14,6 @@ plugins { // id("com.github.johnrengelman.shadow") version "8.1.1" } -group = "dev.chaws.automaticinventory" -version = "4.0.0" -description = "Automatic Inventory PaperMC Plugin" - repositories { mavenLocal() mavenCentral() @@ -33,6 +29,10 @@ repositories { name = "bstats" url = URI("https://oss.sonatype.org/content/groups/public/") } + maven { + name = "codemc-repo" + url = URI("https://repo.codemc.io/repository/maven-public/") + } } java { @@ -44,6 +44,7 @@ dependencies { // paperweight.foliaDevBundle("1.21-R0.1-SNAPSHOT") // paperweight.devBundle("com.example.paperfork", "1.21-R0.1-SNAPSHOT") implementation("org.bstats:bstats-bukkit:3.0.2") + implementation("com.griefcraft:lwc:2.4.1") // Add ASM dependency to support Java 21 class files implementation("org.ow2.asm:asm:9.7") @@ -76,6 +77,7 @@ bukkitPluginYaml { load = BukkitPluginYaml.PluginLoadOrder.STARTUP authors = listOf("Chaws", "Pugabyte", "AllTheCode", "RoboMWM", "Big_Scary") apiVersion = "1.21" + softDepend = listOf("LWC") commands.register("autosort") { description = "Toggles auto-sorting options." permission = "automaticinventory.sortchests" diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..ebbeeab --- /dev/null +++ b/gradle.properties @@ -0,0 +1,3 @@ +group=dev.chaws.automaticinventory +version=4.1.0 +description=Automatic Inventory PaperMC Plugin diff --git a/src/main/java/dev/chaws/automaticinventory/AutomaticInventory.java b/src/main/java/dev/chaws/automaticinventory/AutomaticInventory.java index 2ad6e9f..59f26bc 100644 --- a/src/main/java/dev/chaws/automaticinventory/AutomaticInventory.java +++ b/src/main/java/dev/chaws/automaticinventory/AutomaticInventory.java @@ -4,6 +4,7 @@ import dev.chaws.automaticinventory.configuration.Features; import dev.chaws.automaticinventory.configuration.GlobalConfig; import dev.chaws.automaticinventory.configuration.PlayerConfig; +import dev.chaws.automaticinventory.hooks.LWCHook; import dev.chaws.automaticinventory.listeners.*; import dev.chaws.automaticinventory.messaging.LocalizedMessages; import dev.chaws.automaticinventory.utilities.Metrics; @@ -45,6 +46,10 @@ public void onEnable() { this.registerCommand("depositall", new DepositAllCommand()); this.registerCommand("quickdeposit", new QuickDepositCommand()); + if (pluginManager.getPlugin("LWC") != null) { + LWCHook.enable(); + } + try { new Metrics(this, 16822); } catch (Throwable ignored) { diff --git a/src/main/java/dev/chaws/automaticinventory/commands/AutoSortCommand.java b/src/main/java/dev/chaws/automaticinventory/commands/AutoSortCommand.java index 17747e1..15dca09 100644 --- a/src/main/java/dev/chaws/automaticinventory/commands/AutoSortCommand.java +++ b/src/main/java/dev/chaws/automaticinventory/commands/AutoSortCommand.java @@ -6,6 +6,7 @@ import dev.chaws.automaticinventory.messaging.Messages; import dev.chaws.automaticinventory.utilities.Chat; import dev.chaws.automaticinventory.utilities.Level; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -25,6 +26,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command var playerConfig = PlayerConfig.fromPlayer(player); + // Guard to prevent out of index error + if (args.length < 1) { + Chat.sendMessage(player, NamedTextColor.GOLD, "Usage: /autosort [chest|inv]"); + return false; + } + var optionName = args[0].toLowerCase(); if (optionName.startsWith("chest")) { if (!AutomaticInventory.hasPermission(Features.SortChests, player)) { diff --git a/src/main/java/dev/chaws/automaticinventory/commands/DepositAllCommand.java b/src/main/java/dev/chaws/automaticinventory/commands/DepositAllCommand.java index 65636d8..72cf6fe 100644 --- a/src/main/java/dev/chaws/automaticinventory/commands/DepositAllCommand.java +++ b/src/main/java/dev/chaws/automaticinventory/commands/DepositAllCommand.java @@ -7,6 +7,7 @@ import dev.chaws.automaticinventory.tasks.AsyncChestDepositTask; import dev.chaws.automaticinventory.utilities.Chat; import dev.chaws.automaticinventory.utilities.Level; +import org.bukkit.Bukkit; import org.bukkit.ChunkSnapshot; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -28,6 +29,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command return true; } + Chat.sendMessage(player, Level.Info, Messages.StartingDepositAll); + //gather snapshots of adjacent chunks var location = player.getLocation(); var centerChunk = location.getChunk(); @@ -51,9 +54,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command var startY = player.getEyeLocation().getBlockY(); var startX = player.getEyeLocation().getBlockX(); var startZ = player.getEyeLocation().getBlockZ(); - Thread thread = new AsyncChestDepositTask(world, snapshots, minY, maxY, startX, startY, startZ, player); - thread.setPriority(Thread.MIN_PRIORITY); - thread.start(); + Bukkit.getScheduler().runTaskAsynchronously(AutomaticInventory.instance, () -> new AsyncChestDepositTask(world, snapshots, minY, maxY, startX, startY, startZ, player).start()); if (!playerConfig.hasUsedDepositAll()) { playerConfig.setUsedDepositAll(true); diff --git a/src/main/java/dev/chaws/automaticinventory/configuration/PlayerConfig.java b/src/main/java/dev/chaws/automaticinventory/configuration/PlayerConfig.java index 3325677..654ce36 100644 --- a/src/main/java/dev/chaws/automaticinventory/configuration/PlayerConfig.java +++ b/src/main/java/dev/chaws/automaticinventory/configuration/PlayerConfig.java @@ -54,7 +54,7 @@ public static PlayerConfig fromPlayer(Player player) { if (data.isEmpty()) { return new PlayerConfig(player); } else { - return (PlayerConfig)data.get(0).value(); + return (PlayerConfig) data.get(0).value(); } } @@ -176,9 +176,9 @@ public void setGotQuickDepositInfo(boolean newValue) { } public void saveChanges() { - if (!this.isDirty) { - return; - } + if (!this.isDirty) { + return; + } this.waitForLoadComplete(); this.savingThread = new Thread(new DataSaver()); @@ -242,9 +242,12 @@ private void readDataFromFile() { needRetry = false; FileConfiguration config = YamlConfiguration.loadConfiguration(this.playerConfigFile); this.sortChests = config.getBoolean("Sort Chests", GlobalConfig.autosortEnabledByDefault); - this.sortInventory = config.getBoolean("Sort Personal Inventory", GlobalConfig.autosortEnabledByDefault); - this.quickDepositEnabled = config.getBoolean("Quick Deposit Enabled", GlobalConfig.quickDepositEnabledByDefault); - this.autoRefillEnabled = config.getBoolean("Auto Refill Enabled", GlobalConfig.autoRefillEnabledByDefault); + this.sortInventory = config.getBoolean("Sort Personal Inventory", + GlobalConfig.autosortEnabledByDefault); + this.quickDepositEnabled = config.getBoolean("Quick Deposit Enabled", + GlobalConfig.quickDepositEnabledByDefault); + this.autoRefillEnabled = config.getBoolean("Auto Refill Enabled", + GlobalConfig.autoRefillEnabledByDefault); this.usedQuickDeposit = config.getBoolean("Used Quick Deposit", false); this.receivedChestSortInfo = config.getBoolean("Received Messages.Chest Inventory", false); this.receivedInventorySortInfo = config.getBoolean("Received Messages.Personal Inventory", false); @@ -252,7 +255,8 @@ private void readDataFromFile() { this.receivedDepositAllInfo = config.getBoolean("Received Messages.Deposit All", false); } - //if there's any problem with the file's content, retry up to 5 times with 5 milliseconds between + // if there's any problem with the file's content, retry up to 5 times with 5 + // milliseconds between catch (Exception e) { latestException = e; needRetry = true; @@ -268,10 +272,11 @@ private void readDataFromFile() { } while (needRetry && retriesRemaining >= 0); - //if last attempt failed, log information about the problem + // if last attempt failed, log information about the problem if (needRetry) { var errors = new StringWriter(); - latestException.printStackTrace(new PrintWriter(errors)); + if (latestException != null) + latestException.printStackTrace(new PrintWriter(errors)); AutomaticInventory.log.info("Failed to load data for " + playerID + " " + errors); } } diff --git a/src/main/java/dev/chaws/automaticinventory/hooks/LWCHook.java b/src/main/java/dev/chaws/automaticinventory/hooks/LWCHook.java new file mode 100644 index 0000000..a36355d --- /dev/null +++ b/src/main/java/dev/chaws/automaticinventory/hooks/LWCHook.java @@ -0,0 +1,33 @@ +package dev.chaws.automaticinventory.hooks; + +import com.griefcraft.lwc.LWC; +import org.bukkit.Location; +import org.bukkit.block.DoubleChest; +import org.bukkit.entity.Player; +import org.bukkit.inventory.BlockInventoryHolder; +import org.bukkit.inventory.Inventory; + +public class LWCHook { + + private static boolean enabled = false; + + public static void enable() { + enabled = true; + } + + public static boolean canUseContainer(Inventory inventory, Player player) { + if (inventory.getHolder() instanceof BlockInventoryHolder || inventory.getHolder() instanceof DoubleChest) { + Location location = inventory.getLocation(); + return canUseContainer(location, player); + } + return false; + } + + public static boolean canUseContainer(Location location, Player player) { + if (enabled) { + var protection = LWC.getInstance().findProtection(location); + return protection != null && protection.isRealOwner(player); + } + return true; + } +} diff --git a/src/main/java/dev/chaws/automaticinventory/listeners/RefillStacksListener.java b/src/main/java/dev/chaws/automaticinventory/listeners/RefillStacksListener.java index f25e7e9..90671a0 100644 --- a/src/main/java/dev/chaws/automaticinventory/listeners/RefillStacksListener.java +++ b/src/main/java/dev/chaws/automaticinventory/listeners/RefillStacksListener.java @@ -5,7 +5,6 @@ import dev.chaws.automaticinventory.configuration.GlobalConfig; import dev.chaws.automaticinventory.configuration.PlayerConfig; import dev.chaws.automaticinventory.tasks.AutoRefillHotBarTask; -import dev.chaws.automaticinventory.utilities.ItemUtilities; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; diff --git a/src/main/java/dev/chaws/automaticinventory/listeners/SortChestsListener.java b/src/main/java/dev/chaws/automaticinventory/listeners/SortChestsListener.java index 44fb1f3..320b098 100644 --- a/src/main/java/dev/chaws/automaticinventory/listeners/SortChestsListener.java +++ b/src/main/java/dev/chaws/automaticinventory/listeners/SortChestsListener.java @@ -3,6 +3,7 @@ import dev.chaws.automaticinventory.AutomaticInventory; import dev.chaws.automaticinventory.configuration.Features; import dev.chaws.automaticinventory.configuration.PlayerConfig; +import dev.chaws.automaticinventory.hooks.LWCHook; import dev.chaws.automaticinventory.messaging.Messages; import dev.chaws.automaticinventory.tasks.InventorySorter; import dev.chaws.automaticinventory.utilities.Chat; @@ -31,16 +32,18 @@ public void onInventoryOpen(InventoryOpenEvent event) { if (!player.isSneaking() && PlayerConfig.featureEnabled(Features.SortChests, player)) { var topInventory = event.getView().getTopInventory(); - if (!InventoryUtilities.isSortableChestInventory(topInventory, event.getView().getTitle())) { - return; - } + if (LWCHook.canUseContainer(topInventory, player)) { + if (!InventoryUtilities.isSortableChestInventory(topInventory, event.getView().title().examinableName())) { + return; + } - var sorter = new InventorySorter(topInventory, 0); - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(AutomaticInventory.instance, sorter, 1L); + var sorter = new InventorySorter(topInventory, 0); + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(AutomaticInventory.instance, sorter, 1L); - if (!playerConfig.hasReceivedChestSortInfo()) { - Chat.sendMessage(player, Level.Info, Messages.ChestSortEducation3); - playerConfig.setReceivedChestSortInfo(true); + if (!playerConfig.hasReceivedChestSortInfo()) { + Chat.sendMessage(player, Level.Info, Messages.ChestSortEducation3); + playerConfig.setReceivedChestSortInfo(true); + } } } } diff --git a/src/main/java/dev/chaws/automaticinventory/messaging/LocalizedMessages.java b/src/main/java/dev/chaws/automaticinventory/messaging/LocalizedMessages.java index fa251a2..185e6c0 100644 --- a/src/main/java/dev/chaws/automaticinventory/messaging/LocalizedMessages.java +++ b/src/main/java/dev/chaws/automaticinventory/messaging/LocalizedMessages.java @@ -47,6 +47,7 @@ public static void initialize(File dataFolder) { instance.addDefault(defaults, Messages.AutoRefillHelp, "Options are /autorefill toggle, /autorefill enable, and /autorefill disable."); instance.addDefault(defaults, Messages.AutoRefillEnabled, "Auto refill enabled."); instance.addDefault(defaults, Messages.AutoRefillDisabled, "Auto refill disabled."); + instance.addDefault(defaults, Messages.StartingDepositAll, "Checking for items to deposit.. This may take a moment."); //load the configuration file FileConfiguration config = YamlConfiguration.loadConfiguration(localizationFile); @@ -108,12 +109,10 @@ synchronized public String getMessage(Messages messageID, String... args) { } private static class LocalizedMessage { - public Messages id; public String text; public String notes; public LocalizedMessage(Messages id, String text, String notes) { - this.id = id; this.text = text; this.notes = notes; } diff --git a/src/main/java/dev/chaws/automaticinventory/messaging/Messages.java b/src/main/java/dev/chaws/automaticinventory/messaging/Messages.java index ace3e35..ef90f91 100644 --- a/src/main/java/dev/chaws/automaticinventory/messaging/Messages.java +++ b/src/main/java/dev/chaws/automaticinventory/messaging/Messages.java @@ -22,5 +22,6 @@ public enum Messages { QuickDepositDisabled, AutoRefillHelp, AutoRefillEnabled, - AutoRefillDisabled + AutoRefillDisabled, + StartingDepositAll, } diff --git a/src/main/java/dev/chaws/automaticinventory/tasks/AsyncChestDepositTask.java b/src/main/java/dev/chaws/automaticinventory/tasks/AsyncChestDepositTask.java index a61ae66..fb988df 100644 --- a/src/main/java/dev/chaws/automaticinventory/tasks/AsyncChestDepositTask.java +++ b/src/main/java/dev/chaws/automaticinventory/tasks/AsyncChestDepositTask.java @@ -3,6 +3,7 @@ import dev.chaws.automaticinventory.AutomaticInventory; import dev.chaws.automaticinventory.common.DepositRecord; import dev.chaws.automaticinventory.configuration.*; +import dev.chaws.automaticinventory.hooks.LWCHook; import dev.chaws.automaticinventory.messaging.*; import dev.chaws.automaticinventory.utilities.*; import org.bukkit.*; @@ -183,14 +184,20 @@ public void run() { if (event.useInteractedBlock() != Event.Result.DENY) { var state = block.getState(); if (state instanceof InventoryHolder chest) { - var chestInventory = chest.getInventory(); - if (!this.respectExclusions || InventoryUtilities.isSortableChestInventory(chestInventory, state instanceof Nameable nameable ? nameable.getCustomName() : null)) { - var playerInventory = player.getInventory(); - - var deposits = InventoryUtilities.depositMatching(playerInventory, chestInventory, false); - - this.runningDepositRecord.totalItems += deposits.totalItems; - } + if (LWCHook.canUseContainer(chestLocation, player)) { + var chestInventory = chest.getInventory(); + var name = chest.getClass().getSimpleName(); + if (state instanceof Nameable nameable && nameable.customName() != null) { + name = nameable.customName().toString(); + } + if (!this.respectExclusions || InventoryUtilities.isSortableChestInventory(chestInventory, name)) { + var playerInventory = player.getInventory(); + + var deposits = InventoryUtilities.depositMatching(playerInventory, chestInventory, false); + + this.runningDepositRecord.totalItems += deposits.totalItems; + } + } } } diff --git a/src/main/java/dev/chaws/automaticinventory/tasks/AutoRefillHotBarTask.java b/src/main/java/dev/chaws/automaticinventory/tasks/AutoRefillHotBarTask.java index 2513644..0b86adf 100644 --- a/src/main/java/dev/chaws/automaticinventory/tasks/AutoRefillHotBarTask.java +++ b/src/main/java/dev/chaws/automaticinventory/tasks/AutoRefillHotBarTask.java @@ -3,7 +3,6 @@ import dev.chaws.automaticinventory.configuration.PlayerConfig; import dev.chaws.automaticinventory.messaging.Messages; import dev.chaws.automaticinventory.utilities.Chat; -import dev.chaws.automaticinventory.utilities.ItemUtilities; import dev.chaws.automaticinventory.utilities.Level; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; diff --git a/src/main/java/dev/chaws/automaticinventory/tasks/InventorySorter.java b/src/main/java/dev/chaws/automaticinventory/tasks/InventorySorter.java index ae3a31f..8441094 100644 --- a/src/main/java/dev/chaws/automaticinventory/tasks/InventorySorter.java +++ b/src/main/java/dev/chaws/automaticinventory/tasks/InventorySorter.java @@ -1,12 +1,10 @@ package dev.chaws.automaticinventory.tasks; -import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; public class InventorySorter implements Runnable { @@ -73,18 +71,11 @@ public int compare(ItemStack a, ItemStack b) { return result; } - //noinspection removal - var aData = a.getData(); - //noinspection removal - var bData = b.getData(); + var aMeta = a.getItemMeta(); + var bMeta = b.getItemMeta(); - //noinspection deprecation - var aDataData = aData == null ? null : aData.getData(); - //noinspection deprecation - var bDataData = bData == null ? null : bData.getData(); - - if (aDataData != null && bDataData != null) { - result = Byte.compare(bDataData, aDataData); + if (aMeta != null && bMeta != null) { + result = bMeta.toString().compareTo(aMeta.toString()); if (result != 0) { return result; } diff --git a/src/main/java/dev/chaws/automaticinventory/utilities/BlockUtilities.java b/src/main/java/dev/chaws/automaticinventory/utilities/BlockUtilities.java index d68e74d..599b3d3 100644 --- a/src/main/java/dev/chaws/automaticinventory/utilities/BlockUtilities.java +++ b/src/main/java/dev/chaws/automaticinventory/utilities/BlockUtilities.java @@ -1,6 +1,5 @@ package dev.chaws.automaticinventory.utilities; -import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.block.Barrel; import org.bukkit.block.Block; diff --git a/src/main/java/dev/chaws/automaticinventory/utilities/Chat.java b/src/main/java/dev/chaws/automaticinventory/utilities/Chat.java index ac03491..e441021 100644 --- a/src/main/java/dev/chaws/automaticinventory/utilities/Chat.java +++ b/src/main/java/dev/chaws/automaticinventory/utilities/Chat.java @@ -3,20 +3,22 @@ import dev.chaws.automaticinventory.AutomaticInventory; import dev.chaws.automaticinventory.messaging.LocalizedMessages; import dev.chaws.automaticinventory.messaging.Messages; -import org.bukkit.ChatColor; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + import org.bukkit.entity.Player; public class Chat { - public static void broadcastMessage(ChatColor color, Messages messageID, String... args) { + public static void broadcastMessage(NamedTextColor color, Messages messageID, String... args) { broadcastMessage(color, messageID, 0, args); } - public static void broadcastMessage(ChatColor color, Messages messageID, long delayInTicks, String... args) { + public static void broadcastMessage(NamedTextColor color, Messages messageID, long delayInTicks, String... args) { var message = LocalizedMessages.instance.getMessage(messageID, args); broadcastMessage(color, message, delayInTicks); } - public static void broadcastMessage(ChatColor color, String message) { + public static void broadcastMessage(NamedTextColor color, String message) { if (message == null || message.length() == 0) { return; } @@ -24,25 +26,27 @@ public static void broadcastMessage(ChatColor color, String message) { broadcastMessage(color, message, 0); } - public static void broadcastMessage(ChatColor color, String message, long delayInTicks) { + public static void broadcastMessage(NamedTextColor color, String message, long delayInTicks) { var task = new BroadcastMessageTask(color, message); if (delayInTicks > 0) { - AutomaticInventory.instance.getServer().getScheduler().runTaskLater(AutomaticInventory.instance, task, delayInTicks); + AutomaticInventory.instance.getServer().getScheduler().runTaskLater(AutomaticInventory.instance, task, + delayInTicks); } else { task.run(); } } - public static void sendMessage(Player player, ChatColor color, Messages messageID, String... args) { + public static void sendMessage(Player player, NamedTextColor color, Messages messageID, String... args) { sendMessage(player, color, messageID, 0, args); } - public static void sendMessage(Player player, ChatColor color, Messages messageID, long delayInTicks, String... args) { + public static void sendMessage(Player player, NamedTextColor color, Messages messageID, long delayInTicks, + String... args) { var message = LocalizedMessages.instance.getMessage(messageID, args); sendMessage(player, color, message, delayInTicks); } - public static void sendMessage(Player player, ChatColor color, String message) { + public static void sendMessage(Player player, NamedTextColor color, String message) { if (message == null || message.length() == 0) { return; } @@ -50,24 +54,26 @@ public static void sendMessage(Player player, ChatColor color, String message) { sendMessage(player, color, message, 0); } - public static void sendMessage(Player player, ChatColor color, String message, long delayInTicks) { + public static void sendMessage(Player player, NamedTextColor color, String message, long delayInTicks) { var task = new SendPlayerMessageTask(player, color, message); if (delayInTicks > 0) { - AutomaticInventory.instance.getServer().getScheduler().runTaskLater(AutomaticInventory.instance, task, delayInTicks); + AutomaticInventory.instance.getServer().getScheduler().runTaskLater(AutomaticInventory.instance, task, + delayInTicks); } else { task.run(); } } } -//sends a message to a player -//used to send delayed messages, for example help text triggered by a player's chat +// sends a message to a player +// used to send delayed messages, for example help text triggered by a player's +// chat class SendPlayerMessageTask implements Runnable { private final Player player; - private final ChatColor color; + private final NamedTextColor color; private final String message; - public SendPlayerMessageTask(Player player, ChatColor color, String message) { + public SendPlayerMessageTask(Player player, NamedTextColor color, String message) { this.player = player; this.color = color; this.message = message; @@ -78,22 +84,22 @@ public void run() { if (player == null) { AutomaticInventory.log.info(color + message); } else { - player.sendMessage(color + message); + player.sendMessage(Component.text(message).color(color)); } } } class BroadcastMessageTask implements Runnable { - private final ChatColor color; + private final NamedTextColor color; private final String message; - public BroadcastMessageTask(ChatColor color, String message) { + public BroadcastMessageTask(NamedTextColor color, String message) { this.color = color; this.message = message; } @Override public void run() { - AutomaticInventory.instance.getServer().broadcastMessage(color + message); + AutomaticInventory.instance.getServer().sendMessage(Component.text(message).color(color)); } } diff --git a/src/main/java/dev/chaws/automaticinventory/utilities/InventoryUtilities.java b/src/main/java/dev/chaws/automaticinventory/utilities/InventoryUtilities.java index 8fc1640..19f1038 100644 --- a/src/main/java/dev/chaws/automaticinventory/utilities/InventoryUtilities.java +++ b/src/main/java/dev/chaws/automaticinventory/utilities/InventoryUtilities.java @@ -70,7 +70,8 @@ public static boolean isSortableChestInventory( } public static DepositRecord depositMatching(PlayerInventory source, Inventory destination, boolean depositHotbar) { - var eligibleSignatures = new HashSet(); + + var eligibleSignatures = new HashSet(destination.getSize()); var deposits = new DepositRecord(); for (var i = 0; i < destination.getSize(); i++) { var destinationStack = destination.getItem(i); diff --git a/src/main/java/dev/chaws/automaticinventory/utilities/ItemUtilities.java b/src/main/java/dev/chaws/automaticinventory/utilities/ItemUtilities.java index d98b468..ab292a3 100644 --- a/src/main/java/dev/chaws/automaticinventory/utilities/ItemUtilities.java +++ b/src/main/java/dev/chaws/automaticinventory/utilities/ItemUtilities.java @@ -7,13 +7,9 @@ public class ItemUtilities { public static String getSignature(ItemStack stack) { var signature = stack.getType().name(); if (stack.getMaxStackSize() > 1) { - // getData will not actually be removed according to the spigot forums - //noinspection removal - var data = stack.getData(); + var data = stack.getItemMeta(); if (data != null) { - // getData will not actually be removed according to the spigot forums - //noinspection deprecation - signature += "." + String.valueOf(data.getData()); + signature += "." + String.valueOf(data); } } diff --git a/src/main/java/dev/chaws/automaticinventory/utilities/Level.java b/src/main/java/dev/chaws/automaticinventory/utilities/Level.java index 827d170..5bb7e11 100644 --- a/src/main/java/dev/chaws/automaticinventory/utilities/Level.java +++ b/src/main/java/dev/chaws/automaticinventory/utilities/Level.java @@ -1,11 +1,11 @@ package dev.chaws.automaticinventory.utilities; -import org.bukkit.ChatColor; +import net.kyori.adventure.text.format.NamedTextColor; public class Level { - public final static ChatColor Info = ChatColor.AQUA; - public final static ChatColor Instr = ChatColor.YELLOW; - public final static ChatColor Warn = ChatColor.GOLD; - public final static ChatColor Error = ChatColor.RED; - public final static ChatColor Success = ChatColor.GREEN; + public final static NamedTextColor Info = NamedTextColor.AQUA; + public final static NamedTextColor Instr = NamedTextColor.YELLOW; + public final static NamedTextColor Warn = NamedTextColor.GOLD; + public final static NamedTextColor Error = NamedTextColor.RED; + public final static NamedTextColor Success = NamedTextColor.GREEN; } diff --git a/src/main/java/dev/chaws/automaticinventory/utilities/Metrics.java b/src/main/java/dev/chaws/automaticinventory/utilities/Metrics.java index 02debac..6f47c7d 100644 --- a/src/main/java/dev/chaws/automaticinventory/utilities/Metrics.java +++ b/src/main/java/dev/chaws/automaticinventory/utilities/Metrics.java @@ -21,7 +21,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Method; -import java.net.URL; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.Callable; @@ -136,7 +136,7 @@ private void appendPlatformData(JsonObjectBuilder builder) { } private void appendServiceData(JsonObjectBuilder builder) { - builder.appendField("pluginVersion", plugin.getDescription().getVersion()); + builder.appendField("pluginVersion", plugin.getPluginMeta().getVersion()); } private int getPlayerAmount() { @@ -327,7 +327,7 @@ private void sendData(JsonObjectBuilder.JsonObject data) throws Exception { infoLogger.accept("Sent bStats metrics data: " + data.toString()); } String url = String.format(REPORT_URL, platform); - HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); + HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection(); // Compress the data to save bandwidth byte[] compressedData = compress(data.toString()); connection.setRequestMethod("POST");