Search in sources :

Example 1 with MyPetFeedEvent

use of de.Keyle.MyPet.api.event.MyPetFeedEvent in project MyPet by xXKeyleXx.

the class EntityMyPet method handlePlayerInteraction.

// Obfuscated Method handler ------------------------------------------------------------------------------------
/**
 * Is called when player rightclicks this MyPet
 * return:
 * true: there was a reaction on rightclick
 * false: no reaction on rightclick
 */
public boolean handlePlayerInteraction(final EntityHuman entityhuman, EnumHand enumhand, final ItemStack itemStack) {
    if (itemStack != null && itemStack.getItem() == Items.LEAD) {
        ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutAttachEntity(this, null));
        if (!entityhuman.abilities.canInstantlyBuild) {
            new BukkitRunnable() {

                public void run() {
                    ((EntityPlayer) entityhuman).updateInventory(entityhuman.defaultContainer);
                }
            }.runTaskLater(MyPetApi.getPlugin(), 5);
        }
    }
    if (enumhand == EnumHand.OFF_HAND) {
        return true;
    }
    Player owner = this.getOwner().getPlayer();
    if (isMyPet() && myPet.getOwner().equals(entityhuman)) {
        if (Configuration.Skilltree.Skill.Ride.RIDE_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(Ride.class) && canMove()) {
                if (Permissions.hasExtendedLegacy(owner, "MyPet.extended.ride")) {
                    ((CraftPlayer) owner).getHandle().startRiding(this);
                    return true;
                } else {
                    getOwner().sendMessage(Translation.getString("Message.No.CanUse", myPet.getOwner().getLanguage()));
                }
            }
        }
        if (Configuration.Skilltree.Skill.CONTROL_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(de.Keyle.MyPet.skill.skills.Control.class)) {
                return true;
            }
        }
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG && itemStack.hasName()) {
                if (Permissions.hasLegacy(getOwner(), "MyPet.command.name") && Permissions.hasExtended(getOwner(), "MyPet.extended.nametag")) {
                    final String name = itemStack.getName();
                    getMyPet().setPetName(name);
                    EntityMyPet.super.setCustomName("-");
                    myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Command.Name.New", myPet.getOwner()), name));
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        itemStack.subtract(1);
                    }
                    if (itemStack.getCount() <= 0) {
                        entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, ItemStack.a);
                    }
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return true;
                }
            }
            if (canEat(itemStack) && canUseItem()) {
                if (owner != null && !Permissions.hasExtendedLegacy(owner, "MyPet.extended.feed")) {
                    return false;
                }
                if (this.petTargetSelector.hasGoal("DuelTarget")) {
                    BehaviorDuelTarget duelTarget = (BehaviorDuelTarget) this.petTargetSelector.getGoal("DuelTarget");
                    if (duelTarget.getDuelOpponent() != null) {
                        return true;
                    }
                }
                boolean used = false;
                double saturation = Configuration.HungerSystem.HUNGER_SYSTEM_SATURATION_PER_FEED;
                if (saturation > 0) {
                    if (myPet.getSaturation() < 100) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Eat);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            double missingSaturation = 100 - myPet.getSaturation();
                            myPet.setSaturation(myPet.getSaturation() + saturation);
                            saturation = Math.max(0, saturation - missingSaturation);
                            used = true;
                        }
                    }
                }
                if (saturation > 0) {
                    if (getHealth() < getMaxHealth()) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Heal);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            float missingHealth = getMaxHealth() - getHealth();
                            this.heal(Math.min((float) saturation, missingHealth), RegainReason.EATING);
                            used = true;
                        }
                    }
                }
                if (used) {
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        itemStack.subtract(1);
                        if (itemStack.getCount() <= 0) {
                            entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, ItemStack.a);
                        }
                    }
                    MyPetApi.getPlatformHelper().playParticleEffect(myPet.getLocation().get().add(0, getHeadHeight(), 0), "HEART", 0.5F, 0.5F, 0.5F, 0.5F, 5, 20);
                    return true;
                }
            }
        }
    } else {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG) {
                if (itemStack.hasName()) {
                    EntityMyPet.super.setCustomName("-");
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return false;
                }
            }
        }
    }
    return false;
}
Also used : Ride(de.Keyle.MyPet.skill.skills.Ride) Player(org.bukkit.entity.Player) CraftPlayer(org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetFeedEvent(de.Keyle.MyPet.api.event.MyPetFeedEvent) Ride(de.Keyle.MyPet.skill.skills.Ride)

Example 2 with MyPetFeedEvent

use of de.Keyle.MyPet.api.event.MyPetFeedEvent in project MyPet by xXKeyleXx.

the class EntityMyPet method handlePlayerInteraction.

// Obfuscated Method handler ------------------------------------------------------------------------------------
/**
 * Is called when player rightclicks this MyPet
 * return:
 * true: there was a reaction on rightclick
 * false: no reaction on rightclick
 */
public boolean handlePlayerInteraction(final EntityHuman entityhuman, EnumHand enumhand, final ItemStack itemStack) {
    if (itemStack != null && itemStack.getItem() == Items.LEAD) {
        ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutAttachEntity(this, null));
        if (!entityhuman.abilities.canInstantlyBuild) {
            new BukkitRunnable() {

                public void run() {
                    ((EntityPlayer) entityhuman).updateInventory(entityhuman.defaultContainer);
                }
            }.runTaskLater(MyPetApi.getPlugin(), 5);
        }
    }
    if (enumhand == EnumHand.OFF_HAND) {
        return true;
    }
    Player owner = this.getOwner().getPlayer();
    if (isMyPet() && myPet.getOwner().equals(entityhuman)) {
        if (Configuration.Skilltree.Skill.Ride.RIDE_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(Ride.class) && canMove()) {
                if (Permissions.hasExtendedLegacy(owner, "MyPet.extended.ride")) {
                    ((CraftPlayer) owner).getHandle().startRiding(this);
                    return true;
                } else {
                    getOwner().sendMessage(Translation.getString("Message.No.CanUse", myPet.getOwner().getLanguage()));
                }
            }
        }
        if (Configuration.Skilltree.Skill.CONTROL_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(de.Keyle.MyPet.skill.skills.Control.class)) {
                return true;
            }
        }
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG && itemStack.hasName()) {
                if (Permissions.hasLegacy(getOwner(), "MyPet.command.name") && Permissions.hasExtended(getOwner(), "MyPet.extended.nametag")) {
                    final String name = itemStack.getName();
                    getMyPet().setPetName(name);
                    EntityMyPet.super.setCustomName("-");
                    myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Command.Name.New", myPet.getOwner()), name));
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        --itemStack.count;
                    }
                    if (itemStack.count <= 0) {
                        entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null);
                    }
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return true;
                }
            }
            if (canEat(itemStack) && canUseItem()) {
                if (owner != null && !Permissions.hasExtendedLegacy(owner, "MyPet.extended.feed")) {
                    return false;
                }
                if (this.petTargetSelector.hasGoal("DuelTarget")) {
                    BehaviorDuelTarget duelTarget = (BehaviorDuelTarget) this.petTargetSelector.getGoal("DuelTarget");
                    if (duelTarget.getDuelOpponent() != null) {
                        return true;
                    }
                }
                boolean used = false;
                double saturation = Configuration.HungerSystem.HUNGER_SYSTEM_SATURATION_PER_FEED;
                if (saturation > 0) {
                    if (myPet.getSaturation() < 100) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Eat);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            double missingSaturation = 100 - myPet.getSaturation();
                            myPet.setSaturation(myPet.getSaturation() + saturation);
                            saturation = Math.max(0, saturation - missingSaturation);
                            used = true;
                        }
                    }
                }
                if (saturation > 0) {
                    if (getHealth() < getMaxHealth()) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Heal);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            float missingHealth = getMaxHealth() - getHealth();
                            this.heal(Math.min((float) saturation, missingHealth), RegainReason.EATING);
                            used = true;
                        }
                    }
                }
                if (used) {
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        if (--itemStack.count <= 0) {
                            entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null);
                        }
                    }
                    MyPetApi.getPlatformHelper().playParticleEffect(myPet.getLocation().get().add(0, getHeadHeight(), 0), "HEART", 0.5F, 0.5F, 0.5F, 0.5F, 5, 20);
                    return true;
                }
            }
        }
    } else {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG) {
                if (itemStack.hasName()) {
                    EntityMyPet.super.setCustomName("-");
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return false;
                }
            }
        }
    }
    return false;
}
Also used : Ride(de.Keyle.MyPet.skill.skills.Ride) Player(org.bukkit.entity.Player) CraftPlayer(org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetFeedEvent(de.Keyle.MyPet.api.event.MyPetFeedEvent) Ride(de.Keyle.MyPet.skill.skills.Ride)

Example 3 with MyPetFeedEvent

use of de.Keyle.MyPet.api.event.MyPetFeedEvent in project MyPet by xXKeyleXx.

the class EntityMyPet method handlePlayerInteraction.

// Obfuscated Method handler ------------------------------------------------------------------------------------
/**
 * Is called when player rightclicks this MyPet
 * return:
 * true: there was a reaction on rightclick
 * false: no reaction on rightclick
 */
public boolean handlePlayerInteraction(final EntityHuman entityhuman, EnumHand enumhand, final ItemStack itemStack) {
    if (itemStack != null && itemStack.getItem() == Items.LEAD) {
        ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutAttachEntity(this, null));
        if (!entityhuman.abilities.canInstantlyBuild) {
            new BukkitRunnable() {

                public void run() {
                    ((EntityPlayer) entityhuman).updateInventory(entityhuman.defaultContainer);
                }
            }.runTaskLater(MyPetApi.getPlugin(), 5);
        }
    }
    if (enumhand == EnumHand.OFF_HAND) {
        return true;
    }
    Player owner = this.getOwner().getPlayer();
    if (isMyPet() && myPet.getOwner().equals(entityhuman)) {
        if (Configuration.Skilltree.Skill.Ride.RIDE_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(Ride.class) && canMove()) {
                if (Permissions.hasExtendedLegacy(owner, "MyPet.extended.ride")) {
                    ((CraftPlayer) owner).getHandle().startRiding(this);
                    return true;
                } else {
                    getOwner().sendMessage(Translation.getString("Message.No.CanUse", myPet.getOwner().getLanguage()));
                }
            }
        }
        if (Configuration.Skilltree.Skill.CONTROL_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(de.Keyle.MyPet.skill.skills.Control.class)) {
                return true;
            }
        }
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG && itemStack.hasName()) {
                if (Permissions.hasLegacy(getOwner(), "MyPet.command.name") && Permissions.hasExtended(getOwner(), "MyPet.extended.nametag")) {
                    final String name = itemStack.getName();
                    getMyPet().setPetName(name);
                    EntityMyPet.super.setCustomName("-");
                    myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Command.Name.New", myPet.getOwner()), name));
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        itemStack.subtract(1);
                    }
                    if (itemStack.getCount() <= 0) {
                        entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, ItemStack.a);
                    }
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return true;
                }
            }
            if (canEat(itemStack) && canUseItem()) {
                if (owner != null && !Permissions.hasExtendedLegacy(owner, "MyPet.extended.feed")) {
                    return false;
                }
                if (this.petTargetSelector.hasGoal("DuelTarget")) {
                    BehaviorDuelTarget duelTarget = (BehaviorDuelTarget) this.petTargetSelector.getGoal("DuelTarget");
                    if (duelTarget.getDuelOpponent() != null) {
                        return true;
                    }
                }
                boolean used = false;
                double saturation = Configuration.HungerSystem.HUNGER_SYSTEM_SATURATION_PER_FEED;
                if (saturation > 0) {
                    if (myPet.getSaturation() < 100) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Eat);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            double missingSaturation = 100 - myPet.getSaturation();
                            myPet.setSaturation(myPet.getSaturation() + saturation);
                            saturation = Math.max(0, saturation - missingSaturation);
                            used = true;
                        }
                    }
                }
                if (saturation > 0) {
                    if (getHealth() < getMaxHealth()) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Heal);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            float missingHealth = getMaxHealth() - getHealth();
                            this.heal(Math.min((float) saturation, missingHealth), RegainReason.EATING);
                            used = true;
                        }
                    }
                }
                if (used) {
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        itemStack.subtract(1);
                        if (itemStack.getCount() <= 0) {
                            entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, ItemStack.a);
                        }
                    }
                    MyPetApi.getPlatformHelper().playParticleEffect(myPet.getLocation().get().add(0, getHeadHeight(), 0), "HEART", 0.5F, 0.5F, 0.5F, 0.5F, 5, 20);
                    return true;
                }
            }
        }
    } else {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG) {
                if (itemStack.hasName()) {
                    EntityMyPet.super.setCustomName("-");
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return false;
                }
            }
        }
    }
    return false;
}
Also used : Ride(de.Keyle.MyPet.skill.skills.Ride) Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) CraftPlayer(org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetFeedEvent(de.Keyle.MyPet.api.event.MyPetFeedEvent) Ride(de.Keyle.MyPet.skill.skills.Ride)

Example 4 with MyPetFeedEvent

use of de.Keyle.MyPet.api.event.MyPetFeedEvent in project MyPet by xXKeyleXx.

the class EntityMyPet method handlePlayerInteraction.

// Obfuscated Method handler ------------------------------------------------------------------------------------
/**
 * Is called when player rightclicks this MyPet
 * return:
 * true: there was a reaction on rightclick
 * false: no reaction on rightclick
 */
public boolean handlePlayerInteraction(final EntityHuman entityhuman) {
    final ItemStack itemStack = entityhuman.inventory.getItemInHand();
    if (itemStack != null && itemStack.getItem() == Items.LEAD) {
        ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutAttachEntity(1, this, null));
        if (!entityhuman.abilities.canInstantlyBuild) {
            new BukkitRunnable() {

                public void run() {
                    ((EntityPlayer) entityhuman).updateInventory(entityhuman.defaultContainer);
                }
            }.runTaskLater(MyPetApi.getPlugin(), 5);
        }
    }
    if (isMyPet() && myPet.getOwner().equals(entityhuman)) {
        Player owner = this.getOwner().getPlayer();
        if (Configuration.Skilltree.Skill.Ride.RIDE_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(Ride.class) && canMove()) {
                if (Permissions.hasExtendedLegacy(owner, "MyPet.extended.ride")) {
                    entityhuman.mount(this);
                    return true;
                } else {
                    getOwner().sendMessage(Translation.getString("Message.No.CanUse", myPet.getOwner().getLanguage()));
                }
            }
        }
        if (Configuration.Skilltree.Skill.CONTROL_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(de.Keyle.MyPet.skill.skills.Control.class)) {
                return true;
            }
        }
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG && itemStack.hasName()) {
                if (Permissions.hasLegacy(getOwner(), "MyPet.command.name") && Permissions.hasExtended(getOwner(), "MyPet.extended.nametag")) {
                    final String name = itemStack.getName();
                    getMyPet().setPetName(name);
                    EntityMyPet.super.setCustomName("-");
                    myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Command.Name.New", myPet.getOwner()), name));
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        --itemStack.count;
                    }
                    if (itemStack.count <= 0) {
                        entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null);
                    }
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return true;
                }
            }
            if (canEat(itemStack) && canUseItem()) {
                if (owner != null && !Permissions.hasExtendedLegacy(owner, "MyPet.extended.feed")) {
                    return false;
                }
                if (this.petTargetSelector.hasGoal("DuelTarget")) {
                    BehaviorDuelTarget duelTarget = (BehaviorDuelTarget) this.petTargetSelector.getGoal("DuelTarget");
                    if (duelTarget.getDuelOpponent() != null) {
                        return true;
                    }
                }
                boolean used = false;
                double saturation = Configuration.HungerSystem.HUNGER_SYSTEM_SATURATION_PER_FEED;
                if (saturation > 0) {
                    if (myPet.getSaturation() < 100) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Eat);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            double missingSaturation = 100 - myPet.getSaturation();
                            myPet.setSaturation(myPet.getSaturation() + saturation);
                            saturation = Math.max(0, saturation - missingSaturation);
                            used = true;
                        }
                    }
                }
                if (saturation > 0) {
                    if (getHealth() < getMaxHealth()) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Heal);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            float missingHealth = getMaxHealth() - getHealth();
                            this.heal(Math.min((float) saturation, missingHealth), RegainReason.EATING);
                            used = true;
                        }
                    }
                }
                if (used) {
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        if (--itemStack.count <= 0) {
                            entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null);
                        }
                    }
                    MyPetApi.getPlatformHelper().playParticleEffect(myPet.getLocation().get().add(0, getHeadHeight(), 0), "HEART", 0.5F, 0.5F, 0.5F, 0.5F, 5, 20);
                    return true;
                }
            }
        }
    } else {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG) {
                if (itemStack.hasName()) {
                    EntityMyPet.super.setCustomName("-");
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return false;
                }
            }
        }
    }
    return false;
}
Also used : Ride(de.Keyle.MyPet.skill.skills.Ride) Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetFeedEvent(de.Keyle.MyPet.api.event.MyPetFeedEvent) CraftItemStack(org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack) Ride(de.Keyle.MyPet.skill.skills.Ride)

