use of de.Keyle.MyPet.api.entity.MyPetBukkitEntity in project MyPet by xXKeyleXx.
the class LevelUpListener method on.
@EventHandler
public void on(MyPetLevelUpEvent event) {
MyPet myPet = event.getPet();
int lvl = event.getLevel();
int lastLvl = event.getLastLevel();
if (!event.isQuiet()) {
int maxlevel = myPet.getSkilltree() != null ? myPet.getSkilltree().getMaxLevel() : 0;
if (maxlevel != 0 && lvl >= maxlevel) {
myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.LevelSystem.ReachedMaxLevel", event.getOwner().getLanguage()), myPet.getPetName(), maxlevel));
} else {
myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.LevelSystem.LevelUp", event.getOwner().getLanguage()), myPet.getPetName(), event.getLevel()));
}
}
SkillTree skillTree = myPet.getSkilltree();
if (skillTree != null) {
if (skillTree.getLastLevelWithSkills() < lvl) {
lvl = skillTree.getLastLevelWithSkills();
}
for (int i = lastLvl + 1; i <= lvl; i++) {
if (skillTree.hasLevel(i)) {
SkillTreeLevel level = skillTree.getLevel(i);
if (!event.isQuiet()) {
if (level.hasLevelupMessage()) {
myPet.getOwner().sendMessage(Colorizer.setColors(level.getLevelupMessage()));
}
}
List<SkillInfo> skillList = level.getSkills();
for (SkillInfo skill : skillList) {
myPet.getSkills().getSkill(skill.getName()).upgrade(skill, event.isQuiet());
}
}
}
}
if (myPet.getStatus() == MyPet.PetState.Here) {
MyPetBukkitEntity entity = myPet.getEntity().get();
entity.getHandle().updateNameTag();
if (!event.isQuiet()) {
myPet.setHealth(myPet.getMaxHealth());
myPet.setSaturation(100);
final boolean version17 = MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.8") < 0;
new SpiralAnimation(1, entity.getEyeHeight() + 0.5, new EntityLocationHolder(entity)) {
@Override
protected void playParticleEffect(Location location) {
if (version17) {
MyPetApi.getPlatformHelper().playParticleEffect(location, "magicCrit", 0, 0, 0, 0, 1, 32);
} else {
MyPetApi.getPlatformHelper().playParticleEffect(location, "CRIT_MAGIC", 0, 0, 0, 0, 1, 32);
}
// MyPetApi.getPlatformHelper().playParticleEffect(location, "flame", 0, 0, 0, 0, 1, 32);
}
}.loop(2);
if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
entity.getWorld().playSound(entity.getLocation(), "entity.player.levelup", 1F, 0.7F);
} else {
entity.getWorld().playSound(entity.getLocation(), Sound.valueOf("LEVEL_UP"), 1F, 0.7F);
}
}
}
}
use of de.Keyle.MyPet.api.entity.MyPetBukkitEntity in project MyPet by xXKeyleXx.
the class ProtocolLibHook method registerCompatFix_1_8.
private static void registerCompatFix_1_8() {
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(MyPetApi.getPlugin(), ListenerPriority.HIGHEST, PacketType.Play.Server.SPAWN_ENTITY_LIVING, PacketType.Play.Server.ENTITY_METADATA) {
Class entityClass = ReflectionUtil.getClass("org.bukkit.craftbukkit." + MyPetApi.getCompatUtil().getInternalVersion() + ".entity.CraftEntity");
Method getHandleMethod = ReflectionUtil.getMethod(entityClass, "getHandle");
private final EnumMap<DyeColor, Integer> convertedDyeColors = new EnumMap<DyeColor, Integer>(DyeColor.class) {
{
put(DyeColor.WHITE, 15);
put(DyeColor.ORANGE, 14);
put(DyeColor.MAGENTA, 13);
put(DyeColor.LIGHT_BLUE, 12);
put(DyeColor.YELLOW, 11);
put(DyeColor.LIME, 10);
put(DyeColor.PINK, 9);
put(DyeColor.GRAY, 8);
put(DyeColor.SILVER, 7);
put(DyeColor.CYAN, 6);
put(DyeColor.PURPLE, 5);
put(DyeColor.BLUE, 4);
put(DyeColor.BROWN, 3);
put(DyeColor.GREEN, 2);
put(DyeColor.RED, 1);
put(DyeColor.BLACK, 0);
}
};
@Override
public void onPacketSending(PacketEvent event) {
if (event.isCancelled()) {
return;
}
Player player = event.getPlayer();
if (!isPlayerRunningv1_8(player)) {
return;
}
PacketContainer newPacketContainer = event.getPacket().deepClone();
event.setPacket(newPacketContainer);
if (event.getPacketType() == PacketType.Play.Server.SPAWN_ENTITY_LIVING) {
Entity entity = newPacketContainer.getEntityModifier(event).readSafely(0);
if (entity != null && entity instanceof MyPetBukkitEntity) {
MyPetBukkitEntity petEntity = (MyPetBukkitEntity) entity;
List<WrappedWatchableObject> wrappedWatchableObjectList = newPacketContainer.getDataWatcherModifier().read(0).getWatchableObjects();
newPacketContainer.getDataWatcherModifier().write(0, new WrappedDataWatcher(fixMetadata(petEntity, wrappedWatchableObjectList)));
}
} else if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) {
Entity entity = newPacketContainer.getEntityModifier(event).read(0);
if (entity != null && entity instanceof MyPetBukkitEntity) {
MyPetBukkitEntity petEntity = (MyPetBukkitEntity) entity;
List<WrappedWatchableObject> wrappedWatchableObjectList = newPacketContainer.getWatchableCollectionModifier().read(0);
newPacketContainer.getWatchableCollectionModifier().write(0, fixMetadata(petEntity, wrappedWatchableObjectList));
}
}
}
private List<WrappedWatchableObject> fixMetadata(MyPetBukkitEntity petEntity, List<WrappedWatchableObject> wrappedWatchableObjectList) {
if (petEntity == null || wrappedWatchableObjectList == null) {
return wrappedWatchableObjectList;
}
if (petEntity.getMyPet() instanceof MyPetBaby && hasKey(12, wrappedWatchableObjectList)) {
Object object = getKeyValue(12, wrappedWatchableObjectList);
if (object instanceof Integer) {
int value = ((Number) object).intValue();
removeKey(12, wrappedWatchableObjectList);
if (petEntity.getPetType() == MyPetType.Horse) {
if (value == -24000) {
value = -1;
}
}
wrappedWatchableObjectList.add(new WrappedWatchableObject(12, (byte) value));
}
}
if (petEntity.getPetType() == MyPetType.Wolf && hasKey(20, wrappedWatchableObjectList)) {
Object object = getKeyValue(20, wrappedWatchableObjectList);
if (object instanceof Byte) {
DyeColor color = DyeColor.getByWoolData((byte) ((Byte) object & 0xF));
removeKey(20, wrappedWatchableObjectList);
wrappedWatchableObjectList.add(new WrappedWatchableObject(20, (byte) ((this.convertedDyeColors.get(color)) & 0xF)));
}
}
if (petEntity.getPetType() == MyPetType.Enderman && hasKey(16, wrappedWatchableObjectList)) {
Object object = getKeyValue(16, wrappedWatchableObjectList);
if (object instanceof Byte) {
removeKey(16, wrappedWatchableObjectList);
wrappedWatchableObjectList.add(new WrappedWatchableObject(16, Short.valueOf((Byte) object)));
}
}
return wrappedWatchableObjectList;
}
private boolean hasKey(int key, List<WrappedWatchableObject> wrappedWatchableObjectList) {
for (WrappedWatchableObject next : wrappedWatchableObjectList) {
if (next.getIndex() == key) {
return true;
}
}
return false;
}
private Object getKeyValue(int key, List<WrappedWatchableObject> wrappedWatchableObjectList) {
for (WrappedWatchableObject next : wrappedWatchableObjectList) {
if (next.getIndex() == key) {
return next.getValue();
}
}
return null;
}
private void removeKey(int key, List<WrappedWatchableObject> wrappedWatchableObjectList) {
for (Iterator<WrappedWatchableObject> wrappedWatchableObjectIterator = wrappedWatchableObjectList.iterator(); wrappedWatchableObjectIterator.hasNext(); ) {
WrappedWatchableObject next = wrappedWatchableObjectIterator.next();
if (next.getIndex() == key) {
wrappedWatchableObjectIterator.remove();
break;
}
}
}
@SuppressWarnings("unchecked")
private boolean isPlayerRunningv1_8(Player player) {
try {
Object nmsPlayer = getHandleMethod.invoke(player);
Object playerConnection = ReflectionUtil.getFieldValue(nmsPlayer.getClass(), nmsPlayer, "playerConnection");
Object networkManager = ReflectionUtil.getFieldValue(playerConnection.getClass(), playerConnection, "networkManager");
Method getVersionMethod = ReflectionUtil.getMethod(networkManager.getClass(), "getVersion");
return (Integer) getVersionMethod.invoke(networkManager) > 5;
} catch (Exception exception) {
return false;
}
}
});
}
use of de.Keyle.MyPet.api.entity.MyPetBukkitEntity in project MyPet by xXKeyleXx.
the class ProtocolLibHook method registerEnderDragonFix.
private static void registerEnderDragonFix() {
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(MyPetApi.getPlugin(), PacketType.Play.Server.ENTITY_LOOK, PacketType.Play.Server.ENTITY_MOVE_LOOK, PacketType.Play.Server.ENTITY_TELEPORT) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
final Entity entity = packet.getEntityModifier(event).readSafely(0);
// Now - are we dealing with an invisible slime?
if (entity != null && entity instanceof MyPetBukkitEntity && ((MyPetBukkitEntity) entity).getPetType() == MyPetType.EnderDragon) {
if (packet.getType() == PacketType.Play.Server.ENTITY_LOOK) {
// MyPetLogger.write("ENTITY_LOOK: " + packet.getBytes().getValues());
byte angle = packet.getBytes().read(3);
angle += Byte.MAX_VALUE;
packet.getBytes().write(3, angle);
} else if (packet.getType() == PacketType.Play.Server.ENTITY_MOVE_LOOK) {
// MyPetLogger.write("ENTITY_MOVE_LOOK: " + packet.getBytes().getValues());
byte angle = packet.getBytes().read(3);
angle += Byte.MAX_VALUE;
packet.getBytes().write(3, angle);
} else if (packet.getType() == PacketType.Play.Server.ENTITY_TELEPORT) {
// MyPetLogger.write("ENTITY_TELEPORT: " + packet.getBytes().getValues());
byte angle = packet.getBytes().read(1);
angle += Byte.MAX_VALUE;
packet.getBytes().write(1, angle);
}
}
}
});
}
use of de.Keyle.MyPet.api.entity.MyPetBukkitEntity in project MyPet by xXKeyleXx.
the class ProtocolLibHook method registerEnderDragonFix_post_1_9.
private static void registerEnderDragonFix_post_1_9() {
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(MyPetApi.getPlugin(), PacketType.Play.Server.ENTITY_LOOK, PacketType.Play.Server.ENTITY_MOVE_LOOK, PacketType.Play.Server.ENTITY_TELEPORT) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
final Entity entity = packet.getEntityModifier(event).readSafely(0);
if (entity != null && entity instanceof MyPetBukkitEntity && ((MyPetBukkitEntity) entity).getPetType() == MyPetType.EnderDragon) {
if (packet.getType() == PacketType.Play.Server.ENTITY_LOOK) {
// MyPetLogger.write("ENTITY_LOOK: " + packet.getBytes().getValues());
byte angle = packet.getBytes().read(0);
angle += Byte.MAX_VALUE;
packet.getBytes().write(0, angle);
} else if (packet.getType() == PacketType.Play.Server.ENTITY_MOVE_LOOK) {
// MyPetLogger.write("ENTITY_MOVE_LOOK: " + packet.getBytes().getValues());
byte angle = packet.getBytes().read(0);
angle += Byte.MAX_VALUE;
packet.getBytes().write(0, angle);
} else if (packet.getType() == PacketType.Play.Server.ENTITY_TELEPORT) {
// MyPetLogger.write("ENTITY_TELEPORT: " + packet.getBytes().getValues());
byte angle = packet.getBytes().read(1);
angle += Byte.MAX_VALUE;
packet.getBytes().write(1, angle);
}
}
}
});
}
use of de.Keyle.MyPet.api.entity.MyPetBukkitEntity in project MyPet by xXKeyleXx.
the class Stomp method stomp.
public void stomp(Location location) {
location.getWorld().playEffect(location, Effect.STEP_SOUND, Material.BEDROCK);
if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
location.getWorld().playSound(location, Sound.ENTITY_HOSTILE_BIG_FALL, 0.9F, 0.7F);
} else {
location.getWorld().playSound(location, Sound.valueOf("FALL_BIG"), 0.9F, 0.7F);
}
double posX = location.getX();
double posY = location.getY();
double posZ = location.getZ();
for (Entity e : myPet.getEntity().get().getNearbyEntities(2.5, 2.5, 2.5)) {
if (e instanceof LivingEntity) {
final LivingEntity livingEntity = (LivingEntity) e;
if (livingEntity instanceof Player) {
Player targetPlayer = (Player) livingEntity;
if (myPet.getOwner().equals(targetPlayer)) {
continue;
} else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetPlayer, true)) {
continue;
}
} else if (livingEntity instanceof Tameable) {
Tameable tameable = (Tameable) livingEntity;
if (tameable.isTamed() && tameable.getOwner() != null) {
AnimalTamer tameableOwner = tameable.getOwner();
if (myPet.getOwner().equals(tameableOwner)) {
continue;
} else {
if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), livingEntity)) {
continue;
}
}
}
} else if (livingEntity instanceof MyPetBukkitEntity) {
MyPet targetMyPet = ((MyPetBukkitEntity) livingEntity).getMyPet();
if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetMyPet.getOwner().getPlayer(), true)) {
continue;
}
}
if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), livingEntity)) {
continue;
}
((LivingEntity) e).damage(this.damage, myPet.getEntity().get());
double distancePercent = MyPetApi.getPlatformHelper().distance(livingEntity.getLocation(), new Location(livingEntity.getWorld(), posX, posY, posZ)) / 2.5;
if (distancePercent <= 1.0D) {
double distanceX = livingEntity.getLocation().getX() - posX;
double distanceY = livingEntity.getLocation().getX() + livingEntity.getEyeHeight() - posY;
double distanceZ = livingEntity.getLocation().getX() - posZ;
double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY + distanceZ * distanceZ);
if (distance != 0.0D) {
double motFactor = (1.0D - distancePercent);
final Vector velocity = livingEntity.getVelocity();
velocity.multiply(motFactor);
new BukkitRunnable() {
@Override
public void run() {
livingEntity.setVelocity(velocity);
}
}.runTaskLater(MyPetApi.getPlugin(), 0);
}
}
}
}
}
Aggregations