Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #89 from OnBlock/Beta
Browse files Browse the repository at this point in the history
New Event: PlayerOnChatMessage, Fixed the Disconnect event not getting triggered
  • Loading branch information
ItsEeleeya authored May 25, 2020
2 parents 8aed6c0 + 9dbd88d commit f513dae
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/kilocraft/essentials/KiloCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,8 +29,7 @@ private int execute(CommandContext<ServerCommandSource> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,4 +35,9 @@ public void setCancelled(boolean isCancelled) {
public ServerPlayerEntity getPlayer() {
return player;
}

@Override
public OnlineUser getUser() {
return KiloServer.getServer().getOnlineUser(this.player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,4 +69,9 @@ public ServerPlayNetworkHandler getNetworkHandler() {
public MinecraftServer getServer() {
return player.getServer();
}

@Override
public OnlineUser getUser() {
return KiloServer.getServer().getOnlineUser(this.player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,4 +68,9 @@ public ServerPlayNetworkHandler getNetworkHandler() {
public MinecraftServer getServer() {
return player.getServer();
}

@Override
public OnlineUser getUser() {
return KiloServer.getServer().getOnlineUser(this.player);
}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -21,5 +23,10 @@ public ServerPlayerEntity getPlayer() {
public MinecraftServer getServer() {
return player.getServer();
}

@Override
public OnlineUser getUser() {
return KiloServer.getServer().getOnlineUser(this.player);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,4 +54,8 @@ public Hand getHand() {
return this.hand;
}

@Override
public OnlineUser getUser() {
return KiloServer.getServer().getOnlineUser((ServerPlayerEntity) this.player);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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))) {
Expand Down

0 comments on commit f513dae

Please sign in to comment.