#1095: Add PlayerSpawnChangeEvent

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot
2023-04-17 19:33:17 +10:00
parent 41bd40bcaa
commit 9e30ea471f
5 changed files with 163 additions and 89 deletions

View File

@@ -1,6 +1,6 @@
--- a/net/minecraft/server/level/EntityPlayer.java
+++ b/net/minecraft/server/level/EntityPlayer.java
@@ -155,6 +155,36 @@
@@ -155,6 +155,37 @@
import net.minecraft.world.scores.criteria.IScoreboardCriteria;
import org.slf4j.Logger;
@@ -28,6 +28,7 @@
+import org.bukkit.event.player.PlayerChangedWorldEvent;
+import org.bukkit.event.player.PlayerLocaleChangeEvent;
+import org.bukkit.event.player.PlayerPortalEvent;
+import org.bukkit.event.player.PlayerSpawnChangeEvent;
+import org.bukkit.event.player.PlayerTeleportEvent;
+import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
+import org.bukkit.event.player.PlayerToggleSneakEvent;
@@ -37,7 +38,7 @@
public class EntityPlayer extends EntityHuman {
private static final Logger LOGGER = LogUtils.getLogger();
@@ -212,6 +242,21 @@
@@ -212,6 +243,21 @@
public int latency;
public boolean wonGame;
@@ -59,7 +60,7 @@
public EntityPlayer(MinecraftServer minecraftserver, WorldServer worldserver, GameProfile gameprofile) {
super(worldserver, worldserver.getSharedSpawnPos(), worldserver.getSharedSpawnAngle(), gameprofile);
this.chatVisibility = EnumChatVisibility.FULL;
@@ -275,12 +320,56 @@
@@ -275,12 +321,56 @@
this.advancements = minecraftserver.getPlayerList().getPlayerAdvancements(this);
this.setMaxUpStep(1.0F);
this.fudgeSpawnLocation(worldserver);
@@ -68,8 +69,8 @@
+ this.displayName = this.getScoreboardName();
+ this.bukkitPickUpLoot = 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) {
@@ -106,9 +107,9 @@
+ }
+
+ return blockposition;
+ }
}
+ // CraftBukkit end
+
private void fudgeSpawnLocation(WorldServer worldserver) {
BlockPosition blockposition = worldserver.getSharedSpawnPos();
@@ -117,7 +118,7 @@
int i = Math.max(0, this.server.getSpawnRadius(worldserver));
int j = MathHelper.floor(worldserver.getWorldBorder().getDistanceToBorder((double) blockposition.getX(), (double) blockposition.getZ()));
@@ -329,7 +418,7 @@
@@ -329,7 +419,7 @@
public void readAdditionalSaveData(NBTTagCompound nbttagcompound) {
super.readAdditionalSaveData(nbttagcompound);
if (nbttagcompound.contains("warden_spawn_tracker", 10)) {
@@ -126,7 +127,7 @@
Logger logger = EntityPlayer.LOGGER;
Objects.requireNonNull(logger);
@@ -348,17 +437,26 @@
@@ -348,17 +438,26 @@
if (nbttagcompound.contains("recipeBook", 10)) {
this.recipeBook.fromNbt(nbttagcompound.getCompound("recipeBook"), this.server.getRecipeManager());
}
@@ -154,7 +155,7 @@
Logger logger1 = EntityPlayer.LOGGER;
Objects.requireNonNull(logger1);
@@ -371,7 +469,7 @@
@@ -371,7 +470,7 @@
@Override
public void addAdditionalSaveData(NBTTagCompound nbttagcompound) {
super.addAdditionalSaveData(nbttagcompound);
@@ -163,7 +164,7 @@
Logger logger = EntityPlayer.LOGGER;
Objects.requireNonNull(logger);
@@ -392,7 +490,20 @@
@@ -392,7 +491,20 @@
Entity entity = this.getRootVehicle();
Entity entity1 = this.getVehicle();
@@ -185,13 +186,13 @@
NBTTagCompound nbttagcompound2 = new NBTTagCompound();
NBTTagCompound nbttagcompound3 = new NBTTagCompound();
@@ -417,9 +528,33 @@
@@ -417,8 +529,32 @@
nbttagcompound.put("SpawnDimension", nbtbase);
});
}
+ this.getBukkitEntity().setExtraData(nbttagcompound); // CraftBukkit
}
+
+ }
+ // CraftBukkit start - World fallback code, either respawn location or global spawn
+ public void spawnIn(World world) {
@@ -213,13 +214,12 @@
+ this.setPos(position.x(), position.y(), position.z());
+ }
+ this.gameMode.setLevel((WorldServer) world);
+ }
}
+ // CraftBukkit end
+
public void setExperiencePoints(int i) {
float f = (float) this.getXpNeededForNextLevel();
float f1 = (f - 1.0F) / f;
@@ -478,6 +613,11 @@
@@ -478,6 +614,11 @@
@Override
public void tick() {
@@ -231,7 +231,7 @@
this.gameMode.tick();
this.wardenSpawnTracker.tick();
--this.spawnInvulnerableTime;
@@ -534,7 +674,7 @@
@@ -534,7 +675,7 @@
}
if (this.getHealth() != this.lastSentHealth || this.lastSentFood != this.foodData.getFoodLevel() || this.foodData.getSaturationLevel() == 0.0F != this.lastFoodSaturationZero) {
@@ -240,7 +240,7 @@
this.lastSentHealth = this.getHealth();
this.lastSentFood = this.foodData.getFoodLevel();
this.lastFoodSaturationZero = this.foodData.getSaturationLevel() == 0.0F;
@@ -565,6 +705,12 @@
@@ -565,6 +706,12 @@
this.updateScoreForCriteria(IScoreboardCriteria.EXPERIENCE, MathHelper.ceil((float) this.lastRecordedExperience));
}
@@ -253,7 +253,7 @@
if (this.experienceLevel != this.lastRecordedLevel) {
this.lastRecordedLevel = this.experienceLevel;
this.updateScoreForCriteria(IScoreboardCriteria.LEVEL, MathHelper.ceil((float) this.lastRecordedLevel));
@@ -579,6 +725,20 @@
@@ -579,6 +726,20 @@
CriterionTriggers.LOCATION.trigger(this);
}
@@ -274,7 +274,7 @@
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.forThrowable(throwable, "Ticking player");
CrashReportSystemDetails crashreportsystemdetails = crashreport.addCategory("Player being ticked");
@@ -621,7 +781,8 @@
@@ -621,7 +782,8 @@
}
private void updateScoreForCriteria(IScoreboardCriteria iscoreboardcriteria, int i) {
@@ -284,7 +284,7 @@
scoreboardscore.setScore(i);
});
}
@@ -630,9 +791,47 @@
@@ -630,9 +792,47 @@
public void die(DamageSource damagesource) {
this.gameEvent(GameEvent.ENTITY_DIE);
boolean flag = this.level.getGameRules().getBoolean(GameRules.RULE_SHOWDEATHMESSAGES);
@@ -334,7 +334,7 @@
this.connection.send(new ClientboundPlayerCombatKillPacket(this.getCombatTracker(), ichatbasecomponent), PacketSendListener.exceptionallySend(() -> {
boolean flag1 = true;
@@ -663,12 +862,18 @@
@@ -663,12 +863,18 @@
if (this.level.getGameRules().getBoolean(GameRules.RULE_FORGIVE_DEAD_PLAYERS)) {
this.tellNeutralMobsThatIDied();
}
@@ -357,7 +357,7 @@
EntityLiving entityliving = this.getKillCredit();
if (entityliving != null) {
@@ -706,10 +911,12 @@
@@ -706,10 +912,12 @@
String s = this.getScoreboardName();
String s1 = entity.getScoreboardName();
@@ -372,7 +372,7 @@
} else {
this.awardStat(StatisticList.MOB_KILLS);
}
@@ -727,7 +934,8 @@
@@ -727,7 +935,8 @@
int i = scoreboardteam.getColor().getId();
if (i >= 0 && i < aiscoreboardcriteria.length) {
@@ -382,7 +382,7 @@
}
}
@@ -777,18 +985,20 @@
@@ -777,18 +986,20 @@
}
private boolean isPvpAllowed() {
@@ -406,7 +406,7 @@
} else {
return shapedetectorshape;
}
@@ -797,11 +1007,20 @@
@@ -797,11 +1008,20 @@
@Nullable
@Override
public Entity changeDimension(WorldServer worldserver) {
@@ -430,7 +430,7 @@
this.unRide();
this.getLevel().removePlayerImmediately(this, Entity.RemovalReason.CHANGED_DIMENSION);
if (!this.wonGame) {
@@ -812,6 +1031,8 @@
@@ -812,6 +1032,8 @@
return this;
} else {
@@ -439,7 +439,7 @@
WorldData worlddata = worldserver.getLevelData();
this.connection.send(new PacketPlayOutRespawn(worldserver.dimensionTypeId(), worldserver.dimension(), BiomeManager.obfuscateSeed(worldserver.getSeed()), this.gameMode.getGameModeForPlayer(), this.gameMode.getPreviousGameModeForPlayer(), worldserver.isDebug(), worldserver.isFlat(), (byte) 3, this.getLastDeathLocation()));
@@ -821,20 +1042,50 @@
@@ -821,20 +1043,50 @@
playerlist.sendPlayerPermissionLevel(this);
worldserver1.removePlayerImmediately(this, Entity.RemovalReason.CHANGED_DIMENSION);
this.unsetRemoved();
@@ -495,7 +495,7 @@
this.connection.resetPosition();
worldserver.addDuringPortalTeleport(this);
worldserver1.getProfiler().pop();
@@ -854,39 +1105,66 @@
@@ -854,39 +1106,66 @@
this.lastSentExp = -1;
this.lastSentHealth = -1.0F;
this.lastSentFood = -1;
@@ -568,7 +568,7 @@
}
return optional1;
@@ -896,13 +1174,21 @@
@@ -896,13 +1175,21 @@
public void triggerDimensionChangeTriggers(WorldServer worldserver) {
ResourceKey<World> resourcekey = worldserver.dimension();
ResourceKey<World> resourcekey1 = this.level.dimension();
@@ -593,7 +593,7 @@
this.enteredNetherPosition = null;
}
@@ -919,12 +1205,10 @@
@@ -919,19 +1206,17 @@
this.containerMenu.broadcastChanges();
}
@@ -609,7 +609,15 @@
return Either.left(EntityHuman.EnumBedResult.NOT_POSSIBLE_HERE);
} else if (!this.bedInRange(blockposition, enumdirection)) {
return Either.left(EntityHuman.EnumBedResult.TOO_FAR_AWAY);
@@ -948,7 +1232,36 @@
} else if (this.bedBlocked(blockposition, enumdirection)) {
return Either.left(EntityHuman.EnumBedResult.OBSTRUCTED);
} else {
- this.setRespawnPosition(this.level.dimension(), blockposition, this.getYRot(), false, true);
+ this.setRespawnPosition(this.level.dimension(), blockposition, this.getYRot(), false, true, PlayerSpawnChangeEvent.Cause.BED); // CraftBukkit
if (this.level.isDay()) {
return Either.left(EntityHuman.EnumBedResult.NOT_POSSIBLE_NOW);
} else {
@@ -948,7 +1233,36 @@
}
}
@@ -647,7 +655,7 @@
this.awardStat(StatisticList.SLEEP_IN_BED);
CriterionTriggers.SLEPT_IN_BED.trigger(this);
});
@@ -961,9 +1274,8 @@
@@ -961,9 +1275,8 @@
return either;
}
}
@@ -658,7 +666,7 @@
}
@Override
@@ -990,6 +1302,24 @@
@@ -990,6 +1303,24 @@
@Override
public void stopSleepInBed(boolean flag, boolean flag1) {
@@ -683,7 +691,7 @@
if (this.isSleeping()) {
this.getLevel().getChunkSource().broadcastAndSend(this, new PacketPlayOutAnimation(this, 2));
}
@@ -1038,8 +1368,9 @@
@@ -1038,8 +1369,9 @@
this.connection.send(new PacketPlayOutOpenSignEditor(tileentitysign.getBlockPos()));
}
@@ -694,7 +702,7 @@
}
@Override
@@ -1047,13 +1378,35 @@
@@ -1047,13 +1379,35 @@
if (itileinventory == null) {
return OptionalInt.empty();
} else {
@@ -730,7 +738,7 @@
if (container == null) {
if (this.isSpectator()) {
this.displayClientMessage(IChatBaseComponent.translatable("container.spectatorCantOpen").withStyle(EnumChatFormat.RED), true);
@@ -1061,9 +1414,11 @@
@@ -1061,9 +1415,11 @@
return OptionalInt.empty();
} else {
@@ -744,7 +752,7 @@
return OptionalInt.of(this.containerCounter);
}
}
@@ -1076,13 +1431,24 @@
@@ -1076,13 +1432,24 @@
@Override
public void openHorseInventory(EntityHorseAbstract entityhorseabstract, IInventory iinventory) {
@@ -771,7 +779,7 @@
this.initMenu(this.containerMenu);
}
@@ -1105,6 +1471,7 @@
@@ -1105,6 +1472,7 @@
@Override
public void closeContainer() {
@@ -779,7 +787,7 @@
this.connection.send(new PacketPlayOutCloseWindow(this.containerMenu.containerId));
this.doCloseContainer();
}
@@ -1127,6 +1494,16 @@
@@ -1127,6 +1495,16 @@
}
this.jumping = flag;
@@ -796,7 +804,7 @@
this.setShiftKeyDown(flag1);
}
@@ -1135,7 +1512,7 @@
@@ -1135,7 +1513,7 @@
@Override
public void awardStat(Statistic<?> statistic, int i) {
this.stats.increment(this, statistic, i);
@@ -805,7 +813,7 @@
scoreboardscore.add(i);
});
}
@@ -1143,7 +1520,7 @@
@@ -1143,7 +1521,7 @@
@Override
public void resetStat(Statistic<?> statistic) {
this.stats.setValue(this, statistic, 0);
@@ -814,7 +822,7 @@
}
@Override
@@ -1159,7 +1536,7 @@
@@ -1159,7 +1537,7 @@
for (int j = 0; j < i; ++j) {
MinecraftKey minecraftkey = aminecraftkey1[j];
@@ -823,7 +831,7 @@
Objects.requireNonNull(list);
optional.ifPresent(list::add);
@@ -1194,6 +1571,7 @@
@@ -1194,6 +1572,7 @@
public void resetSentInfo() {
this.lastSentHealth = -1.0E8F;
@@ -831,7 +839,7 @@
}
@Override
@@ -1252,7 +1630,7 @@
@@ -1252,7 +1631,7 @@
this.lastSentExp = -1;
this.lastSentHealth = -1.0F;
this.lastSentFood = -1;
@@ -840,7 +848,7 @@
this.seenCredits = entityplayer.seenCredits;
this.enteredNetherPosition = entityplayer.enteredNetherPosition;
this.setShoulderEntityLeft(entityplayer.getShoulderEntityLeft());
@@ -1302,6 +1680,12 @@
@@ -1302,6 +1681,12 @@
@Override
public boolean teleportTo(WorldServer worldserver, double d0, double d1, double d2, Set<RelativeMovement> set, float f, float f1) {
@@ -853,7 +861,7 @@
ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(BlockPosition.containing(d0, d1, d2));
worldserver.getChunkSource().addRegionTicket(TicketType.POST_TELEPORT, chunkcoordintpair, 1, this.getId());
@@ -1311,9 +1695,9 @@
@@ -1311,9 +1696,9 @@
}
if (worldserver == this.level) {
@@ -865,7 +873,7 @@
}
this.setYHeadRot(f);
@@ -1417,7 +1801,20 @@
@@ -1417,7 +1802,20 @@
}
}
@@ -886,7 +894,7 @@
this.chatVisibility = packetplayinsettings.chatVisibility();
this.canChatColor = packetplayinsettings.chatColors();
this.textFilteringEnabled = packetplayinsettings.textFilteringEnabled();
@@ -1492,7 +1889,7 @@
@@ -1492,7 +1890,7 @@
if (world instanceof WorldServer) {
WorldServer worldserver = (WorldServer) world;
@@ -895,7 +903,7 @@
}
if (entity != null) {
@@ -1529,7 +1926,7 @@
@@ -1529,7 +1927,7 @@
@Nullable
public IChatBaseComponent getTabListDisplayName() {
@@ -904,7 +912,7 @@
}
@Override
@@ -1550,9 +1947,16 @@
@@ -1550,9 +1948,16 @@
return this.advancements;
}
@@ -921,7 +929,7 @@
if (worldserver == this.level) {
this.connection.teleport(d0, d1, d2, f, f1);
} else {
@@ -1572,6 +1976,9 @@
@@ -1572,6 +1977,9 @@
this.server.getPlayerList().sendLevelInfo(this, worldserver);
this.server.getPlayerList().sendAllPlayerInfo(this);
}
@@ -931,7 +939,40 @@
}
@@ -1762,4 +2169,146 @@
@@ -1593,6 +2001,32 @@
}
public void setRespawnPosition(ResourceKey<World> resourcekey, @Nullable BlockPosition blockposition, float f, boolean flag, boolean flag1) {
+ // CraftBukkit start
+ this.setRespawnPosition(resourcekey, blockposition, f, flag, flag1, PlayerSpawnChangeEvent.Cause.UNKNOWN);
+ }
+
+ public void setRespawnPosition(ResourceKey<World> resourcekey, @Nullable BlockPosition blockposition, float f, boolean flag, boolean flag1, PlayerSpawnChangeEvent.Cause cause) {
+ WorldServer newWorld = this.server.getLevel(resourcekey);
+ Location newSpawn = (blockposition != null) ? new Location(newWorld.getWorld(), blockposition.getX(), blockposition.getY(), blockposition.getZ(), f, 0) : null;
+
+ PlayerSpawnChangeEvent event = new PlayerSpawnChangeEvent(this.getBukkitEntity(), newSpawn, flag, cause);
+ Bukkit.getServer().getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ return;
+ }
+ newSpawn = event.getNewSpawn();
+ flag = event.isForced();
+
+ if (newSpawn != null) {
+ resourcekey = ((CraftWorld) newSpawn.getWorld()).getHandle().dimension();
+ blockposition = BlockPosition.containing(newSpawn.getX(), newSpawn.getY(), newSpawn.getZ());
+ f = newSpawn.getYaw();
+ } else {
+ resourcekey = World.OVERWORLD;
+ blockposition = null;
+ f = 0.0F;
+ }
+ // CraftBukkit end
if (blockposition != null) {
boolean flag2 = blockposition.equals(this.respawnPosition) && resourcekey.equals(this.respawnDimension);
@@ -1762,4 +2196,146 @@
this.hurtDir = (float) (MathHelper.atan2(d1, d0) * 57.2957763671875D - (double) this.getYRot());
this.connection.send(new ClientboundHurtAnimationPacket(this));
}