SPIGOT-4395: Additions to PlayerBedEnterEvent.

Contributions by blablubbabc as well - https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/pull-requests/500/overview
This commit is contained in:
Ugleh 2018-11-02 18:31:00 +11:00 committed by md_5
parent aed3aecbaf
commit a8000588cb
3 changed files with 152 additions and 42 deletions

View File

@ -0,0 +1,45 @@
--- a/net/minecraft/server/BlockBed.java
+++ b/net/minecraft/server/BlockBed.java
@@ -32,7 +32,8 @@
}
}
- if (world.worldProvider.canRespawn() && world.getBiome(blockposition) != Biomes.NETHER) {
+ // CraftBukkit - moved world and biome check into EntityHuman
+ if (true || world.worldProvider.canRespawn() && world.getBiome(blockposition) != Biomes.NETHER) {
if (((Boolean) iblockdata.get(BlockBed.OCCUPIED)).booleanValue()) {
EntityHuman entityhuman1 = this.a(world, blockposition);
@@ -59,10 +60,22 @@
} else if (entityhuman_enumbedresult == EntityHuman.EnumBedResult.TOO_FAR_AWAY) {
entityhuman.a((IChatBaseComponent) (new ChatMessage("block.minecraft.bed.too_far_away", new Object[0])), true);
}
+ // CraftBukkit start - handling bed explosion from below here
+ else if (entityhuman_enumbedresult == EntityHuman.EnumBedResult.NOT_POSSIBLE_HERE) {
+ this.explodeBed(iblockdata, world, blockposition);
+ }
+ // CraftBukkit end
return true;
}
+ // CraftBukkit start - moved bed explosion into separate method
} else {
+ return true;
+ }
+ }
+ }
+
+ private boolean explodeBed(IBlockData iblockdata, World world, BlockPosition blockposition) {
world.setAir(blockposition);
BlockPosition blockposition1 = blockposition.shift(((EnumDirection) iblockdata.get(BlockBed.FACING)).opposite());
@@ -72,8 +85,7 @@
world.createExplosion((Entity) null, DamageSource.a(), (double) blockposition.getX() + 0.5D, (double) blockposition.getY() + 0.5D, (double) blockposition.getZ() + 0.5D, 5.0F, true, true);
return true;
- }
- }
+ // CraftBukkit end
}
@Nullable

View File

