use of cn.nukkit.event.entity.EntityInventoryChangeEvent in project Nukkit by Nukkit.
the class BaseInventory method setItem.
@Override
public boolean setItem(int index, Item item, boolean send) {
item = item.clone();
if (index < 0 || index >= this.size) {
return false;
} else if (item.getId() == 0 || item.getCount() <= 0) {
return this.clear(index);
}
InventoryHolder holder = this.getHolder();
if (holder instanceof Entity) {
EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, this.getItem(index), item, index);
Server.getInstance().getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
this.sendSlot(index, this.getViewers());
return false;
}
item = ev.getNewItem();
}
Item old = this.getItem(index);
this.slots.put(index, item.clone());
this.onSlotChange(index, old, send);
return true;
}
use of cn.nukkit.event.entity.EntityInventoryChangeEvent in project Nukkit by Nukkit.
the class BaseInventory method clear.
@Override
public boolean clear(int index, boolean send) {
if (this.slots.containsKey(index)) {
Item item = new ItemBlock(new BlockAir(), null, 0);
Item old = this.slots.get(index);
InventoryHolder holder = this.getHolder();
if (holder instanceof Entity) {
EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, old, item, index);
Server.getInstance().getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
this.sendSlot(index, this.getViewers());
return false;
}
item = ev.getNewItem();
}
if (item.getId() != Item.AIR) {
this.slots.put(index, item.clone());
} else {
this.slots.remove(index);
}
this.onSlotChange(index, old, send);
}
return true;
}
use of cn.nukkit.event.entity.EntityInventoryChangeEvent in project Nukkit by Nukkit.
the class PlayerInventory method clear.
@Override
public boolean clear(int index, boolean send) {
if (this.slots.containsKey(index)) {
Item item = new ItemBlock(new BlockAir(), null, 0);
Item old = this.slots.get(index);
if (index >= this.getSize() && index < this.size) {
EntityArmorChangeEvent ev = new EntityArmorChangeEvent(this.getHolder(), old, item, index);
Server.getInstance().getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
if (index >= this.size) {
this.sendArmorSlot(index, this.getViewers());
} else {
this.sendSlot(index, this.getViewers());
}
return false;
}
item = ev.getNewItem();
} else {
EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent(this.getHolder(), old, item, index);
Server.getInstance().getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
if (index >= this.size) {
this.sendArmorSlot(index, this.getViewers());
} else {
this.sendSlot(index, this.getViewers());
}
return false;
}
item = ev.getNewItem();
}
if (item.getId() != Item.AIR) {
this.slots.put(index, item.clone());
} else {
this.slots.remove(index);
}
this.onSlotChange(index, old, send);
}
return true;
}
use of cn.nukkit.event.entity.EntityInventoryChangeEvent in project Nukkit by Nukkit.
the class PlayerInventory method setItem.
private boolean setItem(int index, Item item, boolean send, boolean ignoreArmorEvents) {
if (index < 0 || index >= this.size) {
return false;
} else if (item.getId() == 0 || item.getCount() <= 0) {
return this.clear(index);
}
// Armor change
if (!ignoreArmorEvents && index >= this.getSize()) {
EntityArmorChangeEvent ev = new EntityArmorChangeEvent(this.getHolder(), this.getItem(index), item, index);
Server.getInstance().getPluginManager().callEvent(ev);
if (ev.isCancelled() && this.getHolder() != null) {
this.sendArmorSlot(index, this.getViewers());
return false;
}
item = ev.getNewItem();
} else {
EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent(this.getHolder(), this.getItem(index), item, index);
Server.getInstance().getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
this.sendSlot(index, this.getViewers());
return false;
}
item = ev.getNewItem();
}
Item old = this.getItem(index);
this.slots.put(index, item.clone());
this.onSlotChange(index, old, send);
return true;
}
Aggregations