use of cn.nukkit.item.Item in project Nukkit by Nukkit.
the class BaseInventory method decreaseCount.
@Override
public void decreaseCount(int slot) {
Item item = this.getItem(slot);
if (item.getCount() > 0) {
item.count--;
this.setItem(slot, item);
}
}
use of cn.nukkit.item.Item in project Nukkit by Nukkit.
the class NetworkInventoryAction method createInventoryAction.
public InventoryAction createInventoryAction(Player player) {
switch(this.sourceType) {
case SOURCE_CONTAINER:
if (this.windowId == ContainerIds.ARMOR) {
// TODO: HACK!
this.inventorySlot += 36;
this.windowId = ContainerIds.INVENTORY;
}
Inventory window = player.getWindowById(this.windowId);
if (window != null) {
return new SlotChangeAction(window, this.inventorySlot, this.oldItem, this.newItem);
}
player.getServer().getLogger().debug("Player " + player.getName() + " has no open container with window ID " + this.windowId);
return null;
case SOURCE_WORLD:
if (this.inventorySlot != InventoryTransactionPacket.ACTION_MAGIC_SLOT_DROP_ITEM) {
player.getServer().getLogger().debug("Only expecting drop-item world actions from the client!");
return null;
}
return new DropItemAction(this.oldItem, this.newItem);
case SOURCE_CREATIVE:
int type;
switch(this.inventorySlot) {
case InventoryTransactionPacket.ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM:
type = CreativeInventoryAction.TYPE_DELETE_ITEM;
break;
case InventoryTransactionPacket.ACTION_MAGIC_SLOT_CREATIVE_CREATE_ITEM:
type = CreativeInventoryAction.TYPE_CREATE_ITEM;
break;
default:
player.getServer().getLogger().debug("Unexpected creative action type " + this.inventorySlot);
return null;
}
return new CreativeInventoryAction(this.oldItem, this.newItem, type);
case SOURCE_TODO:
// These types need special handling.
switch(this.windowId) {
case SOURCE_TYPE_CRAFTING_ADD_INGREDIENT:
case SOURCE_TYPE_CRAFTING_REMOVE_INGREDIENT:
window = player.getCraftingGrid();
return new SlotChangeAction(window, this.inventorySlot, this.oldItem, this.newItem);
case SOURCE_TYPE_CRAFTING_RESULT:
return new CraftingTakeResultAction(this.oldItem, this.newItem);
case SOURCE_TYPE_CRAFTING_USE_INGREDIENT:
return new CraftingTransferMaterialAction(this.oldItem, this.newItem, this.inventorySlot);
case SOURCE_TYPE_CONTAINER_DROP_CONTENTS:
window = player.getCraftingGrid();
inventorySlot = window.first(this.oldItem, true);
if (inventorySlot == -1) {
return null;
}
return new SlotChangeAction(window, inventorySlot, this.oldItem, this.newItem);
}
if (this.windowId >= SOURCE_TYPE_ANVIL_OUTPUT && this.windowId <= SOURCE_TYPE_ANVIL_INPUT) {
// anvil actions
Inventory inv = player.getWindowById(Player.ANVIL_WINDOW_ID);
if (!(inv instanceof AnvilInventory)) {
player.getServer().getLogger().debug("Player " + player.getName() + " has no open anvil inventory");
return null;
}
AnvilInventory anvil = (AnvilInventory) inv;
switch(this.windowId) {
case SOURCE_TYPE_ANVIL_INPUT:
// System.out.println("action input");
this.inventorySlot = 0;
return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
case SOURCE_TYPE_ANVIL_MATERIAL:
// System.out.println("material");
this.inventorySlot = 1;
return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
case SOURCE_TYPE_ANVIL_OUTPUT:
// System.out.println("action output");
break;
case SOURCE_TYPE_ANVIL_RESULT:
this.inventorySlot = 2;
anvil.clear(0);
anvil.clear(1);
anvil.setItem(2, this.oldItem);
// System.out.println("action result");
return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
}
}
if (this.windowId >= SOURCE_TYPE_ENCHANT_OUTPUT && this.windowId <= SOURCE_TYPE_ENCHANT_INPUT) {
Inventory inv = player.getWindowById(Player.ENCHANT_WINDOW_ID);
if (!(inv instanceof EnchantInventory)) {
player.getServer().getLogger().debug("Player " + player.getName() + " has no open enchant inventory");
return null;
}
EnchantInventory enchant = (EnchantInventory) inv;
switch(this.windowId) {
case SOURCE_TYPE_ENCHANT_INPUT:
this.inventorySlot = 0;
Item local = enchant.getItem(0);
if (local.equals(this.newItem, true, false)) {
enchant.setItem(0, this.newItem);
}
break;
case SOURCE_TYPE_ENCHANT_MATERIAL:
this.inventorySlot = 1;
break;
case SOURCE_TYPE_ENCHANT_OUTPUT:
enchant.sendSlot(0, player);
// ignore?
return null;
}
return new SlotChangeAction(enchant, this.inventorySlot, this.oldItem, this.newItem);
}
// TODO: more stuff
player.getServer().getLogger().debug("Player " + player.getName() + " has no open container with window ID " + this.windowId);
return null;
default:
player.getServer().getLogger().debug("Unknown inventory source type " + this.sourceType);
return null;
}
}
use of cn.nukkit.item.Item in project Nukkit by Nukkit.
the class EntityHumanType method attack.
@Override
public boolean attack(EntityDamageEvent source) {
if (!this.isAlive()) {
return false;
}
if (source.getCause() != DamageCause.VOID && source.getCause() != DamageCause.CUSTOM && source.getCause() != DamageCause.MAGIC) {
int points = 0;
int epf = 0;
int toughness = 0;
for (Item armor : inventory.getArmorContents()) {
points += armor.getArmorPoints();
epf += calculateEnchantmentReduction(armor, source);
toughness += armor.getToughness();
}
float originalDamage = source.getDamage();
float finalDamage = (float) (originalDamage * (1 - Math.max(points / 5, points - originalDamage / (2 + toughness / 4)) / 25) * (1 - /*0.75 */
epf * 0.04));
source.setDamage(finalDamage - originalDamage, DamageModifier.ARMOR);
// source.setDamage(source.getDamage(DamageModifier.ARMOR_ENCHANTMENTS) - (originalDamage - originalDamage * (1 - epf / 25)), DamageModifier.ARMOR_ENCHANTMENTS);
}
if (super.attack(source)) {
Entity damager = null;
if (source instanceof EntityDamageByEntityEvent) {
damager = ((EntityDamageByEntityEvent) source).getDamager();
}
for (int slot = 0; slot < 4; slot++) {
Item armor = this.inventory.getArmorItem(slot);
if (armor.hasEnchantments()) {
if (damager != null) {
for (Enchantment enchantment : armor.getEnchantments()) {
enchantment.doPostAttack(damager, this);
}
}
Enchantment durability = armor.getEnchantment(Enchantment.ID_DURABILITY);
if (durability != null && durability.getLevel() > 0 && (100 / (durability.getLevel() + 1)) <= new Random().nextInt(100))
continue;
}
armor.setDamage(armor.getDamage() + 1);
if (armor.getDamage() >= armor.getMaxDurability()) {
inventory.setArmorItem(slot, new ItemBlock(new BlockAir()));
} else {
inventory.setArmorItem(slot, armor, true);
}
}
return true;
} else {
return false;
}
}
use of cn.nukkit.item.Item in project Nukkit by Nukkit.
the class EntityHumanType method setOnFire.
@Override
public void setOnFire(int seconds) {
int level = 0;
for (Item armor : this.inventory.getArmorContents()) {
Enchantment fireProtection = armor.getEnchantment(Enchantment.ID_PROTECTION_FIRE);
if (fireProtection != null && fireProtection.getLevel() > 0) {
level = Math.max(level, fireProtection.getLevel());
}
}
seconds = (int) (seconds * (1 - level * 0.15));
super.setOnFire(seconds);
}
use of cn.nukkit.item.Item in project Nukkit by Nukkit.
the class EntityLiving method kill.
@Override
public void kill() {
if (!this.isAlive()) {
return;
}
super.kill();
EntityDeathEvent ev = new EntityDeathEvent(this, this.getDrops());
this.server.getPluginManager().callEvent(ev);
if (this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
for (cn.nukkit.item.Item item : ev.getDrops()) {
this.getLevel().dropItem(this, item);
}
}
}
Aggregations