From fd8b62cb52178ecd8ac58a1df48281088259a9df Mon Sep 17 00:00:00 2001 From: MarioFinale Date: Fri, 25 Jun 2021 07:32:53 -0400 Subject: [PATCH] Use kasama's method Reverting my revert :P --- out/production/VillagerSaver/plugin.yml | 7 +- .../VillagerSaver_CustomEvent.java | 21 -- .../VillagerSaver_CustomEvent2.java | 23 -- .../mariofinale/VillagerSaver_Listener.java | 199 ++++-------------- .../mariofinale/VillagerSaver_PluginVars.java | 12 ++ src/cl/mariofinale/villagerSaver.java | 13 -- src/plugin.yml | 5 +- 7 files changed, 57 insertions(+), 223 deletions(-) delete mode 100644 src/cl/mariofinale/VillagerSaver_CustomEvent.java delete mode 100644 src/cl/mariofinale/VillagerSaver_CustomEvent2.java diff --git a/out/production/VillagerSaver/plugin.yml b/out/production/VillagerSaver/plugin.yml index f67ebae..f7d9098 100644 --- a/out/production/VillagerSaver/plugin.yml +++ b/out/production/VillagerSaver/plugin.yml @@ -1,6 +1,7 @@ name: VillagerSaver <<<<<<< HEAD <<<<<<< HEAD +<<<<<<< HEAD version: 1.1.6 ======= version: 1.1.5 @@ -8,6 +9,9 @@ version: 1.1.5 ======= version: 1.1.5 >>>>>>> parent of 688c16c (Minor changes) +======= +version: 1.2.0 +>>>>>>> parent of cb5e316 (Revert "Merge branch 'pr/14'") author: MarioFinale main: cl.mariofinale.villagerSaver api-version: 1.13 @@ -15,4 +19,5 @@ website: mariofinale.cl commands: villagersaver: description: VillagerSaver commands. - usage: /villagersaver \ No newline at end of file + usage: /villagersaver + diff --git a/src/cl/mariofinale/VillagerSaver_CustomEvent.java b/src/cl/mariofinale/VillagerSaver_CustomEvent.java deleted file mode 100644 index 1077cfb..0000000 --- a/src/cl/mariofinale/VillagerSaver_CustomEvent.java +++ /dev/null @@ -1,21 +0,0 @@ -package cl.mariofinale; - -import org.bukkit.entity.Entity; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class VillagerSaver_CustomEvent extends Event { - private static final HandlerList HANDLERS = new HandlerList(); - private Entity entity; - public VillagerSaver_CustomEvent(Entity entity) { this.entity = entity; } - public Entity getEntity(){ - return this.entity; - } - public VillagerSaver_CustomEvent() { } - public HandlerList getHandlers() { - return HANDLERS; - } - public static HandlerList getHandlerList() { - return HANDLERS; - } -} diff --git a/src/cl/mariofinale/VillagerSaver_CustomEvent2.java b/src/cl/mariofinale/VillagerSaver_CustomEvent2.java deleted file mode 100644 index 67383c3..0000000 --- a/src/cl/mariofinale/VillagerSaver_CustomEvent2.java +++ /dev/null @@ -1,23 +0,0 @@ -package cl.mariofinale; - -import org.bukkit.entity.Entity; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class VillagerSaver_CustomEvent2 extends Event { - private static final HandlerList HANDLERS = new HandlerList(); - private Entity entity; - public VillagerSaver_CustomEvent2(Entity entity) { - this.entity = entity; - } - public Entity getEntity(){ - return this.entity; - } - public VillagerSaver_CustomEvent2() { } - public HandlerList getHandlers() { - return HANDLERS; - } - public static HandlerList getHandlerList() { - return HANDLERS; - } -} \ No newline at end of file diff --git a/src/cl/mariofinale/VillagerSaver_Listener.java b/src/cl/mariofinale/VillagerSaver_Listener.java index 44e9963..a295da6 100644 --- a/src/cl/mariofinale/VillagerSaver_Listener.java +++ b/src/cl/mariofinale/VillagerSaver_Listener.java @@ -1,175 +1,21 @@ package cl.mariofinale; +import net.minecraft.server.v1_16_R3.*; import net.minecraft.server.v1_16_R3.EntityVillager; -import net.minecraft.server.v1_16_R3.Reputation; -import net.minecraft.server.v1_16_R3.ReputationType; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; import org.bukkit.craftbukkit.v1_16_R3.entity.CraftVillager; +import org.bukkit.craftbukkit.v1_16_R3.entity.CraftVillagerZombie; import org.bukkit.entity.*; -import org.bukkit.entity.memory.MemoryKey; +import org.bukkit.entity.Entity; import org.bukkit.event.entity.*; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; +import org.bukkit.event.entity.EntityTransformEvent.TransformReason; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.MerchantRecipe; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; public class VillagerSaver_Listener implements Listener{ - @EventHandler - public void CustomEvent(VillagerSaver_CustomEvent event){ //CALLED AFTER A ZOMBIE VILLAGER HAS TRANSFORMED FROM VILLAGER - Villager villager = (Villager) event.getEntity(); - UUID villagerUUID = villager.getUniqueId(); - - villager.setVillagerType(villagerSaver.VillagersTypes.get(villagerUUID)); - villager.setProfession(villagerSaver.VillagersProfessions.get(villagerUUID)); - villager.setVillagerExperience(1); - villager.setVillagerLevel(villagerSaver.VillagersLevels.get(villagerUUID)); - villager.setVillagerExperience(villagerSaver.VillagersExp.get(villagerUUID)); - villager.setRecipes(villagerSaver.VillagersTrades.get(villagerUUID)); - villager.setVillagerExperience(villagerSaver.VillagersExp.get(villagerUUID)); - villager.setMemory(MemoryKey.JOB_SITE,villagerSaver.VillagersJobSites.get(villagerUUID)); - villager.setMemory(MemoryKey.HOME,villagerSaver.VillagersHomes.get(villagerUUID)); - - villagerSaver.VillagersTypes.remove(villagerUUID); - villagerSaver.VillagersProfessions.remove(villagerUUID); - villagerSaver.VillagersLevels.remove(villagerUUID); - villagerSaver.VillagersExp.remove(villagerUUID); - villagerSaver.VillagersTrades.remove(villagerUUID); - villagerSaver.VillagersJobSites.remove(villagerUUID); - villagerSaver.VillagersHomes.remove(villagerUUID); - - } - - @EventHandler - public void CustomEvent2(VillagerSaver_CustomEvent2 event){ //CALLED AFTER A ZOMBIE VILLAGER HAS TRANSFORMED FROM VILLAGER AND HAS BEEN ASSIGNED A PROFESSION - Villager villager = (Villager) event.getEntity(); - UUID villagerUUID = villager.getUniqueId(); - - if (villagerSaver.VillagersHealers.containsKey(villagerUUID)){ - CraftVillager newCraftVillager = (CraftVillager) villager; - EntityVillager newEntVillager = newCraftVillager.getHandle(); - UUID healerUUID = villagerSaver.VillagersHealers.get(villagerUUID); - Reputation newReputations = newEntVillager.fj(); - Reputation originalReputations = villagerSaver.VillagersReputation.get(villagerUUID); - Map originalReputationsMap = originalReputations.getReputations(); - - for (Map.Entry rep: originalReputationsMap.entrySet()){ - UUID entityUUID = rep.getKey(); - int majorPositive = originalReputations.a(entityUUID, reputationType -> reputationType == ReputationType.MAJOR_POSITIVE); - int minorPositive = originalReputations.a(entityUUID, reputationType -> reputationType == ReputationType.MINOR_POSITIVE); - int majorNegative = originalReputations.a(entityUUID, reputationType -> reputationType == ReputationType.MAJOR_NEGATIVE); - int minorNegative = originalReputations.a(entityUUID, reputationType -> reputationType == ReputationType.MINOR_NEGATIVE); - int trading = originalReputations.a(entityUUID, reputationType -> reputationType == ReputationType.TRADING); - - int oldMajorPositive = newReputations.a(entityUUID, reputationType -> reputationType == ReputationType.MAJOR_POSITIVE); - int oldMinorPositive = newReputations.a(entityUUID, reputationType -> reputationType == ReputationType.MINOR_POSITIVE); - int oldMajorNegative = newReputations.a(entityUUID, reputationType -> reputationType == ReputationType.MAJOR_NEGATIVE); - int oldMinorNegative = newReputations.a(entityUUID, reputationType -> reputationType == ReputationType.MINOR_NEGATIVE); - int oldTrading = newReputations.a(entityUUID, reputationType -> reputationType == ReputationType.TRADING); - - newReputations.a(entityUUID, ReputationType.MAJOR_POSITIVE, -1 * oldMajorPositive); - newReputations.a(entityUUID, ReputationType.MINOR_POSITIVE, -1 * oldMinorPositive); - newReputations.a(entityUUID, ReputationType.MAJOR_NEGATIVE, -1 * oldMajorNegative); - newReputations.a(entityUUID, ReputationType.MINOR_NEGATIVE, -1 * oldMinorNegative); - newReputations.a(entityUUID, ReputationType.TRADING, -1 * oldTrading); - - if (entityUUID == healerUUID){ - majorPositive += 20; - minorPositive += 25; - if (majorPositive > 100) majorPositive = 100; - if (minorPositive > 200) minorPositive = 200; - } - - newReputations.a(entityUUID, ReputationType.MAJOR_POSITIVE,majorPositive); - newReputations.a(entityUUID, ReputationType.MINOR_POSITIVE,minorPositive); - newReputations.a(entityUUID, ReputationType.MAJOR_NEGATIVE, majorNegative); - newReputations.a(entityUUID, ReputationType.MINOR_NEGATIVE, minorNegative); - newReputations.a(entityUUID, ReputationType.TRADING,trading); - - } - } - villagerSaver.VillagersReputation.remove(villagerUUID); - villagerSaver.VillagersHealers.remove(villagerUUID); - } - - @EventHandler - public void CreatureSpawnEvent(CreatureSpawnEvent event){ - if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CURED){ - Entity ent = (Villager) event.getEntity(); - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(villagerSaver.plugin, new Runnable(){ - @Override - public void run(){ - Bukkit.getPluginManager().callEvent(new VillagerSaver_CustomEvent(ent)); - } - }, 10L); - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(villagerSaver.plugin, new Runnable(){ - @Override - public void run(){ - Bukkit.getPluginManager().callEvent(new VillagerSaver_CustomEvent2(ent)); - } - }, 25L); - - } - } - - @EventHandler - public void entityTransformEvent(EntityTransformEvent event){ - Entity zombie = event.getEntity(); - Entity villager = event.getTransformedEntity(); - UUID zombieUUID = zombie.getUniqueId(); - UUID villagerUUID = villager.getUniqueId(); - if (villagerSaver.VillagersProfessions.containsKey(zombieUUID)){ - villagerSaver.VillagersTypes.put(villagerUUID,villagerSaver.VillagersTypes.get(zombieUUID)); - villagerSaver.VillagersProfessions.put(villagerUUID,villagerSaver.VillagersProfessions.get(zombieUUID)); - villagerSaver.VillagersLevels.put(villagerUUID, villagerSaver.VillagersLevels.get(zombieUUID)); - villagerSaver.VillagersExp.put(villagerUUID, villagerSaver.VillagersExp.get(zombieUUID)); - villagerSaver.VillagersTrades.put(villagerUUID, villagerSaver.VillagersTrades.get(zombieUUID)); - villagerSaver.VillagersReputation.put(villagerUUID, villagerSaver.VillagersReputation.get(zombieUUID)); - villagerSaver.VillagersJobSites.put(villagerUUID, villagerSaver.VillagersJobSites.get(zombieUUID)); - villagerSaver.VillagersHomes.put(villagerUUID, villagerSaver.VillagersHomes.get(zombieUUID)); - - villagerSaver.VillagersTypes.remove(zombieUUID); - villagerSaver.VillagersProfessions.remove(zombieUUID); - villagerSaver.VillagersLevels.remove(zombieUUID); - villagerSaver.VillagersExp.remove(zombieUUID); - villagerSaver.VillagersTrades.remove(zombieUUID); - villagerSaver.VillagersReputation.remove(zombieUUID); - villagerSaver.VillagersJobSites.remove(zombieUUID); - villagerSaver.VillagersHomes.remove(zombieUUID); - } - if (villagerSaver.VillagersHealers.containsKey(zombieUUID)){ //In a different "if" so baby villagers reputation also gets updated - villagerSaver.VillagersHealers.put(villagerUUID, villagerSaver.VillagersHealers.get(zombieUUID)); - villagerSaver.VillagersHealers.remove(zombieUUID); - } - } - - @EventHandler - public void PlayerInteractEntityEvent(PlayerInteractEntityEvent event){ - Player player = event.getPlayer(); - Entity entity = event.getRightClicked(); - if(!(entity instanceof LivingEntity)) return; - if (!(entity.getType() == EntityType.ZOMBIE_VILLAGER)) return; - LivingEntity zombieVillager = (LivingEntity) entity; - PotionEffect effect = zombieVillager.getPotionEffect(PotionEffectType.WEAKNESS); - if (effect == null) return; - ItemStack offHandItem = player.getInventory().getItemInOffHand(); - ItemStack mainHandItem = player.getInventory().getItemInMainHand(); - ItemStack gApple = new ItemStack(Material.GOLDEN_APPLE, 1); - if (!(offHandItem.isSimilar(gApple) || mainHandItem.isSimilar(gApple))) return; - villagerSaver.VillagersHealers.put(entity.getUniqueId(),player.getUniqueId()); - } - @EventHandler public void onEntityDamageByEntity(EntityDamageByEntityEvent event){ +<<<<<<< HEAD Entity villager = event.getEntity(); if(!(villager instanceof LivingEntity)) return; LivingEntity tVillager = (LivingEntity)villager; @@ -247,6 +93,23 @@ public void StoreAndClearVillager(Entity villager){ //CALLED WHEN A VILLAGER IS >>>>>>> parent of 688c16c (Minor changes) public void handleSpawnZombieVillager(LivingEntity entityliving) { CraftVillager craftVillager = (CraftVillager) entityliving; +======= + Entity damagedEntity = event.getEntity(); + if(!(damagedEntity instanceof LivingEntity)) return; + LivingEntity damagedVillager = (LivingEntity) damagedEntity; + Entity entityDamager = event.getDamager(); + if (entityDamager == null) return; + if (!(damagedVillager.getHealth() - event.getDamage() <= 0)) return; + if (!(damagedVillager.getType() == EntityType.VILLAGER)) return; + if (!(VillagerSaver_PluginVars.ZombieTypes.contains(entityDamager.getType()))) return; //Check if the zombie types list contains the damager + if (villagerSaver.WorldBlackList.contains(damagedVillager.getWorld().getName())) return; + handleSpawnZombieVillager(damagedVillager); + event.setCancelled(true); + } + + public void handleSpawnZombieVillager(LivingEntity livingEnt) { + CraftVillager craftVillager = (CraftVillager) livingEnt; +>>>>>>> parent of cb5e316 (Revert "Merge branch 'pr/14'") Entity vehicle = craftVillager.getVehicle(); EntityVillager entityvillager = craftVillager.getHandle(); @@ -262,18 +125,28 @@ public void handleSpawnZombieVillager(LivingEntity entityliving) { TransformReason.INFECTION, SpawnReason.INFECTION ); +<<<<<<< HEAD if (entityZombieVillager == null) { return; } +======= + if (entityZombieVillager == null) return; +>>>>>>> parent of cb5e316 (Revert "Merge branch 'pr/14'") CraftVillagerZombie craftVillagerZombie = (CraftVillagerZombie) entityZombieVillager.getBukkitEntity(); if (vehicle != null && !vehicle.getPassengers().contains(craftVillagerZombie)) { vehicle.addPassenger(craftVillagerZombie); +<<<<<<< HEAD >>>>>>> parent of 688c16c (Minor changes) +======= +>>>>>>> parent of cb5e316 (Revert "Merge branch 'pr/14'") } - if (vehicle != null) { // Make entity ride vehicle if the original was in one - vehicle.addPassenger(zVillager); - } + entityZombieVillager.setVillagerData(villagerData); + // retain entityZombieVillager reputation with BaseNBT from the previous villager + // DynamicOpsNBT.a is necessary because Gossips don't follow a specific Schema + entityZombieVillager.a(reputation.a(DynamicOpsNBT.a).getValue()); + entityZombieVillager.a(experience); + entityZombieVillager.setOffers(offers.a()); } } \ No newline at end of file diff --git a/src/cl/mariofinale/VillagerSaver_PluginVars.java b/src/cl/mariofinale/VillagerSaver_PluginVars.java index 3c1260d..be5221f 100644 --- a/src/cl/mariofinale/VillagerSaver_PluginVars.java +++ b/src/cl/mariofinale/VillagerSaver_PluginVars.java @@ -1,6 +1,18 @@ package cl.mariofinale; import org.bukkit.ChatColor; +import org.bukkit.entity.EntityType; + +import java.util.ArrayList; public class VillagerSaver_PluginVars { public static String PluginPrefix = ChatColor.BOLD + "" + ChatColor.GREEN + "[Villager Saver]:" + ChatColor.RESET; + public static ArrayList ZombieTypes = new ArrayList(){ + { + add(EntityType.ZOMBIE); + add(EntityType.ZOMBIE_VILLAGER); + add(EntityType.DROWNED); + add(EntityType.HUSK); + add(EntityType.ZOMBIFIED_PIGLIN); + } + }; } diff --git a/src/cl/mariofinale/villagerSaver.java b/src/cl/mariofinale/villagerSaver.java index cf88159..993fdcd 100644 --- a/src/cl/mariofinale/villagerSaver.java +++ b/src/cl/mariofinale/villagerSaver.java @@ -1,10 +1,6 @@ package cl.mariofinale; -import net.minecraft.server.v1_16_R3.Reputation; -import org.bukkit.Location; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Villager; -import org.bukkit.inventory.MerchantRecipe; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; @@ -12,15 +8,6 @@ public class villagerSaver extends JavaPlugin{ - public static HashMap VillagersLevels = new HashMap(); - public static HashMap> VillagersTrades = new HashMap(); - public static HashMap VillagersExp = new HashMap(); - public static HashMap VillagersProfessions = new HashMap(); - public static HashMap VillagersTypes = new HashMap(); - public static HashMap VillagersReputation = new HashMap(); - public static HashMap VillagersJobSites = new HashMap(); - public static HashMap VillagersHomes = new HashMap(); - public static HashMap VillagersHealers = new HashMap(); public static ArrayList WorldBlackList = new ArrayList<>(); public static Plugin plugin; diff --git a/src/plugin.yml b/src/plugin.yml index 5288940..a90f1cc 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: VillagerSaver -version: 1.1.6 +version: 1.2.0 author: MarioFinale main: cl.mariofinale.villagerSaver api-version: 1.13 @@ -7,4 +7,5 @@ website: mariofinale.cl commands: villagersaver: description: VillagerSaver commands. - usage: /villagersaver \ No newline at end of file + usage: /villagersaver +