Skip to content

Commit

Permalink
Add duration:<duration> argument to Light command
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Feb 18, 2015
1 parent 0d37cbf commit b6e9394
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
Expand Up @@ -1489,16 +1489,17 @@ public void registerCoreMembers() {

// <--[command]
// @Name Light
// @Syntax light [<location>] [<#>/reset]
// @Syntax light [<location>] [<#>/reset] (duration:<duration>)
// @Required 2
// @Stable stable
// @Short Creates a light source at the location.
// @Short Creates a light source at the location with a specified brightness.
// @Author Morphan1
// @Group world
// @Description
// This command can create and reset a light source at a specified location, regardless of the type
// of block. It will be shown to all players near the location until it is reset, with certain
// exceptions.
// of block. It will be shown to all players near the location until it is reset.
// The brightness must be between 0 and 15, inclusive.
// Optionally, specify the amount of time the light should exist before being removed.
// @Tags
// <l@location.light>
// <l@location.light.blocks>
Expand All @@ -1510,7 +1511,7 @@ public void registerCoreMembers() {
// - light l@MyFancyLightOfWool reset
// -->
registerCoreMember(LightCommand.class,
"LIGHT", "light [<location>] [<#>/reset]", 2);
"LIGHT", "light [<location>] [<#>/reset] (duration:<duration>)", 2);


// <--[command]
Expand Down
Expand Up @@ -5,6 +5,7 @@
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
Expand All @@ -29,6 +30,11 @@ else if (!scriptEntry.hasObject("reset")
&& arg.matches("reset"))
scriptEntry.addObject("reset", new Element(true));

else if (!scriptEntry.hasObject("duration")
&& arg.matchesPrefix("d", "duration")
&& arg.matchesArgumentType(Duration.class))
scriptEntry.addObject("duration", arg.asType(Duration.class));

}

if (!scriptEntry.hasObject("location") ||
Expand All @@ -45,12 +51,20 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dLocation location = scriptEntry.getdObject("location");
Element light = scriptEntry.getElement("light");
Element reset = scriptEntry.getElement("reset");
Duration duration = scriptEntry.getdObject("duration");

dB.report(scriptEntry, getName(), location.debug() + (light != null ? light.debug() : "") + reset.debug());
dB.report(scriptEntry, getName(), location.debug() + reset.debug()
+ (light != null ? light.debug() : "") + (duration != null ? duration.debug() : ""));

if (!reset.asBoolean())
BlockLight.createLight(location, light.asInt());
else
if (!reset.asBoolean()) {
int brightness = light.asInt();
if (brightness < 0 || brightness > 15) {
throw new CommandExecutionException("Light brightness must be between 0 and 15, inclusive!");
}
BlockLight.createLight(location, brightness, duration);
}
else {
BlockLight.removeLight(location);
}
}
}
Expand Up @@ -2,6 +2,7 @@

import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.Duration;
import net.minecraft.server.v1_8_R1.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -78,8 +79,9 @@ public void run() {
private final int originalLight;
private int currentLight;
private int cachedLight;
private BukkitTask removeTask;

private BlockLight(Location location) {
private BlockLight(final Location location, Duration duration) {
this.craftWorld = (CraftWorld) location.getWorld();
this.worldServer = craftWorld.getHandle();
this.craftChunk = (CraftChunk) location.getChunk();
Expand All @@ -89,17 +91,23 @@ private BlockLight(Location location) {
this.originalLight = block.getLightLevel();
this.currentLight = originalLight;
this.cachedLight = originalLight;
this.removeLater(duration);
}

public static BlockLight createLight(Location location, int lightLevel) {
public static BlockLight createLight(Location location, int lightLevel, Duration duration) {
location = location.getBlock().getLocation();
BlockLight blockLight;
if (lightsByLocation.containsKey(location)) {
blockLight = lightsByLocation.get(location);
if (blockLight.removeTask != null) {
blockLight.removeTask.cancel();
blockLight.removeTask = null;
}
blockLight.reset(true);
blockLight.removeLater(duration);
}
else {
blockLight = new BlockLight(location);
blockLight = new BlockLight(location, duration);
lightsByLocation.put(location, blockLight);
if (!lightsByChunk.containsKey(blockLight.chunk))
lightsByChunk.put(blockLight.chunk, new ArrayList<BlockLight>());
Expand All @@ -114,13 +122,30 @@ public static void removeLight(Location location) {
if (lightsByLocation.containsKey(location)) {
BlockLight blockLight = lightsByLocation.get(location);
blockLight.reset(true);
if (blockLight.removeTask != null)
blockLight.removeTask.cancel();
lightsByLocation.remove(location);
lightsByChunk.get(blockLight.chunk).remove(blockLight);
if (lightsByChunk.get(blockLight.chunk).isEmpty())
lightsByChunk.remove(blockLight.chunk);
}
}

private void removeLater(Duration duration) {
if (duration != null) {
long ticks = duration.getTicks();
if (ticks > 0) {
this.removeTask = new BukkitRunnable() {
@Override
public void run() {
removeTask = null;
removeLight(block.getLocation());
}
}.runTaskLater(DenizenAPI.getCurrentInstance(), ticks);
}
}
}

private void reset(boolean updateChunk) {
this.update(originalLight, updateChunk);
}
Expand Down

0 comments on commit b6e9394

Please sign in to comment.