From 9dbd88d96c2f6b6bcc30afaf099b11a09e32bcb5 Mon Sep 17 00:00:00 2001 From: CODY_AI Date: Mon, 25 May 2020 04:52:09 +0430 Subject: [PATCH] New Event: PlayerOnChatMessage, Fixed the Disconnect event not getting triggered --- .../kilocraft/essentials/KiloCommands.java | 2 +- .../api/event/context/PlayerContext.java | 7 ++ .../player/PlayerOnChatMessageEvent.java | 13 ++++ .../commands/server/ReloadCommand.java | 4 +- .../player/PlayerClientCommandEventImpl.java | 7 ++ .../events/player/PlayerConnectEventImpl.java | 7 ++ .../player/PlayerConnectedEventImpl.java | 7 ++ .../events/player/PlayerDeathEventImpl.java | 7 ++ .../player/PlayerDisconnectEventImpl.java | 7 ++ .../player/PlayerInteractBlockEventImpl.java | 7 ++ .../player/PlayerInteractItemEventImpl.java | 7 ++ .../PlayerInteractItemStartEventImpl.java | 6 ++ .../player/PlayerOnChatMessageEventImpl.java | 65 +++++++++++++++++++ .../player/PlayerStopRidingEventImpl.java | 7 ++ ...ServerPlayNetworkHandler$PlayerEvents.java | 5 ++ .../essentials/user/ServerUserManager.java | 9 ++- 16 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/kilocraft/essentials/api/event/player/PlayerOnChatMessageEvent.java create mode 100644 src/main/java/org/kilocraft/essentials/events/player/PlayerOnChatMessageEventImpl.java diff --git a/src/main/java/org/kilocraft/essentials/KiloCommands.java b/src/main/java/org/kilocraft/essentials/KiloCommands.java index 7d8b01e1..ca134f62 100644 --- a/src/main/java/org/kilocraft/essentials/KiloCommands.java +++ b/src/main/java/org/kilocraft/essentials/KiloCommands.java @@ -399,7 +399,7 @@ public final void sendUsage(final ServerCommandSource source, final EssentialCom source.sendFeedback(Texter.newText(builder.toString()), false); } - public int execute(final ServerCommandSource executor, final String command) { + public int execute(@NotNull final ServerCommandSource executor, @NotNull final String command) { CommandSourceUser src = KiloServer.getServer().getCommandSourceUser(executor); OnCommandExecutionEvent event = new OnCommandExecutionEventImpl(executor, command); String cmd = command; diff --git a/src/main/java/org/kilocraft/essentials/api/event/context/PlayerContext.java b/src/main/java/org/kilocraft/essentials/api/event/context/PlayerContext.java index 930be9e8..c904581c 100644 --- a/src/main/java/org/kilocraft/essentials/api/event/context/PlayerContext.java +++ b/src/main/java/org/kilocraft/essentials/api/event/context/PlayerContext.java @@ -2,6 +2,7 @@ import net.minecraft.server.network.ServerPlayerEntity; import org.kilocraft.essentials.api.event.Event; +import org.kilocraft.essentials.api.user.OnlineUser; /** * Represents a context where a player is involved, often as the cause of the event. @@ -12,4 +13,10 @@ public interface PlayerContext extends Contextual { * @return the player that fired this event */ ServerPlayerEntity getPlayer(); + + /** + * Gets the User that fired this event + * @return the users that fired this event + */ + OnlineUser getUser(); } diff --git a/src/main/java/org/kilocraft/essentials/api/event/player/PlayerOnChatMessageEvent.java b/src/main/java/org/kilocraft/essentials/api/event/player/PlayerOnChatMessageEvent.java new file mode 100644 index 00000000..32d52106 --- /dev/null +++ b/src/main/java/org/kilocraft/essentials/api/event/player/PlayerOnChatMessageEvent.java @@ -0,0 +1,13 @@ +package org.kilocraft.essentials.api.event.player; + +import org.kilocraft.essentials.api.event.Event; +import org.kilocraft.essentials.api.event.context.CancellableReasonContext; +import org.kilocraft.essentials.api.event.context.Contextual; +import org.kilocraft.essentials.api.event.context.PlayerContext; +import org.kilocraft.essentials.api.event.context.WorldContext; + +public interface PlayerOnChatMessageEvent extends Event, PlayerContext, WorldContext, CancellableReasonContext, Contextual { + String getMessage(); + + void setMessage(final String message); +} diff --git a/src/main/java/org/kilocraft/essentials/commands/server/ReloadCommand.java b/src/main/java/org/kilocraft/essentials/commands/server/ReloadCommand.java index a8cc92a2..e9b87fe4 100644 --- a/src/main/java/org/kilocraft/essentials/commands/server/ReloadCommand.java +++ b/src/main/java/org/kilocraft/essentials/commands/server/ReloadCommand.java @@ -5,6 +5,7 @@ import net.minecraft.server.command.ServerCommandSource; import org.apache.commons.lang3.time.StopWatch; import org.kilocraft.essentials.CommandPermission; +import org.kilocraft.essentials.api.ModConstants; import org.kilocraft.essentials.api.command.EssentialCommand; import org.kilocraft.essentials.chat.KiloChat; @@ -28,8 +29,7 @@ private int execute(CommandContext ctx) { server.reload(); watch.stop(); - String timeElapsed = new DecimalFormat("##.##").format(watch.getTime(TimeUnit.MILLISECONDS)); - KiloChat.sendLangMessageTo(ctx.getSource(), "command.reload.end", timeElapsed); + KiloChat.sendLangMessageTo(ctx.getSource(), "command.reload.end", ModConstants.DECIMAL_FORMAT.format(watch.getTime(TimeUnit.MILLISECONDS))); return SUCCESS; } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerClientCommandEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerClientCommandEventImpl.java index 6b3be715..d1fb5c57 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerClientCommandEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerClientCommandEventImpl.java @@ -2,7 +2,9 @@ import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket; import net.minecraft.server.network.ServerPlayerEntity; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerClientCommandEvent; +import org.kilocraft.essentials.api.user.OnlineUser; public class PlayerClientCommandEventImpl implements PlayerClientCommandEvent { private boolean cancelled = false; @@ -33,4 +35,9 @@ public void setCancelled(boolean isCancelled) { public ServerPlayerEntity getPlayer() { return player; } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser(this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerConnectEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerConnectEventImpl.java index 69cc20df..39ce241c 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerConnectEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerConnectEventImpl.java @@ -4,7 +4,9 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerConnectEvent; +import org.kilocraft.essentials.api.user.OnlineUser; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -67,4 +69,9 @@ public ServerPlayNetworkHandler getNetworkHandler() { public MinecraftServer getServer() { return player.getServer(); } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser(this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerConnectedEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerConnectedEventImpl.java index 138294ab..97a33fac 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerConnectedEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerConnectedEventImpl.java @@ -4,7 +4,9 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerConnectedEvent; +import org.kilocraft.essentials.api.user.OnlineUser; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -66,4 +68,9 @@ public ServerPlayNetworkHandler getNetworkHandler() { public MinecraftServer getServer() { return player.getServer(); } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser(this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerDeathEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerDeathEventImpl.java index 1460143a..03bc1a80 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerDeathEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerDeathEventImpl.java @@ -1,7 +1,9 @@ package org.kilocraft.essentials.events.player; import net.minecraft.server.network.ServerPlayerEntity; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerDeathEvent; +import org.kilocraft.essentials.api.user.OnlineUser; public class PlayerDeathEventImpl implements PlayerDeathEvent { @@ -26,5 +28,10 @@ public boolean isCancelled() { public void setCancelled(boolean set) { this.cancelled = set; } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser(this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerDisconnectEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerDisconnectEventImpl.java index b792949c..ee43106b 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerDisconnectEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerDisconnectEventImpl.java @@ -2,7 +2,9 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerDisconnectEvent; +import org.kilocraft.essentials.api.user.OnlineUser; public class PlayerDisconnectEventImpl implements PlayerDisconnectEvent { @@ -21,5 +23,10 @@ public ServerPlayerEntity getPlayer() { public MinecraftServer getServer() { return player.getServer(); } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser(this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractBlockEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractBlockEventImpl.java index b7e59ece..9810708b 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractBlockEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractBlockEventImpl.java @@ -3,7 +3,9 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerInteractBlockEvent; +import org.kilocraft.essentials.api.user.OnlineUser; public class PlayerInteractBlockEventImpl implements PlayerInteractBlockEvent { private boolean cancelled = false; @@ -41,4 +43,9 @@ public boolean isCancelled() { public void setCancelled(boolean isCancelled) { this.cancelled = isCancelled; } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser(this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractItemEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractItemEventImpl.java index 322fe6c9..81b523ab 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractItemEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractItemEventImpl.java @@ -7,7 +7,9 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.world.World; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerInteractItem; +import org.kilocraft.essentials.api.user.OnlineUser; public class PlayerInteractItemEventImpl implements PlayerInteractItem { private boolean cancelled = false; @@ -63,4 +65,9 @@ public void setReturnValue(ActionResult actionResult) { public ActionResult getReturnValue() { return this.actionResult; } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser((ServerPlayerEntity) this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractItemStartEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractItemStartEventImpl.java index 08c0c147..5efa83bd 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractItemStartEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerInteractItemStartEventImpl.java @@ -6,7 +6,9 @@ import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Hand; import net.minecraft.world.World; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerInteractItemStartEvent; +import org.kilocraft.essentials.api.user.OnlineUser; public class PlayerInteractItemStartEventImpl implements PlayerInteractItemStartEvent { private boolean cancelled = false; @@ -52,4 +54,8 @@ public Hand getHand() { return this.hand; } + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser((ServerPlayerEntity) this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerOnChatMessageEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerOnChatMessageEventImpl.java new file mode 100644 index 00000000..522c818c --- /dev/null +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerOnChatMessageEventImpl.java @@ -0,0 +1,65 @@ +package org.kilocraft.essentials.events.player; + +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import org.jetbrains.annotations.NotNull; +import org.kilocraft.essentials.api.KiloServer; +import org.kilocraft.essentials.api.event.player.PlayerOnChatMessageEvent; +import org.kilocraft.essentials.api.user.OnlineUser; + +public class PlayerOnChatMessageEventImpl implements PlayerOnChatMessageEvent { + private boolean cancelled = false; + private String cancelReason; + private final ServerPlayerEntity player; + private String message; + + public PlayerOnChatMessageEventImpl(@NotNull final ServerPlayerEntity player, @NotNull final String message) { + this.player = player; + this.message = message; + } + + @Override + public String getMessage() { + return this.message; + } + + @Override + public void setMessage(String message) { + this.message = message; + } + + @Override + public String getCancelReason() { + return this.cancelReason; + } + + @Override + public void setCancelReason(String reason) { + this.cancelReason = reason; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean isCancelled) { + this.cancelled = isCancelled; + } + + @Override + public ServerPlayerEntity getPlayer() { + return this.player; + } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser(this.player); + } + + @Override + public ServerWorld getWorld() { + return this.player.getServerWorld(); + } +} diff --git a/src/main/java/org/kilocraft/essentials/events/player/PlayerStopRidingEventImpl.java b/src/main/java/org/kilocraft/essentials/events/player/PlayerStopRidingEventImpl.java index fd40d242..6da15008 100644 --- a/src/main/java/org/kilocraft/essentials/events/player/PlayerStopRidingEventImpl.java +++ b/src/main/java/org/kilocraft/essentials/events/player/PlayerStopRidingEventImpl.java @@ -1,7 +1,9 @@ package org.kilocraft.essentials.events.player; import net.minecraft.server.network.ServerPlayerEntity; +import org.kilocraft.essentials.api.KiloServer; import org.kilocraft.essentials.api.event.player.PlayerStopRidingEvent; +import org.kilocraft.essentials.api.user.OnlineUser; public class PlayerStopRidingEventImpl implements PlayerStopRidingEvent { private boolean cancelled = false; @@ -25,4 +27,9 @@ public boolean isCancelled() { public void setCancelled(boolean isCancelled) { this.cancelled = isCancelled; } + + @Override + public OnlineUser getUser() { + return KiloServer.getServer().getOnlineUser(this.player); + } } diff --git a/src/main/java/org/kilocraft/essentials/mixin/events/MixinServerPlayNetworkHandler$PlayerEvents.java b/src/main/java/org/kilocraft/essentials/mixin/events/MixinServerPlayNetworkHandler$PlayerEvents.java index 88229933..c4630ff7 100644 --- a/src/main/java/org/kilocraft/essentials/mixin/events/MixinServerPlayNetworkHandler$PlayerEvents.java +++ b/src/main/java/org/kilocraft/essentials/mixin/events/MixinServerPlayNetworkHandler$PlayerEvents.java @@ -60,6 +60,11 @@ private static boolean shouldContinueUsingItem(ServerPlayerEntity serverPlayerEn } } + @Redirect(at = @At(value = "HEAD"), method = "onDisconnected") + private void ke$triggerEvent$onDisconnect() { + KiloServer.getServer().triggerEvent(new PlayerDisconnectEventImpl(this.player)); + } + @Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"), method = "onDisconnected") private void ke$remove$sendToAll(PlayerManager playerManager, Text text, MessageType messageType, UUID uUID) { //Ignored diff --git a/src/main/java/org/kilocraft/essentials/user/ServerUserManager.java b/src/main/java/org/kilocraft/essentials/user/ServerUserManager.java index 321d151a..930ee33f 100644 --- a/src/main/java/org/kilocraft/essentials/user/ServerUserManager.java +++ b/src/main/java/org/kilocraft/essentials/user/ServerUserManager.java @@ -20,6 +20,7 @@ import org.kilocraft.essentials.KiloDebugUtils; import org.kilocraft.essentials.api.KiloEssentials; import org.kilocraft.essentials.api.KiloServer; +import org.kilocraft.essentials.api.event.player.PlayerOnChatMessageEvent; import org.kilocraft.essentials.api.text.TextFormat; import org.kilocraft.essentials.api.util.Cached; import org.kilocraft.essentials.chat.LangText; @@ -31,6 +32,7 @@ import org.kilocraft.essentials.chat.ServerChat; import org.kilocraft.essentials.chat.TextMessage; import org.kilocraft.essentials.config.KiloConfig; +import org.kilocraft.essentials.events.player.PlayerOnChatMessageEventImpl; import org.kilocraft.essentials.extensions.betterchairs.SeatManager; import org.kilocraft.essentials.user.setting.Settings; import org.kilocraft.essentials.util.CacheManager; @@ -357,8 +359,13 @@ public void onChatMessage(OnlineUser user, ChatMessageC2SPacket packet) { ServerPlayerEntity player = user.asPlayer(); NetworkThreadUtils.forceMainThread(packet, player.networkHandler, player.getServerWorld()); + PlayerOnChatMessageEvent event = KiloServer.getServer().triggerEvent(new PlayerOnChatMessageEventImpl(player, packet.getChatMessage())); + if (event.isCancelled()) { + return; + } + player.updateLastActionTime(); - String string = StringUtils.normalizeSpace(packet.getChatMessage()); + String string = StringUtils.normalizeSpace(event.getMessage()); for (int i = 0; i < string.length(); ++i) { if (!SharedConstants.isValidChar(string.charAt(i))) {