use of org.bukkit.entity.EntityType in project modules-extra by CubeEngine.
the class LookupCommands method readEntities.
private boolean readEntities(QueryParameter params, String entity, User user) {
if (entity == null) {
return true;
}
String[] names = StringUtils.explode(",", entity);
for (String name : names) {
boolean negate = name.startsWith("!");
if (negate) {
name = name.substring(1);
}
EntityType entityType = Match.entity().mob(name);
if (entityType == null) {
user.sendTranslated(NEGATIVE, "Unknown EntityType: {name#entity}", name);
return false;
}
if (negate) {
params.excludeEntity(entityType);
} else {
params.includeEntity(entityType);
}
}
return true;
}
use of org.bukkit.entity.EntityType in project Essentials by EssentialsX.
the class SpawnEggRefl method fromItemStack.
/**
* Converts from an item stack to a spawn egg
*
* @param item - ItemStack, quantity is disregarded
* @return SpawnEgg
*/
public static SpawnEggRefl fromItemStack(ItemStack item) throws Exception {
if (item == null)
throw new IllegalArgumentException("Item cannot be null");
if (item.getType() != Material.MONSTER_EGG)
throw new IllegalArgumentException("Item is not a monster egg");
Class<?> NMSItemStackClass = ReflUtil.getNMSClass("ItemStack");
Class<?> craftItemStackClass = ReflUtil.getOBCClass("inventory.CraftItemStack");
Method asNMSCopyMethod = ReflUtil.getMethodCached(craftItemStackClass, "asNMSCopy", ItemStack.class);
Object stack = asNMSCopyMethod.invoke(null, item);
Object tagCompound = ReflUtil.getMethodCached(NMSItemStackClass, "getTag").invoke(stack);
if (tagCompound != null) {
Method tagGetCompound = ReflUtil.getMethodCached(tagCompound.getClass(), "getCompound", String.class);
Object entityTag = tagGetCompound.invoke(tagCompound, "EntityTag");
Method tagGetString = ReflUtil.getMethodCached(entityTag.getClass(), "getString", String.class);
String idString = (String) tagGetString.invoke(entityTag, "id");
if (ReflUtil.getNmsVersionObject().isHigherThanOrEqualTo(ReflUtil.V1_11_R1)) {
idString = idString.split("minecraft:")[1];
}
@SuppressWarnings("deprecation") EntityType type = EntityType.fromName(idString);
if (type != null) {
return new SpawnEggRefl(type);
} else {
throw new IllegalArgumentException("Unable to parse type from item");
}
} else {
throw new IllegalArgumentException("Item is lacking tag compound");
}
}
use of org.bukkit.entity.EntityType in project Essentials by EssentialsX.
the class SpawnerProvider method tryProvider.
@Override
public boolean tryProvider() {
try {
EntityType type = EntityType.CREEPER;
ItemStack is = setEntityType(new ItemStack(Material.MOB_SPAWNER), type);
EntityType readType = getEntityType(is);
return type == readType;
} catch (Throwable t) {
return false;
}
}
use of org.bukkit.entity.EntityType in project Essentials by EssentialsX.
the class EssentialsBlockListener method onBlockPlace.
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent event) {
// Do not rely on getItemInHand();
// http://leaky.bukkit.org/issues/663
final ItemStack is = LocationUtil.convertBlockToItem(event.getBlockPlaced());
if (is == null) {
return;
}
if (is.getType() == Material.MOB_SPAWNER && event.getItemInHand() != null && event.getPlayer() != null && event.getItemInHand().getType() == Material.MOB_SPAWNER) {
final BlockState blockState = event.getBlockPlaced().getState();
if (blockState instanceof CreatureSpawner) {
final CreatureSpawner spawner = (CreatureSpawner) blockState;
final EntityType type = ess.getSpawnerProvider().getEntityType(event.getItemInHand());
if (type != null && Mob.fromBukkitType(type) != null) {
if (ess.getUser(event.getPlayer()).isAuthorized("essentials.spawnerconvert." + Mob.fromBukkitType(type).name().toLowerCase(Locale.ENGLISH))) {
spawner.setSpawnedType(type);
spawner.update();
}
}
}
}
final User user = ess.getUser(event.getPlayer());
if (user.hasUnlimited(is) && user.getBase().getGameMode() == GameMode.SURVIVAL) {
class UnlimitedItemSpawnTask implements Runnable {
@Override
public void run() {
user.getBase().getInventory().addItem(is);
user.getBase().updateInventory();
}
}
ess.scheduleSyncDelayedTask(new UnlimitedItemSpawnTask());
}
}
use of org.bukkit.entity.EntityType in project Essentials by EssentialsX.
the class ItemDb method get.
@Override
public ItemStack get(final String id) throws Exception {
int itemid = 0;
String itemname;
short metaData = 0;
Matcher parts = splitPattern.matcher(id);
if (parts.matches()) {
itemname = parts.group(2);
metaData = Short.parseShort(parts.group(3));
} else {
itemname = id;
}
if (NumberUtil.isInt(itemname)) {
itemid = Integer.parseInt(itemname);
} else if (NumberUtil.isInt(id)) {
itemid = Integer.parseInt(id);
} else {
itemname = itemname.toLowerCase(Locale.ENGLISH);
}
if (itemid < 1) {
if (items.containsKey(itemname)) {
itemid = items.get(itemname);
if (durabilities.containsKey(itemname) && metaData == 0) {
metaData = durabilities.get(itemname);
}
} else if (Material.getMaterial(itemname.toUpperCase(Locale.ENGLISH)) != null) {
Material bMaterial = Material.getMaterial(itemname.toUpperCase(Locale.ENGLISH));
itemid = bMaterial.getId();
} else {
try {
Material bMaterial = Bukkit.getUnsafe().getMaterialFromInternalName(itemname.toLowerCase(Locale.ENGLISH));
itemid = bMaterial.getId();
} catch (Throwable throwable) {
throw new Exception(tl("unknownItemName", itemname), throwable);
}
}
}
if (itemid < 1) {
throw new Exception(tl("unknownItemName", itemname));
}
final Material mat = Material.getMaterial(itemid);
if (mat == null) {
throw new Exception(tl("unknownItemId", itemid));
}
ItemStack retval = new ItemStack(mat);
if (nbtData.containsKey(itemname)) {
String nbt = nbtData.get(itemname);
if (nbt.startsWith("*")) {
nbt = nbtData.get(nbt.substring(1));
}
retval = ess.getServer().getUnsafe().modifyItemStack(retval, nbt);
}
if (mat == Material.MOB_SPAWNER) {
if (metaData == 0)
metaData = EntityType.PIG.getTypeId();
try {
retval = ess.getSpawnerProvider().setEntityType(retval, EntityType.fromId(metaData));
} catch (IllegalArgumentException e) {
throw new Exception("Can't spawn entity ID " + metaData + " from mob spawners.");
}
} else if (mat == Material.MONSTER_EGG) {
EntityType type;
try {
type = EntityType.fromId(metaData);
} catch (IllegalArgumentException e) {
throw new Exception("Can't spawn entity ID " + metaData + " from spawn eggs.");
}
retval = ess.getSpawnEggProvider().createEggItem(type);
} else if (mat.name().endsWith("POTION") && ReflUtil.getNmsVersionObject().isLowerThan(ReflUtil.V1_11_R1)) {
// Only apply this to pre-1.11 as items.csv might only work in 1.11
retval = ess.getPotionMetaProvider().createPotionItem(mat, metaData);
} else {
retval.setDurability(metaData);
}
retval.setAmount(mat.getMaxStackSize());
return retval;
}
Aggregations