Skip to content

Commit 3ee8df7

Browse files
committed
feat: player patch
1 parent 2cf0e16 commit 3ee8df7

3 files changed

Lines changed: 42 additions & 63 deletions

File tree

nms-patches/net/minecraft/server/level/EntityPlayer.patch

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,6 @@
33

44
public class EntityPlayer extends EntityHuman {
55

6-
@@ -1002,19 +1307,17 @@
7-
this.containerMenu.broadcastChanges();
8-
}
9-
10-
- @Override
11-
- public Either<EntityHuman.EnumBedResult, Unit> startSleepInBed(BlockPosition blockposition) {
12-
- EnumDirection enumdirection = (EnumDirection) this.level().getBlockState(blockposition).getValue(BlockFacingHorizontal.FACING);
13-
-
14-
+ // CraftBukkit start - moved bed result checks from below into separate method
15-
+ private Either<EntityHuman.EnumBedResult, Unit> getBedResult(BlockPosition blockposition, EnumDirection enumdirection) {
16-
if (!this.isSleeping() && this.isAlive()) {
17-
- if (!this.level().dimensionType().natural()) {
18-
+ if (!this.level().dimensionType().natural() || !this.level().dimensionType().bedWorks()) {
19-
return Either.left(EntityHuman.EnumBedResult.NOT_POSSIBLE_HERE);
20-
} else if (!this.bedInRange(blockposition, enumdirection)) {
21-
return Either.left(EntityHuman.EnumBedResult.TOO_FAR_AWAY);
22-
} else if (this.bedBlocked(blockposition, enumdirection)) {
23-
return Either.left(EntityHuman.EnumBedResult.OBSTRUCTED);
24-
} else {
25-
- this.setRespawnPosition(this.level().dimension(), blockposition, this.getYRot(), false, true);
26-
+ this.setRespawnPosition(this.level().dimension(), blockposition, this.getYRot(), false, true, PlayerSpawnChangeEvent.Cause.BED); // CraftBukkit
27-
if (this.level().isDay()) {
28-
return Either.left(EntityHuman.EnumBedResult.NOT_POSSIBLE_NOW);
29-
} else {
30-
@@ -1031,7 +1334,36 @@
31-
}
32-
}
33-
34-
- Either<EntityHuman.EnumBedResult, Unit> either = super.startSleepInBed(blockposition).ifRight((unit) -> {
35-
+ return Either.right(Unit.INSTANCE);
36-
+ }
37-
+ }
38-
+ } else {
39-
+ return Either.left(EntityHuman.EnumBedResult.OTHER_PROBLEM);
40-
+ }
41-
+ }
42-
+
43-
+ @Override
44-
+ public Either<EntityHuman.EnumBedResult, Unit> startSleepInBed(BlockPosition blockposition, boolean force) {
45-
+ EnumDirection enumdirection = (EnumDirection) this.level().getBlockState(blockposition).getValue(BlockFacingHorizontal.FACING);
46-
+ Either<EntityHuman.EnumBedResult, Unit> bedResult = this.getBedResult(blockposition, enumdirection);
47-
+
48-
+ if (bedResult.left().orElse(null) == EntityHuman.EnumBedResult.OTHER_PROBLEM) {
49-
+ return bedResult; // return immediately if the result is not bypassable by plugins
50-
+ }
51-
+
52-
+ if (force) {
53-
+ bedResult = Either.right(Unit.INSTANCE);
54-
+ }
55-
+
56-
+ bedResult = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerBedEnterEvent(this, blockposition, bedResult);
57-
+ if (bedResult.left().isPresent()) {
58-
+ return bedResult;
59-
+ }
60-
+
61-
+ {
62-
+ {
63-
+ {
64-
+ Either<EntityHuman.EnumBedResult, Unit> either = super.startSleepInBed(blockposition, force).ifRight((unit) -> {
65-
this.awardStat(StatisticList.SLEEP_IN_BED);
66-
CriterionTriggers.SLEPT_IN_BED.trigger(this);
67-
});
686
@@ -1044,9 +1376,8 @@
697
return either;
708
}

src/main/java/org/teneted/neotenet/injection/server/level/InjectionServerPlayer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.minecraft.world.level.portal.DimensionTransition;
44
import org.bukkit.Location;
55
import org.bukkit.craftbukkit.event.CraftPortalEvent;
6+
import org.bukkit.entity.HumanEntity;
67
import org.bukkit.event.player.PlayerRespawnEvent;
78
import org.teneted.neotenet.injection.world.entity.player.InjectionPlayer;
89
import com.mojang.datafixers.util.Either;
@@ -119,4 +120,8 @@ default DimensionTransition findRespawnPositionAndUseSpawnBlock(boolean p_348590
119120
default CraftPortalEvent callPortalEvent(Entity entity, Location exit, PlayerTeleportEvent.TeleportCause cause, int searchRadius, int creationRadius) {
120121
throw new IllegalStateException("Not implemented");
121122
}
123+
124+
default Either<Player.BedSleepingProblem, Unit> startSleepInBed(BlockPos blockposition, boolean force) {
125+
throw new IllegalStateException("Not implemented");
126+
}
122127
}

src/main/java/org/teneted/neotenet/mixin/server/level/MixinServerPlayer.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import net.minecraft.server.players.PlayerList;
3838
import net.minecraft.stats.RecipeBook;
3939
import net.minecraft.stats.ServerRecipeBook;
40+
import net.minecraft.stats.Stats;
4041
import net.minecraft.util.Mth;
4142
import net.minecraft.util.RandomSource;
4243
import net.minecraft.util.Unit;
@@ -60,6 +61,8 @@
6061
import net.minecraft.world.item.enchantment.EnchantmentHelper;
6162
import net.minecraft.world.level.GameType;
6263
import net.minecraft.world.level.Level;
64+
import net.minecraft.world.level.block.CampfireBlock;
65+
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
6366
import net.minecraft.world.level.border.WorldBorder;
6467
import net.minecraft.world.level.dimension.LevelStem;
6568
import net.minecraft.world.level.portal.DimensionTransition;
@@ -452,7 +455,6 @@ public DimensionTransition findRespawnPositionAndUseSpawnBlock(boolean p_348590_
452455
}
453456

454457

455-
456458
@Redirect(method = "adjustSpawnLocation", at = @At(value = "INVOKE",
457459
target = "Lnet/minecraft/world/level/storage/ServerLevelData;getGameType()Lnet/minecraft/world/level/GameType;"))
458460
private GameType neotenet$useWorldGameType(ServerLevelData instance, @Local(argsOnly = true) ServerLevel p_352206_) {
@@ -656,6 +658,40 @@ public Either<BedSleepingProblem, Unit> getBedResult(BlockPos blockposition, Dir
656658
return Either.left(BedSleepingProblem.OTHER_PROBLEM);
657659
}
658660

661+
@Override
662+
public Either<Player.BedSleepingProblem, Unit> startSleepInBed(BlockPos blockposition, boolean force) {
663+
Direction enumdirection = (Direction) this.level().getBlockState(blockposition).getValue(BlockStateProperties.HORIZONTAL_FACING);
664+
Either<Player.BedSleepingProblem, Unit> bedResult = this.getBedResult(blockposition, enumdirection);
665+
666+
if (bedResult.left().orElse(null) == Player.BedSleepingProblem.OTHER_PROBLEM) {
667+
return bedResult; // return immediately if the result is not bypassable by plugins
668+
}
669+
670+
if (force) {
671+
bedResult = Either.right(Unit.INSTANCE);
672+
}
673+
674+
bedResult = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerBedEnterEvent(this, blockposition, bedResult);
675+
if (bedResult.left().isPresent()) {
676+
return bedResult;
677+
}
678+
{
679+
{
680+
// Start vanilla code
681+
Either<Player.BedSleepingProblem, Unit> either = super.startSleepInBed(blockposition).ifRight(p_9029_ -> {
682+
this.awardStat(Stats.SLEEP_IN_BED);
683+
CriteriaTriggers.SLEPT_IN_BED.trigger((ServerPlayer) (Object) this);
684+
});
685+
if (!this.serverLevel().canSleepThroughNights()) {
686+
this.displayClientMessage(Component.translatable("sleep.not_possible"), true);
687+
}
688+
689+
((ServerLevel) this.level()).updateSleepingPlayerList();
690+
return either;
691+
}
692+
}
693+
}
694+
659695
@Override
660696
public Scoreboard getScoreboard() {
661697
return this.getBukkitEntity().getScoreboard().getHandle();

0 commit comments

Comments
 (0)