From f2acf8b17e0050aa22ba2aee6c5878fcee58b0b7 Mon Sep 17 00:00:00 2001 From: Samuel Voeller <12182390+xCykrix@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:17:03 -0400 Subject: [PATCH] feat: add google style format and update to latest PluginCommon --- pom.xml | 10 +-- .../xcykrix/dynamiclights/DynamicLights.java | 11 +-- .../module/DynamicLightCommand.java | 35 ++++---- .../module/events/PlayerHandlerEvent.java | 22 ++--- .../dynamiclights/util/LightManager.java | 83 ++++++++++++------- .../dynamiclights/util/LightSources.java | 29 ++++--- 6 files changed, 109 insertions(+), 81 deletions(-) diff --git a/pom.xml b/pom.xml index 8cbc638..29de8e6 100644 --- a/pom.xml +++ b/pom.xml @@ -71,10 +71,6 @@ sonatype https://oss.sonatype.org/content/groups/public/ - - jitpack.io - https://jitpack.io - @@ -85,9 +81,9 @@ provided - com.github.xCykrix - PluginCommon - 51387786db + com.github.xcykrix + plugincommon + 1.0.0-SNAPSHOT com.comphenix.protocol diff --git a/src/main/java/com/github/xcykrix/dynamiclights/DynamicLights.java b/src/main/java/com/github/xcykrix/dynamiclights/DynamicLights.java index 8de7d0c..7add9df 100644 --- a/src/main/java/com/github/xcykrix/dynamiclights/DynamicLights.java +++ b/src/main/java/com/github/xcykrix/dynamiclights/DynamicLights.java @@ -1,15 +1,16 @@ package com.github.xcykrix.dynamiclights; -import org.bukkit.entity.Player; - import com.comphenix.protocol.utility.MinecraftVersion; import com.github.xcykrix.dynamiclights.module.DynamicLightCommand; import com.github.xcykrix.dynamiclights.util.LightManager; import com.github.xcykrix.dynamiclights.util.LightSources; import com.github.xcykrix.plugincommon.PluginCommon; import com.github.xcykrix.plugincommon.api.records.Resource; +import org.bukkit.entity.Player; +@SuppressWarnings({"unused", "Entrypoint to Spigot API"}) public final class DynamicLights extends PluginCommon { + private LightManager lightManager; @Override @@ -19,9 +20,9 @@ public void initialize() { // Register Configurations this.configurationAPI - .register(new Resource("config.yml", null, this.getResource("config.yml"))) - .register(new Resource("lights.yml", null, this.getResource("lights.yml"))) - .registerLanguageFile(this.getResource("language.yml")); + .register(new Resource("config.yml", null, this.getResource("config.yml"))) + .register(new Resource("lights.yml", null, this.getResource("lights.yml"))) + .registerLanguageFile(this.getResource("language.yml")); // Initialize Light Manager this.lightManager = new LightManager(this, new LightSources(this)); diff --git a/src/main/java/com/github/xcykrix/dynamiclights/module/DynamicLightCommand.java b/src/main/java/com/github/xcykrix/dynamiclights/module/DynamicLightCommand.java index 14f2688..9e2789c 100644 --- a/src/main/java/com/github/xcykrix/dynamiclights/module/DynamicLightCommand.java +++ b/src/main/java/com/github/xcykrix/dynamiclights/module/DynamicLightCommand.java @@ -1,11 +1,5 @@ package com.github.xcykrix.dynamiclights.module; -import java.io.IOException; - -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - import com.github.xcykrix.dynamiclights.module.events.PlayerHandlerEvent; import com.github.xcykrix.dynamiclights.util.LightManager; import com.github.xcykrix.dynamiclights.util.LightSources; @@ -19,11 +13,16 @@ import com.shaded._100.aikar.commands.annotation.Description; import com.shaded._100.aikar.commands.annotation.HelpCommand; import com.shaded._100.aikar.commands.annotation.Subcommand; +import java.io.IOException; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; @CommandAlias("dynamiclights|dynamiclight|dl") public class DynamicLightCommand extends BaseCommand implements Initialize { + private final PluginCommon pluginCommon; - private LightManager lightManager; + private final LightManager lightManager; private final LanguageFile languageFile; public DynamicLightCommand(PluginCommon pluginCommon, LightManager lightManager) { @@ -35,22 +34,23 @@ public DynamicLightCommand(PluginCommon pluginCommon, LightManager lightManager) @Override public void initialize() { pluginCommon.getServer().getPluginManager() - .registerEvents(new PlayerHandlerEvent(pluginCommon, this.lightManager), pluginCommon); + .registerEvents(new PlayerHandlerEvent(pluginCommon, this.lightManager), pluginCommon); } @Subcommand("toggle") @CommandPermission("dynamiclights.toggle") @Description("Toggle rendering light sources for your client.") public void toggle(Player player) { - boolean status = this.lightManager.lightToggleStatus.getOrDefault(player.getUniqueId().toString(), - this.lightManager.toggle); + boolean status = this.lightManager.lightToggleStatus.getOrDefault( + player.getUniqueId().toString(), + this.lightManager.toggle); if (!status) { this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage( - this.languageFile.getComponentFromID("toggle-on", true)); + this.languageFile.getComponentFromID("toggle-on", true)); this.lightManager.lightToggleStatus.put(player.getUniqueId().toString(), true); } else { this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage( - this.languageFile.getComponentFromID("toggle-off", true)); + this.languageFile.getComponentFromID("toggle-off", true)); this.lightManager.lightToggleStatus.put(player.getUniqueId().toString(), false); } } @@ -59,14 +59,15 @@ public void toggle(Player player) { @CommandPermission("dynamiclights.lock") @Description("Toggle placing light sources from your Off Hand.") public void lock(Player player) { - boolean status = this.lightManager.lightLockStatus.getOrDefault(player.getUniqueId().toString(), true); + boolean status = this.lightManager.lightLockStatus.getOrDefault( + player.getUniqueId().toString(), true); if (!status) { this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage( - this.languageFile.getComponentFromID("enable-lock", true)); + this.languageFile.getComponentFromID("enable-lock", true)); this.lightManager.lightLockStatus.put(player.getUniqueId().toString(), true); } else { this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage( - this.languageFile.getComponentFromID("disable-lock", true)); + this.languageFile.getComponentFromID("disable-lock", true)); this.lightManager.lightLockStatus.put(player.getUniqueId().toString(), false); } } @@ -79,10 +80,10 @@ public void reload(Player player) { this.pluginCommon.configurationAPI.get("lights.yml").reload(); this.lightManager.updateLightSources(new LightSources(this.pluginCommon)); this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage( - this.languageFile.getComponentFromID("reload", true)); + this.languageFile.getComponentFromID("reload", true)); } catch (IOException e) { this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage( - this.languageFile.getComponentFromID("reload-error", true)); + this.languageFile.getComponentFromID("reload-error", true)); this.pluginCommon.getLogger().severe("Failed to reload lights.yml configuration."); this.pluginCommon.getLogger().severe(ExceptionUtils.getStackTrace(e)); } diff --git a/src/main/java/com/github/xcykrix/dynamiclights/module/events/PlayerHandlerEvent.java b/src/main/java/com/github/xcykrix/dynamiclights/module/events/PlayerHandlerEvent.java index 929ff7b..786a682 100644 --- a/src/main/java/com/github/xcykrix/dynamiclights/module/events/PlayerHandlerEvent.java +++ b/src/main/java/com/github/xcykrix/dynamiclights/module/events/PlayerHandlerEvent.java @@ -1,5 +1,9 @@ package com.github.xcykrix.dynamiclights.module.events; +import com.github.xcykrix.dynamiclights.util.LightManager; +import com.github.xcykrix.plugincommon.PluginCommon; +import com.github.xcykrix.plugincommon.api.helper.configuration.LanguageFile; +import com.github.xcykrix.plugincommon.extendables.Stateful; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -8,12 +12,8 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.EquipmentSlot; -import com.github.xcykrix.dynamiclights.util.LightManager; -import com.github.xcykrix.plugincommon.PluginCommon; -import com.github.xcykrix.plugincommon.api.helper.configuration.LanguageFile; -import com.github.xcykrix.plugincommon.extendables.Stateful; - public class PlayerHandlerEvent extends Stateful implements Listener { + private final LightManager lightManager; private final LanguageFile languageFile; private final boolean defaultState; @@ -23,7 +23,7 @@ public PlayerHandlerEvent(PluginCommon pluginCommon, LightManager lightManager) this.lightManager = lightManager; this.languageFile = this.pluginCommon.configurationAPI.getLanguageFile(); this.defaultState = this.pluginCommon.configurationAPI.get("config.yml") - .getOptionalBoolean("default-lock-state").orElse(false); + .getOptionalBoolean("default-lock-state").orElse(false); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @@ -33,12 +33,14 @@ public void playerBlockPlaceEvent(BlockPlaceEvent event) { } if (event.getHand() == EquipmentSlot.OFF_HAND) { - if (!this.lightManager.lightSources.isProtectedLight(event.getItemInHand().getType())) + if (!this.lightManager.lightSources.isProtectedLight(event.getItemInHand().getType())) { return; - if (this.lightManager.lightLockStatus.getOrDefault(event.getPlayer().getUniqueId().toString(), - defaultState)) { + } + if (this.lightManager.lightLockStatus.getOrDefault( + event.getPlayer().getUniqueId().toString(), + defaultState)) { pluginCommon.adventureAPI.getAudiences().player(event.getPlayer()).sendMessage( - this.languageFile.getComponentFromID("prevent-block-place", true)); + this.languageFile.getComponentFromID("prevent-block-place", true)); event.setCancelled(true); } } diff --git a/src/main/java/com/github/xcykrix/dynamiclights/util/LightManager.java b/src/main/java/com/github/xcykrix/dynamiclights/util/LightManager.java index 50a8746..74c529f 100644 --- a/src/main/java/com/github/xcykrix/dynamiclights/util/LightManager.java +++ b/src/main/java/com/github/xcykrix/dynamiclights/util/LightManager.java @@ -1,8 +1,11 @@ package com.github.xcykrix.dynamiclights.util; +import com.github.xcykrix.plugincommon.PluginCommon; +import com.github.xcykrix.plugincommon.extendables.Stateful; +import com.github.xcykrix.plugincommon.extendables.implement.Shutdown; +import com.shaded._100.org.h2.mvstore.MVMap; import java.util.HashMap; import java.util.UUID; - import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -14,12 +17,8 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitTask; -import com.github.xcykrix.plugincommon.PluginCommon; -import com.github.xcykrix.plugincommon.extendables.Stateful; -import com.github.xcykrix.plugincommon.extendables.implement.Shutdown; -import com.shaded._100.org.h2.mvstore.MVMap; - public class LightManager extends Stateful implements Shutdown { + public LightSources lightSources; private final HashMap lastLightLocation = new HashMap<>(); private final HashMap tasks = new HashMap<>(); @@ -35,13 +34,16 @@ public class LightManager extends Stateful implements Shutdown { public LightManager(PluginCommon pluginCommon, LightSources lightSources) { super(pluginCommon); - this.lightToggleStatus = this.pluginCommon.h2MVStoreAPI.getStore().openMap("lightToggleStatus"); + this.lightToggleStatus = this.pluginCommon.h2MVStoreAPI.getStore() + .openMap("lightToggleStatus"); this.lightLockStatus = this.pluginCommon.h2MVStoreAPI.getStore().openMap("lightLockStatus"); this.lightSources = lightSources; this.refresh = this.pluginCommon.configurationAPI.get("config.yml").getLong("update-rate"); - this.distance = this.pluginCommon.configurationAPI.get("config.yml").getInt("light-culling-distance"); - this.toggle = this.pluginCommon.configurationAPI.get("config.yml").getBoolean("default-toggle-state"); + this.distance = this.pluginCommon.configurationAPI.get("config.yml") + .getInt("light-culling-distance"); + this.toggle = this.pluginCommon.configurationAPI.get("config.yml") + .getBoolean("default-toggle-state"); } @Override @@ -60,10 +62,12 @@ public void updateLightSources(LightSources lightSources) { public void addPlayer(Player player) { synchronized (this.tasks) { - if (this.tasks.containsKey(player.getUniqueId())) + if (this.tasks.containsKey(player.getUniqueId())) { return; + } this.tasks.put(player.getUniqueId(), - this.pluginCommon.getServer().getScheduler().runTaskTimerAsynchronously(this.pluginCommon, () -> { + this.pluginCommon.getServer().getScheduler() + .runTaskTimerAsynchronously(this.pluginCommon, () -> { ItemStack mainHand = player.getInventory().getItemInMainHand(); ItemStack offHand = player.getInventory().getItemInOffHand(); @@ -71,13 +75,15 @@ public void addPlayer(Player player) { boolean valid = this.valid(player, mainHand, offHand); int lightLevel = 0; if (valid) { - lightLevel = lightSources.getLightLevel(offHand.getType(), mainHand.getType()); + lightLevel = lightSources.getLightLevel(offHand.getType(), + mainHand.getType()); } // Deploy Lighting for (Player targetPlayer : Bukkit.getOnlinePlayers()) { // Pull Last Location - String locationId = player.getUniqueId() + "/" + targetPlayer.getUniqueId(); + String locationId = + player.getUniqueId() + "/" + targetPlayer.getUniqueId(); Location lastLocation = this.getLastLocation(locationId); // Test and Remove Old Lights @@ -93,12 +99,16 @@ public void addPlayer(Player player) { Location nextLocation = player.getEyeLocation(); // Add Light Sources - if (this.lightToggleStatus.getOrDefault(targetPlayer.getUniqueId().toString(), - this.toggle)) { - if (lightLevel > 0 && differentLocations(lastLocation, nextLocation)) { - if (player.getWorld().getName().equals(targetPlayer.getWorld().getName())) { + if (this.lightToggleStatus.getOrDefault( + targetPlayer.getUniqueId().toString(), + this.toggle)) { + if (lightLevel > 0 && differentLocations(lastLocation, + nextLocation)) { + if (player.getWorld().getName() + .equals(targetPlayer.getWorld().getName())) { if (player.getLocation() - .distance(targetPlayer.getLocation()) <= this.distance) { + .distance(targetPlayer.getLocation()) + <= this.distance) { this.addLight(targetPlayer, nextLocation, lightLevel); this.setLastLocation(locationId, nextLocation); } @@ -107,7 +117,8 @@ public void addPlayer(Player player) { } // Remove Last Locations - if (lastLocation != null && differentLocations(lastLocation, nextLocation)) { + if (lastLocation != null && differentLocations(lastLocation, + nextLocation)) { this.removeLight(targetPlayer, lastLocation); } } @@ -125,11 +136,13 @@ public void removePlayer(UUID uid) { } public void addLight(Player player, Location location, int lightLevel) { - if (lightLevel == 0) + if (lightLevel == 0) { return; + } Light light = (Light) Material.LIGHT.createBlockData(); - if (location.getWorld() == null) + if (location.getWorld() == null) { location.setWorld(player.getWorld()); + } World world = location.getWorld(); switch (world.getBlockAt(location).getType()) { case AIR, CAVE_AIR -> { @@ -148,8 +161,9 @@ public void addLight(Player player, Location location, int lightLevel) { } public void removeLight(Player player, Location location) { - if (location.getWorld() == null) + if (location.getWorld() == null) { location.setWorld(player.getWorld()); + } player.sendBlockChange(location, location.getWorld().getBlockAt(location).getBlockData()); } @@ -157,15 +171,20 @@ public boolean valid(Player player, ItemStack mainHand, ItemStack offHand) { Material main = mainHand.getType(); Material off = offHand.getType(); boolean hasLightLevel = lightSources.hasLightLevel(off); - if (!hasLightLevel) + if (!hasLightLevel) { hasLightLevel = lightSources.hasLightLevel(main); - if (!hasLightLevel) + } + if (!hasLightLevel) { return false; + } Block currentLocation = player.getEyeLocation().getBlock(); - if (currentLocation.getType() == Material.AIR || currentLocation.getType() == Material.CAVE_AIR) + if (currentLocation.getType() == Material.AIR + || currentLocation.getType() == Material.CAVE_AIR) { return true; - if (currentLocation instanceof Waterlogged && ((Waterlogged) currentLocation).isWaterlogged()) { + } + if (currentLocation instanceof Waterlogged + && ((Waterlogged) currentLocation).isWaterlogged()) { return false; } if (currentLocation.getType() == Material.WATER) { @@ -187,12 +206,16 @@ public void removeLastLocation(String uuid) { } private boolean differentLocations(Location l1, Location l2) { - if (l1 == null || l2 == null) + if (l1 == null || l2 == null) { return true; - if (l1.getWorld() == null || l2.getWorld() == null) + } + if (l1.getWorld() == null || l2.getWorld() == null) { return true; - if (!l1.getWorld().getName().equals(l2.getWorld().getName())) + } + if (!l1.getWorld().getName().equals(l2.getWorld().getName())) { return true; - return l1.getBlockX() != l2.getBlockX() || l1.getBlockY() != l2.getBlockY() || l1.getBlockZ() != l2.getBlockZ(); + } + return l1.getBlockX() != l2.getBlockX() || l1.getBlockY() != l2.getBlockY() + || l1.getBlockZ() != l2.getBlockZ(); } } diff --git a/src/main/java/com/github/xcykrix/dynamiclights/util/LightSources.java b/src/main/java/com/github/xcykrix/dynamiclights/util/LightSources.java index 016854f..cbedbb0 100644 --- a/src/main/java/com/github/xcykrix/dynamiclights/util/LightSources.java +++ b/src/main/java/com/github/xcykrix/dynamiclights/util/LightSources.java @@ -1,18 +1,17 @@ package com.github.xcykrix.dynamiclights.util; +import com.github.xcykrix.plugincommon.PluginCommon; +import com.github.xcykrix.plugincommon.extendables.Stateful; +import com.shaded._100.dev.dejvokep.boostedyaml.YamlDocument; +import com.shaded._100.dev.dejvokep.boostedyaml.block.Block; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; - import org.bukkit.Material; -import com.github.xcykrix.plugincommon.PluginCommon; -import com.github.xcykrix.plugincommon.extendables.Stateful; -import com.shaded._100.dev.dejvokep.boostedyaml.YamlDocument; -import com.shaded._100.dev.dejvokep.boostedyaml.block.Block; - public class LightSources extends Stateful { + private final HashMap levelOfLights = new HashMap<>(); private final HashSet submersibleLights = new HashSet<>(); private final HashSet protectedLights = new HashSet<>(); @@ -29,10 +28,12 @@ public LightSources(PluginCommon pluginCommon) { this.levelOfLights.put(Material.valueOf((String) material), level); } catch (Exception exception) { this.pluginCommon.getLogger() - .warning("Unable to register level for '" + material + "'. " + exception.getMessage()); + .warning("Unable to register level for '" + material + "'. " + + exception.getMessage()); } } - this.pluginCommon.getLogger().info("Registered " + this.levelOfLights.size() + " items for Dynamic Lights."); + this.pluginCommon.getLogger() + .info("Registered " + this.levelOfLights.size() + " items for Dynamic Lights."); // Load Submersible Status List submersibles = lights.getStringList("submersibles"); @@ -41,11 +42,13 @@ public LightSources(PluginCommon pluginCommon) { this.submersibleLights.add(Material.valueOf(material)); } catch (Exception exception) { this.pluginCommon.getLogger() - .warning("Unable to register submersible for '" + material + "'. " + exception.getMessage()); + .warning("Unable to register submersible for '" + material + "'. " + + exception.getMessage()); } } this.pluginCommon.getLogger() - .info("Registered " + this.submersibleLights.size() + " items for Dynamic Submersible Lights."); + .info("Registered " + this.submersibleLights.size() + + " items for Dynamic Submersible Lights."); // Load Lockable Status List lockables = lights.getStringList("lockables"); @@ -54,11 +57,13 @@ public LightSources(PluginCommon pluginCommon) { this.protectedLights.add(Material.valueOf(material)); } catch (Exception exception) { this.pluginCommon.getLogger() - .warning("Unable to register lockable for '" + material + "'. " + exception.getMessage()); + .warning("Unable to register lockable for '" + material + "'. " + + exception.getMessage()); } } this.pluginCommon.getLogger() - .info("Registered " + this.protectedLights.size() + " items for Dynamic Lockable Lights."); + .info("Registered " + this.protectedLights.size() + + " items for Dynamic Lockable Lights."); } public boolean hasLightLevel(Material material) {