@Override public boolean onEnable() { try { // reverse dragon facing direction if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) { registerEnderDragonFix_post_1_9(); } else { registerEnderDragonFix(); } if (MyPetApi.getCompatUtil().getInternalVersion().equals("v1_7_R4")) { boolean activate = true; try { Class.forName("org.spigotmc.SpigotConfig"); } catch (Throwable throwable) { activate = false; } if (activate) { registerCompatFix_1_8(); } } return true; } catch (Exception e) { return false; } }
public static void registerSkill(Class<? extends SkillTreeSkill> clazz) { if (!SkillInstance.class.isAssignableFrom(clazz)) { MyPetApi.getLogger() .warning(ChatColor.RED + clazz.getName() + " doesn't implements [ISkillInstance]!"); return; } if (!registeredSkillsNames.containsKey(clazz)) { try { // MyPetApi.getLogger().warning("Skill Annotations: " + // Arrays.toString(clazz.getAnnotations())); SkillName sn = clazz.getAnnotation(SkillName.class); if (sn != null) { String skillName = sn.value(); if (!registeredNamesSkills.containsKey(skillName)) { registeredSkillsNames.put(clazz, skillName); // DebugLogger.info("registered skill: " + clazz.getName()); } else { MyPetApi.getLogger() .warning( ChatColor.RED + "There is already a skill registered with the the name " + skillName); } } else { MyPetApi.getLogger() .warning(ChatColor.RED + clazz.getName() + " is not annotated with [SkillName]!"); } } catch (Exception e) { MyPetApi.getLogger().warning(ChatColor.RED + clazz.getName() + " is not a valid skill!"); } } }
@Override public boolean onCommandOption(CommandSender sender, String[] args) { try { File ticketFile = new File(MyPetApi.getPlugin().getDataFolder(), "ticket.zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(ticketFile)); addFileToZip(new File(MyPetApi.getPlugin().getDataFolder(), "config.yml"), out, ""); addFileToZip(new File(MyPetApi.getPlugin().getDataFolder(), "pet-config.yml"), out, ""); addFileToZip(new File(MyPetApi.getPlugin().getDataFolder(), "My.Pets.old"), out, ""); addFileToZip(new File(MyPetApi.getPlugin().getDataFolder(), "pets.db"), out, ""); addFileToZip(new File(MyPetApi.getPlugin().getDataFolder(), "worldgroups.yml"), out, ""); addFileToZip(new File(MyPetApi.getPlugin().getDataFolder(), "skilltrees"), out, ""); addFileToZip( new File(MyPetApi.getPlugin().getDataFolder(), "logs" + File.separator + "MyPet.log"), out, ""); addFileToZip( new File( MyPetApi.getPlugin().getDataFolder().getParentFile().getParentFile(), "logs" + File.separator + "latest.log"), out, ""); out.close(); sender.sendMessage(ChatColor.RED + "------------------------------------------------"); sender.sendMessage( "Ticket file created. Please upload this file somewhere and add the link to your ticket."); sender.sendMessage(" " + ticketFile.getAbsoluteFile()); sender.sendMessage(ChatColor.RED + "------------------------------------------------"); } catch (IOException e) { e.printStackTrace(); } return true; }
@Override public boolean onCommandOption(CommandSender sender, String[] args) { if (sender instanceof Player && MyPetApi.getPlayerManager().isMyPetPlayer((Player) sender)) { MyPetPlayer myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer((Player) sender); myPetPlayer.setHealthBarActive(!myPetPlayer.isHealthBarActive()); sender.sendMessage(Translation.getString("Message.Command.Success", sender)); return true; } sender.sendMessage(Translation.getString("Message.Command.Fail", sender)); return true; }
@SuppressWarnings("unchecked") public void registerEntityType( MyPetType type, Class<? extends MyPetMinecraftEntity> entityClass) { try { Field EntityTypes_d = ReflectionUtil.getField(EntityTypes.class, "d"); Field EntityTypes_f = ReflectionUtil.getField(EntityTypes.class, "f"); Map<Class, String> d = (Map) EntityTypes_d.get(EntityTypes_d); Map<Class, Integer> f = (Map) EntityTypes_f.get(EntityTypes_f); Iterator cIterator = d.keySet().iterator(); while (cIterator.hasNext()) { Class clazz = (Class) cIterator.next(); if (clazz.getCanonicalName().equals(entityClass.getCanonicalName())) { cIterator.remove(); } } Iterator eIterator = f.keySet().iterator(); while (eIterator.hasNext()) { Class clazz = (Class) eIterator.next(); if (clazz.getCanonicalName().equals(entityClass.getCanonicalName())) { eIterator.remove(); } } d.put(entityClass, type.getMinecraftName()); f.put(entityClass, type.getTypeID()); } catch (Exception e) { MyPetApi.getLogger().warning("Error while registering " + entityClass.getCanonicalName()); } }
@Override @SuppressWarnings("unchecked") public void unregisterEntityTypes() { try { Field EntityTypes_d = ReflectionUtil.getField(EntityTypes.class, "d"); Field EntityTypes_f = ReflectionUtil.getField(EntityTypes.class, "f"); Map<Class, String> d = (Map) EntityTypes_d.get(EntityTypes_d); Map<Class, Integer> f = (Map) EntityTypes_f.get(EntityTypes_f); Iterator dIterator = d.keySet().iterator(); while (dIterator.hasNext()) { Class clazz = (Class) dIterator.next(); if (clazz.getCanonicalName().startsWith("de.Keyle.MyPet")) { dIterator.remove(); } } Iterator fIterator = f.keySet().iterator(); while (fIterator.hasNext()) { Class clazz = (Class) fIterator.next(); if (clazz.getCanonicalName().startsWith("de.Keyle.MyPet")) { fIterator.remove(); } } } catch (Exception e) { MyPetApi.getLogger().warning("Error while unregistering MyPet entities"); } }
@Override public boolean comparePlayerWithEntity(MyPetPlayer player, Object obj) { EntityHuman entityHuman = (EntityHuman) obj; if (MyPetApi.getPlugin().isInOnlineMode()) { return player.getPlayerUUID().equals(entityHuman.getUniqueID()); } else { return entityHuman.getName().equals(player.getName()); } }
@Override public TagCompound writeExtendedInfo() { TagCompound info = super.writeExtendedInfo(); if (hasSaddle()) { info.getCompoundData() .put("Saddle", MyPetApi.getPlatformHelper().itemStackToCompund(getSaddle())); } info.getCompoundData().put("Baby", new TagByte(isBaby())); return info; }
@Override public List<String> onTabComplete( CommandSender sender, Command command, String s, String[] strings) { if (sender instanceof Player) { if (MyPetApi.getMyPetManager().hasActiveMyPet((Player) sender)) { return storeList; } } return CommandAdmin.EMPTY_LIST; }
public void addSkill(Class<? extends SkillTreeSkill> skillClass) { if (!isValidSkill(skillClass)) { MyPetApi.getLogger().warning(ChatColor.RED + skillClass.getName() + " is not a valid skill!"); } try { Constructor<?> ctor = skillClass.getConstructor(boolean.class); Object obj = ctor.newInstance(false); if (obj instanceof SkillInstance) { SkillInstance skill = (SkillInstance) obj; String skillName = skill.getName(); skill.setMyPet(this.myPet); skillsNamesClass.put(skillName, skill); } } catch (Exception e) { MyPetApi.getLogger().warning(ChatColor.RED + skillClass.getName() + " is not a valid skill!"); e.printStackTrace(); registeredSkillsNames.remove(skillClass); } }
public static SkillInstance getNewSkillInstance( Class<? extends SkillTreeSkill> clazz, boolean is) { if (clazz == null) { return null; } try { Constructor<?> ctor = clazz.getConstructor(boolean.class); Object obj = ctor.newInstance(is); return (SkillInstance) obj; } catch (Exception e) { MyPetApi.getLogger().warning(ChatColor.RED + clazz.getName() + " is not a valid skill)!"); e.printStackTrace(); } return null; }
@Override public void readExtendedInfo(TagCompound info) { if (info.containsKeyAs("Saddle", TagByte.class)) { boolean saddle = info.getAs("Saddle", TagByte.class).getBooleanData(); if (saddle) { ItemStack item = new ItemStack(Material.SADDLE); setSaddle(item); } } else if (info.containsKeyAs("Saddle", TagCompound.class)) { TagCompound itemTag = info.get("Saddle"); ItemStack item = MyPetApi.getPlatformHelper().compundToItemStack(itemTag); setSaddle(item); } if (info.getCompoundData().containsKey("Baby")) { setBaby(info.getAs("Baby", TagByte.class).getBooleanData()); } }
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); } } } }); }
@Override public MyPetMinecraftEntity createMinecraftEntity(MyPet pet, org.bukkit.World bukkitWorld) { EntityMyPet petEntity = null; Class<? extends MyPetMinecraftEntity> entityClass = entityClasses.get(pet.getPetType()); World world = ((CraftWorld) bukkitWorld).getHandle(); try { Constructor<?> ctor = entityClass.getConstructor(World.class, MyPet.class); Object obj = ctor.newInstance(world, pet); if (obj instanceof EntityMyPet) { petEntity = (EntityMyPet) obj; } } catch (Exception e) { MyPetApi.getLogger() .info(ChatColor.RED + entityClass.getName() + " is no valid MyPet(Entity)!"); e.printStackTrace(); } return petEntity; }
public boolean onCommand( final CommandSender sender, Command command, String label, String[] args) { if (!(sender instanceof Player)) { sender.sendMessage("You can't use this command from server console!"); return true; } Player player = (Player) sender; if (!Permissions.hasLegacy(player, "MyPet.command.switch")) { player.sendMessage(Translation.getString("Message.No.Allowed", player)); return true; } if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) { final MyPetPlayer owner = MyPetApi.getPlayerManager().getMyPetPlayer(player); MyPetApi.getRepository() .getMyPets( owner, new RepositoryCallback<List<StoredMyPet>>() { @Override public void callback(List<StoredMyPet> pets) { if (pets.size() - (owner.hasMyPet() ? 1 : 0) == 0) { owner.sendMessage( Translation.getString("Message.Command.Switch.NoStoredPets", owner)); return; } if (owner.isOnline()) { String worldGroup = WorldGroup.getGroupByWorld(owner.getPlayer().getWorld().getName()) .getName(); int inactivePetCount = getInactivePetCount(pets, worldGroup); int maxPetCount = getMaxPetCount(owner.getPlayer()); String title; if (owner.hasMyPet()) { inactivePetCount--; title = Translation.getString("Message.Npc.SwitchTitle", owner); } else { title = Translation.getString("Message.SelectMyPet", owner); } String stats = "(" + inactivePetCount + "/" + maxPetCount + ")"; final MyPetSelectionGui gui = new MyPetSelectionGui(owner, title + " " + stats); gui.open( pets, new RepositoryCallback<StoredMyPet>() { @Override public void callback(StoredMyPet storedMyPet) { Optional<MyPet> activePet = MyPetApi.getMyPetManager().activateMyPet(storedMyPet); if (activePet.isPresent() && owner.isOnline()) { Player player = owner.getPlayer(); activePet .get() .getOwner() .sendMessage( Util.formatText( Translation.getString("Message.Npc.ChosenPet", owner), activePet.get().getPetName())); WorldGroup wg = WorldGroup.getGroupByWorld(player.getWorld().getName()); owner.setMyPetForWorldGroup(wg, activePet.get().getUUID()); switch (activePet.get().createEntity()) { case Canceled: owner.sendMessage( Util.formatText( Translation.getString("Message.Spawn.Prevent", owner), activePet.get().getPetName())); break; case NoSpace: owner.sendMessage( Util.formatText( Translation.getString("Message.Spawn.NoSpace", owner), activePet.get().getPetName())); break; case NotAllowed: owner.sendMessage( Util.formatText( Translation.getString("Message.No.AllowedHere", owner), activePet.get().getPetName())); break; case Dead: owner.sendMessage( Util.formatText( Translation.getString("Message.Spawn.Respawn.In", owner), activePet.get().getPetName(), activePet.get().getRespawnTime())); break; case Spectator: sender.sendMessage( Util.formatText( Translation.getString("Message.Spawn.Spectator", owner), activePet.get().getPetName())); break; } } } }); } } }); } else { sender.sendMessage(Translation.getString("Message.No.HasPet", player)); } return true; }
@Override public void onDisable() { ProtocolLibrary.getProtocolManager().removePacketListeners(MyPetApi.getPlugin()); }
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; } } }); }