Skip to content

Commit

Permalink
feat: add google style format and update to latest PluginCommon
Browse files Browse the repository at this point in the history
  • Loading branch information
xCykrix committed Aug 1, 2024
1 parent be47d52 commit f2acf8b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 81 deletions.
10 changes: 3 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -85,9 +81,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.xCykrix</groupId>
<artifactId>PluginCommon</artifactId>
<version>51387786db</version>
<groupId>com.github.xcykrix</groupId>
<artifactId>plugincommon</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, Location> lastLightLocation = new HashMap<>();
private final HashMap<UUID, BukkitTask> tasks = new HashMap<>();
Expand All @@ -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
Expand All @@ -60,24 +62,28 @@ 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();

// Check Light Source Validity
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
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Expand All @@ -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 -> {
Expand All @@ -148,24 +161,30 @@ 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());
}

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) {
Expand All @@ -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();
}
}
Loading

0 comments on commit f2acf8b

Please sign in to comment.