SPIGOT-6316: PlayerBedLeaveEvent implements Cancellable

By: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot
2021-01-29 11:22:42 +11:00
parent 7405148b11
commit 022d868fa2
2 changed files with 92 additions and 105 deletions

View File

@@ -1,6 +1,6 @@
--- a/net/minecraft/server/EntityPlayer.java
+++ b/net/minecraft/server/EntityPlayer.java
@@ -15,6 +15,27 @@
@@ -15,6 +15,28 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -15,6 +15,7 @@
+import org.bukkit.craftbukkit.event.CraftPortalEvent;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.event.inventory.InventoryType;
+import org.bukkit.event.player.PlayerBedLeaveEvent;
+import org.bukkit.event.player.PlayerChangedMainHandEvent;
+import org.bukkit.event.player.PlayerChangedWorldEvent;
+import org.bukkit.event.player.PlayerGameModeChangeEvent;
@@ -28,7 +29,7 @@
public class EntityPlayer extends EntityHuman implements ICrafting {
private static final Logger LOGGER = LogManager.getLogger();
@@ -60,6 +81,20 @@
@@ -60,6 +82,20 @@
public int ping;
public boolean viewingCredits;
@@ -49,7 +50,7 @@
public EntityPlayer(MinecraftServer minecraftserver, WorldServer worldserver, GameProfile gameprofile, PlayerInteractManager playerinteractmanager) {
super(worldserver, worldserver.getSpawn(), worldserver.v(), gameprofile);
this.spawnDimension = World.OVERWORLD;
@@ -71,12 +106,56 @@
@@ -71,12 +107,56 @@
this.G = 1.0F;
this.c(worldserver);
this.co = minecraftserver.a(this);
@@ -58,8 +59,8 @@
+ this.displayName = this.getName();
+ this.canPickUpLoot = true;
+ this.maxHealthCache = this.getMaxHealth();
+ }
+
}
+ // Yes, this doesn't match Vanilla, but it's the best we can do for now.
+ // If this is an issue, PRs are welcome
+ public final BlockPosition getSpawnPoint(WorldServer worldserver) {
@@ -96,9 +97,9 @@
+ }
+
+ return blockposition;
}
+ }
+ // CraftBukkit end
+
private void c(WorldServer worldserver) {
BlockPosition blockposition = worldserver.getSpawn();
@@ -107,7 +108,7 @@
int i = Math.max(0, this.server.a(worldserver));
int j = MathHelper.floor(worldserver.getWorldBorder().b((double) blockposition.getX(), (double) blockposition.getZ()));
@@ -142,11 +221,20 @@
@@ -142,11 +222,20 @@
if (nbttagcompound.hasKeyOfType("recipeBook", 10)) {
this.recipeBook.a(nbttagcompound.getCompound("recipeBook"), this.server.getCraftingManager());
}
@@ -128,7 +129,7 @@
if (nbttagcompound.hasKeyOfType("SpawnX", 99) && nbttagcompound.hasKeyOfType("SpawnY", 99) && nbttagcompound.hasKeyOfType("SpawnZ", 99)) {
this.spawn = new BlockPosition(nbttagcompound.getInt("SpawnX"), nbttagcompound.getInt("SpawnY"), nbttagcompound.getInt("SpawnZ"));
this.spawnForced = nbttagcompound.getBoolean("SpawnForced");
@@ -180,7 +268,20 @@
@@ -180,7 +269,20 @@
Entity entity = this.getRootVehicle();
Entity entity1 = this.getVehicle();
@@ -150,7 +151,7 @@
NBTTagCompound nbttagcompound2 = new NBTTagCompound();
NBTTagCompound nbttagcompound3 = new NBTTagCompound();
@@ -198,7 +299,7 @@
@@ -198,7 +300,7 @@
nbttagcompound.setInt("SpawnZ", this.spawn.getZ());
nbttagcompound.setBoolean("SpawnForced", this.spawnForced);
nbttagcompound.setFloat("SpawnAngle", this.spawnAngle);
@@ -159,7 +160,7 @@
Logger logger = EntityPlayer.LOGGER;
logger.getClass();
@@ -206,9 +307,33 @@
@@ -206,9 +308,33 @@
nbttagcompound.set("SpawnDimension", nbtbase);
});
}
@@ -193,7 +194,7 @@
public void a(int i) {
float f = (float) this.getExpToLevel();
float f1 = (f - 1.0F) / f;
@@ -262,6 +387,11 @@
@@ -262,6 +388,11 @@
@Override
public void tick() {
@@ -205,7 +206,7 @@
this.playerInteractManager.a();
--this.invulnerableTicks;
if (this.noDamageTicks > 0) {
@@ -329,7 +459,7 @@
@@ -329,7 +460,7 @@
}
if (this.getHealth() != this.lastHealthSent || this.lastFoodSent != this.foodData.getFoodLevel() || this.foodData.getSaturationLevel() == 0.0F != this.lastSentSaturationZero) {
@@ -214,7 +215,7 @@
this.lastHealthSent = this.getHealth();
this.lastFoodSent = this.foodData.getFoodLevel();
this.lastSentSaturationZero = this.foodData.getSaturationLevel() == 0.0F;
@@ -360,6 +490,12 @@
@@ -360,6 +491,12 @@
this.a(IScoreboardCriteria.XP, MathHelper.f((float) this.lastExpTotalScored));
}
@@ -227,7 +228,7 @@
if (this.expLevel != this.lastExpLevelScored) {
this.lastExpLevelScored = this.expLevel;
this.a(IScoreboardCriteria.LEVEL, MathHelper.f((float) this.lastExpLevelScored));
@@ -374,6 +510,16 @@
@@ -374,6 +511,16 @@
CriterionTriggers.p.a(this);
}
@@ -244,7 +245,7 @@
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.a(throwable, "Ticking player");
CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Player being ticked");
@@ -384,7 +530,8 @@
@@ -384,7 +531,8 @@
}
private void a(IScoreboardCriteria iscoreboardcriteria, int i) {
@@ -254,7 +255,7 @@
scoreboardscore.setScore(i);
});
}
@@ -392,9 +539,46 @@
@@ -392,9 +540,46 @@
@Override
public void die(DamageSource damagesource) {
boolean flag = this.world.getGameRules().getBoolean(GameRules.SHOW_DEATH_MESSAGES);
@@ -303,7 +304,7 @@
this.playerConnection.a((Packet) (new PacketPlayOutCombatEvent(this.getCombatTracker(), PacketPlayOutCombatEvent.EnumCombatEventType.ENTITY_DIED, ichatbasecomponent)), (future) -> {
if (!future.isSuccess()) {
@@ -428,12 +612,18 @@
@@ -428,12 +613,18 @@
if (this.world.getGameRules().getBoolean(GameRules.FORGIVE_DEAD_PLAYERS)) {
this.eW();
}
@@ -326,7 +327,7 @@
EntityLiving entityliving = this.getKillingEntity();
if (entityliving != null) {
@@ -469,10 +659,12 @@
@@ -469,10 +660,12 @@
String s = this.getName();
String s1 = entity.getName();
@@ -341,7 +342,7 @@
} else {
this.a(StatisticList.MOB_KILLS);
}
@@ -490,7 +682,8 @@
@@ -490,7 +683,8 @@
int i = scoreboardteam.getColor().b();
if (i >= 0 && i < aiscoreboardcriteria.length) {
@@ -351,7 +352,7 @@
}
}
@@ -534,18 +727,20 @@
@@ -534,18 +728,20 @@
}
private boolean canPvP() {
@@ -375,7 +376,7 @@
} else {
return shapedetectorshape;
}
@@ -554,11 +749,20 @@
@@ -554,11 +750,20 @@
@Nullable
@Override
public Entity b(WorldServer worldserver) {
@@ -399,7 +400,7 @@
this.decouple();
this.getWorldServer().removePlayer(this);
if (!this.viewingCredits) {
@@ -569,6 +773,8 @@
@@ -569,6 +774,8 @@
return this;
} else {
@@ -408,7 +409,7 @@
WorldData worlddata = worldserver.getWorldData();
this.playerConnection.sendPacket(new PacketPlayOutRespawn(worldserver.getDimensionManager(), worldserver.getDimensionKey(), BiomeManager.a(worldserver.getSeed()), this.playerInteractManager.getGameMode(), this.playerInteractManager.c(), worldserver.isDebugWorld(), worldserver.isFlatWorld(), true));
@@ -578,22 +784,52 @@
@@ -578,22 +785,52 @@
playerlist.d(this);
worldserver1.removePlayer(this);
this.dead = false;
@@ -467,7 +468,7 @@
worldserver1.getMethodProfiler().exit();
this.triggerDimensionAdvancements(worldserver1);
this.playerInteractManager.a(worldserver);
@@ -612,12 +848,31 @@
@@ -612,12 +849,31 @@
this.lastSentExp = -1;
this.lastHealthSent = -1.0F;
this.lastFoodSent = -1;
@@ -499,7 +500,7 @@
private void a(WorldServer worldserver, BlockPosition blockposition) {
BlockPosition.MutableBlockPosition blockposition_mutableblockposition = blockposition.i();
@@ -634,17 +889,17 @@
@@ -634,17 +890,17 @@
}
@Override
@@ -522,7 +523,7 @@
}
return optional1;
@@ -688,10 +943,8 @@
@@ -688,10 +944,8 @@
this.activeContainer.c();
}
@@ -535,7 +536,7 @@
if (!this.isSleeping() && this.isAlive()) {
if (!this.world.getDimensionManager().isNatural()) {
return Either.left(EntityHuman.EnumBedResult.NOT_POSSIBLE_HERE);
@@ -717,7 +970,36 @@
@@ -717,7 +971,36 @@
}
}
@@ -573,7 +574,7 @@
this.a(StatisticList.SLEEP_IN_BED);
CriterionTriggers.q.a(this);
});
@@ -726,9 +1008,8 @@
@@ -726,9 +1009,8 @@
return either;
}
}
@@ -584,15 +585,32 @@
}
@Override
@@ -755,6 +1036,7 @@
@@ -755,6 +1037,24 @@
@Override
public void wakeup(boolean flag, boolean flag1) {
+ if (!this.isSleeping()) return; // CraftBukkit - Can't leave bed if not in one!
+ // CraftBukkit start - fire PlayerBedLeaveEvent
+ CraftPlayer player = this.getBukkitEntity();
+ BlockPosition bedPosition = this.getBedPosition().orElse(null);
+
+ org.bukkit.block.Block bed;
+ if (bedPosition != null) {
+ bed = this.world.getWorld().getBlockAt(bedPosition.getX(), bedPosition.getY(), bedPosition.getZ());
+ } else {
+ bed = this.world.getWorld().getBlockAt(player.getLocation());
+ }
+
+ PlayerBedLeaveEvent event = new PlayerBedLeaveEvent(player, bed, true);
+ this.world.getServer().getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ return;
+ }
+ // CraftBukkit end
if (this.isSleeping()) {
this.getWorldServer().getChunkProvider().broadcastIncludingSelf(this, new PacketPlayOutAnimation(this, 2));
}
@@ -826,8 +1108,9 @@
@@ -826,8 +1126,9 @@
this.playerConnection.sendPacket(new PacketPlayOutOpenSignEditor(tileentitysign.getPosition()));
}
@@ -603,7 +621,7 @@
}
@Override
@@ -842,6 +1125,24 @@
@@ -842,6 +1143,24 @@
this.nextContainerCounter();
Container container = itileinventory.createMenu(this.containerCounter, this.inventory, this);
@@ -628,7 +646,7 @@
if (container == null) {
if (this.isSpectator()) {
this.a((IChatBaseComponent) (new ChatMessage("container.spectatorCantOpen")).a(EnumChatFormat.RED), true);
@@ -849,9 +1150,11 @@
@@ -849,9 +1168,11 @@
return OptionalInt.empty();
} else {
@@ -642,7 +660,7 @@
return OptionalInt.of(this.containerCounter);
}
}
@@ -864,13 +1167,24 @@
@@ -864,13 +1185,24 @@
@Override
public void openHorseInventory(EntityHorseAbstract entityhorseabstract, IInventory iinventory) {
@@ -669,7 +687,7 @@
this.activeContainer.addSlotListener(this);
}
@@ -915,6 +1229,11 @@
@@ -915,6 +1247,11 @@
public void a(Container container, NonNullList<ItemStack> nonnulllist) {
this.playerConnection.sendPacket(new PacketPlayOutWindowItems(container.windowId, nonnulllist));
this.playerConnection.sendPacket(new PacketPlayOutSetSlot(-1, -1, this.inventory.getCarried()));
@@ -681,7 +699,7 @@
}
@Override
@@ -924,6 +1243,7 @@
@@ -924,6 +1261,7 @@
@Override
public void closeInventory() {
@@ -689,7 +707,7 @@
this.playerConnection.sendPacket(new PacketPlayOutCloseWindow(this.activeContainer.windowId));
this.o();
}
@@ -958,7 +1278,7 @@
@@ -958,7 +1296,7 @@
@Override
public void a(Statistic<?> statistic, int i) {
this.serverStatisticManager.b(this, statistic, i);
@@ -698,7 +716,7 @@
scoreboardscore.addScore(i);
});
}
@@ -966,7 +1286,7 @@
@@ -966,7 +1304,7 @@
@Override
public void a(Statistic<?> statistic) {
this.serverStatisticManager.setStatistic(this, statistic, 0);
@@ -707,25 +725,24 @@
}
@Override
@@ -1015,8 +1335,17 @@
@@ -1015,7 +1353,16 @@
public void triggerHealthUpdate() {
this.lastHealthSent = -1.0E8F;
+ this.lastSentExp = -1; // CraftBukkit - Added to reset
}
+ }
+
+ // CraftBukkit start - Support multi-line messages
+ public void sendMessage(UUID uuid, IChatBaseComponent[] ichatbasecomponent) {
+ for (IChatBaseComponent component : ichatbasecomponent) {
+ this.sendMessage(component, (uuid == null) ? SystemUtils.b : uuid);
+ }
+ }
}
+ // CraftBukkit end
+
@Override
public void a(IChatBaseComponent ichatbasecomponent, boolean flag) {
this.playerConnection.sendPacket(new PacketPlayOutChat(ichatbasecomponent, flag ? ChatMessageType.GAME_INFO : ChatMessageType.CHAT, SystemUtils.b));
@@ -1068,12 +1397,13 @@
@@ -1068,12 +1415,13 @@
this.lastSentExp = -1;
this.lastHealthSent = -1.0F;
this.lastFoodSent = -1;
@@ -740,7 +757,7 @@
}
@Override
@@ -1141,6 +1471,18 @@
@@ -1141,6 +1489,18 @@
@Override
public void a(EnumGamemode enumgamemode) {
@@ -759,7 +776,7 @@
this.playerInteractManager.setGameMode(enumgamemode);
this.playerConnection.sendPacket(new PacketPlayOutGameStateChange(PacketPlayOutGameStateChange.d, (float) enumgamemode.getId()));
if (enumgamemode == EnumGamemode.SPECTATOR) {
@@ -1190,7 +1532,20 @@
@@ -1190,7 +1550,20 @@
return s;
}
@@ -780,7 +797,7 @@
this.bY = packetplayinsettings.d();
this.bZ = packetplayinsettings.e();
this.getDataWatcher().set(EntityPlayer.bi, (byte) packetplayinsettings.f());
@@ -1226,13 +1581,13 @@
@@ -1226,13 +1599,13 @@
if (entity instanceof EntityHuman) {
this.playerConnection.sendPacket(new PacketPlayOutEntityDestroy(new int[]{entity.getId()}));
} else {
@@ -796,7 +813,7 @@
}
@Override
@@ -1256,7 +1611,7 @@
@@ -1256,7 +1629,7 @@
this.spectatedEntity = (Entity) (entity == null ? this : entity);
if (entity1 != this.spectatedEntity) {
this.playerConnection.sendPacket(new PacketPlayOutCamera(this.spectatedEntity));
@@ -805,7 +822,7 @@
}
}
@@ -1285,7 +1640,7 @@
@@ -1285,7 +1658,7 @@
@Nullable
public IChatBaseComponent getPlayerListName() {
@@ -814,7 +831,7 @@
}
@Override
@@ -1306,9 +1661,16 @@
@@ -1306,9 +1679,16 @@
return this.advancementDataPlayer;
}
@@ -831,7 +848,7 @@
if (worldserver == this.world) {
this.playerConnection.a(d0, d1, d2, f, f1);
} else {
@@ -1329,6 +1691,9 @@
@@ -1329,6 +1709,9 @@
this.server.getPlayerList().a(this, worldserver);
this.server.getPlayerList().updateClient(this);
}
@@ -841,7 +858,7 @@
}
@@ -1426,4 +1791,144 @@
@@ -1426,4 +1809,144 @@
public ITextFilter Q() {
return this.co;
}