Skip to content

Commit

Permalink
Finish entity ai patches
Browse files Browse the repository at this point in the history
  • Loading branch information
JustRed23 committed Nov 14, 2023
1 parent ad4f076 commit f7e9306
Show file tree
Hide file tree
Showing 26 changed files with 581 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ that enables the use of Bukkit plugins on Forge servers.
Ketting is still in development and is not ready for production use.

### PATCHES DONE
Bukkit: 266 / 530
Bukkit: 291 / 530
<br>
CraftBukkit: 0 / 685

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--- a/net/minecraft/world/entity/ai/behavior/AssignProfessionFromJobSite.java
+++ b/net/minecraft/world/entity/ai/behavior/AssignProfessionFromJobSite.java
@@ -8,6 +_,9 @@
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.npc.VillagerProfession;
+import org.bukkit.craftbukkit.v1_20_R2.entity.CraftVillager;
+import org.bukkit.craftbukkit.v1_20_R2.event.CraftEventFactory;
+import org.bukkit.event.entity.VillagerCareerChangeEvent;

public class AssignProfessionFromJobSite {
public static BehaviorControl<Villager> create() {
@@ -32,7 +_,14 @@
return p_217125_.heldJobSite().test(p_258313_);
}).findFirst();
}).ifPresent((p_22464_) -> {
- p_258310_.setVillagerData(p_258310_.getVillagerData().setProfession(p_22464_));
+ // CraftBukkit start - Fire VillagerCareerChangeEvent where Villager gets employed
+ VillagerCareerChangeEvent event = CraftEventFactory.callVillagerCareerChangeEvent(p_258310_, CraftVillager.CraftProfession.minecraftToBukkit(p_22464_), VillagerCareerChangeEvent.ChangeReason.EMPLOYED);
+ if (event.isCancelled()) {
+ return;
+ }
+
+ p_258310_.setVillagerData(p_258310_.getVillagerData().setProfession(CraftVillager.CraftProfession.bukkitToMinecraft(event.getProfession())));
+ // CraftBukkit end
p_258310_.refreshBrain(p_258309_);
});
return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--- a/net/minecraft/world/entity/ai/behavior/BabyFollowAdult.java
+++ b/net/minecraft/world/entity/ai/behavior/BabyFollowAdult.java
@@ -7,6 +_,10 @@
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.memory.WalkTarget;
+import org.bukkit.craftbukkit.v1_20_R2.entity.CraftLivingEntity;
+import org.bukkit.craftbukkit.v1_20_R2.event.CraftEventFactory;
+import org.bukkit.event.entity.EntityTargetEvent;
+import org.bukkit.event.entity.EntityTargetLivingEntityEvent;

