Skip to content

Commit

Permalink
Merge pull request #14 from xCykrix/13-improvement-in-functions
Browse files Browse the repository at this point in the history
Add reload and toggle commands
  • Loading branch information
xCykrix authored Apr 16, 2024
2 parents d21ca07 + 6d51ff4 commit 633ce77
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"java.configuration.updateBuildConfiguration": "automatic",
"java.compile.nullAnalysis.mode": "automatic"
}
10 changes: 7 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.xcykrix</groupId>
<artifactId>DynamicLights</artifactId>
<version>1.0.5-SNAPSHOT</version>
<version>1.0.6-SNAPSHOT</version>
<packaging>jar</packaging>

<name>DynamicLights</name>
Expand Down Expand Up @@ -62,6 +62,10 @@
<repository>
<id>aikar</id>
<url>https://repo.aikar.co/content/groups/aikar/</url>
</repository>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
<repository>
<id>sonatype</id>
Expand All @@ -77,7 +81,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -88,7 +92,7 @@
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.1.0</version>
<version>5.2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@

import com.github.xcykrix.dynamiclights.module.events.PlayerHandlerEvent;
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.helper.configuration.LanguageFile;
import com.github.xcykrix.plugincommon.extendables.implement.Initialize;
import com.shaded._100.aikar.commands.BaseCommand;
import com.shaded._100.aikar.commands.CommandHelp;
import com.shaded._100.aikar.commands.annotation.*;

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 final LightManager lightManager;
private LightManager lightManager;
private final LanguageFile languageFile;

public DynamicLightCommand(PluginCommon pluginCommon, LightManager lightManager) {
Expand All @@ -29,24 +34,61 @@ public void initialize() {
pluginCommon.getServer().getPluginManager().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);
if (!status) {
this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage(
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.lightManager.lightToggleStatus.put(player.getUniqueId().toString(), false);
}
}

@Subcommand("lock")
@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);
if (!status) {
pluginCommon.adventureAPI.getAudiences().player(player).sendMessage(
this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage(
this.languageFile.getComponentFromID("enable-lock", true)
);
this.lightManager.lightLockStatus.put(player.getUniqueId().toString(), true);
} else {
pluginCommon.adventureAPI.getAudiences().player(player).sendMessage(
this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage(
this.languageFile.getComponentFromID("disable-lock", true)
);
this.lightManager.lightLockStatus.put(player.getUniqueId().toString(), false);
}
}

@Subcommand("reload")
@CommandPermission("dynamiclights.reload")
@Description("Reload the light level configuration file. Changes to config.yml require a Server Reboot.")
public void reload(Player player) {
try {
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)
);
} catch (IOException e) {
this.pluginCommon.adventureAPI.getAudiences().player(player).sendMessage(
this.languageFile.getComponentFromID("reload-error", true)
);
this.pluginCommon.getLogger().severe("Failed to reload lights.yml configuration.");
this.pluginCommon.getLogger().severe(ExceptionUtils.getStackTrace(e));
}
}

@HelpCommand
public void help(CommandSender sender, CommandHelp helpCommand) {
helpCommand.showHelp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@
import java.util.UUID;

public class LightManager extends Stateful implements Shutdown {
public final LightSources lightSources;
public LightSources lightSources;
private final HashMap<String, Location> lastLightLocation = new HashMap<>();
private final HashMap<UUID, BukkitTask> tasks = new HashMap<>();

// Local Database Map
public final MVMap<String, Boolean> lightToggleStatus;
public final MVMap<String, Boolean> lightLockStatus;

// Configuration
private long refresh = 5L;
private int distance = 64;
public boolean toggle = true;

public LightManager(PluginCommon pluginCommon, LightSources lightSources) {
super(pluginCommon);
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");
}

@Override
Expand All @@ -49,6 +53,10 @@ public void shutdown() {
}
}

public void updateLightSources(LightSources lightSources) {
this.lightSources = lightSources;
}

public void addPlayer(Player player) {
synchronized (this.tasks) {
if (this.tasks.containsKey(player.getUniqueId())) return;
Expand Down Expand Up @@ -82,11 +90,13 @@ public void addPlayer(Player player) {
Location nextLocation = player.getEyeLocation();

// Add Light Sources
if (lightLevel > 0 && differentLocations(lastLocation, nextLocation)) {
if (player.getWorld().getName().equals(targetPlayer.getWorld().getName())) {
if (player.getLocation().distance(targetPlayer.getLocation()) <= this.distance) {
this.addLight(targetPlayer, nextLocation, lightLevel);
this.setLastLocation(locationId, nextLocation);
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) {
this.addLight(targetPlayer, nextLocation, lightLevel);
this.setLastLocation(locationId, nextLocation);
}
}
}
}
Expand Down Expand Up @@ -123,6 +133,9 @@ public void addLight(Player player, Location location, int lightLevel) {
light.setWaterlogged(true);
light.setLevel(lightLevel - 2);
}
default -> {
// NO OP
}
}
player.sendBlockChange(location, light);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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 org.bukkit.Material;

import java.util.HashMap;
Expand All @@ -20,11 +22,11 @@ public LightSources(PluginCommon pluginCommon) {
YamlDocument lights = this.pluginCommon.configurationAPI.get("lights.yml");

// Load Light Levels
Map<String, Object> levels = lights.getSection("levels").getStringRouteMappedValues(false);
for (String material : levels.keySet()) {
Map<Object, Block<?>> levels = lights.getSection("levels").getStoredValue();
for (Object material : levels.keySet()) {
try {
int level = Integer.parseInt(levels.get(material).toString());
this.levelOfLights.put(Material.valueOf(material), level);
int level = Integer.parseInt(levels.get(material).getStoredValue().toString());
this.levelOfLights.put(Material.valueOf((String)material), level);
} catch(Exception exception) {
this.pluginCommon.getLogger().warning("Unable to register level for '" + material + "'. " + exception.getMessage());
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ update-rate: 5
# Light Culling Distance: Controls how far away you should see another players DynamicLight.
light-culling-distance: 64

# Default Toggle State: Controls if Dynamic Lighting is enabled by default.
default-toggle-state: true

# Default Locking State: Controls if player's offhand are automatically locked by default.
default-lock-state: true

# DO NOT EDIT THE VERSION. Manual edits may corrupt or reset configuration files.
#
version: 2
version: 3
6 changes: 5 additions & 1 deletion src/main/resources/language.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ language:
prevent-block-place: "<prefix> <gray>Light locking mode is currently enabled. You must sneak to place light sources from your Off Hand. This can be toggled with \"/dl lock\".</gray>"
enable-lock: "<prefix> <gray>Enabled light lock mode.</gray>"
disable-lock: "<prefix> <gray>Disabled light lock mode.</gray>"
toggle-on: "<prefix> <gray>Light rendering enabled for client.</gray>"
toggle-off: "<prefix> <gray>Light rendering disabled for client.</gray>"
reload: "<prefix> <gray>Reloaded lights.yml configuration.</gray>"
reload-error: "<prefix> <red>Failed to reload lights.yml configuration. Check Console.</red><gray>.</gray>"

# DO NOT EDIT THE VERSION. Manual edits may corrupt or reset configuration files.
#
version: 1
version: 2

0 comments on commit 633ce77

Please sign in to comment.