@ -1,6 +1,6 @@
--- a/net/minecraft/server/EntityHuman.java
+++ b/net/minecraft/server/EntityHuman.java
@@ -15,6 +15,19 @@
@@ -15,6 +15,18 @@
import java.util.function.ToIntFunction;
import javax.annotation.Nullable;
@ -10,7 +10,6 @@
+import org.bukkit.entity.Player;
+import org.bukkit.event.entity.CreatureSpawnEvent;
+import org.bukkit.event.entity.EntityCombustByEntityEvent;
+import org.bukkit.event.player.PlayerBedEnterEvent;
+import org.bukkit.event.player.PlayerBedLeaveEvent;
+import org.bukkit.event.player.PlayerDropItemEvent;
+import org.bukkit.event.player.PlayerVelocityEvent;
@ -20,7 +19,7 @@
public abstract class EntityHuman extends EntityLiving {
private static final DataWatcherObject<Float> a = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.c);
@@ -24,10 +37,10 @@
@@ -24,10 +36,10 @@
protected static final DataWatcherObject<NBTTagCompound> bz = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.p);
protected static final DataWatcherObject<NBTTagCompound> bA = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.p);
public PlayerInventory inventory = new PlayerInventory(this);
@ -33,7 +32,7 @@
protected int bG;
public float bH;
public float bI;
@@ -60,6 +73,17 @@
@@ -60,6 +72,17 @@
@Nullable
public EntityFishingHook hookedFish;
@ -51,7 +50,7 @@
public EntityHuman(World world, GameProfile gameprofile) {
super(EntityTypes.PLAYER, world);
this.cd = ItemStack.a;
@@ -184,7 +208,7 @@
@@ -184,7 +207,7 @@
ItemStack itemstack = this.getEquipment(EnumItemSlot.HEAD);
if (itemstack.getItem() == Items.TURTLE_HELMET && !this.a(TagsFluid.WATER)) {
@ -60,7 +59,7 @@
}
}
@@ -369,7 +393,8 @@
@@ -369,7 +392,8 @@
if (this.world.getDifficulty() == EnumDifficulty.PEACEFUL && this.world.getGameRules().getBoolean("naturalRegeneration")) {
if (this.getHealth() < this.getMaxHealth() && this.ticksLived % 20 == 0) {
@ -70,7 +69,7 @@
}
if (this.foodData.c() && this.ticksLived % 10 == 0) {
@@ -393,7 +418,7 @@
@@ -393,7 +417,7 @@
this.o((float) attributeinstance.getValue());
float f = MathHelper.sqrt(this.motX * this.motX + this.motZ * this.motZ);
@ -79,7 +78,7 @@
if (f > 0.1F) {
f = 0.1F;
@@ -516,6 +541,7 @@
@@ -516,6 +540,7 @@
@Nullable
public EntityItem a(boolean flag) {
@ -87,7 +86,7 @@
return this.a(this.inventory.splitStack(this.inventory.itemInHandIndex, flag && !this.inventory.getItemInHand().isEmpty() ? this.inventory.getItemInHand().getCount() : 1), false, true);
}
@@ -558,6 +584,30 @@
@@ -558,6 +583,30 @@
entityitem.motZ += Math.sin((double) f1) * (double) f;
}
@ -118,7 +117,7 @@
ItemStack itemstack1 = this.a(entityitem);
if (flag1) {
@@ -655,6 +705,13 @@
@@ -655,6 +704,13 @@
this.a(true, true, false);
}
@ -132,7 +131,7 @@
if (nbttagcompound.hasKeyOfType("SpawnX", 99) && nbttagcompound.hasKeyOfType("SpawnY", 99) && nbttagcompound.hasKeyOfType("SpawnZ", 99)) {
this.e = new BlockPosition(nbttagcompound.getInt("SpawnX"), nbttagcompound.getInt("SpawnY"), nbttagcompound.getInt("SpawnZ"));
this.f = nbttagcompound.getBoolean("SpawnForced");
@@ -705,6 +762,7 @@
@@ -705,6 +761,7 @@
if (!this.getShoulderEntityRight().isEmpty()) {
nbttagcompound.set("ShoulderEntityRight", this.getShoulderEntityRight());
}
@ -140,7 +139,7 @@
}
@@ -722,10 +780,10 @@
@@ -722,10 +779,10 @@
this.a(true, true, false);
}
@ -153,7 +152,7 @@
}
if (this.world.getDifficulty() == EnumDifficulty.EASY) {
@@ -737,7 +795,13 @@
@@ -737,7 +794,13 @@
}
}
@ -168,7 +167,7 @@
}
}
}
@@ -751,10 +815,29 @@
@@ -751,10 +814,29 @@
}
public boolean a(EntityHuman entityhuman) {
@ -201,7 +200,7 @@
}
protected void damageArmor(float f) {
@@ -797,7 +880,12 @@
@@ -797,7 +879,12 @@
return (float) i / (float) this.inventory.armor.size();
}
@ -215,7 +214,7 @@
if (!this.isInvulnerable(damagesource)) {
f = this.applyArmorModifier(damagesource, f);
f = this.applyMagicModifier(damagesource, f);
@@ -823,6 +911,7 @@
@@ -823,6 +910,7 @@
}
}
@ -223,7 +222,7 @@
}
public void openSign(TileEntitySign tileentitysign) {}
@@ -944,8 +1033,15 @@
@@ -944,8 +1032,15 @@
if (entity instanceof EntityLiving) {
f3 = ((EntityLiving) entity).getHealth();
if (j > 0 && !entity.isBurning()) {
@ -241,7 +240,7 @@
}
}
@@ -976,8 +1072,11 @@
@@ -976,8 +1071,11 @@
EntityLiving entityliving = (EntityLiving) iterator.next();
if (entityliving != this && entityliving != entity && !this.r(entityliving) && (!(entityliving instanceof EntityArmorStand) || !((EntityArmorStand) entityliving).isMarker()) && this.h(entityliving) < 9.0D) {
@ -254,7 +253,7 @@
}
}
@@ -986,11 +1085,28 @@
@@ -986,11 +1084,28 @@
}
if (entity instanceof EntityPlayer && entity.velocityChanged) {
@ -283,7 +282,7 @@
}
if (flag2) {
@@ -1039,7 +1155,14 @@
@@ -1039,7 +1154,14 @@
this.a(StatisticList.DAMAGE_DEALT, Math.round(f5 * 10.0F));
if (j > 0) {
@ -299,7 +298,7 @@
}
if (this.world instanceof WorldServer && f5 > 2.0F) {
@@ -1055,6 +1178,11 @@
@@ -1055,6 +1177,11 @@
if (flag4) {
entity.extinguish();
}
@ -311,28 +310,54 @@
}
}
@@ -1151,6 +1279,20 @@
this.stopRiding();
}
@@ -1116,15 +1243,15 @@
return this.h;
}
+ // CraftBukkit start - fire PlayerBedEnterEvent
- public EntityHuman.EnumBedResult a(BlockPosition blockposition) {
- EnumDirection enumdirection = (EnumDirection) this.world.getType(blockposition).get(BlockFacingHorizontal.FACING);
-
+ // CraftBukkit start - moved bed result checks from below into separate method
+ private EntityHuman.EnumBedResult getBedResult(BlockPosition blockposition, EnumDirection enumdirection) {
if (!this.world.isClientSide) {
if (this.isSleeping() || !this.isAlive()) {
return EntityHuman.EnumBedResult.OTHER_PROBLEM;
}
- if (!this.world.worldProvider.isOverworld()) {
+ // CraftBukkit - moved world and biome check from BlockBed interact handling here
+ if (!world.worldProvider.canRespawn() || world.getBiome(blockposition) == Biomes.NETHER || !this.world.worldProvider.isOverworld()) {
return EntityHuman.EnumBedResult.NOT_POSSIBLE_HERE;
}
@@ -1146,6 +1273,26 @@
}
}
}
+ return EntityHuman.EnumBedResult.OK;
+ }
+
+ public EntityHuman.EnumBedResult a(BlockPosition blockposition) {
+ EnumDirection enumdirection = (EnumDirection) this.world.getType(blockposition).get(BlockFacingHorizontal.FACING);
+ // CraftBukkit start - moved checks into separate method above
+ EntityHuman.EnumBedResult bedResult = this.getBedResult(blockposition, enumdirection);
+
+ if (bedResult == EntityHuman.EnumBedResult.OTHER_PROBLEM) {
+ return bedResult; // return immediately if the result is not bypassable by plugins
+ }
+
+ if (this.getBukkitEntity() instanceof Player) {
+ Player player = (Player) this.getBukkitEntity();
+ org.bukkit.block.Block bed = this.world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ bedResult = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerBedEnterEvent(this, blockposition, bedResult);
+
+ PlayerBedEnterEvent event = new PlayerBedEnterEvent(player, bed);
+ this.world.getServer().getPluginManager().callEvent(event);
+
+ if (event.isCancelled()) {
+ return EnumBedResult.OTHER_PROBLEM;
+ if (bedResult != EntityHuman.EnumBedResult.OK) {
+ return bedResult;
+ }
+ }
+ // CraftBukkit end
+
this.releaseShoulderEntities();
this.a(StatisticList.CUSTOM.b(StatisticList.TIME_SINCE_REST));
this.setSize(0.2F, 0.2F);
@@ -1212,6 +1354,24 @@
if (this.isPassenger()) {
this.stopRiding();
@@ -1212,6 +1359,24 @@
this.world.everyoneSleeping();
}
@ -357,7 +382,7 @@
this.sleepTicks = flag ? 0 : 100;
if (flag2) {
this.setRespawnPosition(this.bedPosition, false);
@@ -1263,9 +1423,11 @@
@@ -1263,9 +1428,11 @@
if (blockposition != null) {
this.e = blockposition;
this.f = flag;
@ -369,7 +394,7 @@
}
}
@@ -1331,7 +1493,11 @@
@@ -1331,7 +1498,11 @@
this.motY = d3 * 0.6D;
this.aU = f3;
this.fallDistance = 0.0F;
@ -382,7 +407,7 @@
} else {
super.a(f, f1, f2);
}
@@ -1631,13 +1797,17 @@
@@ -1631,13 +1802,17 @@
}
protected void releaseShoulderEntities() {
@ -405,7 +430,7 @@
if (!this.world.isClientSide && !nbttagcompound.isEmpty()) {
Entity entity = EntityTypes.a(nbttagcompound, this.world);
@@ -1646,9 +1816,10 @@
@@ -1646,9 +1821,10 @@
}
entity.setPosition(this.locX, this.locY + 0.699999988079071D, this.locZ);
@ -417,7 +442,7 @@
}
public abstract boolean isSpectator();
@@ -1859,7 +2030,7 @@
@@ -1859,7 +2035,7 @@
return entitymonster.c(this.a);
}

