Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move certain events to the newly introduced ServerPlayerEvent #1153

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/net/neoforged/neoforge/common/CommonHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@
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.PlayerEvent;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import net.neoforged.neoforge.event.entity.player.ServerPlayerEvent;
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,9 +719,9 @@ public static void onEmptyLeftClick(Player player) {
* @return null if game type should not be changed, desired new GameType otherwise
*/
@Nullable
public static GameType onChangeGameType(Player player, GameType currentGameType, GameType newGameType) {
public static GameType onChangeGameType(ServerPlayer player, GameType currentGameType, GameType newGameType) {
if (currentGameType != newGameType) {
PlayerEvent.PlayerChangeGameModeEvent evt = new PlayerEvent.PlayerChangeGameModeEvent(player, currentGameType, newGameType);
ServerPlayerEvent.PlayerChangeGameModeEvent evt = new ServerPlayerEvent.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,7 +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.PlayerEvent;
import net.neoforged.neoforge.event.entity.player.ServerPlayerEvent;
import net.neoforged.neoforge.event.level.ChunkEvent;
import net.neoforged.neoforge.event.level.LevelEvent;
import net.neoforged.neoforge.event.tick.ServerTickEvent;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void playerChangeDimension(PlayerEvent.PlayerChangedDimensionEvent event)
*/

@SubscribeEvent
public void playerLogin(PlayerEvent.PlayerLoggedInEvent event) {
public void playerLogin(ServerPlayerEvent.PlayerLoggedInEvent event) {
UsernameCache.setUsername(event.getEntity().getUUID(), event.getEntity().getGameProfile().getName());
}

Expand Down
105 changes: 52 additions & 53 deletions src/main/java/net/neoforged/neoforge/event/EventHooks.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public class ItemTossEvent extends ItemEvent implements ICancellableEvent {
/**
* Creates a new event for EntityItems tossed by a player.
*
* @param entityItem The EntityItem being tossed.
* @param itemEntity The EntityItem being tossed.
Spinoscythe marked this conversation as resolved.
Show resolved Hide resolved
* @param player The player tossing the item.
*/
public ItemTossEvent(ItemEntity entityItem, Player player) {
super(entityItem);
public ItemTossEvent(ItemEntity itemEntity, Player player) {
super(itemEntity);
this.player = player;
}
Spinoscythe marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.advancements.AdvancementProgress;
import net.minecraft.world.entity.player.Player;
import net.neoforged.bus.api.Event;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.common.NeoForge;

/**
Expand All @@ -17,10 +16,10 @@
* @see AdvancementEarnEvent
* @see AdvancementProgressEvent
*/
public abstract class AdvancementEvent extends PlayerEvent {
public abstract class AdvancementEvent extends ServerPlayerEvent {
private final AdvancementHolder advancement;

public AdvancementEvent(Player player, AdvancementHolder advancement) {
public AdvancementEvent(ServerPlayer player, AdvancementHolder advancement) {
super(player);
this.advancement = advancement;
}
Expand All @@ -35,23 +34,23 @@ public AdvancementHolder getAdvancement() {
* <p>Note that advancements may be hidden from the player or used in background mechanics, such as recipe
* advancements for unlocking recipes in the recipe book.</p>
*
* <p>This event is not {@linkplain net.neoforged.bus.api.ICancellableEvent cancellable}, and does not {@linkplain Event.HasResult have a result}.</p>
* <p>This event is not {@linkplain net.neoforged.bus.api.ICancellableEvent cancellable}, and does not have a result.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain net.neoforged.fml.LogicalSide#SERVER logical server}.</p>
*
* @see AdvancementProgress#isDone()
*/
public static class AdvancementEarnEvent extends AdvancementEvent {
public AdvancementEarnEvent(Player player, AdvancementHolder earned) {
public AdvancementEarnEvent(ServerPlayer player, AdvancementHolder earned) {
super(player, earned);
}
}

/**
* Fired when the player's progress on an advancement criterion is granted or revoked.
*
* <p>This event is not {@linkplain net.neoforged.bus.api.ICancellableEvent cancellable}, and does not {@linkplain Event.HasResult have a result}.</p>
* <p>This event is not {@linkplain net.neoforged.bus.api.ICancellableEvent cancellable}, and does not have a result.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain net.neoforged.fml.LogicalSide#SERVER logical server}.</p>
Expand All @@ -65,7 +64,7 @@ public static class AdvancementProgressEvent extends AdvancementEvent {
private final String criterionName;
private final AdvancementEvent.AdvancementProgressEvent.ProgressType progressType;

public AdvancementProgressEvent(Player player, AdvancementHolder progressed, AdvancementProgress advancementProgress, String criterionName, AdvancementEvent.AdvancementProgressEvent.ProgressType progressType) {
public AdvancementProgressEvent(ServerPlayer player, AdvancementHolder progressed, AdvancementProgress advancementProgress, String criterionName, AdvancementEvent.AdvancementProgressEvent.ProgressType progressType) {
super(player, progressed);
this.advancementProgress = advancementProgress;
this.criterionName = criterionName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@

package net.neoforged.neoforge.event.entity.player;

import net.minecraft.world.entity.player.Player;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.AbstractContainerMenu;

public class PlayerContainerEvent extends PlayerEvent {
public abstract class PlayerContainerEvent extends ServerPlayerEvent {
private final AbstractContainerMenu container;

public PlayerContainerEvent(Player player, AbstractContainerMenu container) {
public PlayerContainerEvent(ServerPlayer player, AbstractContainerMenu container) {
super(player);
this.container = container;
}

public AbstractContainerMenu getContainer() {
return container;
}

public static class Open extends PlayerContainerEvent {
public Open(Player player, AbstractContainerMenu container) {
public Open(ServerPlayer player, AbstractContainerMenu container) {
super(player, container);
}
}

public static class Close extends PlayerContainerEvent {
public Close(Player player, AbstractContainerMenu container) {
public Close(ServerPlayer player, AbstractContainerMenu container) {
super(player, container);
}
}

public AbstractContainerMenu getContainer() {
return container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
import java.util.Optional;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.ICancellableEvent;
Expand Down Expand Up @@ -418,83 +415,4 @@ public ItemStack getSmelting() {
return this.smelting;
}
}

public static class PlayerLoggedInEvent extends PlayerEvent {
public PlayerLoggedInEvent(Player player) {
super(player);
}
}

public static class PlayerLoggedOutEvent extends PlayerEvent {
public PlayerLoggedOutEvent(Player player) {
super(player);
}
}

public static class PlayerRespawnEvent extends PlayerEvent {
private final boolean endConquered;

public PlayerRespawnEvent(Player 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 PlayerEvent {
private final ResourceKey<Level> fromDim;
private final ResourceKey<Level> toDim;

public PlayerChangedDimensionEvent(Player player, ResourceKey<Level> fromDim, ResourceKey<Level> toDim) {
super(player);
this.fromDim = fromDim;
this.toDim = toDim;
}

public ResourceKey<Level> getFrom() {
return this.fromDim;
}

public ResourceKey<Level> getTo() {
return this.toDim;
}
}

/**
* 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 PlayerEvent implements ICancellableEvent {
private final GameType currentGameMode;
private GameType newGameMode;

public PlayerChangeGameModeEvent(Player 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 @@ -12,21 +12,21 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.portal.DimensionTransition;

/**
* Fired by {@link PlayerList#respawn(ServerPlayer, boolean)} before the server respawns a player.
* Fired by {@link PlayerList#respawn(ServerPlayer, boolean, Entity.RemovalReason)} before the server respawns a player.
* This may be used to change the {@link ServerLevel} the player respawns in, as well as their respawn position.
* This event is fired after {@link BlockState#getRespawnPosition(EntityType, LevelReader, BlockPos, float, LivingEntity)} is called.
* This event is fired after {@link BlockState#getRespawnPosition(EntityType, LevelReader, BlockPos, float)} is called.
* <p>
* This event is only fired on the logical server.
*/
public class PlayerRespawnPositionEvent extends PlayerEvent {
public class PlayerRespawnPositionEvent extends ServerPlayerEvent {
private DimensionTransition dimensionTransition;
private final DimensionTransition originalDimensionTransition;
private final boolean fromEndFight;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.GameType;
import net.minecraft.world.level.Level;
import net.neoforged.bus.api.ICancellableEvent;
import net.neoforged.neoforge.event.entity.living.LivingEvent;

public abstract class ServerPlayerEvent extends LivingEvent {
Spinoscythe marked this conversation as resolved.
Show resolved Hide resolved
public ServerPlayerEvent(ServerPlayer entity) {
super(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;
}
}
}
Loading
Loading