Skip to content

Commit

Permalink
Allow entities to be adjusted in spawn events
Browse files Browse the repository at this point in the history
Magic!
  • Loading branch information
Morphan1 committed Jun 25, 2015
1 parent f79cc85 commit ae51bee
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Expand Up @@ -14,6 +14,7 @@
import net.aufdemrand.denizencore.utilities.CoreUtilities;

import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
Expand Down Expand Up @@ -116,7 +117,8 @@ public HashMap<String, dObject> getContext() {

@EventHandler
public void onEntityInteract(CreatureSpawnEvent event) {
entity = new dEntity(event.getEntity());
Entity entity = event.getEntity();
this.entity = new dEntity(entity);
location = new dLocation(event.getLocation());
cuboids = new dList();
for (dCuboid cuboid: dCuboid.getNotableCuboidsContaining(location)) {
Expand All @@ -125,7 +127,9 @@ public void onEntityInteract(CreatureSpawnEvent event) {
reason = new Element(event.getSpawnReason().name());
cancelled = event.isCancelled();
this.event = event;
dEntity.rememberEntity(entity);
fire();
dEntity.forgetEntity(entity);
event.setCancelled(cancelled);
}

Expand Down
Expand Up @@ -11,6 +11,7 @@
import net.aufdemrand.denizencore.utilities.CoreUtilities;

import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ItemSpawnEvent;
Expand Down Expand Up @@ -102,12 +103,15 @@ public HashMap<String, dObject> getContext() {

@EventHandler
public void onItemSpawns(ItemSpawnEvent event) {
Item entity = event.getEntity();
location = new dLocation(event.getLocation());
item = new dItem(event.getEntity().getItemStack());
entity = new dEntity(event.getEntity());
item = new dItem(entity.getItemStack());
this.entity = new dEntity(entity);
cancelled = event.isCancelled();
this.event = event;
dEntity.rememberEntity(entity);
fire();
dEntity.forgetEntity(entity);
event.setCancelled(cancelled);
}
}
36 changes: 18 additions & 18 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -630,16 +630,18 @@ public dInventory getInventory() {
}

public String getName() {
if (isCitizensNPC())
if (isCitizensNPC()) {
return getDenizenNPC().getCitizen().getName();
if (entity instanceof CraftFakePlayer)
}
if (entity instanceof CraftFakePlayer) {
return ((CraftFakePlayer) entity).getFullName();
if (entity instanceof Player)
}
if (entity instanceof Player) {
return ((Player) entity).getName();
if (isLivingEntity()) {
String customName = getLivingEntity().getCustomName();
if (customName != null)
return customName;
}
String customName = entity.getCustomName();
if (customName != null) {
return customName;
}
return entity_type.getName();
}
Expand Down Expand Up @@ -1207,7 +1209,8 @@ public String toString() {

@Override
public boolean isUnique() {
return (isPlayer() || isCitizensNPC() || isSpawned()); // || isSaved()
return isPlayer() || isCitizensNPC() || isSpawned()
|| (entity != null && rememberedEntities.containsKey(entity.getUniqueId())); // || isSaved()
}

public boolean matchesEntity(String ent) {
Expand Down Expand Up @@ -1488,9 +1491,7 @@ public String getAttribute(Attribute attribute) {
// Otherwise, returns null.
// -->
if (attribute.startsWith("custom_name")) {
if (!isLivingEntity() || getLivingEntity().getCustomName() == null)
return null;
return new Element(getLivingEntity().getCustomName()).getAttribute(attribute.fulfill(1));
return new Element(entity.getCustomName()).getAttribute(attribute.fulfill(1));
}

// <--[tag]
Expand All @@ -1501,9 +1502,7 @@ public String getAttribute(Attribute attribute) {
// Returns true if the entity's custom name is visible.
// -->
if (attribute.startsWith("custom_name.visible")) {
if (!isLivingEntity())
return null;
return new Element(getLivingEntity().isCustomNameVisible())
return new Element(entity.isCustomNameVisible())
.getAttribute(attribute.fulfill(2));
}

Expand Down Expand Up @@ -2157,6 +2156,9 @@ public void applyProperty(Mechanism mechanism) {
if (isGeneric()) {
mechanisms.add(mechanism);
}
else if (rememberedEntities.containsKey(entity.getUniqueId())) {
adjust(mechanism);
}
else {
dB.echoError("Cannot apply properties to an already-spawned entity!");
}
Expand Down Expand Up @@ -2191,25 +2193,23 @@ public void adjust(Mechanism mechanism) {
// @input Element
// @description
// Sets the custom name of the entity.
// The entity must be living.
// @tags
// <e@entity.custom_name>
// -->
if (mechanism.matches("custom_name"))
getLivingEntity().setCustomName(value.asString());
entity.setCustomName(value.asString());

// <--[mechanism]
// @object dEntity
// @name custom_name_visibility
// @input Element(Boolean)
// @description
// Sets whether the custom name is visible.
// The entity must be living.
// @tags
// <e@entity.custom_name.visible>
// -->
if (mechanism.matches("custom_name_visibility") && mechanism.requireBoolean())
getLivingEntity().setCustomNameVisible(value.asBoolean());
entity.setCustomNameVisible(value.asBoolean());

// <--[mechanism]
// @object dEntity
Expand Down

0 comments on commit ae51bee

Please sign in to comment.