diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/StatusEffect.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/StatusEffect.java index 74398f96b..ca78da5e1 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/StatusEffect.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/StatusEffect.java @@ -4,6 +4,7 @@ import java.util.Optional; import java.util.OptionalInt; import java.util.concurrent.TimeUnit; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; @@ -84,7 +85,7 @@ public class StatusEffect implements Keyed { Optional optional = PersistentDataAPI.getOptionalString(p, getKey()); if (optional.isPresent()) { - String[] data = optional.get().split(";"); + String[] data = PatternUtils.SEMICOLON.split(optional.get()); long timestamp = Long.parseLong(data[1]); if (timestamp == 0 || timestamp >= System.currentTimeMillis()) { @@ -109,7 +110,7 @@ public class StatusEffect implements Keyed { Optional optional = PersistentDataAPI.getOptionalString(p, getKey()); if (optional.isPresent()) { - String[] data = optional.get().split(";"); + String[] data = PatternUtils.SEMICOLON.split(optional.get()); return OptionalInt.of(Integer.parseInt(data[0])); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/BookSlimefunGuide.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/BookSlimefunGuide.java index d7e77c331..de62610e5 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/BookSlimefunGuide.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/BookSlimefunGuide.java @@ -227,7 +227,7 @@ public class BookSlimefunGuide implements ISlimefunGuide { if (item.getItem().hasItemMeta() && item.getItem().getItemMeta().hasLore()) { for (String line : item.getItem().getItemMeta().getLore()) { - tooltip.append("\n" + line); + tooltip.append('\n').append(line); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/GuideSettings.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/GuideSettings.java index 766ac35d3..6db43b6f3 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/GuideSettings.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/GuideSettings.java @@ -6,6 +6,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -379,18 +380,18 @@ public final class GuideSettings { String info = entry.getKey(); if (!info.startsWith("&")) { - String[] segments = info.split(","); + String[] segments = PatternUtils.COMMA.split(info); info = SlimefunPlugin.getLocal().getMessage(p, "guide.credits.roles." + segments[0]); if (segments.length == 2) { - info += " &7(" + SlimefunPlugin.getLocal().getMessage(p, "languages." + segments[1]) + ")"; + info += " &7(" + SlimefunPlugin.getLocal().getMessage(p, "languages." + segments[1]) + ')'; } } if (entry.getValue() > 0) { String commits = SlimefunPlugin.getLocal().getMessage(p, "guide.credits." + (entry.getValue() > 1 ? "commits": "commit")); - info += " &7(" + entry.getValue() + " " + commits + ")"; + info += " &7(" + entry.getValue() + ' ' + commits + ')'; } lore.add(ChatColors.color(info)); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java index 374dab4c2..a67fd736e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Material; import org.bukkit.Tag; import org.bukkit.block.Block; @@ -271,7 +272,7 @@ public abstract class ProgrammableAndroid extends ScriptHolder implements Invent } } else { - String[] script = BlockStorage.getLocationInfo(b.getLocation(), "script").split("-"); + String[] script = PatternUtils.DASH.split(BlockStorage.getLocationInfo(b.getLocation(), "script")); int index = Integer.parseInt(BlockStorage.getLocationInfo(b.getLocation(), "index")) + 1; if (index >= script.length) index = 0; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ScriptHolder.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ScriptHolder.java index e93c64c13..22801e310 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ScriptHolder.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ScriptHolder.java @@ -7,6 +7,7 @@ import java.util.Comparator; import java.util.List; import java.util.function.Predicate; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -71,7 +72,7 @@ abstract class ScriptHolder extends SimpleSlimefunItem { public void openScript(Player p, Block b, String script) { ChestMenu menu = new ChestMenu(ChatColor.DARK_AQUA + SlimefunPlugin.getLocal().getMessage(p, "android.scripts.editor")); - String[] commands = script.split("-"); + String[] commands = PatternUtils.SLASH_SEPARATOR.split(script); menu.addItem(0, new CustomItem(ScriptPart.START.getItem(), SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions.START"), "", "&7\u21E8 &eLeft Click &7to return to the Android's interface")); menu.addMenuClickHandler(0, (pl, slot, item, action) -> { @@ -112,10 +113,10 @@ abstract class ScriptHolder extends SimpleSlimefunItem { for (String command : commands) { if (j > 0) { if (j == index) { - builder.append(commands[j] + "-"); - builder.append(commands[j] + "-"); + builder.append(commands[j]).append('-') + .append(commands[j]).append('-'); } - else if (j < commands.length - 1) builder.append(command + "-"); + else if (j < commands.length - 1) builder.append(command).append('-'); } j++; } @@ -129,7 +130,7 @@ abstract class ScriptHolder extends SimpleSlimefunItem { StringBuilder builder = new StringBuilder(ScriptPart.START + "-"); for (String command : commands) { - if (j != index && j > 0 && j < commands.length - 1) builder.append(command + "-"); + if (j != index && j > 0 && j < commands.length - 1) builder.append(command).append('-'); j++; } @@ -391,7 +392,7 @@ abstract class ScriptHolder extends SimpleSlimefunItem { protected void openScriptComponentEditor(Player p, Block b, String script, int index) { ChestMenu menu = new ChestMenu(ChatColor.DARK_AQUA + SlimefunPlugin.getLocal().getMessage(p, "android.scripts.editor")); - String[] commands = script.split("-"); + String[] commands = PatternUtils.SLASH_SEPARATOR.split(script); ChestMenuUtils.drawBackground(menu, 0, 1, 2, 3, 4, 5, 6, 7, 8); @@ -400,7 +401,7 @@ abstract class ScriptHolder extends SimpleSlimefunItem { StringBuilder builder = new StringBuilder("START-"); for (String command : commands) { - if (i != index && i > 0 && i < commands.length - 1) builder.append(command + "-"); + if (i != index && i > 0 && i < commands.length - 1) builder.append(command).append('-'); i++; } @@ -419,8 +420,8 @@ abstract class ScriptHolder extends SimpleSlimefunItem { for (String command : commands) { if (j > 0) { - if (j == index) builder.append(part.toString() + "-"); - else if (j < commands.length - 1) builder.append(command + "-"); + if (j == index) builder.append(part).append('-'); + else if (j < commands.length - 1) builder.append(command).append('-'); } j++; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/EnhancedCraftingTable.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/EnhancedCraftingTable.java index 5bebafc99..a69570c46 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/EnhancedCraftingTable.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/EnhancedCraftingTable.java @@ -3,6 +3,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks; import java.util.List; import java.util.UUID; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -102,7 +103,8 @@ public class EnhancedCraftingTable extends MultiBlockMachine { for (String line : backpack.getItemMeta().getLore()) { if (line.startsWith(ChatColor.translateAlternateColorCodes('&', "&7ID: ")) && line.contains("#")) { id = line.replace(ChatColor.translateAlternateColorCodes('&', "&7ID: "), ""); - PlayerProfile.fromUUID(UUID.fromString(id.split("#")[0])).getBackpack(Integer.parseInt(id.split("#")[1])).setSize(size); + String[] idSplit = PatternUtils.HASH.split(id); + PlayerProfile.fromUUID(UUID.fromString(idSplit[0])).getBackpack(Integer.parseInt(idSplit[1])).setSize(size); break; } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/MagicWorkbench.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/MagicWorkbench.java index eef51a723..5863ca5f4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/MagicWorkbench.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/MagicWorkbench.java @@ -3,6 +3,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks; import java.util.List; import java.util.UUID; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Effect; @@ -105,7 +106,8 @@ public class MagicWorkbench extends MultiBlockMachine { for (String line : backpack.getItemMeta().getLore()) { if (line.startsWith(ChatColor.translateAlternateColorCodes('&', "&7ID: ")) && line.contains("#")) { id = line.replace(ChatColor.translateAlternateColorCodes('&', "&7ID: "), ""); - PlayerProfile.fromUUID(UUID.fromString(id.split("#")[0])).getBackpack(Integer.parseInt(id.split("#")[1])).setSize(size); + String[] idSplit = PatternUtils.HASH.split(id); + PlayerProfile.fromUUID(UUID.fromString(idSplit[0])).getBackpack(Integer.parseInt(idSplit[1])).setSize(size); break; } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/GearListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/GearListener.java index feb414284..ea8a255a6 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/GearListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/GearListener.java @@ -1,68 +1,57 @@ package io.github.thebusybiscuit.slimefun4.implementation.listeners; -import me.mrCookieSlime.Slimefun.SlimefunPlugin; -import me.mrCookieSlime.Slimefun.Lists.SlimefunItems; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.JetBoots; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.Jetpack; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.Setup.SlimefunManager; -import me.mrCookieSlime.Slimefun.api.Slimefun; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerToggleSneakEvent; - import io.github.thebusybiscuit.slimefun4.implementation.tasks.JetBootsTask; import io.github.thebusybiscuit.slimefun4.implementation.tasks.JetpackTask; import io.github.thebusybiscuit.slimefun4.implementation.tasks.MagnetTask; import io.github.thebusybiscuit.slimefun4.implementation.tasks.ParachuteTask; +import me.mrCookieSlime.Slimefun.Lists.SlimefunItems; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.JetBoots; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.Jetpack; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.Setup.SlimefunManager; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.api.Slimefun; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerToggleSneakEvent; public class GearListener implements Listener { - public GearListener(SlimefunPlugin plugin) { - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } + public GearListener(SlimefunPlugin plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } - @EventHandler - public void onToggleSneak(PlayerToggleSneakEvent e) { - if (e.isSneaking()) { - Player p = e.getPlayer(); - SlimefunItem chestplate = SlimefunItem.getByItem(p.getInventory().getChestplate()); - SlimefunItem boots = SlimefunItem.getByItem(p.getInventory().getBoots()); - - if (chestplate != null) { - if (chestplate instanceof Jetpack) { - if (Slimefun.hasUnlocked(p, chestplate, true)) { - double thrust = ((Jetpack) chestplate).getThrust(); + @EventHandler + public void onToggleSneak(PlayerToggleSneakEvent e) { + if (!e.isSneaking()) return; + Player p = e.getPlayer(); - if (thrust > 0.2) { - JetpackTask task = new JetpackTask(p, thrust); - task.setID(Bukkit.getScheduler().scheduleSyncRepeatingTask(SlimefunPlugin.instance, task, 0L, 3L)); - } - } - } - else if (chestplate.getID().equals("PARACHUTE") && Slimefun.hasUnlocked(p, chestplate, true)) { - ParachuteTask task = new ParachuteTask(p); - task.setID(Bukkit.getScheduler().scheduleSyncRepeatingTask(SlimefunPlugin.instance, task, 0L, 3L)); - } - } - - if (boots instanceof JetBoots && Slimefun.hasUnlocked(p, boots, true)) { - double speed = ((JetBoots) boots).getSpeed(); - - if (speed > 0.2) { - JetBootsTask task = new JetBootsTask(p, speed); - task.setID(Bukkit.getScheduler().scheduleSyncRepeatingTask(SlimefunPlugin.instance, task, 0L, 2L)); - } - } - - if (SlimefunManager.containsSimilarItem(p.getInventory(), SlimefunItems.INFUSED_MAGNET, true)) { - MagnetTask task = new MagnetTask(p); - task.setID(Bukkit.getScheduler().scheduleSyncRepeatingTask(SlimefunPlugin.instance, task, 0L, 8L)); - } - } - } + if (p.getInventory().getChestplate() != null) { + SlimefunItem chestplate = SlimefunItem.getByItem(p.getInventory().getChestplate()); + if (chestplate == null || !Slimefun.hasUnlocked(p, chestplate, true)) return; + if (chestplate instanceof Jetpack) { + double thrust = ((Jetpack) chestplate).getThrust(); + + if (thrust > 0.2) + new JetpackTask(p, thrust).scheduleRepeating(0, 3); + } else if (chestplate.getID().equals("PARACHUTE")) + new ParachuteTask(p).scheduleRepeating(0, 3); + } + + if (p.getInventory().getBoots() != null) { + SlimefunItem boots = SlimefunItem.getByItem(p.getInventory().getBoots()); + + if (boots instanceof JetBoots && Slimefun.hasUnlocked(p, boots, true)) { + double speed = ((JetBoots) boots).getSpeed(); + + if (speed > 0.2) + new JetBootsTask(p, speed).scheduleRepeating(0, 2); + } + } + + if (SlimefunManager.containsSimilarItem(p.getInventory(), SlimefunItems.INFUSED_MAGNET, true)) + new MagnetTask(p).scheduleRepeating(0, 8); + } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java index 10f360545..ea59fa6f3 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java @@ -7,6 +7,7 @@ import java.util.Random; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Sound; @@ -142,16 +143,17 @@ public class TalismanListener implements Listener { for (Enchantment en : Enchantment.values()) { for (int i = 1; i <= en.getMaxLevel(); i++) { - if ((boolean) Slimefun.getItemValue("MAGICIAN_TALISMAN", "allow-enchantments." + en.getKey().getKey() + ".level." + i)) { - if (en.canEnchantItem(e.getItem()) || e.getItem().getType() == Material.BOOK) { - enchantments.add(en.getKey().getKey() + '-' + i); - } - } + if ((boolean) Slimefun.getItemValue("MAGICIAN_TALISMAN", "allow-enchantments." + en.getKey().getKey() + ".level." + i) + && en.canEnchantItem(e.getItem()) || e.getItem().getType() == Material.BOOK + ) { + enchantments.add(en.getKey().getKey() + '-' + i); + } } } String enchant = enchantments.get(random.nextInt(enchantments.size())); - e.getEnchantsToAdd().put(Enchantment.getByKey(NamespacedKey.minecraft(enchant.split("-")[0])), Integer.parseInt(enchant.split("-")[1])); + String[] enchantSplit = PatternUtils.SLASH_SEPARATOR.split(enchant); + e.getEnchantsToAdd().put(Enchantment.getByKey(NamespacedKey.minecraft(enchantSplit[0])), Integer.parseInt(enchantSplit[1])); } if (!e.getEnchantsToAdd().containsKey(Enchantment.SILK_TOUCH) && Enchantment.LOOT_BONUS_BLOCKS.canEnchantItem(e.getItem()) && Talisman.checkFor(e, (SlimefunItemStack) SlimefunItems.TALISMAN_WIZARD)) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/MagnetTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/MagnetTask.java index 3d5ae5085..a3987344a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/MagnetTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/MagnetTask.java @@ -23,8 +23,8 @@ public class MagnetTask extends SlimefunTask { } @Override - protected boolean isInvalid() { - return super.isInvalid() || p.getGameMode() == GameMode.SPECTATOR; + protected boolean isValid() { + return super.isValid() && p.getGameMode() != GameMode.SPECTATOR; } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/SlimefunTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/SlimefunTask.java index e1b30e866..175547cb7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/SlimefunTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/SlimefunTask.java @@ -1,5 +1,6 @@ package io.github.thebusybiscuit.slimefun4.implementation.tasks; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -16,22 +17,29 @@ public abstract class SlimefunTask implements Runnable { this.id = id; } + public void schedule(long delay) { + setID(Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, this, delay)); + } + + public void scheduleRepeating(long delay, long interval) { + setID(Bukkit.getScheduler().scheduleSyncRepeatingTask(SlimefunPlugin.instance, this, delay, interval)); + } + @Override public void run() { - if (!isInvalid()) executeTask(); + if (isValid()) executeTask(); } /** - * * @return True if task was cancelled. */ - protected boolean isInvalid() { + protected boolean isValid() { if (!p.isOnline() || !p.isValid() || p.isDead() || !p.isSneaking()) { Bukkit.getScheduler().cancelTask(id); - return true; + return false; } - return false; + return true; } abstract void executeTask(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/TickerTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/TickerTask.java index 1acd70d3e..29f298c24 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/TickerTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/TickerTask.java @@ -14,6 +14,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.logging.Level; import java.util.stream.Collectors; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Chunk; @@ -276,7 +277,7 @@ public class TickerTask implements Runnable { .append(chunkItemCount.getOrDefault(entry.getKey(), 0)) .append("x &7(") .append(toMillis(entry.getValue())) - .append(")"); + .append(')'); } else hidden++; } @@ -303,8 +304,8 @@ public class TickerTask implements Runnable { } private String formatChunk(String chunk) { - String[] components = chunk.split(";"); - return components[0] + " [" + components[2] + "," + components[3] + "]"; + String[] components = PatternUtils.SEMICOLON.split(chunk); + return components[0] + " [" + components[2] + ',' + components[3] + ']'; } public long getTimings(Block b) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChatUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChatUtils.java index bca5a1212..e61434e4c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChatUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChatUtils.java @@ -34,14 +34,14 @@ public final class ChatUtils { String[] segments = string.toLowerCase().split("_"); - builder.append(Character.toUpperCase(segments[0].charAt(0))); - builder.append(segments[0].substring(1)); + builder.append(Character.toUpperCase(segments[0].charAt(0))) + .append(segments[0].substring(1)); for (int i = 1; i < segments.length; i++) { String segment = segments[i]; - builder.append(" "); - builder.append(Character.toUpperCase(segment.charAt(0))); - builder.append(segment.substring(1)); + builder.append(' ') + .append(Character.toUpperCase(segment.charAt(0))) + .append(segment.substring(1)); } return builder.toString(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChestMenuUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChestMenuUtils.java index 1de824f51..ddca658b8 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChestMenuUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChestMenuUtils.java @@ -34,11 +34,11 @@ public final class ChestMenuUtils { private static final MenuClickHandler CLICK_HANDLER = (p, s, i, a) -> false; - public static final ItemStack getBackground() { + public static ItemStack getBackground() { return UI_BACKGROUND; } - public static final MenuClickHandler getEmptyClickHandler() { + public static MenuClickHandler getEmptyClickHandler() { return CLICK_HANDLER; } @@ -92,17 +92,17 @@ public final class ChestMenuUtils { } } - public static void updateProgressbar(ChestMenu menu, int slot, int timeleft, int time, ItemStack indicator) { + public static void updateProgressbar(ChestMenu menu, int slot, int timeLeft, int time, ItemStack indicator) { ItemStack item = indicator.clone(); ItemMeta im = item.getItemMeta(); im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); if (im instanceof Damageable) { - ((Damageable) im).setDamage(getDurability(item, timeleft, time)); + ((Damageable) im).setDamage(getDurability(item, timeLeft, time)); } im.setDisplayName(" "); - im.setLore(Arrays.asList(getProgressBar(timeleft, time), "", ChatColor.GRAY + NumberUtils.getTimeLeft(timeleft / 2) + " left")); + im.setLore(Arrays.asList(getProgressBar(timeLeft, time), "", ChatColor.GRAY + NumberUtils.getTimeLeft(timeLeft / 2) + " left")); item.setItemMeta(im); menu.replaceExistingItem(slot, item); @@ -111,32 +111,32 @@ public final class ChestMenuUtils { public static String getProgressBar(int time, int total) { StringBuilder progress = new StringBuilder(); float percentage = Math.round(((((total - time) * 100.0F) / total) * 100.0F) / 100.0F); - + if (percentage < 16.0F) progress.append("&4"); else if (percentage < 32.0F) progress.append("&c"); else if (percentage < 48.0F) progress.append("&6"); else if (percentage < 64.0F) progress.append("&e"); else if (percentage < 80.0F) progress.append("&2"); - else progress = progress.append("&a"); - + else progress.append("&a"); + int rest = 20; for (int i = (int) percentage; i >= 5; i = i - 5) { - progress.append(":"); + progress.append(':'); rest--; } progress.append("&7"); for (int i = 0; i < rest; i++) { - progress.append(":"); + progress.append(':'); } - progress.append(" - " + percentage + "%"); + progress.append(" - ").append(percentage).append('%'); return ChatColors.color(progress.toString()); } - private static short getDurability(ItemStack item, int timeleft, int max) { - return (short) ((item.getType().getMaxDurability() / max) * timeleft); + private static short getDurability(ItemStack item, int timeLeft, int max) { + return (short) ((item.getType().getMaxDurability() / max) * timeLeft); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/PatternUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/PatternUtils.java new file mode 100644 index 000000000..51c0ceba5 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/PatternUtils.java @@ -0,0 +1,19 @@ +package io.github.thebusybiscuit.slimefun4.utils; + +import java.util.regex.Pattern; + +/** + * This class is created for common-use patterns used in things such as {@link String#split(String)}.
+ * Every time something like {@link String#split(String)} is called it will compile a {@link Pattern}, for code that is called + * often this can be a massive performance loss. This class solves that, one compile but many uses! + */ +public final class PatternUtils { + + private PatternUtils() {} + + public static final Pattern SEMICOLON = Pattern.compile(";"); + public static final Pattern HASH = Pattern.compile("#"); + public static final Pattern COMMA = Pattern.compile(","); + public static final Pattern SLASH_SEPARATOR = Pattern.compile(" / "); + public static final Pattern DASH = Pattern.compile("-"); +} diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Research.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Research.java index d1805c3ae..8f35c3a1a 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Research.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Research.java @@ -45,8 +45,7 @@ public class Research implements Keyed { /** * The constructor for a Research. * - * Create a new research by calling {@link #Research(int, String, int)}, then - * bind this research to the Slimefun items you want by calling + * Create a new research, then bind this research to the Slimefun items you want by calling * {@link #addItems(SlimefunItem...)}. Once you're finished, call {@link #register()} * to register it. * @@ -56,7 +55,7 @@ public class Research implements Keyed { * @param key A unique identifier for this research * @param id old way of identifying researches * @param name Display name of the research - * @param cost Cost in XP levels to unlock the research + * @param defaultCost Cost in XP levels to unlock the research * */ public Research(NamespacedKey key, int id, String name, int defaultCost) { @@ -215,7 +214,7 @@ public class Research implements Keyed { public void register() { SlimefunPlugin.getResearchCfg().setDefaultValue("enable-researching", true); - String path = key.getNamespace() + "." + key.getKey(); + String path = key.getNamespace() + '.' + key.getKey(); migrate(id, path); if (SlimefunPlugin.getResearchCfg().contains(path + ".enabled") && !SlimefunPlugin.getResearchCfg().getBoolean(path + ".enabled")) { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java index e2bd96939..e59adf025 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java @@ -14,6 +14,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; @@ -69,12 +70,12 @@ public class BlockStorage { } private static String serializeChunk(World world, int x, int z) { - return world.getName() + ";Chunk;" + x + ";" + z; + return world.getName() + ";Chunk;" + x + ';' + z; } private static Location deserializeLocation(String l) { try { - String[] components = l.split(";"); + String[] components = PatternUtils.SEMICOLON.split(l); if (components.length != 4) return null; World w = Bukkit.getWorld(components[0]); @@ -170,7 +171,7 @@ public class BlockStorage { for (String key : cfg.getKeys(false)) { try { - if (world.getName().equals(key.split(";")[0])) { + if (world.getName().equals(PatternUtils.SEMICOLON.split(key)[0])) { SlimefunPlugin.getRegistry().getChunks().put(key, new BlockInfoConfig(parseJSON(cfg.getString(key)))); } } catch (Exception x) { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/PlayerProfile.java b/src/main/java/me/mrCookieSlime/Slimefun/api/PlayerProfile.java index 4d8f9feb2..f7894609e 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/PlayerProfile.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/PlayerProfile.java @@ -13,6 +13,7 @@ import java.util.UUID; import java.util.function.Consumer; import java.util.stream.IntStream; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; @@ -320,10 +321,11 @@ public final class PlayerProfile { String uuid = ""; for (String line : item.getItemMeta().getLore()) { - if (line.startsWith(ChatColor.translateAlternateColorCodes('&', "&7ID: ")) && line.contains("#")) { + if (line.startsWith(ChatColor.translateAlternateColorCodes('&', "&7ID: ")) && line.indexOf('#') != -1) { try { - id = Optional.of(Integer.parseInt(line.split("#")[1])); - uuid = line.split("#")[0].replace(ChatColor.translateAlternateColorCodes('&', "&7ID: "), ""); + String[] splitLine = PatternUtils.HASH.split(line); + id = Optional.of(Integer.parseInt(splitLine[1])); + uuid = splitLine[0].replace(ChatColor.translateAlternateColorCodes('&', "&7ID: "), ""); } catch(NumberFormatException x) { return null; } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/energy/ItemEnergy.java b/src/main/java/me/mrCookieSlime/Slimefun/api/energy/ItemEnergy.java index 117e6ff4c..ab838f827 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/energy/ItemEnergy.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/energy/ItemEnergy.java @@ -3,6 +3,7 @@ package me.mrCookieSlime.Slimefun.api.energy; import java.math.BigDecimal; import java.util.List; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -22,7 +23,7 @@ public final class ItemEnergy { for (String line : item.getItemMeta().getLore()) { if (line.startsWith(ChatColors.color("&c&o&8\u21E8 &e\u26A1 &7")) && line.contains(" / ") && line.endsWith(" J")) { - return Float.valueOf(line.split(" / ")[0].replace(ChatColors.color("&c&o&8\u21E8 &e\u26A1 &7"), "")); + return Float.parseFloat(PatternUtils.SLASH_SEPARATOR.split(line)[0].replace(ChatColors.color("&c&o&8\u21E8 &e\u26A1 &7"), "")); } } @@ -35,7 +36,7 @@ public final class ItemEnergy { for (String line : item.getItemMeta().getLore()) { if (line.startsWith(ChatColors.color("&c&o&8\u21E8 &e\u26A1 &7")) && line.contains(" / ") && line.endsWith(" J")) { - return Float.valueOf(line.split(" / ")[1].replace(" J", "")); + return Float.parseFloat(PatternUtils.SLASH_SEPARATOR.split(line)[1].replace(" J", "")); } }