View File

@ -52,6 +52,7 @@ import org.bukkit.entity.ThrownExpBottle;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.*;
import org.bukkit.event.block.BlockIgniteEvent.IgniteCause;
import org.bukkit.event.entity.*;
@ -63,6 +64,7 @@ import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerBedEnterEvent.BedEnterResult;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.InventoryView;
@ -99,6 +101,44 @@ public class CraftEventFactory {
return event;
}
/**
* PlayerBedEnterEvent
*/
public static EntityHuman.EnumBedResult callPlayerBedEnterEvent(EntityHuman player, BlockPosition bed, EntityHuman.EnumBedResult nmsBedResult) {
BedEnterResult bedEnterResult;
switch (nmsBedResult) {
case OK:
bedEnterResult = BedEnterResult.OK;
break;
case NOT_POSSIBLE_HERE:
bedEnterResult = BedEnterResult.NOT_POSSIBLE_HERE;
break;
case NOT_POSSIBLE_NOW:
bedEnterResult = BedEnterResult.NOT_POSSIBLE_NOW;
break;
case TOO_FAR_AWAY:
bedEnterResult = BedEnterResult.TOO_FAR_AWAY;
break;
case NOT_SAFE:
bedEnterResult = BedEnterResult.NOT_SAFE;
break;
default:
bedEnterResult = BedEnterResult.OTHER_PROBLEM;
}
PlayerBedEnterEvent event = new PlayerBedEnterEvent((Player) player.getBukkitEntity(), CraftBlock.at(player.world, bed), bedEnterResult);
Bukkit.getServer().getPluginManager().callEvent(event);
Result result = event.useBed();
if (result == Result.ALLOW) {
return EntityHuman.EnumBedResult.OK;
} else if (result == Result.DENY) {
return EntityHuman.EnumBedResult.OTHER_PROBLEM;
}
return nmsBedResult;
}
/**
* Block place methods
*/