Skip to content

Commit

Permalink
move events out of server player event
Browse files Browse the repository at this point in the history
  • Loading branch information
Spinoscythe committed Jun 23, 2024
1 parent 74ea0f6 commit 1a3027a
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 130 deletions.
8 changes: 2 additions & 6 deletions src/main/java/net/neoforged/neoforge/common/CommonHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
26 changes: 5 additions & 21 deletions src/main/java/net/neoforged/neoforge/event/EventHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -869,15 +853,15 @@ public static boolean onPermissionChanged(GameProfile gameProfile, int newLevel,
}

public static void firePlayerChangedDimensionEvent(ServerPlayer player, ResourceKey<Level> originalDimension, ResourceKey<Level> 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));
}

/**
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
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;

/**
* This event will fire when the player is opped or deopped.
* <p>
* 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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <code>newGameMode</code> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Level> originalDimension;
private final ResourceKey<Level> newDimension;
public PlayerChangedDimensionEvent(ServerPlayer player, ResourceKey<Level> originalDimension, ResourceKey<Level> newDimension) {
super(player);
this.originalDimension = originalDimension;
this.newDimension = newDimension;
}

public ResourceKey<Level> getOriginalDimension() {
return originalDimension;
}

public ResourceKey<Level> getNewDimension() {
return newDimension;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<Level> originalDimension;
private final ResourceKey<Level> newDimension;

public PlayerChangedDimensionEvent(ServerPlayer player, ResourceKey<Level> originalDimension, ResourceKey<Level> newDimension) {
super(player);
this.originalDimension = originalDimension;
this.newDimension = newDimension;
}

public ResourceKey<Level> getOriginalDimension() {
return this.originalDimension;
}

public ResourceKey<Level> 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 <code>newGameMode</code> 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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> lastTests = playerTestStore().getLast(event.getEntity().getUUID());
if (lastTests == null) return;

Expand Down
Loading

0 comments on commit 1a3027a

Please sign in to comment.