public class BabyFollowAdult {
public static OneShot<AgeableMob> create(UniformInt p_260109_, float p_259621_) {
@@ -22,9 +_,21 @@
if (!p_258327_.isBaby()) {
return false;
} else {
- AgeableMob ageablemob = p_258331_.get(p_258317_);
+ LivingEntity ageablemob = p_258331_.get(p_258317_); // CraftBukkit - type
if (p_258327_.closerThan(ageablemob, (double)(p_259321_.getMaxValue() + 1)) && !p_258327_.closerThan(ageablemob, (double)p_259321_.getMinValue())) {
+ // CraftBukkit start
+ EntityTargetLivingEntityEvent event = CraftEventFactory.callEntityTargetLivingEvent(p_258327_, ageablemob, EntityTargetEvent.TargetReason.FOLLOW_LEADER);
+ if (event.isCancelled()) {
+ return false;
+ }
+ if (event.getTarget() == null) {
+ p_258317_.erase();
+ return true;
+ }
+ ageablemob = ((CraftLivingEntity) event.getTarget()).getHandle();
+ // CraftBukkit end
WalkTarget walktarget = new WalkTarget(new EntityTracker(ageablemob, false), p_259190_.apply(p_258327_), p_259321_.getMinValue() - 1);
+
p_258318_.set(new EntityTracker(ageablemob, true));
p_258319_.set(walktarget);
return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--- a/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java
+++ b/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java
@@ -86,6 +_,7 @@
}

public static void throwItem(LivingEntity p_217134_, ItemStack p_217135_, Vec3 p_217136_, Vec3 p_217137_, float p_217138_) {
+ if (p_217135_.isEmpty()) return; // CraftBukkit - SPIGOT-4940: no empty loot
double d0 = p_217134_.getEyeY() - (double)p_217138_;
ItemEntity itementity = new ItemEntity(p_217134_.level(), p_217134_.getX(), d0, p_217134_.getZ(), p_217135_);
itementity.setThrower(p_217134_.getUUID());
@@ -93,6 +_,13 @@
vec3 = vec3.normalize().multiply(p_217137_.x, p_217137_.y, p_217137_.z);
itementity.setDeltaMovement(vec3);
itementity.setDefaultPickUpDelay();
+ // CraftBukkit start
+ org.bukkit.event.entity.EntityDropItemEvent event = new org.bukkit.event.entity.EntityDropItemEvent(p_217134_.getBukkitEntity(), (org.bukkit.entity.Item) itementity.getBukkitEntity());
+ p_217134_.level().getCraftServer().getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ return;
+ }
+ // CraftBukkit end
p_217134_.level().addFreshEntity(itementity);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--- a/net/minecraft/world/entity/ai/behavior/GoToWantedItem.java
+++ b/net/minecraft/world/entity/ai/behavior/GoToWantedItem.java
@@ -23,7 +_,22 @@
return (p_258380_, p_258381_, p_258382_) -> {
ItemEntity itementity = p_258371_.get(p_258389_);
if (p_258371_.tryGet(p_258390_).isEmpty() && p_259490_.test(p_258381_) && itementity.closerThan(p_258381_, (double)p_259054_) && p_258381_.level().getWorldBorder().isWithinBounds(itementity.blockPosition())) {
+ // CraftBukkit start
+ if (p_258381_ instanceof net.minecraft.world.entity.animal.allay.Allay) {
+ org.bukkit.event.entity.EntityTargetEvent event = org.bukkit.craftbukkit.v1_20_R2.event.CraftEventFactory.callEntityTargetEvent(p_258381_, itementity, org.bukkit.event.entity.EntityTargetEvent.TargetReason.CLOSEST_ENTITY);
+
+ if (event.isCancelled()) {
+ return false;
+ }
+ if (!(event.getTarget() instanceof ItemEntity)) {
+ p_258389_.erase();
+ }
+
+ itementity = (ItemEntity) ((org.bukkit.craftbukkit.v1_20_R2.entity.CraftEntity) event.getTarget()).getHandle();
+ }
+ // CraftBukkit end
WalkTarget walktarget = new WalkTarget(new EntityTracker(itementity, false), p_260346_, 0);
+
p_258387_.set(new EntityTracker(itementity, true));
p_258388_.set(walktarget);
return true;
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
--- a/net/minecraft/world/entity/ai/behavior/HarvestFarmland.java
+++ b/net/minecraft/world/entity/ai/behavior/HarvestFarmland.java
@@ -21,10 +_,12 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.block.Block;
+import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.FarmBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
+import org.bukkit.craftbukkit.v1_20_R2.event.CraftEventFactory;

public class HarvestFarmland extends Behavior<Villager> {
private static final int HARVEST_DURATION = 200;
@@ -40,7 +_,7 @@
}

Expand All @@ -9,14 +22,40 @@
return false;
} else if (p_23175_.getVillagerData().getProfession() != VillagerProfession.FARMER) {
return false;
@@ -115,6 +_,11 @@
p_23196_.setBlockAndUpdate(this.aboveFarmlandPos, blockstate1);
p_23196_.gameEvent(GameEvent.BLOCK_PLACE, this.aboveFarmlandPos, GameEvent.Context.of(p_23197_, blockstate1));
flag = true;
+ } else if (itemstack.getItem() instanceof net.minecraftforge.common.IPlantable) {
+ if (((net.minecraftforge.common.IPlantable)itemstack.getItem()).getPlantType(p_23196_, aboveFarmlandPos) == net.minecraftforge.common.PlantType.CROP) {
+ p_23196_.setBlock(aboveFarmlandPos, ((net.minecraftforge.common.IPlantable)itemstack.getItem()).getPlant(p_23196_, aboveFarmlandPos), 3);
@@ -98,7 +_,9 @@
Block block = blockstate.getBlock();
Block block1 = p_23196_.getBlockState(this.aboveFarmlandPos.below()).getBlock();
if (block instanceof CropBlock && ((CropBlock)block).isMaxAge(blockstate)) {
- p_23196_.destroyBlock(this.aboveFarmlandPos, true, p_23197_);
+ if (CraftEventFactory.callEntityChangeBlockEvent(p_23197_, this.aboveFarmlandPos, Blocks.AIR.defaultBlockState())) { // CraftBukkit
+ p_23196_.destroyBlock(this.aboveFarmlandPos, true, p_23197_);
+ } // CraftBukkit
}

if (blockstate.isAir() && block1 instanceof FarmBlock && p_23197_.hasFarmSeeds()) {
@@ -112,9 +_,23 @@
if ($$11 instanceof BlockItem) {
BlockItem blockitem = (BlockItem)$$11;
BlockState blockstate1 = blockitem.getBlock().defaultBlockState();
- p_23196_.setBlockAndUpdate(this.aboveFarmlandPos, blockstate1);
- p_23196_.gameEvent(GameEvent.BLOCK_PLACE, this.aboveFarmlandPos, GameEvent.Context.of(p_23197_, blockstate1));
- flag = true;
+
+ if (CraftEventFactory.callEntityChangeBlockEvent(p_23197_, this.aboveFarmlandPos, blockstate1)) { // CraftBukkit
+ p_23196_.setBlockAndUpdate(this.aboveFarmlandPos, blockstate1);
+ p_23196_.gameEvent(GameEvent.BLOCK_PLACE, this.aboveFarmlandPos, GameEvent.Context.of(p_23197_, blockstate1));
+ flag = true;
+ } // CraftBukkit
+ } else if (itemstack.getItem() instanceof net.minecraftforge.common.IPlantable plantable) { //Ketting - variable name
+ if (plantable.getPlantType(p_23196_, aboveFarmlandPos) == net.minecraftforge.common.PlantType.CROP) {
+ //Ketting start - fire bukkit event
+ BlockState blockstate2 = plantable.getPlant(p_23196_, aboveFarmlandPos);
+
+ if (CraftEventFactory.callEntityChangeBlockEvent(p_23197_, this.aboveFarmlandPos, blockstate2)) { // CraftBukkit
+ p_23196_.setBlock(aboveFarmlandPos, blockstate2, 3);
+ flag = true;
+ }
+ //Ketting end
+ }
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--- a/net/minecraft/world/entity/ai/behavior/InteractWithDoor.java
+++ b/net/minecraft/world/entity/ai/behavior/InteractWithDoor.java
@@ -54,6 +_,13 @@
})) {
DoorBlock doorblock = (DoorBlock)blockstate.getBlock();
if (!doorblock.isOpen(blockstate)) {
+ // CraftBukkit start - entities opening doors
+ org.bukkit.event.entity.EntityInteractEvent event = new org.bukkit.event.entity.EntityInteractEvent(p_258470_.getBukkitEntity(), org.bukkit.craftbukkit.v1_20_R2.block.CraftBlock.at(p_258470_.level(), blockpos));
+ p_258470_.level().getCraftServer().getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ return false;
+ }
+ // CraftBukkit end
doorblock.setOpen(p_258470_, p_258469_, blockstate, blockpos, true);
}

@@ -67,6 +_,13 @@
})) {
DoorBlock doorblock1 = (DoorBlock)blockstate1.getBlock();
if (!doorblock1.isOpen(blockstate1)) {
+ // CraftBukkit start - entities opening doors
+ org.bukkit.event.entity.EntityInteractEvent event = new org.bukkit.event.entity.EntityInteractEvent(p_258470_.getBukkitEntity(), org.bukkit.craftbukkit.v1_20_R2.block.CraftBlock.at(p_258470_.level(), blockpos1));
+ p_258470_.level().getCraftServer().getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ return false;
+ }
+ // CraftBukkit end
doorblock1.setOpen(p_258470_, p_258469_, blockstate1, blockpos1, true);
optional = rememberDoorToClose(p_258461_, optional, p_258469_, blockpos1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--- a/net/minecraft/world/entity/ai/behavior/PrepareRamNearestTarget.java
+++ b/net/minecraft/world/entity/ai/behavior/PrepareRamNearestTarget.java
@@ -10,6 +_,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
+import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
@@ -25,6 +_,9 @@
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
import net.minecraft.world.phys.Vec3;
+import org.bukkit.craftbukkit.v1_20_R2.entity.CraftLivingEntity;
+import org.bukkit.craftbukkit.v1_20_R2.event.CraftEventFactory;
+import org.bukkit.event.entity.EntityTargetEvent;

public class PrepareRamNearestTarget<E extends PathfinderMob> extends Behavior<E> {
public static final int TIME_OUT_DURATION = 160;
@@ -56,6 +_,13 @@
return this.ramTargeting.test(p_147737_, p_147789_);
});
}).ifPresent((p_147778_) -> {
+ // CraftBukkit start
+ EntityTargetEvent event = CraftEventFactory.callEntityTargetLivingEvent(p_147737_, p_147778_, (p_147778_ instanceof ServerPlayer) ? EntityTargetEvent.TargetReason.CLOSEST_PLAYER : EntityTargetEvent.TargetReason.CLOSEST_ENTITY);
+ if (event.isCancelled() || event.getTarget() == null) {
+ return;
+ }
+ p_147778_ = ((CraftLivingEntity) event.getTarget()).getHandle();
+ // CraftBukkit end
this.chooseRamPosition(p_147737_, p_147778_);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--- a/net/minecraft/world/entity/ai/behavior/ResetProfession.java
+++ b/net/minecraft/world/entity/ai/behavior/ResetProfession.java
@@ -5,6 +_,9 @@
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.npc.VillagerData;
import net.minecraft.world.entity.npc.VillagerProfession;
+import org.bukkit.craftbukkit.v1_20_R2.entity.CraftVillager;
+import org.bukkit.craftbukkit.v1_20_R2.event.CraftEventFactory;
+import org.bukkit.event.entity.VillagerCareerChangeEvent;

public class ResetProfession {
public static BehaviorControl<Villager> create() {
@@ -13,7 +_,14 @@
return (p_260244_, p_260084_, p_259597_) -> {
VillagerData villagerdata = p_260084_.getVillagerData();
if (villagerdata.getProfession() != VillagerProfession.NONE && villagerdata.getProfession() != VillagerProfession.NITWIT && p_260084_.getVillagerXp() == 0 && villagerdata.getLevel() <= 1) {
- p_260084_.setVillagerData(p_260084_.getVillagerData().setProfession(VillagerProfession.NONE));
+ // CraftBukkit start
+ VillagerCareerChangeEvent event = CraftEventFactory.callVillagerCareerChangeEvent(p_260084_, CraftVillager.CraftProfession.minecraftToBukkit(VillagerProfession.NONE), VillagerCareerChangeEvent.ChangeReason.LOSING_JOB);
+ if (event.isCancelled()) {
+ return false;
+ }
+
+ p_260084_.setVillagerData(p_260084_.getVillagerData().setProfession(CraftVillager.CraftProfession.bukkitToMinecraft(event.getProfession())));
+ // CraftBukkit end
p_260084_.refreshBrain(p_260244_);
return true;
} else {
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
--- a/net/minecraft/world/entity/ai/behavior/StartAttacking.java
+++ b/net/minecraft/world/entity/ai/behavior/StartAttacking.java
@@ -30,7 +_,11 @@
@@ -3,10 +_,15 @@
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
+
+import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
+import org.bukkit.craftbukkit.v1_20_R2.entity.CraftLivingEntity;
+import org.bukkit.craftbukkit.v1_20_R2.event.CraftEventFactory;
+import org.bukkit.event.entity.EntityTargetEvent;

public class StartAttacking {
public static <E extends Mob> BehaviorControl<E> create(Function<E, Optional<? extends LivingEntity>> p_259868_) {
@@ -30,7 +_,22 @@
if (!p_258774_.canAttack(livingentity)) {
return false;
} else {
Expand All @@ -9,7 +25,18 @@
+ if (changeTargetEvent.isCanceled())
+ return false;
+
+ p_258778_.set(changeTargetEvent.getNewTarget());
+ // CraftBukkit start
+ EntityTargetEvent event = CraftEventFactory.callEntityTargetLivingEvent(p_258774_, changeTargetEvent.getNewTarget(), (changeTargetEvent.getNewTarget() instanceof ServerPlayer) ? EntityTargetEvent.TargetReason.CLOSEST_PLAYER : EntityTargetEvent.TargetReason.CLOSEST_ENTITY); //Ketting - forge compat
+ if (event.isCancelled()) {
+ return false;
+ }
+ if (event.getTarget() == null) {
+ p_258779_.erase();
+ return true;
+ }
+ livingentity = ((CraftLivingEntity) event.getTarget()).getHandle();
+ // CraftBukkit end
+ p_258778_.set(livingentity); //Ketting
p_258779_.erase();
return true;
}
Loading

0 comments on commit f7e9306

Please sign in to comment.