Skip to content

Commit

Permalink
Add range option to <l@location.precise_cursor_on>
Browse files Browse the repository at this point in the history
Also fix dEntity errors on shutdown
  • Loading branch information
Morphan1 committed Feb 18, 2015
1 parent b6e9394 commit 038449f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -785,6 +785,11 @@ public void onDisable() {
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");
Bukkit.getServer().getScheduler().cancelTasks(this);
HandlerList.unregisterAll(this);

for (World world : getServer().getWorlds()) {
EntityScriptHelper.unlinkWorld(world);
}

saveSaves();
}

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -572,17 +572,24 @@ public String getAttribute(Attribute attribute) {
/////////////////

// <--[tag]
// @attribute <l@location.precise_cursor_on>
// @attribute <l@location.precise_cursor_on[<range>]>
// @returns dLocation
// @description
// Returns the exact location this location is pointing at.
// Optionally, specify a maximum range to find the location from.
// -->
if (attribute.startsWith("precise_cursor_on")) {
int range = attribute.getIntContext(1);
if (range < 1) range = 200;
double xzLen = Math.cos((getPitch() % 360) * (Math.PI/180));
double nx = xzLen * Math.sin(-getYaw() * (Math.PI/180));
double ny = Math.sin(getPitch() * (Math.PI/180));
double nz = xzLen * Math.cos(getYaw() * (Math.PI/180));
return new dLocation(Rotation.rayTrace(this, new org.bukkit.util.Vector(nx, -ny, nz), 200)).getAttribute(attribute.fulfill(1));
Location location = Rotation.rayTrace(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
if (location != null)
return new dLocation(location).getAttribute(attribute.fulfill(1));
else
return null;
}

// <--[tag]
Expand Down
Expand Up @@ -6,6 +6,7 @@
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.world.DenizenWorldAccess;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
Expand All @@ -18,15 +19,31 @@
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


public class EntityScriptHelper implements Listener {

static HashMap<UUID, String> entities = new HashMap<UUID, String>();
private static final Field iWorldAccessList;
private static final Map<World, DenizenWorldAccess> worlds = new HashMap<World, DenizenWorldAccess>();

static {
Field field = null;
try {
field = net.minecraft.server.v1_8_R1.World.class.getDeclaredField("u");
field.setAccessible(true);
} catch (Exception e) {
dB.echoError(e);
}
iWorldAccessList = field;
}

public EntityScriptHelper() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
Expand All @@ -43,14 +60,30 @@ public void onEntityDeath(EntityDeathEvent event) {
}

public static void linkWorld(World world) {
((CraftWorld)world).getHandle().addIWorldAccess(new DenizenWorldAccess());
DenizenWorldAccess denizenWorldAccess = new DenizenWorldAccess();
worlds.put(world, denizenWorldAccess);
((CraftWorld) world).getHandle().addIWorldAccess(denizenWorldAccess);
}

public static void unlinkWorld(World world) {
try {
((List) iWorldAccessList.get(((CraftWorld) world).getHandle())).remove(worlds.get(world));
worlds.remove(world);
} catch (Exception e) {
dB.echoError(e);
}
}

@EventHandler(priority = EventPriority.MONITOR)
public void onWorldLoad(WorldLoadEvent event) {
linkWorld(event.getWorld());
}

@EventHandler(priority = EventPriority.MONITOR)
public void onWorldUnload(WorldUnloadEvent event) {
unlinkWorld(event.getWorld());
}

@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
// TODO: This doesn't work. Awaiting Entity Despawn Event PR's for Bukkit:
Expand Down

0 comments on commit 038449f

Please sign in to comment.