Skip to content

Commit

Permalink
Added entity functions for equipment droprates, pickup ability, and n…
Browse files Browse the repository at this point in the history
…ame visibility.
  • Loading branch information
Jim Bilbrey committed Jun 29, 2013
1 parent d34035c commit dc0896b
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
Expand Up @@ -11,10 +11,14 @@ public interface MCEntityEquipment {

public void clearEquipment();
public int getSize();
public MCEntity getHolder();

public Map<MCEquipmentSlot, MCItemStack> getAllEquipment();
public void setAllEquipment(Map<MCEquipmentSlot, MCItemStack> stackmap);

public Map<MCEquipmentSlot, Float> getAllDropChances();
public void setAllDropChances(Map<MCEquipmentSlot, Float> slots);

public MCItemStack getWeapon();
public MCItemStack getHelmet();
public MCItemStack getChestplate();
Expand Down
@@ -1,5 +1,6 @@
package com.laytonsmith.abstraction.bukkit;

import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.MCEntityEquipment;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.enums.MCEquipmentSlot;
Expand All @@ -26,6 +27,10 @@ public void clearEquipment() {
public int getSize() {
return MCEquipmentSlot.values().length;
}

public MCEntity getHolder() {
return BukkitConvertor.BukkitGetCorrectEntity(ee.getHolder());
}

public Map<MCEquipmentSlot, MCItemStack> getAllEquipment() {
Map<MCEquipmentSlot, MCItemStack> slots = new EnumMap<MCEquipmentSlot, MCItemStack>(MCEquipmentSlot.class);
Expand Down
135 changes: 135 additions & 0 deletions src/main/java/com/laytonsmith/core/functions/EntityManagement.java
Expand Up @@ -1250,4 +1250,139 @@ public String docs() {
+ " or minecart.";
}
}

@api
public static class get_equipment_droprates extends EntityGetterFunction {

public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
MCEntityEquipment eq = Static.getLivingEntity(Static.getInt32(args[0], t), t).getEquipment();
if (eq.getHolder() instanceof MCPlayer) {
throw new ConfigRuntimeException(getName() + " does not work on players.", ExceptionType.BadEntityException, t);
}
CArray ret = new CArray(t);
for (Map.Entry<MCEquipmentSlot, Float> ent : eq.getAllDropChances().entrySet()) {
ret.set(ent.getKey().name(), new CDouble(ent.getValue(), t), t);
}
return ret;
}

public String getName() {
return "get_equipment_droprates";
}

public String docs() {
return "array {entityID} Returns an associative array of the drop rate for each equipment slot."
+ " If the rate is 0, the equipment will not drop. If it is 1, it is guaranteed to drop.";
}
}

@api
public static class set_equipment_droprates extends EntitySetterFunction {

public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
MCEntityEquipment ee = Static.getLivingEntity(Static.getInt32(args[0], t), t).getEquipment();
Map<MCEquipmentSlot, Float> eq = ee.getAllDropChances();
if (ee.getHolder() instanceof MCPlayer) {
throw new ConfigRuntimeException(getName() + " does not work on players.", ExceptionType.BadEntityException, t);
}
if (args[1] instanceof CNull) {
for (Map.Entry<MCEquipmentSlot, Float> ent : eq.entrySet()) {
eq.put(ent.getKey(), 0F);
}
} else if (args[1] instanceof CArray) {
CArray ea = (CArray) args[1];
for (String key : ea.keySet()) {
try {
eq.put(MCEquipmentSlot.valueOf(key.toUpperCase()), Static.getDouble32(ea.get(key, t), t));
} catch (IllegalArgumentException iae) {
throw new Exceptions.FormatException("Not an equipment slot: " + key, t);
}
}
} else {
throw new Exceptions.FormatException("Expected argument 2 to be an array", t);
}
ee.setAllDropChances(eq);
return new CVoid(t);
}

public String getName() {
return "set_equipment_droprates";
}

public String docs() {
return "void {entityID, array} Sets the drop chances for each equipment slot on a mob,"
+ " but does not work on players. Passing null instead of an array will automatically"
+ " set all rates to 0, which will cause nothing to drop. A rate of 1 will guarantee a drop.";
}
}

@api
public static class get_name_visible extends EntityGetterFunction {

public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
return new CBoolean(Static.getLivingEntity(Static.getInt32(args[0], t), t).isCustomNameVisible(), t);
}

public String getName() {
return "get_name_visible";
}

public String docs() {
return "boolean {entityID} Returns whether or not a mob's custom name is always visible."
+ " If this is true it will be as visible as player names, otherwise it will only be"
+ " visible when near the mob.";
}
}

@api
public static class set_name_visible extends EntitySetterFunction {

public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
Static.getLivingEntity(Static.getInt32(args[0], t), t).setCustomNameVisible(Static.getBoolean(args[1]));
return new CVoid(t);
}

public String getName() {
return "set_name_visible";
}

public String docs() {
return "void {entityID, boolean} Sets the visibility of a mob's custom name."
+ " True means it will be visible from a distance, like a playername."
+ " False means it will only be visible when near the mob.";
}
}

@api
public static class can_pickup_items extends EntityGetterFunction {

public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
return new CBoolean(Static.getLivingEntity(Static.getInt32(args[0], t), t).getCanPickupItems(), t);
}

public String getName() {
return "can_pickup_items";
}

public String docs() {
return "boolean {entityID} Returns whether the specified living entity can pick up items.";
}
}

@api
public static class set_can_pickup_items extends EntitySetterFunction {

public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
Static.getLivingEntity(Static.getInt32(args[0], t), t).setCanPickupItems(Static.getBoolean(args[1]));
return new CVoid(t);
}

public String getName() {
return "set_can_pickup_items";
}

public String docs() {
return "void {entityID, boolean} Sets a living entity's ability to pick up items.";
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/laytonsmith/core/functions/World.java
Expand Up @@ -766,6 +766,9 @@ public Construct exec(Target t, Environment environment, Construct... args) thro
save = Static.getBoolean(args[1]);
}
MCWorld world = Static.getServer().getWorld(args[0].val());
if (world == null) {
throw new ConfigRuntimeException("Unknown world: " + args[0].val(), ExceptionType.InvalidWorldException, t);
}
return new CBoolean(Static.getServer().unloadWorld(world, save), t);
}

Expand Down

0 comments on commit dc0896b

Please sign in to comment.