Automagically copy and delete premium jars in the LibsDisguises folder
This commit is contained in:
		| @@ -0,0 +1,149 @@ | |||||||
|  | package me.libraryaddict.disguise.disguisetypes; | ||||||
|  |  | ||||||
|  | import lombok.Getter; | ||||||
|  | import me.libraryaddict.disguise.disguisetypes.watchers.CustomWatcher; | ||||||
|  | import me.libraryaddict.disguise.utilities.modded.CustomEntity; | ||||||
|  | import org.bukkit.entity.Entity; | ||||||
|  | import org.bukkit.entity.Player; | ||||||
|  |  | ||||||
|  | import java.security.InvalidParameterException; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Created by libraryaddict on 15/04/2020. | ||||||
|  |  */ | ||||||
|  | public class CustomDisguise extends TargetedDisguise { | ||||||
|  |     @Getter | ||||||
|  |     private CustomEntity customEntity; | ||||||
|  |  | ||||||
|  |     public CustomDisguise(CustomEntity customEntity) { | ||||||
|  |         super(customEntity.isLiving() ? DisguiseType.CUSTOM_LIVING : DisguiseType.CUSTOM_MISC); | ||||||
|  |  | ||||||
|  |         this.customEntity = customEntity; | ||||||
|  |         createDisguise(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public CustomDisguise(DisguiseType disguiseType) { | ||||||
|  |         super(disguiseType); | ||||||
|  |  | ||||||
|  |         if (disguiseType != DisguiseType.UNKNOWN) { | ||||||
|  |             throw new InvalidParameterException( | ||||||
|  |                     "CustomDisguise is only for DisguiseType.CUSTOM and DisguiseType.UNKNOWN"); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         createDisguise(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public boolean isCustomDisguise() { | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public boolean isMobDisguise() { | ||||||
|  |         return getCustomEntity().isLiving(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public boolean isMiscDisguise() { | ||||||
|  |         return !getCustomEntity().isLiving(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise addPlayer(Player player) { | ||||||
|  |         return (CustomDisguise) super.addPlayer(player); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise addPlayer(String playername) { | ||||||
|  |         return (CustomDisguise) super.addPlayer(playername); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise clone() { | ||||||
|  |         CustomDisguise disguise = new CustomDisguise(getCustomEntity()); | ||||||
|  |  | ||||||
|  |         clone(disguise); | ||||||
|  |  | ||||||
|  |         return disguise; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomWatcher getWatcher() { | ||||||
|  |         return (CustomWatcher) super.getWatcher(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setWatcher(FlagWatcher newWatcher) { | ||||||
|  |         return (CustomDisguise) super.setWatcher(newWatcher); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise removePlayer(Player player) { | ||||||
|  |         return (CustomDisguise) super.removePlayer(player); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise removePlayer(String playername) { | ||||||
|  |         return (CustomDisguise) super.removePlayer(playername); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setDisguiseTarget(TargetType newTargetType) { | ||||||
|  |         return (CustomDisguise) super.setDisguiseTarget(newTargetType); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setEntity(Entity entity) { | ||||||
|  |         return (CustomDisguise) super.setEntity(entity); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setHearSelfDisguise(boolean hearSelfDisguise) { | ||||||
|  |         return (CustomDisguise) super.setHearSelfDisguise(hearSelfDisguise); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setHideArmorFromSelf(boolean hideArmor) { | ||||||
|  |         return (CustomDisguise) super.setHideArmorFromSelf(hideArmor); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setHideHeldItemFromSelf(boolean hideHeldItem) { | ||||||
|  |         return (CustomDisguise) super.setHideHeldItemFromSelf(hideHeldItem); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) { | ||||||
|  |         return (CustomDisguise) super.setKeepDisguiseOnPlayerDeath(keepDisguise); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setModifyBoundingBox(boolean modifyBox) { | ||||||
|  |         return (CustomDisguise) super.setModifyBoundingBox(modifyBox); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setReplaceSounds(boolean areSoundsReplaced) { | ||||||
|  |         return (CustomDisguise) super.setReplaceSounds(areSoundsReplaced); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setVelocitySent(boolean sendVelocity) { | ||||||
|  |         return (CustomDisguise) super.setVelocitySent(sendVelocity); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise setViewSelfDisguise(boolean viewSelfDisguise) { | ||||||
|  |         return (CustomDisguise) super.setViewSelfDisguise(viewSelfDisguise); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise silentlyAddPlayer(String playername) { | ||||||
|  |         return (CustomDisguise) super.silentlyAddPlayer(playername); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public CustomDisguise silentlyRemovePlayer(String playername) { | ||||||
|  |         return (CustomDisguise) super.silentlyRemovePlayer(playername); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -7,6 +7,7 @@ import me.libraryaddict.disguise.utilities.plugin.PluginInformation; | |||||||
| import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; | import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; | ||||||
| import org.bukkit.configuration.InvalidConfigurationException; | import org.bukkit.configuration.InvalidConfigurationException; | ||||||
| import org.bukkit.configuration.file.YamlConfiguration; | import org.bukkit.configuration.file.YamlConfiguration; | ||||||
|  | import org.bukkit.util.FileUtil; | ||||||
|  |  | ||||||
| import java.io.*; | import java.io.*; | ||||||
| import java.net.URL; | import java.net.URL; | ||||||
| @@ -312,6 +313,53 @@ public class LibsPremium { | |||||||
|             doSecondaryCheck(version); |             doSecondaryCheck(version); | ||||||
|         } else { |         } else { | ||||||
|             DisguiseUtilities.getLogger().info("Registered to: " + getSanitizedUser(getUserID())); |             DisguiseUtilities.getLogger().info("Registered to: " + getSanitizedUser(getUserID())); | ||||||
|  |  | ||||||
|  |             boolean foundBetter = false; | ||||||
|  |  | ||||||
|  |             // Lets not do any sanity checks since it won't affect legit users | ||||||
|  |             for (File f : LibsDisguises.getInstance().getDataFolder().listFiles()) { | ||||||
|  |                 if (f.isDirectory() || !f.getName().endsWith(".jar")) { | ||||||
|  |                     continue; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 try { | ||||||
|  |                     PluginInformation info = getInformation(f); | ||||||
|  |  | ||||||
|  |                     if (info.getBuildNumber() == null || !info.getBuildNumber().matches("[0-9]+")) { | ||||||
|  |                         f.delete(); | ||||||
|  |                         DisguiseUtilities.getLogger().info("Ew, I don't recognize " + f.getName()); | ||||||
|  |                         continue; | ||||||
|  |                     } else if (Integer.parseInt(info.getBuildNumber()) < | ||||||
|  |                             Integer.parseInt(LibsDisguises.getInstance().getBuildNo())) { | ||||||
|  |                         f.delete(); | ||||||
|  |                         DisguiseUtilities.getLogger().info("Ew, " + f.getName() + " is so old"); | ||||||
|  |                         continue; | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     if (!info.isLegit()) { | ||||||
|  |                         f.delete(); | ||||||
|  |                         DisguiseUtilities.getLogger().info("Ew, I saw something nasty in " + f.getName()); | ||||||
|  |                         continue; | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     foundBetter = true; | ||||||
|  |                     break; | ||||||
|  |                 } | ||||||
|  |                 catch (Exception e) { | ||||||
|  |                     e.printStackTrace(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             if (!foundBetter) { | ||||||
|  |                 File f = new File( | ||||||
|  |                         LibsDisguises.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation() | ||||||
|  |                                 .getFile()); | ||||||
|  |  | ||||||
|  |                 FileUtil.copy(f, new File(LibsDisguises.getInstance().getDataFolder(), f.getName())); | ||||||
|  |  | ||||||
|  |                 DisguiseUtilities.getLogger().info("Copied " + f.getName() + | ||||||
|  |                         " to the plugin folder! This is incase you want to use dev builds."); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (isPremium()) { |         if (isPremium()) { | ||||||
|   | |||||||
| @@ -0,0 +1,22 @@ | |||||||
|  | package me.libraryaddict.disguise.utilities.modded; | ||||||
|  |  | ||||||
|  | import lombok.AllArgsConstructor; | ||||||
|  | import lombok.Getter; | ||||||
|  | import lombok.Setter; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Created by libraryaddict on 14/04/2020. | ||||||
|  |  */ | ||||||
|  | @AllArgsConstructor | ||||||
|  | @Getter | ||||||
|  | public class CustomEntity { | ||||||
|  |     @Setter | ||||||
|  |     private Object entityType; | ||||||
|  |     private final String name; | ||||||
|  |     private final boolean living; | ||||||
|  |     private final String mod; | ||||||
|  |     private final String[] versions; | ||||||
|  |     private final String required; | ||||||
|  |     @Setter | ||||||
|  |     private int typeId; | ||||||
|  | } | ||||||
| @@ -0,0 +1,71 @@ | |||||||
|  | package me.libraryaddict.disguise.utilities.modded; | ||||||
|  |  | ||||||
|  | import me.libraryaddict.disguise.disguisetypes.DisguiseType; | ||||||
|  | import me.libraryaddict.disguise.utilities.parser.DisguisePerm; | ||||||
|  | import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; | ||||||
|  | import org.bukkit.NamespacedKey; | ||||||
|  |  | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Created by libraryaddict on 14/04/2020. | ||||||
|  |  */ | ||||||
|  | public class ModdedManager { | ||||||
|  |     private static final HashMap<NamespacedKey, CustomEntity> entities = new HashMap<>(); | ||||||
|  |  | ||||||
|  |     public static void registerCustomEntity(NamespacedKey name, CustomEntity entity, boolean register) { | ||||||
|  |         if (entities.keySet().stream().anyMatch(n -> n.toString().equalsIgnoreCase(name.toString()))) { | ||||||
|  |             throw new IllegalArgumentException(name + " has already been registered"); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (entities.values().stream().anyMatch(n -> n.getName().equalsIgnoreCase(entity.getName()))) { | ||||||
|  |             throw new IllegalArgumentException("Modded entity " + entity.getName() + " has already been registered"); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (register) { | ||||||
|  |             Object entityType = ReflectionManager.registerEntityType(name); | ||||||
|  |             int entityId = ReflectionManager.getEntityTypeId(entityType); | ||||||
|  |  | ||||||
|  |             entity.setTypeId(entityId); | ||||||
|  |             entity.setEntityType(entityType); | ||||||
|  |         } else { | ||||||
|  |             Object entityType = ReflectionManager.getEntityType(name); | ||||||
|  |             int entityId = ReflectionManager.getEntityTypeId(entityType); | ||||||
|  |  | ||||||
|  |             entity.setTypeId(entityId); | ||||||
|  |             entity.setEntityType(entityType); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         entities.put(name, entity); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static CustomEntity getCustomEntity(NamespacedKey name) { | ||||||
|  |         return entities.get(name); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static CustomEntity getCustomEntity(String name) { | ||||||
|  |         for (CustomEntity entity : entities.values()) { | ||||||
|  |             if (!entity.getName().equalsIgnoreCase(name)) { | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             return entity; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static ArrayList<DisguisePerm> getDisguiseTypes() { | ||||||
|  |         ArrayList<DisguisePerm> perms = new ArrayList<>(); | ||||||
|  |  | ||||||
|  |         for (Map.Entry<NamespacedKey, CustomEntity> entry : entities.entrySet()) { | ||||||
|  |             perms.add(new DisguisePerm( | ||||||
|  |                     entry.getValue().isLiving() ? DisguiseType.CUSTOM_LIVING : DisguiseType.CUSTOM_MISC, | ||||||
|  |                     entry.getValue().getName())); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return perms; | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user