Skip to content

Commit

Permalink
[EXPERIMENTAL] Disallow any NPC ever being considered a dPlayer
Browse files Browse the repository at this point in the history
To make future additions to Denizen easier...
  • Loading branch information
Morphan1 committed Feb 20, 2015
1 parent 9f93dec commit d38a1ab
Show file tree
Hide file tree
Showing 39 changed files with 222 additions and 203 deletions.
15 changes: 1 addition & 14 deletions src/main/java/net/aufdemrand/denizen/BukkitScriptEntryData.java
Expand Up @@ -3,10 +3,8 @@
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.tags.TagContext;
import net.citizensnpcs.api.CitizensAPI;

public class BukkitScriptEntryData extends ScriptEntryData {
private dPlayer player;
Expand Down Expand Up @@ -34,21 +32,10 @@ public boolean hasPlayer() {
}

public void setPlayer(dPlayer player) {
if (player != null && player.isOnline() && Depends.citizens != null
&& CitizensAPI.getNPCRegistry().isNPC(player.getPlayerEntity())) {
dontFixMe = true;
setNPC(new dNPC(CitizensAPI.getNPCRegistry().getNPC(player.getPlayerEntity())));
}
else
this.player = player;
this.player = player;
}

private boolean dontFixMe = false;

public void setNPC(dNPC npc) {
if (npc == null && dontFixMe) {
dontFixMe = false;
}
this.npc = npc;
}

Expand Down
Expand Up @@ -2,6 +2,7 @@

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizencore.events.OldSmartEvent;
import net.aufdemrand.denizencore.objects.Element;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void asyncPlayerChat(final AsyncPlayerChatEvent event) {
context.put("message", new Element(event.getMessage()));
context.put("format", new Element(event.getMessage()));

final dPlayer player = new dPlayer(event.getPlayer());
final dPlayer player = dEntity.getPlayerFrom(event.getPlayer());

Callable<List<String>> call = new Callable<List<String>>() {
@Override
Expand Down
@@ -1,6 +1,7 @@
package net.aufdemrand.denizen.events.core;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizencore.events.OldSmartEvent;
import net.aufdemrand.denizencore.objects.Element;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void playerMoveEvent(PlayerMoveEvent event) {
List<String> determinations = OldEventManager.doEvents(Arrays.asList(
"player enters biome", "player exits biome",
"player enters " + to.name(), "player exits " + from.name()
), new BukkitScriptEntryData(new dPlayer(event.getPlayer()), null), context, true);
), new BukkitScriptEntryData(dEntity.getPlayerFrom(event.getPlayer()), null), context, true);

for (String determination: determinations) {
if (determination.toUpperCase().startsWith("CANCELLED"))
Expand Down
Expand Up @@ -213,7 +213,7 @@ public void playerCommandPreprocess(PlayerCommandPreprocessEvent event) {

// Run any event scripts and get the determination.
determination = BukkitWorldScriptHelper.doEvents(events,
null, new dPlayer(event.getPlayer()), context).toUpperCase();
null, dEntity.getPlayerFrom(event.getPlayer()), context).toUpperCase();

// If a script has determined fulfilled, cancel this event so the player doesn't
// receive the default 'Invalid command' gibberish from bukkit.
Expand Down
Expand Up @@ -191,7 +191,7 @@ private boolean Fire(PlayerMoveEvent event, dList cuboids, String EventName) {
events.add(EventName);

String determination = BukkitWorldScriptHelper.doEvents(events,
null, new dPlayer(event.getPlayer()), context, true);
null, dEntity.getPlayerFrom(event.getPlayer()), context, true);

if (determination.toUpperCase().startsWith("CANCELLED")) {
event.setCancelled(true);
Expand Down
Expand Up @@ -105,8 +105,8 @@ public void entityDamage(EntityDamageEvent event) {
context.put("final_damage", new Element(event.getFinalDamage()));
context.put("cause", new Element(event.getCause().name()));

if (entity.isNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = new dPlayer(entity.getPlayer());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();

boolean isFatal = false;

Expand Down Expand Up @@ -227,15 +227,15 @@ public void entityDamage(EntityDamageEvent event) {
events.add(entity.identifySimple() + " damaged by " + damager.identifyType());
events.add(entity.identifySimple() + " damaged by " + damager.identifySimple());

if (damager.isNPC()) {
if (damager.isCitizensNPC()) {
subNPC = damager.getDenizenNPC();

// If we had no NPC in our regular context, use this one
if (npc == null) npc = subNPC;
}

else if (damager.isPlayer()) {
subPlayer = new dPlayer(damager.getPlayer());
subPlayer = damager.getDenizenPlayer();

// If we had no player in our regular context, use this one
if (player == null) player = subPlayer;
Expand Down
Expand Up @@ -111,8 +111,8 @@ public void entityDeath(EntityDeathEvent event) {
}
context.put("drops", drops_dlist);

if (entity.isNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = new dPlayer(entity.getPlayer());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();

// If this entity has a stored killer, get it and then
// remove it from the entityKillers map
Expand Down
Expand Up @@ -143,7 +143,7 @@ public void creatureSpawn(CreatureSpawnEvent event) {
context.put("location", new dLocation(event.getLocation()));

String determination = BukkitWorldScriptHelper.doEvents(events,
(entity.isNPC() ? entity.getDenizenNPC() : null), null, context, true);
(entity.isCitizensNPC() ? entity.getDenizenNPC() : null), null, context, true);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
Expand Down
Expand Up @@ -89,7 +89,7 @@ public void playerScrollsHotbar(PlayerItemHeldEvent event) {
context.put("previous_slot", new Element(event.getPreviousSlot() + 1));

String determination = BukkitWorldScriptHelper.doEvents(events,
null, new dPlayer(event.getPlayer()), context, true);
null, dEntity.getPlayerFrom(event.getPlayer()), context, true);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
Expand Down
Expand Up @@ -105,7 +105,7 @@ public void blockDispense(BlockDispenseEvent event) {

if (isArmor(item)) {
for (final Player player : location.getWorld().getPlayers()) {
if (Utilities.checkLocation(player, location, 2.5)) {
if (!dEntity.isNPC(player) && Utilities.checkLocation(player, location, 2.5)) {
final ItemStack[] armor_contents = player.getInventory().getArmorContents().clone();
final Vector velocity = event.getVelocity();
new BukkitRunnable() {
Expand Down Expand Up @@ -345,7 +345,7 @@ private boolean playerEquipsArmorEvent(final Player player, final ItemStack item
"player equips " + getArmorType(item),
"player equips " + armor.identifySimple(),
"player equips " + armor.identifyMaterial()),
null, new dPlayer(player), context).toUpperCase();
null, dEntity.getPlayerFrom(player), context).toUpperCase();

if (determination.startsWith("CANCELLED")) {
new BukkitRunnable() {
Expand Down Expand Up @@ -374,7 +374,7 @@ private boolean playerUnequipsArmorEvent(final Player player, final ItemStack it
"player unequips " + getArmorType(item),
"player unequips " + armor.identifySimple(),
"player unequips " + armor.identifyMaterial()),
null, new dPlayer(player), context).toUpperCase();
null, dEntity.getPlayerFrom(player), context).toUpperCase();

if (determination.startsWith("CANCELLED")) {
new BukkitRunnable() {
Expand Down
Expand Up @@ -79,7 +79,8 @@ public void onPlayerMove(PlayerMoveEvent event) {
// Not perfect checking, but close enough until Bukkit adds a proper event
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("location", new dLocation(event.getTo()));
BukkitWorldScriptHelper.doEvents(Arrays.asList("player jumps"), null, new dPlayer(event.getPlayer()), context);
BukkitWorldScriptHelper.doEvents(Arrays.asList("player jumps"), null,
dEntity.getPlayerFrom(event.getPlayer()), context);
}
}
}
Expand Up @@ -116,7 +116,8 @@ public void onPlayerMove(PlayerMoveEvent event) {
// Add in cuboids context, with either the cuboids or an empty list
context.put("cuboids", cuboid_context);
// Fire event
String Determination = BukkitWorldScriptHelper.doEvents(events, null, new dPlayer(event.getPlayer()), context, true);
String Determination = BukkitWorldScriptHelper.doEvents(events, null,
dEntity.getPlayerFrom(event.getPlayer()), context, true);
if (Determination.equalsIgnoreCase("CANCELLED"))
event.setCancelled(true);
}
Expand Down
@@ -1,5 +1,6 @@
package net.aufdemrand.denizen.events.core;

import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.scripts.containers.core.BukkitWorldScriptHelper;
import net.aufdemrand.denizencore.events.OldSmartEvent;
import net.aufdemrand.denizen.objects.dLocation;
Expand Down Expand Up @@ -80,7 +81,8 @@ public void onPlayerMove(PlayerMoveEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("old_location", new dLocation(event.getFrom()));
context.put("new_location", new dLocation(event.getTo()));
String determination = BukkitWorldScriptHelper.doEvents(Arrays.asList("player walks"), null, new dPlayer(event.getPlayer()), context, true);
String determination = BukkitWorldScriptHelper.doEvents(Arrays.asList("player walks"), null,
dEntity.getPlayerFrom(event.getPlayer()), context, true);
if (determination.equalsIgnoreCase("CANCELLED"))
event.setCancelled(true);
}
Expand Down
@@ -1,6 +1,7 @@
package net.aufdemrand.denizen.events.core;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.scripts.containers.core.BukkitWorldScriptHelper;
import net.aufdemrand.denizencore.events.OldSmartEvent;
import net.aufdemrand.denizencore.objects.Element;
Expand Down Expand Up @@ -96,7 +97,7 @@ public void playerChat(final PlayerChatEvent event) {
context.put("message", new Element(event.getMessage()));
context.put("format", new Element(event.getMessage()));

dPlayer player = new dPlayer(event.getPlayer());
dPlayer player = dEntity.getPlayerFrom(event.getPlayer());

String determination = BukkitWorldScriptHelper.doEvents(Arrays.asList("player chats"), null, player, context);

Expand Down
Expand Up @@ -134,8 +134,8 @@ public void vehicleEntityCollision(VehicleEntityCollisionEvent event) {
context.put("vehicle", vehicle);
context.put("entity", entity.getDenizenObject());

if (entity.isNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = new dPlayer(entity.getPlayer());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();

List<String> events = new ArrayList<String>();
events.add("vehicle collides with entity");
Expand Down
@@ -1,6 +1,7 @@
package net.aufdemrand.denizen.events.core;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizencore.events.OldSmartEvent;
import net.aufdemrand.denizen.objects.dLocation;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void onPlayerMove(PlayerMoveEvent event) {
context.put("location", new dLocation(event.getTo()));
// Fire the event!
List<String> determinations = OldEventManager.doEvents(Arrays.asList("x or y or z"),
new BukkitScriptEntryData(new dPlayer(event.getPlayer()), null), context);
new BukkitScriptEntryData(dEntity.getPlayerFrom(event.getPlayer()), null), context);
// Parse the determination and edit the event accordingly here
for (String determination: determinations) {
if (determination.equalsIgnoreCase("CANCELLED"))
Expand Down
Expand Up @@ -124,8 +124,8 @@ else if (dLocation.matches(determination)) {

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(pEvent != null ? new dPlayer(pEvent.getPlayer()): null,
entity.isNPC() ? entity.getDenizenNPC(): null);
return new BukkitScriptEntryData(pEvent != null ? dEntity.getPlayerFrom(pEvent.getPlayer()) : null,
entity.isCitizensNPC() ? entity.getDenizenNPC(): null);
}

@Override
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void run() {
List<Entity> removeme = new ArrayList<Entity>();
removeme.addAll(inrange);
for (Entity ent: nearby) {
if (ent instanceof LivingEntity && !(ent instanceof Player) && (acceptnpc || (!new dEntity(ent).isNPC()))) {
if (ent instanceof LivingEntity && !(ent instanceof Player) && (acceptnpc || (!dEntity.isCitizensNPC(ent)))) {
if (removeme.contains(ent)) {
removeme.remove(ent);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dChunk.java
Expand Up @@ -250,8 +250,8 @@ public String getAttribute(Attribute attribute) {
if (attribute.startsWith("players")) {
dList entities = new dList();
for (Entity ent : chunk.getEntities())
if (ent instanceof Player && (Depends.citizens == null || !CitizensAPI.getNPCRegistry().isNPC(ent)))
entities.add(new dPlayer((Player) ent).identify());
if (dEntity.isPlayer(ent))
entities.add(dEntity.getPlayerFrom(ent).identify());

return entities.getAttribute(attribute.fulfill(1));
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dCuboid.java
Expand Up @@ -1047,8 +1047,7 @@ public String getAttribute(Attribute attribute) {
if (attribute.startsWith("list_living_entities")) {
ArrayList<dEntity> entities = new ArrayList<dEntity>();
for (Entity ent : getWorld().getLivingEntities()) {
if (ent.isValid() && isInsideCuboid(ent.getLocation())
&& (Depends.citizens == null || !CitizensAPI.getNPCRegistry().isNPC(ent)))
if (ent.isValid() && isInsideCuboid(ent.getLocation()) && !dEntity.isCitizensNPC(ent))
entities.add(new dEntity(ent));
}
return new dList(entities).getAttribute(attribute.fulfill(1));
Expand Down

0 comments on commit d38a1ab

Please sign in to comment.