Skip to content

Commit

Permalink
Use kasama's method
Browse files Browse the repository at this point in the history
Reverting my revert :P
  • Loading branch information
MarioFinale committed Jun 25, 2021
1 parent 07f4023 commit fd8b62c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 223 deletions.
7 changes: 6 additions & 1 deletion out/production/VillagerSaver/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
name: VillagerSaver
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
version: 1.1.6
=======
version: 1.1.5
>>>>>>> parent of 688c16c (Minor changes)
=======
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
website: mariofinale.cl
commands:
villagersaver:
description: VillagerSaver commands.
usage: /villagersaver <blacklistworld - unblacklistworld>
usage: /villagersaver <blacklistworld - unblacklistworld>

21 changes: 0 additions & 21 deletions src/cl/mariofinale/VillagerSaver_CustomEvent.java

This file was deleted.

23 changes: 0 additions & 23 deletions src/cl/mariofinale/VillagerSaver_CustomEvent2.java

This file was deleted.

199 changes: 36 additions & 163 deletions src/cl/mariofinale/VillagerSaver_Listener.java
Original file line number Diff line number Diff line change
@@ -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<UUID, Reputation.a> originalReputationsMap = originalReputations.getReputations();

for (Map.Entry<UUID, Reputation.a> 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;
Expand Down Expand Up @@ -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();
Expand All @@ -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());
}
}
12 changes: 12 additions & 0 deletions src/cl/mariofinale/VillagerSaver_PluginVars.java
Original file line number Diff line number Diff line change
@@ -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<EntityType>(){
{
add(EntityType.ZOMBIE);
add(EntityType.ZOMBIE_VILLAGER);
add(EntityType.DROWNED);
add(EntityType.HUSK);
add(EntityType.ZOMBIFIED_PIGLIN);
}
};
}
13 changes: 0 additions & 13 deletions src/cl/mariofinale/villagerSaver.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
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;
import java.util.*;

public class villagerSaver extends JavaPlugin{

public static HashMap<UUID, Integer> VillagersLevels = new HashMap();
public static HashMap<UUID, List<MerchantRecipe>> VillagersTrades = new HashMap();
public static HashMap<UUID, Integer> VillagersExp = new HashMap();
public static HashMap<UUID, Villager.Profession> VillagersProfessions = new HashMap();
public static HashMap<UUID, Villager.Type> VillagersTypes = new HashMap();
public static HashMap<UUID, Reputation> VillagersReputation = new HashMap();
public static HashMap<UUID, Location> VillagersJobSites = new HashMap();
public static HashMap<UUID, Location> VillagersHomes = new HashMap();
public static HashMap<UUID, UUID> VillagersHealers = new HashMap();
public static ArrayList<String> WorldBlackList = new ArrayList<>();
public static Plugin plugin;

Expand Down
5 changes: 3 additions & 2 deletions src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: VillagerSaver
version: 1.1.6
version: 1.2.0
author: MarioFinale
main: cl.mariofinale.villagerSaver
api-version: 1.13
website: mariofinale.cl
commands:
villagersaver:
description: VillagerSaver commands.
usage: /villagersaver <blacklistworld - unblacklistworld>
usage: /villagersaver <blacklistworld - unblacklistworld>

0 comments on commit fd8b62c

Please sign in to comment.