Example 5 with MyPetFeedEvent

use of de.Keyle.MyPet.api.event.MyPetFeedEvent in project MyPet by xXKeyleXx.

the class EntityMyPet method handlePlayerInteraction.

// Obfuscated Method handler ------------------------------------------------------------------------------------
/**
 * Is called when player rightclicks this MyPet
 * return:
 * true: there was a reaction on rightclick
 * false: no reaction on rightclick
 */
public boolean handlePlayerInteraction(final EntityHuman entityhuman) {
    ItemStack itemStack = entityhuman.inventory.getItemInHand();
    if (itemStack != null && itemStack.getItem() == Items.LEAD) {
        ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutAttachEntity(1, this, null));
        if (!entityhuman.abilities.canInstantlyBuild) {
            new BukkitRunnable() {

                public void run() {
                    ((EntityPlayer) entityhuman).updateInventory(entityhuman.defaultContainer);
                }
            }.runTaskLater(MyPetApi.getPlugin(), 5);
        }
    }
    if (isMyPet() && myPet.getOwner().equals(entityhuman)) {
        Player owner = this.getOwner().getPlayer();
        if (Configuration.Skilltree.Skill.Ride.RIDE_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(Ride.class) && canMove()) {
                if (Permissions.hasExtendedLegacy(owner, "MyPet.extended.ride")) {
                    entityhuman.mount(this);
                    return true;
                } else {
                    getOwner().sendMessage(Translation.getString("Message.No.CanUse", myPet.getOwner().getLanguage()));
                }
            }
        }
        if (Configuration.Skilltree.Skill.CONTROL_ITEM.compare(itemStack)) {
            if (myPet.getSkills().isSkillActive(de.Keyle.MyPet.skill.skills.Control.class)) {
                return true;
            }
        }
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG && itemStack.hasName()) {
                if (Permissions.hasLegacy(getOwner(), "MyPet.command.name") && Permissions.hasExtended(getOwner(), "MyPet.extended.nametag")) {
                    final String name = itemStack.getName();
                    getMyPet().setPetName(name);
                    EntityMyPet.super.setCustomName("-");
                    myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Command.Name.New", myPet.getOwner()), name));
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        --itemStack.count;
                    }
                    if (itemStack.count <= 0) {
                        entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null);
                    }
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return true;
                }
            }
            if (canEat(itemStack) && canUseItem()) {
                if (owner != null && !Permissions.hasExtendedLegacy(owner, "MyPet.extended.feed")) {
                    return false;
                }
                if (this.petTargetSelector.hasGoal("DuelTarget")) {
                    BehaviorDuelTarget duelTarget = (BehaviorDuelTarget) this.petTargetSelector.getGoal("DuelTarget");
                    if (duelTarget.getDuelOpponent() != null) {
                        return true;
                    }
                }
                boolean used = false;
                double saturation = Configuration.HungerSystem.HUNGER_SYSTEM_SATURATION_PER_FEED;
                if (saturation > 0) {
                    if (myPet.getSaturation() < 100) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Eat);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            double missingSaturation = 100 - myPet.getSaturation();
                            myPet.setSaturation(myPet.getSaturation() + saturation);
                            saturation = Math.max(0, saturation - missingSaturation);
                            used = true;
                        }
                    }
                }
                if (saturation > 0) {
                    if (getHealth() < getMaxHealth()) {
                        MyPetFeedEvent feedEvent = new MyPetFeedEvent(getMyPet(), CraftItemStack.asCraftMirror(itemStack), saturation, MyPetFeedEvent.Result.Heal);
                        Bukkit.getPluginManager().callEvent(feedEvent);
                        if (!feedEvent.isCancelled()) {
                            saturation = feedEvent.getSaturation();
                            float missingHealth = getMaxHealth() - getHealth();
                            this.heal(Math.min((float) saturation, missingHealth), RegainReason.EATING);
                            used = true;
                        }
                    }
                }
                if (used) {
                    if (!entityhuman.abilities.canInstantlyBuild) {
                        if (--itemStack.count <= 0) {
                            entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null);
                        }
                    }
                    MyPetApi.getPlatformHelper().playParticleEffect(myPet.getLocation().get().add(0, getHeadHeight(), 0), "HEART", 0.5F, 0.5F, 0.5F, 0.5F, 5, 20);
                    return true;
                }
            }
        }
    } else {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.NAME_TAG) {
                if (itemStack.hasName()) {
                    EntityMyPet.super.setCustomName("-");
                    new BukkitRunnable() {

                        public void run() {
                            updateNameTag();
                        }
                    }.runTaskLater(MyPetApi.getPlugin(), 1L);
                    return false;
                }
            }
        }
    }
    return false;
}
Also used : Ride(de.Keyle.MyPet.skill.skills.Ride) Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetFeedEvent(de.Keyle.MyPet.api.event.MyPetFeedEvent) CraftItemStack(org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack) Ride(de.Keyle.MyPet.skill.skills.Ride)

Aggregations

MyPetFeedEvent (de.Keyle.MyPet.api.event.MyPetFeedEvent)9 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)9 Ride (de.Keyle.MyPet.skill.skills.Ride)9 Player (org.bukkit.entity.Player)9 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)9 CraftPlayer (org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer)1 CraftPlayer (org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer)1 CraftPlayer (org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer)1 CraftPlayer (org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer)1 CraftItemStack (org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack)1 CraftItemStack (org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack)1 CraftItemStack (org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack)1 CraftItemStack (org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack)1 CraftPlayer (org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer)1 CraftPlayer (org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer)1