use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class PlayerBreaksItemScriptEvent method onPlayerItemBreak.
@EventHandler
public void onPlayerItemBreak(PlayerItemBreakEvent event) {
if (dEntity.isNPC(event.getPlayer())) {
return;
}
item = new dItem(event.getBrokenItem());
this.event = event;
fire();
if (cancelled) {
final Player player = event.getPlayer();
final ItemStack itemstack = event.getBrokenItem();
itemstack.setAmount(itemstack.getAmount() + 1);
new BukkitRunnable() {
public void run() {
itemstack.setDurability(itemstack.getType().getMaxDurability());
player.updateInventory();
}
}.runTaskLater(DenizenAPI.getCurrentInstance(), 1);
}
}
use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class Particle_v1_8_R3 method playFor.
@Override
public <T> void playFor(Player player, Location location, int count, Vector offset, double extra, T data) {
int[] dataArray;
if (data instanceof ItemStack) {
dataArray = new int[] { ((ItemStack) data).getType().getId(), ((ItemStack) data).getDurability() };
} else if (data instanceof MaterialData) {
dataArray = new int[] { ((MaterialData) data).getItemTypeId() + (((MaterialData) data).getData() << 12) };
}
PacketHelper_v1_8_R3.sendPacket(player, new PacketPlayOutWorldParticles(particle, true, (float) location.getX(), (float) location.getY(), (float) location.getZ(), (float) offset.getX(), (float) offset.getY(), (float) offset.getZ(), (float) extra, count));
}
use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class InventoryScriptContainer method getInventoryFrom.
public dInventory getInventoryFrom(dPlayer player, dNPC npc) {
// TODO: Clean all this code!
dInventory inventory = null;
BukkitTagContext context = new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this));
try {
if (contains("INVENTORY")) {
if (InventoryType.valueOf(getString("INVENTORY").toUpperCase()) != null) {
inventory = new dInventory(InventoryType.valueOf(getString("INVENTORY").toUpperCase()));
if (contains("TITLE")) {
inventory.setTitle(TagManager.tag(getString("TITLE"), context));
}
inventory.setIdentifiers("script", getName());
} else {
dB.echoError("Invalid inventory type specified. Assuming \"CHEST\"");
}
}
int size = 0;
if (contains("SIZE")) {
if (inventory != null && !getInventoryType().name().equalsIgnoreCase("CHEST")) {
dB.echoError("You can only set the size of chest inventories!");
} else {
size = aH.getIntegerFrom(TagManager.tag(getString("SIZE"), context));
if (size == 0) {
dB.echoError("Inventory size can't be 0. Assuming default of inventory type...");
}
if (size % 9 != 0) {
size = (int) Math.ceil(size / 9) * 9;
dB.echoError("Inventory size must be a multiple of 9! Rounding up to " + size + "...");
}
if (size < 0) {
size = size * -1;
dB.echoError("Inventory size must be a positive number! Inverting to " + size + "...");
}
inventory = new dInventory(size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
inventory.setIdentifiers("script", getName());
}
}
if (size == 0) {
size = getInventoryType().getDefaultSize();
}
boolean[] filledSlots = new boolean[size];
if (contains("SLOTS")) {
ItemStack[] finalItems = new ItemStack[size];
int itemsAdded = 0;
for (String items : getStringList("SLOTS")) {
items = TagManager.tag(items, context).trim();
if (items.isEmpty()) {
continue;
}
if (!items.startsWith("[") || !items.endsWith("]")) {
dB.echoError("Inventory script \"" + getName() + "\" has an invalid slots line: [" + items + "]... Ignoring it");
continue;
}
String[] itemsInLine = items.substring(1, items.length() - 1).split("\\[?\\]?\\s+\\[", -1);
for (String item : itemsInLine) {
if (contains("DEFINITIONS." + item)) {
dItem def = dItem.valueOf(TagManager.tag(getString("DEFINITIONS." + item), context), player, npc);
if (def == null) {
dB.echoError("Invalid definition '" + item + "' in inventory script '" + getName() + "'" + "... Ignoring it and assuming \"AIR\"");
finalItems[itemsAdded] = new ItemStack(Material.AIR);
} else {
finalItems[itemsAdded] = def.getItemStack();
}
} else if (dItem.matches(item)) {
finalItems[itemsAdded] = dItem.valueOf(item, player, npc).getItemStack();
} else {
finalItems[itemsAdded] = new ItemStack(Material.AIR);
if (!item.isEmpty()) {
dB.echoError("Inventory script \"" + getName() + "\" has an invalid slot item: [" + item + "]... Ignoring it and assuming \"AIR\"");
}
}
filledSlots[itemsAdded] = !item.isEmpty();
itemsAdded++;
}
}
if (inventory == null) {
size = finalItems.length % 9 == 0 ? finalItems.length : Math.round(finalItems.length / 9) * 9;
inventory = new dInventory(size == 0 ? 9 : size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
}
inventory.setContents(finalItems);
}
if (contains("PROCEDURAL ITEMS")) {
// TODO: Document this feature!
if (inventory == null) {
size = InventoryType.CHEST.getDefaultSize();
inventory = new dInventory(size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
}
List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "PROCEDURAL ITEMS");
if (!entries.isEmpty()) {
long id = DetermineCommand.getNewId();
ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
InstantQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId("INV_SCRIPT_ITEM_PROC"));
queue.addEntries(entries);
queue.setReqId(id);
if (contains("DEFINITIONS")) {
YamlConfiguration section = getConfigurationSection("DEFINITIONS");
for (StringHolder string : section.getKeys(false)) {
String definition = string.str;
queue.addDefinition(definition, section.getString(definition));
}
}
queue.start();
if (DetermineCommand.hasOutcome(id)) {
dList list = dList.valueOf(DetermineCommand.getOutcome(id).get(0));
if (list != null) {
int x = 0;
for (dItem item : list.filter(dItem.class)) {
while (x < filledSlots.length && filledSlots[x]) {
x++;
}
if (x >= filledSlots.length || filledSlots[x]) {
break;
}
inventory.setSlots(x, item.getItemStack());
filledSlots[x] = true;
}
}
}
}
}
} catch (Exception e) {
dB.echoError("Woah! An exception has been called with this inventory script!");
dB.echoError(e);
inventory = null;
}
if (inventory != null) {
InventoryScriptHelper.tempInventoryScripts.put(inventory.getInventory(), getName());
}
return inventory;
}
use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptHelper method processSpecialRecipes.
// Compare a crafting matrix with all stored special recipes right
// after a click or drag has been made in it
public boolean processSpecialRecipes(final CraftingInventory inventory, final Player player) {
// Store the current matrix
ItemStack[] matrix1 = inventory.getMatrix();
for (int i = 0; i < matrix1.length; i++) {
matrix1[i] = matrix1[i] == null ? new ItemStack(Material.AIR) : matrix1[i].clone();
}
// Get the result of the special recipe that this matrix matches,
// if any
dItem result1 = getSpecialRecipeResult(matrix1, player);
boolean returnme = result1 != null;
// Run a task 1 tick later than the event from which this method
// was called, to check the new state of the CraftingInventory's matrix
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
// Store the current matrix
ItemStack[] matrix = inventory.getMatrix();
for (int i = 0; i < matrix.length; i++) {
matrix[i] = matrix[i] == null ? new ItemStack(Material.AIR) : matrix[i].clone();
}
// Get the result of the special recipe that this matrix matches,
// if any
dItem result = getSpecialRecipeResult(matrix, player);
// Proceed only if the result was not null
if (result != null) {
dList recipeList = new dList();
for (ItemStack item : matrix) {
if (item != null) {
recipeList.add(new dItem(item).identify());
} else {
recipeList.add(new dItem(Material.AIR).identify());
}
}
ItemRecipeFormedScriptEvent event = ItemRecipeFormedScriptEvent.instance;
event.result = result;
event.recipe = recipeList;
event.inventory = inventory;
event.player = dPlayer.mirrorBukkitPlayer(player);
event.cancelled = false;
event.resultChanged = false;
event.fire();
if (event.cancelled) {
inventory.setResult(null);
} else {
// If this was a valid match, set the crafting's result
inventory.setResult(event.result.getItemStack());
}
// Update the player's inventory
player.updateInventory();
}
}
}, 2);
return returnme;
}
use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class PlayEffectCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Extract objects from ScriptEntry
List<dLocation> locations = (List<dLocation>) scriptEntry.getObject("location");
List<dPlayer> targets = (List<dPlayer>) scriptEntry.getObject("targets");
Effect effect = (Effect) scriptEntry.getObject("effect");
Particle particleEffect = (Particle) scriptEntry.getObject("particleeffect");
Element iconcrack = scriptEntry.getElement("iconcrack");
Element iconcrack_data = scriptEntry.getElement("iconcrack_data");
Element iconcrack_type = scriptEntry.getElement("iconcrack_type");
Element radius = scriptEntry.getElement("radius");
Element data = scriptEntry.getElement("data");
Element qty = scriptEntry.getElement("qty");
dLocation offset = scriptEntry.getdObject("offset");
// Report to dB
dB.report(scriptEntry, getName(), (effect != null ? aH.debugObj("effect", effect.getName()) : particleEffect != null ? aH.debugObj("special effect", particleEffect.getName()) : iconcrack_type.debug() + iconcrack.debug() + (iconcrack_data != null ? iconcrack_data.debug() : "")) + aH.debugObj("locations", locations.toString()) + (targets != null ? aH.debugObj("targets", targets.toString()) : "") + radius.debug() + data.debug() + qty.debug() + offset.debug());
for (dLocation location : locations) {
// Slightly increase the location's Y so effects don't seem to come out of the ground
location.add(0, 1, 0);
// Play the Bukkit effect the number of times specified
if (effect != null) {
for (int n = 0; n < qty.asInt(); n++) {
if (targets != null) {
for (dPlayer player : targets) {
if (player.isValid() && player.isOnline()) {
effect.playFor(player.getPlayerEntity(), location, data.asInt());
}
}
} else {
effect.play(location, data.asInt(), radius.asInt());
}
}
} else // Play a ParticleEffect
if (particleEffect != null) {
float osX = (float) offset.getX();
float osY = (float) offset.getY();
float osZ = (float) offset.getZ();
List<Player> players = new ArrayList<Player>();
if (targets == null) {
float rad = radius.asFloat();
for (Player player : location.getWorld().getPlayers()) {
if (player.getLocation().distanceSquared(location) < rad * rad) {
players.add(player);
}
}
} else {
for (dPlayer player : targets) {
if (player.isValid() && player.isOnline()) {
players.add(player.getPlayerEntity());
}
}
}
for (Player player : players) {
particleEffect.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat());
}
} else // Play an iconcrack (item break) effect
{
List<Player> players = new ArrayList<Player>();
if (targets == null) {
float rad = radius.asFloat();
for (Player player : location.getWorld().getPlayers()) {
if (player.getLocation().distanceSquared(location) < rad * rad) {
players.add(player);
}
}
} else {
for (dPlayer player : targets) {
if (player.isValid() && player.isOnline()) {
players.add(player.getPlayerEntity());
}
}
}
// TODO: better this all
if (iconcrack_type.asString().equalsIgnoreCase("iconcrack")) {
ItemStack itemStack = new ItemStack(iconcrack.asInt(), 1, (short) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("ITEM_CRACK");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), itemStack);
}
} else if (iconcrack_type.asString().equalsIgnoreCase("blockcrack")) {
MaterialData materialData = new MaterialData(iconcrack.asInt(), (byte) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("BLOCK_CRACK");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), materialData);
}
} else {
// blockdust
MaterialData materialData = new MaterialData(iconcrack.asInt(), (byte) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("BLOCK_DUST");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), materialData);
}
}
}
}
}
Aggregations