use of cn.nukkit.item.Item in project Nukkit by Nukkit.
the class GiveCommand method execute.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return true;
}
if (args.length < 2) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
}
Player player = sender.getServer().getPlayer(args[0]);
Item item;
try {
item = Item.fromString(args[1]);
} catch (Exception e) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
}
try {
item.setCount(Integer.parseInt(args[2]));
} catch (Exception e) {
item.setCount(item.getMaxStackSize());
}
if (player != null) {
if (item.getId() == 0) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.give.item.notFound", args[1]));
return true;
}
player.getInventory().addItem(item.clone());
} else {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
return true;
}
Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.give.success", new String[] { item.getName() + " (" + item.getId() + ":" + item.getDamage() + ")", String.valueOf(item.getCount()), player.getName() }));
return true;
}
use of cn.nukkit.item.Item in project Nukkit by Nukkit.
the class AnvilInventory method onRename.
public boolean onRename(Player player, Item resultItem) {
Item local = getItem(TARGET);
Item second = getItem(SACRIFICE);
if (!resultItem.equals(local, true, false) || resultItem.getCount() != local.getCount()) {
// Item does not match target item. Everything must match except the tags.
return false;
}
if (local.equals(resultItem)) {
// just item transaction
return true;
}
if (local.getId() != 0 && second.getId() == 0) {
// only rename
local.setCustomName(resultItem.getCustomName());
setItem(RESULT, local);
player.getInventory().addItem(local);
clearAll();
player.getInventory().sendContents(player);
sendContents(player);
player.getLevel().addSound(player, Sound.RANDOM_ANVIL_USE);
return true;
} else if (local.getId() != 0 && second.getId() != 0) {
// enchants combining
if (!local.equals(second, true, false)) {
return false;
}
if (local.getId() != 0 && second.getId() != 0) {
Item result = local.clone();
int enchants = 0;
ArrayList<Enchantment> enchantments = new ArrayList<>(Arrays.asList(second.getEnchantments()));
ArrayList<Enchantment> baseEnchants = new ArrayList<>();
for (Enchantment ench : local.getEnchantments()) {
if (ench.isMajor()) {
baseEnchants.add(ench);
}
}
for (Enchantment enchantment : enchantments) {
if (enchantment.getLevel() < 0 || enchantment.getId() < 0) {
continue;
}
if (enchantment.isMajor()) {
boolean same = false;
boolean another = false;
for (Enchantment baseEnchant : baseEnchants) {
if (baseEnchant.getId() == enchantment.getId())
same = true;
else {
another = true;
}
}
if (!same && another) {
continue;
}
}
Enchantment localEnchantment = local.getEnchantment(enchantment.getId());
if (localEnchantment != null) {
int level = Math.max(localEnchantment.getLevel(), enchantment.getLevel());
if (localEnchantment.getLevel() == enchantment.getLevel())
level++;
enchantment.setLevel(level);
result.addEnchantment(enchantment);
continue;
}
result.addEnchantment(enchantment);
enchants++;
}
result.setCustomName(resultItem.getCustomName());
player.getInventory().addItem(result);
player.getInventory().sendContents(player);
clearAll();
sendContents(player);
player.getLevel().addSound(player, Sound.RANDOM_ANVIL_USE);
return true;
}
}
return false;
}
use of cn.nukkit.item.Item 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.item.Item in project Nukkit by Nukkit.
the class BaseInventory method contains.
@Override
public boolean contains(Item item) {
int count = Math.max(1, item.getCount());
boolean checkDamage = item.hasMeta() && item.getDamage() >= 0;
boolean checkTag = item.getCompoundTag() != null;
for (Item i : this.getContents().values()) {
if (item.equals(i, checkDamage, checkTag)) {
count -= i.getCount();
if (count <= 0) {
return true;
}
}
}
return false;
}
use of cn.nukkit.item.Item in project Nukkit by Nukkit.
the class BaseInventory method addItem.
@Override
public Item[] addItem(Item... slots) {
List<Item> itemSlots = new ArrayList<>();
for (Item slot : slots) {
if (slot.getId() != 0 && slot.getCount() > 0) {
itemSlots.add(slot.clone());
}
}
List<Integer> emptySlots = new ArrayList<>();
for (int i = 0; i < this.getSize(); ++i) {
Item item = this.getItem(i);
if (item.getId() == Item.AIR || item.getCount() <= 0) {
emptySlots.add(i);
}
for (Item slot : new ArrayList<>(itemSlots)) {
if (slot.equals(item) && item.getCount() < item.getMaxStackSize()) {
int amount = Math.min(item.getMaxStackSize() - item.getCount(), slot.getCount());
amount = Math.min(amount, this.getMaxStackSize());
if (amount > 0) {
slot.setCount(slot.getCount() - amount);
item.setCount(item.getCount() + amount);
this.setItem(i, item);
if (slot.getCount() <= 0) {
itemSlots.remove(slot);
}
}
}
}
if (itemSlots.isEmpty()) {
break;
}
}
if (!itemSlots.isEmpty() && !emptySlots.isEmpty()) {
for (int slotIndex : emptySlots) {
if (!itemSlots.isEmpty()) {
Item slot = itemSlots.get(0);
int amount = Math.min(slot.getMaxStackSize(), slot.getCount());
amount = Math.min(amount, this.getMaxStackSize());
slot.setCount(slot.getCount() - amount);
Item item = slot.clone();
item.setCount(amount);
this.setItem(slotIndex, item);
if (slot.getCount() <= 0) {
itemSlots.remove(slot);
}
}
}
}
return itemSlots.stream().toArray(Item[]::new);
}
Aggregations