Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dEntity.inventory_contents property, for #1005
- Loading branch information
1 parent
db641a3
commit a6654d5
Showing
3 changed files
with
106 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/java/net/aufdemrand/denizen/objects/properties/entity/EntityInventory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package net.aufdemrand.denizen.objects.properties.entity; | ||
|
||
import net.aufdemrand.denizen.objects.dEntity; | ||
import net.aufdemrand.denizen.objects.dInventory; | ||
import net.aufdemrand.denizen.objects.dItem; | ||
import net.aufdemrand.denizen.objects.properties.inventory.InventoryContents; | ||
import net.aufdemrand.denizencore.objects.Mechanism; | ||
import net.aufdemrand.denizencore.objects.dList; | ||
import net.aufdemrand.denizencore.objects.dObject; | ||
import net.aufdemrand.denizencore.objects.properties.Property; | ||
import net.aufdemrand.denizencore.tags.Attribute; | ||
import org.bukkit.inventory.InventoryHolder; | ||
|
||
public class EntityInventory implements Property { | ||
|
||
public static boolean describes(dObject entity) { | ||
return entity instanceof dEntity | ||
&& ((dEntity) entity).getBukkitEntity() instanceof InventoryHolder; | ||
} | ||
|
||
public static EntityInventory getFrom(dObject entity) { | ||
if (!describes(entity)) return null; | ||
|
||
else return new EntityInventory((dEntity) entity); | ||
} | ||
|
||
|
||
/////////////////// | ||
// Instance Fields and Methods | ||
///////////// | ||
|
||
private EntityInventory(dEntity ent) { | ||
entity = ent; | ||
} | ||
|
||
dEntity entity; | ||
|
||
///////// | ||
// Property Methods | ||
/////// | ||
|
||
@Override | ||
public String getPropertyString() { | ||
return new InventoryContents(entity.getInventory()).getContents(0).identify(); | ||
} | ||
|
||
@Override | ||
public String getPropertyId() { | ||
return "inventory_contents"; | ||
} | ||
|
||
|
||
/////////// | ||
// dObject Attributes | ||
//////// | ||
|
||
@Override | ||
public String getAttribute(Attribute attribute) { | ||
|
||
if (attribute == null) return "null"; | ||
|
||
// <--[tag] | ||
// @attribute <e@entity.inventory> | ||
// @returns dInventory | ||
// @group inventory | ||
// @description | ||
// Returns the entity's inventory, if it has one. | ||
// --> | ||
if (attribute.startsWith("inventory")) { | ||
dInventory inventory = entity.getInventory(); | ||
if (inventory != null) | ||
return inventory.getAttribute(attribute.fulfill(1)); | ||
else | ||
return null; | ||
} | ||
|
||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public void adjust(Mechanism mechanism) { | ||
|
||
// <--[mechanism] | ||
// @object dEntity | ||
// @name inventory_contents | ||
// @input dList(dItem) | ||
// @description | ||
// Clears the entity's inventory and sets it's item list to match the input. | ||
// @tags | ||
// <e@entity.inventory> | ||
// <in@inventory.list_contents> | ||
// --> | ||
if (mechanism.matches("inventory_contents")) { | ||
dList list = dList.valueOf(mechanism.getValue().asString()); | ||
dInventory inv = entity.getInventory(); | ||
inv.clear(); | ||
int i = 0; | ||
for (String str: list) { | ||
inv.setSlots(i, dItem.valueOf(str).getItemStack()); | ||
i++; | ||
} | ||
} | ||
} | ||
} |