SPIGOT-4946: Add VillagerCareerChangeEvent

This commit is contained in:
i509VCB 2019-05-25 20:58:11 -05:00 committed by md_5
parent a4a2f67946
commit fcb9f90bf3
4 changed files with 85 additions and 2 deletions

View File

@ -0,0 +1,30 @@
--- a/net/minecraft/server/BehaviorCareer.java
+++ b/net/minecraft/server/BehaviorCareer.java
@@ -1,6 +1,11 @@
package net.minecraft.server;
import com.google.common.collect.ImmutableMap;
+// CraftBukkit start
+import org.bukkit.craftbukkit.entity.CraftVillager;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.event.entity.VillagerCareerChangeEvent;
+// CraftBukkit end
public class BehaviorCareer extends Behavior<EntityVillager> {
@@ -20,7 +25,14 @@
IRegistry.VILLAGER_PROFESSION.d().filter((villagerprofession) -> {
return villagerprofession.b() == villageplacetype;
}).findFirst().ifPresent((villagerprofession) -> {
- entityvillager.setVillagerData(entityvillager.getVillagerData().withProfession(villagerprofession));
+ // CraftBukkit start - Fire VillagerCareerChangeEvent where Villager gets employed
+ VillagerCareerChangeEvent event = CraftEventFactory.callVillagerCareerChangeEvent(entityvillager, CraftVillager.nmsToBukkitProfession(villagerprofession), VillagerCareerChangeEvent.ChangeReason.EMPLOYED);
+ if (event.isCancelled()) {
+ return;
+ }
+
+ entityvillager.setVillagerData(entityvillager.getVillagerData().withProfession(CraftVillager.bukkitToNmsProfession(event.getProfession())));
+ // CraftBukkit end
entityvillager.a(worldserver);
});
});

View File

@ -0,0 +1,30 @@
--- a/net/minecraft/server/BehaviorProfession.java
+++ b/net/minecraft/server/BehaviorProfession.java
@@ -1,6 +1,11 @@
package net.minecraft.server;
import com.google.common.collect.ImmutableMap;
+// CraftBukkit start
+import org.bukkit.craftbukkit.entity.CraftVillager;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.event.entity.VillagerCareerChangeEvent;
+// CraftBukkit end
public class BehaviorProfession extends Behavior<EntityVillager> {
@@ -15,7 +20,14 @@
}
protected void a(WorldServer worldserver, EntityVillager entityvillager, long i) {
- entityvillager.setVillagerData(entityvillager.getVillagerData().withProfession(VillagerProfession.NONE));
+ // CraftBukkit start
+ VillagerCareerChangeEvent event = CraftEventFactory.callVillagerCareerChangeEvent(entityvillager, CraftVillager.nmsToBukkitProfession(VillagerProfession.NONE), VillagerCareerChangeEvent.ChangeReason.EMPLOYED);
+ if (event.isCancelled()) {
+ return;
+ }
+
+ entityvillager.setVillagerData(entityvillager.getVillagerData().withProfession(CraftVillager.bukkitToNmsProfession(event.getProfession())));
+ // CraftBukkit end
entityvillager.a(worldserver);
}
}

View File

@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import java.util.Locale; import java.util.Locale;
import net.minecraft.server.EntityVillager; import net.minecraft.server.EntityVillager;
import net.minecraft.server.IRegistry; import net.minecraft.server.IRegistry;
import net.minecraft.server.VillagerProfession;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftNamespacedKey; import org.bukkit.craftbukkit.util.CraftNamespacedKey;
@ -33,13 +34,13 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
@Override @Override
public Profession getProfession() { public Profession getProfession() {
return Profession.valueOf(IRegistry.VILLAGER_PROFESSION.getKey(getHandle().getVillagerData().getProfession()).getKey().toUpperCase(Locale.ROOT)); return CraftVillager.nmsToBukkitProfession(getHandle().getVillagerData().getProfession());
} }
@Override @Override
public void setProfession(Profession profession) { public void setProfession(Profession profession) {
Validate.notNull(profession); Validate.notNull(profession);
getHandle().setVillagerData(getHandle().getVillagerData().withProfession(IRegistry.VILLAGER_PROFESSION.get(CraftNamespacedKey.toMinecraft(profession.getKey())))); getHandle().setVillagerData(getHandle().getVillagerData().withProfession(CraftVillager.bukkitToNmsProfession(profession)));
} }
@Override @Override
@ -76,4 +77,12 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
getHandle().t(experience); getHandle().t(experience);
} }
public static Profession nmsToBukkitProfession(VillagerProfession nms) {
return Profession.valueOf(IRegistry.VILLAGER_PROFESSION.getKey(nms).getKey().toUpperCase(Locale.ROOT));
}
public static VillagerProfession bukkitToNmsProfession(Profession bukkit) {
return IRegistry.VILLAGER_PROFESSION.get(CraftNamespacedKey.toMinecraft(bukkit.getKey()));
}
} }

View File

@ -40,6 +40,7 @@ import net.minecraft.server.EntityPotion;
import net.minecraft.server.EntitySheep; import net.minecraft.server.EntitySheep;
import net.minecraft.server.EntitySlime; import net.minecraft.server.EntitySlime;
import net.minecraft.server.EntityTypes; import net.minecraft.server.EntityTypes;
import net.minecraft.server.EntityVillager;
import net.minecraft.server.EntityWaterAnimal; import net.minecraft.server.EntityWaterAnimal;
import net.minecraft.server.EnumDirection; import net.minecraft.server.EnumDirection;
import net.minecraft.server.EnumHand; import net.minecraft.server.EnumHand;
@ -103,6 +104,8 @@ import org.bukkit.entity.Sheep;
import org.bukkit.entity.ThrownExpBottle; import org.bukkit.entity.ThrownExpBottle;
import org.bukkit.entity.ThrownPotion; import org.bukkit.entity.ThrownPotion;
import org.bukkit.entity.Vehicle; import org.bukkit.entity.Vehicle;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Villager.Profession;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.Event.Result; import org.bukkit.event.Event.Result;
@ -163,6 +166,7 @@ import org.bukkit.event.entity.PlayerLeashEntityEvent;
import org.bukkit.event.entity.PotionSplashEvent; import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.entity.VillagerCareerChangeEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent; import org.bukkit.event.inventory.PrepareAnvilEvent;
@ -465,6 +469,16 @@ public class CraftEventFactory {
return event; return event;
} }
/**
* VillagerCareerChangeEvent
*/
public static VillagerCareerChangeEvent callVillagerCareerChangeEvent(EntityVillager vilager, Profession future, VillagerCareerChangeEvent.ChangeReason reason) {
VillagerCareerChangeEvent event = new VillagerCareerChangeEvent((Villager) vilager.getBukkitEntity(), future, reason);
Bukkit.getPluginManager().callEvent(event);
return event;
}
/** /**
* BlockDamageEvent * BlockDamageEvent
*/ */