use of de.Keyle.MyPet.api.util.hooks.types.LeashHook in project MyPet by xXKeyleXx.
the class EntityListener method on.
@EventHandler(priority = EventPriority.MONITOR)
public void on(final EntityDamageByEntityEvent event) {
// noinspection ConstantConditions
if (event.getEntity() == null) {
// catch invalid events (i.e. EnchantmentAPI)
return;
}
if (WorldGroup.getGroupByWorld(event.getEntity().getWorld()).isDisabled()) {
return;
}
if (!event.getEntity().isDead() && !(event.getEntity() instanceof MyPetBukkitEntity)) {
if (MyPetApi.getMyPetInfo().isLeashableEntityType(event.getEntity().getType())) {
ItemStack leashItem = null;
ItemStack leashItemArrow = null;
Player player;
if (Configuration.Misc.ALLOW_RANGED_LEASHING && event.getDamager() instanceof Projectile) {
Projectile projectile = (Projectile) event.getDamager();
if (!(projectile.getShooter() instanceof Player)) {
return;
}
player = (Player) projectile.getShooter();
List<MetadataValue> metaList;
if (projectile.hasMetadata("MyPetLeashItem")) {
metaList = projectile.getMetadata("MyPetLeashItem");
for (MetadataValue meta : metaList) {
if (meta.getOwningPlugin().getName().equals("MyPet")) {
leashItem = (ItemStack) meta.value();
break;
}
}
if (leashItem == null) {
return;
}
projectile.removeMetadata("MyPetLeashItem", MyPetApi.getPlugin());
}
if (projectile.hasMetadata("MyPetLeashItemArrow")) {
metaList = projectile.getMetadata("MyPetLeashItemArrow");
for (MetadataValue meta : metaList) {
if (meta.getOwningPlugin().getName().equals("MyPet")) {
leashItemArrow = (ItemStack) meta.value();
break;
}
}
if (leashItemArrow == null) {
return;
}
projectile.removeMetadata("MyPetLeashItemArrow", MyPetApi.getPlugin());
}
} else if (event.getDamager() instanceof Player) {
player = (Player) event.getDamager();
if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
leashItem = player.getEquipment().getItemInMainHand();
} else {
leashItem = player.getItemInHand();
}
} else {
return;
}
if (!MyPetApi.getMyPetManager().hasActiveMyPet(player) && !justLeashed.contains(player.getUniqueId())) {
LivingEntity leashTarget = (LivingEntity) event.getEntity();
MyPetType petType = MyPetType.byEntityTypeName(leashTarget.getType().name());
ConfigItem neededLeashItem = MyPetApi.getMyPetInfo().getLeashItem(petType);
if (!Permissions.has(player, "MyPet.leash." + petType.name())) {
return;
}
boolean usedArrow = false;
if (!neededLeashItem.compare(leashItem)) {
if (leashItemArrow == null || !neededLeashItem.compare(leashItemArrow)) {
return;
} else {
usedArrow = true;
}
}
for (LeashHook hook : MyPetApi.getPluginHookManager().getHooks(LeashHook.class)) {
if (!hook.canLeash(player, leashTarget)) {
return;
}
}
boolean willBeLeashed = true;
for (Settings flagSettings : MyPetApi.getMyPetInfo().getLeashFlagSettings(petType)) {
String flagName = flagSettings.getName();
LeashFlag flag = MyPetApi.getLeashFlagManager().getLeashFlag(flagName);
if (flag == null) {
MyPetApi.getLogger().warning("\"" + flagName + "\" is not a valid leash requirement!");
continue;
}
MyPetPlayer myPetPlayer = null;
if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(player);
}
if (!flag.check(player, leashTarget, event.getDamage(), flagSettings)) {
willBeLeashed = false;
if (myPetPlayer != null) {
if (myPetPlayer.isCaptureHelperActive()) {
String message = flag.getMissingMessage(player, leashTarget, event.getDamage(), flagSettings);
if (message != null) {
myPetPlayer.sendMessage(LeashFlag.getMessagePrefix(false) + message, 10000);
}
}
}
} else {
if (myPetPlayer != null) {
if (myPetPlayer.isCaptureHelperActive()) {
String message = flag.getMissingMessage(player, leashTarget, event.getDamage(), flagSettings);
if (message != null) {
myPetPlayer.sendMessage(LeashFlag.getMessagePrefix(true) + message, 10000);
}
}
}
}
}
if (willBeLeashed) {
event.setCancelled(true);
final MyPetPlayer owner;
if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
} else {
owner = MyPetApi.getPlayerManager().registerMyPetPlayer(player);
}
final InactiveMyPet inactiveMyPet = new InactiveMyPet(owner);
inactiveMyPet.setPetType(petType);
inactiveMyPet.setPetName(Translation.getString("Name." + petType.name(), inactiveMyPet.getOwner()));
WorldGroup worldGroup = WorldGroup.getGroupByWorld(player.getWorld().getName());
inactiveMyPet.setWorldGroup(worldGroup.getName());
inactiveMyPet.getOwner().setMyPetForWorldGroup(worldGroup, inactiveMyPet.getUUID());
/*
if(leashTarget.getCustomName() != null)
{
inactiveMyPet.setPetName(leashTarget.getCustomName());
}
*/
Optional<EntityConverterService> converter = MyPetApi.getServiceManager().getService(EntityConverterService.class);
converter.ifPresent(service -> inactiveMyPet.setInfo(service.convertEntity(leashTarget)));
boolean remove = true;
for (LeashEntityHook hook : MyPetApi.getPluginHookManager().getHooks(LeashEntityHook.class)) {
if (!hook.prepare(leashTarget)) {
remove = false;
}
}
if (remove) {
leashTarget.remove();
}
if (!usedArrow) {
if (Configuration.Misc.CONSUME_LEASH_ITEM && player.getGameMode() != GameMode.CREATIVE && leashItem != null) {
if (leashItem.getAmount() > 1) {
leashItem.setAmount(leashItem.getAmount() - 1);
} else {
if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
player.getEquipment().setItemInMainHand(null);
} else {
player.setItemInHand(null);
}
}
}
}
MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.Leash);
Bukkit.getServer().getPluginManager().callEvent(createEvent);
MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
Bukkit.getServer().getPluginManager().callEvent(saveEvent);
justLeashed.add(player.getUniqueId());
MyPetApi.getPlugin().getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {
@Override
public void callback(Boolean value) {
owner.sendMessage(Translation.getString("Message.Leash.Add", owner));
Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
myPet.ifPresent(MyPet::createEntity);
if (owner.isCaptureHelperActive()) {
owner.setCaptureHelperActive(false);
owner.sendMessage(Util.formatText(Translation.getString("Message.Command.CaptureHelper.Mode", owner), Translation.getString("Name.Disabled", owner)));
}
justLeashed.remove(player.getUniqueId());
}
});
}
}
}
}
}
use of de.Keyle.MyPet.api.util.hooks.types.LeashHook in project MyPet by xXKeyleXx.
the class MyPetPlayerImpl method schedule.
public void schedule() {
if (!isOnline()) {
return;
}
long currentTime = System.currentTimeMillis();
sentMessages.keySet().removeIf(message -> currentTime >= sentMessages.get(message));
if (hasMyPet()) {
MyPet myPet = getMyPet();
Player p = this.getPlayer();
if (myPet.getStatus() == PetState.Here) {
if (myPet.getLocation().get().getWorld() != p.getLocation().getWorld() || MyPetApi.getPlatformHelper().distance(myPet.getLocation().get(), p.getLocation()) > 40) {
myPet.removePet(Configuration.Misc.RECALL_PET_AFTER_DESPAWN);
myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.Despawn", myPet.getOwner()), myPet.getPetName()));
}
if (!Configuration.Misc.DISABLE_ALL_ACTIONBAR_MESSAGES && showHealthBar) {
String msg = myPet.getPetName() + ChatColor.RESET + ": ";
if (myPet.getHealth() > myPet.getMaxHealth() / 3 * 2) {
msg += ChatColor.GREEN;
} else if (myPet.getHealth() > myPet.getMaxHealth() / 3) {
msg += ChatColor.YELLOW;
} else {
msg += ChatColor.RED;
}
msg += String.format("%1.2f", myPet.getHealth()) + ChatColor.WHITE + "/" + String.format("%1.2f", myPet.getMaxHealth());
MyPetApi.getPlatformHelper().sendMessageActionBar(getPlayer(), msg);
}
} else if (myPet.getStatus() == PetState.Despawned) {
if (myPet.wantsToRespawn() && !p.isFlying()) {
boolean velocity = p.getVelocity().getY() >= 0;
boolean fall = p.getFallDistance() == 0;
if (velocity || fall || p.isOnGround()) {
boolean spawn = true;
if (velocity) {
spawn = !p.isInsideVehicle();
if (spawn && MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
spawn = !p.isGliding();
}
}
if (spawn && fall) {
switch(p.getWorld().getBlockAt(p.getLocation().subtract(0, 0.5, 0)).getType().name()) {
case "AIR":
case "CAVE_AIR":
case "VOID_AIR":
case "WATER":
case "STATIONARY_WATER":
case "LAVA":
case "STATIONARY_LAVA":
spawn = false;
}
}
if (spawn && myPet.createEntity() == MyPet.SpawnFlags.Success) {
p.sendMessage(Util.formatText(Translation.getString("Message.Command.Call.Success", p), myPet.getPetName()));
}
}
}
}
}
if (isCaptureHelperActive()) {
if (captureHelperTimer-- <= 0) {
setCaptureHelperActive(false);
}
Player p = getPlayer();
List<Entity> entities = p.getNearbyEntities(7, 7, 7);
int count = 0;
entityLoop: for (Entity entity : entities) {
if (entity instanceof LivingEntity && !(entity instanceof Player) && !(entity instanceof MyPetBukkitEntity)) {
if (MyPetApi.getMyPetInfo().isLeashableEntityType(entity.getType())) {
for (LeashHook hook : MyPetApi.getPluginHookManager().getHooks(LeashHook.class)) {
if (!hook.canLeash(p, entity)) {
continue entityLoop;
}
}
if (!MyPetApi.getHookHelper().canHurt(p, entity)) {
continue;
}
if (!Permissions.has(this, "MyPet.leash." + MyPetType.byEntityTypeName(entity.getType().name()))) {
continue;
}
Location l = entity.getLocation();
l.add(0, ((LivingEntity) entity).getEyeHeight(true) + 1, 0);
if (checkTamable((LivingEntity) entity, p)) {
MyPetApi.getPlatformHelper().playParticleEffect(p, l, ParticleCompat.ITEM_CRACK.get(), 0, 0, 0, 0.02f, 20, 100, ParticleCompat.LIME_GREEN_WOOL_DATA);
} else {
MyPetApi.getPlatformHelper().playParticleEffect(p, l, ParticleCompat.ITEM_CRACK.get(), 0, 0, 0, 0.02f, 20, 100, ParticleCompat.RED_WOOL_DATA);
}
if (count++ > 20) {
break;
}
}
}
}
}
}
Aggregations