diff --git a/src/main/java/net/neoforged/neoforge/common/CommonHooks.java b/src/main/java/net/neoforged/neoforge/common/CommonHooks.java index c161fae73c..a57b8c7a5f 100644 --- a/src/main/java/net/neoforged/neoforge/common/CommonHooks.java +++ b/src/main/java/net/neoforged/neoforge/common/CommonHooks.java @@ -184,11 +184,7 @@ import net.neoforged.neoforge.event.entity.living.LivingUseTotemEvent; import net.neoforged.neoforge.event.entity.living.MobEffectEvent; import net.neoforged.neoforge.event.entity.living.ShieldBlockEvent; -import net.neoforged.neoforge.event.entity.player.AnvilRepairEvent; -import net.neoforged.neoforge.event.entity.player.AttackEntityEvent; -import net.neoforged.neoforge.event.entity.player.CriticalHitEvent; -import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; -import net.neoforged.neoforge.event.entity.player.ServerPlayerEvent; +import net.neoforged.neoforge.event.entity.player.*; import net.neoforged.neoforge.event.level.BlockDropsEvent; import net.neoforged.neoforge.event.level.BlockEvent; import net.neoforged.neoforge.event.level.NoteBlockEvent; @@ -719,7 +715,7 @@ public static void onEmptyLeftClick(Player player) { @Nullable public static GameType onChangeGameType(ServerPlayer player, GameType currentGameType, GameType newGameType) { if (currentGameType != newGameType) { - ServerPlayerEvent.PlayerChangeGameModeEvent evt = new ServerPlayerEvent.PlayerChangeGameModeEvent(player, currentGameType, newGameType); + PlayerChangeGameModeEvent evt = new PlayerChangeGameModeEvent(player, currentGameType, newGameType); NeoForge.EVENT_BUS.post(evt); return evt.isCanceled() ? null : evt.getNewGameMode(); } diff --git a/src/main/java/net/neoforged/neoforge/common/NeoForgeEventHandler.java b/src/main/java/net/neoforged/neoforge/common/NeoForgeEventHandler.java index b53f372eec..0548e3cdd8 100644 --- a/src/main/java/net/neoforged/neoforge/common/NeoForgeEventHandler.java +++ b/src/main/java/net/neoforged/neoforge/common/NeoForgeEventHandler.java @@ -31,6 +31,7 @@ import net.neoforged.neoforge.event.RegisterCommandsEvent; import net.neoforged.neoforge.event.TagsUpdatedEvent; import net.neoforged.neoforge.event.entity.EntityJoinLevelEvent; +import net.neoforged.neoforge.event.entity.player.PlayerLoggedInEvent; import net.neoforged.neoforge.event.entity.player.ServerPlayerEvent; import net.neoforged.neoforge.event.level.ChunkEvent; import net.neoforged.neoforge.event.level.LevelEvent; @@ -95,7 +96,7 @@ public void playerChangeDimension(PlayerEvent.PlayerChangedDimensionEvent event) */ @SubscribeEvent - public void playerLogin(ServerPlayerEvent.PlayerLoggedInEvent event) { + public void playerLogin(PlayerLoggedInEvent event) { UsernameCache.setUsername(event.getEntity().getUUID(), event.getEntity().getGameProfile().getName()); } diff --git a/src/main/java/net/neoforged/neoforge/event/EventHooks.java b/src/main/java/net/neoforged/neoforge/event/EventHooks.java index 44eb28157c..3aaccf2886 100644 --- a/src/main/java/net/neoforged/neoforge/event/EventHooks.java +++ b/src/main/java/net/neoforged/neoforge/event/EventHooks.java @@ -137,26 +137,10 @@ import net.neoforged.neoforge.event.entity.living.MobSpawnEvent.SpawnPlacementCheck; import net.neoforged.neoforge.event.entity.living.MobSplitEvent; import net.neoforged.neoforge.event.entity.living.SpawnClusterSizeEvent; +import net.neoforged.neoforge.event.entity.player.*; import net.neoforged.neoforge.event.entity.player.AdvancementEvent.AdvancementEarnEvent; import net.neoforged.neoforge.event.entity.player.AdvancementEvent.AdvancementProgressEvent; import net.neoforged.neoforge.event.entity.player.AdvancementEvent.AdvancementProgressEvent.ProgressType; -import net.neoforged.neoforge.event.entity.player.ArrowLooseEvent; -import net.neoforged.neoforge.event.entity.player.ArrowNockEvent; -import net.neoforged.neoforge.event.entity.player.BonemealEvent; -import net.neoforged.neoforge.event.entity.player.CanContinueSleepingEvent; -import net.neoforged.neoforge.event.entity.player.CanPlayerSleepEvent; -import net.neoforged.neoforge.event.entity.player.ItemEntityPickupEvent; -import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent; -import net.neoforged.neoforge.event.entity.player.PermissionsChangedEvent; -import net.neoforged.neoforge.event.entity.player.PlayerDestroyItemEvent; -import net.neoforged.neoforge.event.entity.player.PlayerEvent; -import net.neoforged.neoforge.event.entity.player.PlayerFlyableFallEvent; -import net.neoforged.neoforge.event.entity.player.PlayerHeartTypeEvent; -import net.neoforged.neoforge.event.entity.player.PlayerRespawnPositionEvent; -import net.neoforged.neoforge.event.entity.player.PlayerSetSpawnEvent; -import net.neoforged.neoforge.event.entity.player.PlayerSpawnPhantomsEvent; -import net.neoforged.neoforge.event.entity.player.PlayerWakeUpEvent; -import net.neoforged.neoforge.event.entity.player.ServerPlayerEvent; import net.neoforged.neoforge.event.furnace.FurnaceFuelBurnTimeEvent; import net.neoforged.neoforge.event.level.AlterGroundEvent; import net.neoforged.neoforge.event.level.AlterGroundEvent.StateProvider; @@ -869,15 +853,15 @@ public static boolean onPermissionChanged(GameProfile gameProfile, int newLevel, } public static void firePlayerChangedDimensionEvent(ServerPlayer player, ResourceKey originalDimension, ResourceKey newDimension) { - NeoForge.EVENT_BUS.post(new ServerPlayerEvent.PlayerChangedDimensionEvent(player, originalDimension, newDimension)); + NeoForge.EVENT_BUS.post(new PlayerChangedDimensionEvent(player, originalDimension, newDimension)); } public static void firePlayerLoggedIn(ServerPlayer player) { - NeoForge.EVENT_BUS.post(new ServerPlayerEvent.PlayerLoggedInEvent(player)); + NeoForge.EVENT_BUS.post(new PlayerLoggedInEvent(player)); } public static void firePlayerLoggedOut(ServerPlayer player) { - NeoForge.EVENT_BUS.post(new ServerPlayerEvent.PlayerLoggedOutEvent(player)); + NeoForge.EVENT_BUS.post(new PlayerLoggedOutEvent(player)); } /** @@ -900,7 +884,7 @@ public static PlayerRespawnPositionEvent firePlayerRespawnPositionEvent(ServerPl * @param fromEndFight Whether the player is respawning because they jumped through the End return portal */ public static void firePlayerRespawnEvent(ServerPlayer player, boolean fromEndFight) { - NeoForge.EVENT_BUS.post(new ServerPlayerEvent.PlayerRespawnEvent(player, fromEndFight)); + NeoForge.EVENT_BUS.post(new PlayerRespawnEvent(player, fromEndFight)); } public static void firePlayerCraftingEvent(Player player, ItemStack crafted, Container craftMatrix) { diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/CanPlayerSleepEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/CanPlayerSleepEvent.java index fa1e158124..86a5289312 100644 --- a/src/main/java/net/neoforged/neoforge/event/entity/player/CanPlayerSleepEvent.java +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/CanPlayerSleepEvent.java @@ -21,7 +21,7 @@ * * @see {@link CanContinueSleepingEvent} for per-tick sleeping checks. */ -public class CanPlayerSleepEvent extends PlayerEvent { +public class CanPlayerSleepEvent extends ServerPlayerEvent { private final BlockPos pos; private final BlockState state; @@ -38,11 +38,6 @@ public CanPlayerSleepEvent(ServerPlayer player, BlockPos pos, @Nullable BedSleep this.vanillaProblem = this.problem = problem; } - @Override - public ServerPlayer getEntity() { - return (ServerPlayer) super.getEntity(); - } - public Level getLevel() { return this.getEntity().level(); } diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/PermissionsChangedEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/PermissionsChangedEvent.java index bee310cd2b..5b4c9e0c2c 100644 --- a/src/main/java/net/neoforged/neoforge/event/entity/player/PermissionsChangedEvent.java +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/PermissionsChangedEvent.java @@ -6,6 +6,7 @@ package net.neoforged.neoforge.event.entity.player; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.player.Player; import net.neoforged.bus.api.ICancellableEvent; /** @@ -13,7 +14,7 @@ *

* This event is cancelable which will stop the op or deop from happening. */ -public class PermissionsChangedEvent extends PlayerEvent implements ICancellableEvent { +public class PermissionsChangedEvent extends ServerPlayerEvent implements ICancellableEvent { private final int newLevel; private final int oldLevel; diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerChangeGameModeEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerChangeGameModeEvent.java new file mode 100644 index 0000000000..522f45e271 --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerChangeGameModeEvent.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.event.entity.player; + +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.GameType; +import net.neoforged.bus.api.ICancellableEvent; + +/** + * Fired when the game type of a server player is changed to a different value than what it was previously. Eg Creative to Survival, not Survival to Survival. + * If the event is cancelled the game mode of the player is not changed and the value of newGameMode is ignored. + */ +public class PlayerChangeGameModeEvent extends ServerPlayerEvent implements ICancellableEvent { + private final GameType currentGameMode; + private GameType newGameMode; + + public PlayerChangeGameModeEvent(ServerPlayer player, GameType currentGameMode, GameType newGameMode) { + super(player); + this.currentGameMode = currentGameMode; + this.newGameMode = newGameMode; + } + + public GameType getCurrentGameMode() { + return currentGameMode; + } + + public GameType getNewGameMode() { + return newGameMode; + } + + /** + * Sets the game mode the player will be changed to if this event is not cancelled. + */ + public void setNewGameMode(GameType newGameMode) { + this.newGameMode = newGameMode; + } +} diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerChangedDimensionEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerChangedDimensionEvent.java new file mode 100644 index 0000000000..57bdae745e --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerChangedDimensionEvent.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.event.entity.player; + +import net.minecraft.resources.ResourceKey; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.Level; + +public class PlayerChangedDimensionEvent extends ServerPlayerEvent { + private final ResourceKey originalDimension; + private final ResourceKey newDimension; + public PlayerChangedDimensionEvent(ServerPlayer player, ResourceKey originalDimension, ResourceKey newDimension) { + super(player); + this.originalDimension = originalDimension; + this.newDimension = newDimension; + } + + public ResourceKey getOriginalDimension() { + return originalDimension; + } + + public ResourceKey getNewDimension() { + return newDimension; + } +} diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerLoggedInEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerLoggedInEvent.java new file mode 100644 index 0000000000..6388e05b8b --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerLoggedInEvent.java @@ -0,0 +1,14 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.event.entity.player; + +import net.minecraft.server.level.ServerPlayer; + +public class PlayerLoggedInEvent extends ServerPlayerEvent { + public PlayerLoggedInEvent(ServerPlayer player) { + super(player); + } +} diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerLoggedOutEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerLoggedOutEvent.java new file mode 100644 index 0000000000..7dc42e927e --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerLoggedOutEvent.java @@ -0,0 +1,14 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.event.entity.player; + +import net.minecraft.server.level.ServerPlayer; + +public class PlayerLoggedOutEvent extends ServerPlayerEvent { + public PlayerLoggedOutEvent(ServerPlayer entity) { + super(entity); + } +} diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerRespawnEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerRespawnEvent.java new file mode 100644 index 0000000000..282a458f72 --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerRespawnEvent.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.event.entity.player; + +import net.minecraft.server.level.ServerPlayer; + +public class PlayerRespawnEvent extends ServerPlayerEvent { + private final boolean endConquered; + + public PlayerRespawnEvent(ServerPlayer player, boolean endConquered) { + super(player); + this.endConquered = endConquered; + } + + /** + * Did this respawn event come from the player conquering the end? + * + * @return if this respawn was because the player conquered the end + */ + public boolean isEndConquered() { + return this.endConquered; + } +} diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/ServerPlayerEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/ServerPlayerEvent.java index b939af8030..932558f199 100644 --- a/src/main/java/net/neoforged/neoforge/event/entity/player/ServerPlayerEvent.java +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/ServerPlayerEvent.java @@ -5,11 +5,7 @@ package net.neoforged.neoforge.event.entity.player; -import net.minecraft.resources.ResourceKey; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.level.GameType; -import net.minecraft.world.level.Level; -import net.neoforged.bus.api.ICancellableEvent; public abstract class ServerPlayerEvent extends PlayerEvent { protected ServerPlayerEvent(ServerPlayer entity) { @@ -19,83 +15,4 @@ protected ServerPlayerEvent(ServerPlayer entity) { public ServerPlayer getEntity() { return ((ServerPlayer) super.getEntity()); } - - public static class PlayerLoggedInEvent extends ServerPlayerEvent { - public PlayerLoggedInEvent(ServerPlayer player) { - super(player); - } - } - - public static class PlayerLoggedOutEvent extends ServerPlayerEvent { - public PlayerLoggedOutEvent(ServerPlayer player) { - super(player); - } - } - - public static class PlayerRespawnEvent extends ServerPlayerEvent { - private final boolean endConquered; - - public PlayerRespawnEvent(ServerPlayer player, boolean endConquered) { - super(player); - this.endConquered = endConquered; - } - - /** - * Did this respawn event come from the player conquering the end? - * - * @return if this respawn was because the player conquered the end - */ - public boolean isEndConquered() { - return this.endConquered; - } - } - - public static class PlayerChangedDimensionEvent extends ServerPlayerEvent { - private final ResourceKey originalDimension; - private final ResourceKey newDimension; - - public PlayerChangedDimensionEvent(ServerPlayer player, ResourceKey originalDimension, ResourceKey newDimension) { - super(player); - this.originalDimension = originalDimension; - this.newDimension = newDimension; - } - - public ResourceKey getOriginalDimension() { - return this.originalDimension; - } - - public ResourceKey getNewDimension() { - return this.newDimension; - } - } - - /** - * Fired when the game type of a server player is changed to a different value than what it was previously. Eg Creative to Survival, not Survival to Survival. - * If the event is cancelled the game mode of the player is not changed and the value of newGameMode is ignored. - */ - public static class PlayerChangeGameModeEvent extends ServerPlayerEvent implements ICancellableEvent { - private final GameType currentGameMode; - private GameType newGameMode; - - public PlayerChangeGameModeEvent(ServerPlayer player, GameType currentGameMode, GameType newGameMode) { - super(player); - this.currentGameMode = currentGameMode; - this.newGameMode = newGameMode; - } - - public GameType getCurrentGameMode() { - return currentGameMode; - } - - public GameType getNewGameMode() { - return newGameMode; - } - - /** - * Sets the game mode the player will be changed to if this event is not cancelled. - */ - public void setNewGameMode(GameType newGameMode) { - this.newGameMode = newGameMode; - } - } -} +} \ No newline at end of file diff --git a/testframework/src/main/java/net/neoforged/testframework/impl/TestFrameworkImpl.java b/testframework/src/main/java/net/neoforged/testframework/impl/TestFrameworkImpl.java index 9f1962333a..3b438573b8 100644 --- a/testframework/src/main/java/net/neoforged/testframework/impl/TestFrameworkImpl.java +++ b/testframework/src/main/java/net/neoforged/testframework/impl/TestFrameworkImpl.java @@ -43,6 +43,8 @@ import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.RegisterGameTestsEvent; +import net.neoforged.neoforge.event.entity.player.PlayerLoggedInEvent; +import net.neoforged.neoforge.event.entity.player.PlayerLoggedOutEvent; import net.neoforged.neoforge.event.entity.player.ServerPlayerEvent; import net.neoforged.neoforge.event.server.ServerStartedEvent; import net.neoforged.neoforge.event.server.ServerStoppedEvent; @@ -139,8 +141,8 @@ public TestFrameworkImpl(FrameworkConfiguration configuration) { }); if (configuration().isEnabled(Feature.TEST_STORE)) { - NeoForge.EVENT_BUS.addListener((final ServerPlayerEvent.PlayerLoggedOutEvent event) -> playerTestStore().put(event.getEntity().getUUID(), tests.tests.keySet())); - NeoForge.EVENT_BUS.addListener((final ServerPlayerEvent.PlayerLoggedInEvent event) -> { + NeoForge.EVENT_BUS.addListener((final PlayerLoggedOutEvent event) -> playerTestStore().put(event.getEntity().getUUID(), tests.tests.keySet())); + NeoForge.EVENT_BUS.addListener((final PlayerLoggedInEvent event) -> { final Set lastTests = playerTestStore().getLast(event.getEntity().getUUID()); if (lastTests == null) return; diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/entity/player/PlayerEventTests.java b/tests/src/main/java/net/neoforged/neoforge/debug/entity/player/PlayerEventTests.java index 411f91d993..c6cbe59de7 100644 --- a/tests/src/main/java/net/neoforged/neoforge/debug/entity/player/PlayerEventTests.java +++ b/tests/src/main/java/net/neoforged/neoforge/debug/entity/player/PlayerEventTests.java @@ -31,13 +31,7 @@ import net.minecraft.world.level.portal.DimensionTransition; import net.neoforged.bus.api.EventPriority; import net.neoforged.neoforge.event.StatAwardEvent; -import net.neoforged.neoforge.event.entity.player.ItemEntityPickupEvent; -import net.neoforged.neoforge.event.entity.player.PermissionsChangedEvent; -import net.neoforged.neoforge.event.entity.player.PlayerEvent; -import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; -import net.neoforged.neoforge.event.entity.player.PlayerRespawnPositionEvent; -import net.neoforged.neoforge.event.entity.player.ServerPlayerEvent; -import net.neoforged.neoforge.event.entity.player.UseItemOnBlockEvent; +import net.neoforged.neoforge.event.entity.player.*; import net.neoforged.testframework.DynamicTest; import net.neoforged.testframework.annotation.ForEachTest; import net.neoforged.testframework.annotation.TestHolder; @@ -156,7 +150,7 @@ public static void itemPickupEvent(final DynamicTest test) { @EmptyTemplate @TestHolder(description = "Tests if the PlayerChangeGameModeEvent is fired and can modify the outcome") static void playerChangeGameModeEvent(final DynamicTest test) { - test.eventListeners().forge().addListener((final ServerPlayerEvent.PlayerChangeGameModeEvent event) -> { + test.eventListeners().forge().addListener((final PlayerChangeGameModeEvent event) -> { // Only affect the players with a custom name to not interfere with other tests if (!Objects.equals(event.getEntity().getCustomName(), Component.literal("gamemode-changes"))) { return; @@ -266,7 +260,7 @@ static void playerRespawnPositionEvent(final DynamicTest test, final Registratio @EmptyTemplate @TestHolder(description = "Tests if the PlayerRespawnEvent fires correctly and can modify the player after respawning") static void playerRespawnEvent(final DynamicTest test, final RegistrationHelper reg) { - test.eventListeners().forge().addListener((final ServerPlayerEvent.PlayerRespawnEvent event) -> { + test.eventListeners().forge().addListener((final PlayerRespawnEvent event) -> { event.getEntity().setItemInHand(InteractionHand.MAIN_HAND, new ItemStack(Items.APPLE)); });