use of net.minecraft.world.item.ItemStack in project MyPet by xXKeyleXx.
the class EntityMyFox method handlePlayerInteraction.
@Override
public InteractionResult handlePlayerInteraction(Player entityhuman, InteractionHand enumhand, ItemStack itemStack) {
if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
return InteractionResult.CONSUME;
}
if (getOwner().equals(entityhuman)) {
if (itemStack != null && itemStack.getItem() != Items.AIR && canUseItem() && getOwner().getPlayer().isSneaking()) {
if (itemStack.getItem() != Items.SHEARS && getOwner().getPlayer().isSneaking() && canEquip()) {
ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(EquipmentSlot.MainHand));
if (itemInSlot != null && itemInSlot.getItem() != Items.AIR && itemInSlot != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), itemInSlot);
entityitem.pickupDelay = 10;
entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
this.level.addFreshEntity(entityitem);
}
getMyPet().setEquipment(EquipmentSlot.MainHand, CraftItemStack.asBukkitCopy(itemStack));
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
itemStack.shrink(1);
if (itemStack.getCount() <= 0) {
entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
}
}
return InteractionResult.CONSUME;
} else if (Configuration.MyPet.Fox.GROW_UP_ITEM.compare(itemStack) && canUseItem() && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
itemStack.shrink(1);
if (itemStack.getCount() <= 0) {
entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
}
}
getMyPet().setBaby(false);
return InteractionResult.CONSUME;
} else if (itemStack.getItem() == Items.SHEARS && getOwner().getPlayer().isSneaking() && canEquip()) {
boolean hadEquipment = false;
for (EquipmentSlot slot : EquipmentSlot.values()) {
ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot));
if (itemInSlot != null && itemInSlot.getItem() != Items.AIR) {
ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), itemInSlot);
entityitem.pickupDelay = 10;
entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
this.level.addFreshEntity(entityitem);
getMyPet().setEquipment(slot, null);
hadEquipment = true;
}
}
if (hadEquipment) {
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
try {
itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
} catch (Error e) {
// TODO REMOVE
itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
try {
CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
});
}
}
}
return InteractionResult.CONSUME;
}
}
}
return InteractionResult.PASS;
}
use of net.minecraft.world.item.ItemStack in project MyPet by xXKeyleXx.
the class IconMenuInventory method open.
@Override
public void open(IconMenu menu, HumanEntity player) {
size = menu.getSize();
minecraftInventory = new CustomInventory(size, menu.getTitle());
for (int slot = 0; slot < size; slot++) {
IconMenuItem menuItem = menu.getOption(slot);
if (menuItem != null) {
ItemStack item = createItemStack(menuItem);
minecraftInventory.setItem(slot, item);
}
}
minecraftInventory.open((Player) player);
}
use of net.minecraft.world.item.ItemStack in project MyPet by xXKeyleXx.
the class IconMenuInventory method update.
@Override
public void update(IconMenu menu) {
if (minecraftInventory != null) {
for (int slot = 0; slot < size; slot++) {
IconMenuItem menuItem = menu.getOption(slot);
if (menuItem != null) {
ItemStack item = createItemStack(menuItem);
minecraftInventory.setItem(slot, item);
} else {
minecraftInventory.setItem(slot, ItemStack.EMPTY);
}
}
}
}
use of net.minecraft.world.item.ItemStack in project MyPet by xXKeyleXx.
the class IconMenuInventory method createItemStack.
protected ItemStack createItemStack(IconMenuItem icon) {
// TODO Check if this works properly
ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(icon.getMaterial(), icon.getAmount()));
if (is == null) {
is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.STONE));
}
if (is.getTag() == null) {
is.setTag(new CompoundTag());
}
if (icon.getBukkitMeta() != null) {
try {
applyToItemMethod.invoke(icon.getBukkitMeta(), is.getTag());
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
// add enchantment glowing
if (icon.isGlowing()) {
TagCompound enchTag = new TagCompound();
enchTag.put("id", new TagString("minecraft:feather_falling"));
enchTag.put("lvl", new TagShort(1));
TagList enchList = new TagList();
enchList.addTag(enchTag);
is.getTag().put("Enchantments", ItemStackNBTConverter.compoundToVanillaCompound(enchList));
} else {
is.getTag().remove("Enchantments");
}
// hide item attributes like attack damage
is.getTag().putInt("HideFlags", 63);
// Prepare display tag
CompoundTag display;
if (is.getTag().contains("display")) {
display = is.getTag().getCompound("display");
} else {
display = new CompoundTag();
is.getTag().put("display", display);
}
// set Title
if (!icon.getTitle().equals("")) {
display.putString("Name", "{\"text\":\"" + icon.getTitle() + "\"}");
}
if (icon.getLore().size() > 0) {
// set Lore
ListTag loreTag = new ListTag();
display.put("Lore", loreTag);
for (String loreLine : icon.getLore()) {
Component cm = CraftChatMessage.fromStringOrNull(loreLine);
loreTag.add(StringTag.valueOf(Component.Serializer.toJson(cm)));
}
}
if (icon.hasMeta()) {
TagCompound tag = new TagCompound();
icon.getMeta().applyTo(tag);
CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(tag);
for (String key : vanillaTag.getAllKeys()) {
is.getTag().put(key, vanillaTag.get(key));
}
}
if (icon.getTags() != null) {
CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(icon.getTags());
for (String key : vanillaTag.getAllKeys()) {
is.getTag().put(key, vanillaTag.get(key));
}
}
return is;
}
use of net.minecraft.world.item.ItemStack in project MyPet by xXKeyleXx.
the class CustomInventory method load.
@Override
public void load(TagCompound nbtTagCompound) {
TagList items = nbtTagCompound.getAs("Items", TagList.class);
for (int i = 0; i < items.size(); i++) {
TagCompound itemCompound = items.getTagAs(i, TagCompound.class);
ItemStack itemStack = ItemStackNBTConverter.compoundToItemStack(itemCompound);
setItem(itemCompound.getAs("Slot", TagByte.class).getByteData(), itemStack);
}
}
Aggregations