This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b5b26c
commit b448e2a
Showing
8 changed files
with
200 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/de/cubeattack/neoprotect/velocity/unsign/command/KeyedCommandListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package de.cubeattack.neoprotect.velocity.unsign.command; | ||
|
||
import com.google.inject.Inject; | ||
import com.velocitypowered.api.event.ResultedEvent; | ||
import com.velocitypowered.api.event.command.CommandExecuteEvent; | ||
import com.velocitypowered.proxy.VelocityServer; | ||
import com.velocitypowered.proxy.connection.client.ConnectedPlayer; | ||
import com.velocitypowered.proxy.protocol.packet.chat.CommandHandler; | ||
import com.velocitypowered.proxy.protocol.packet.chat.builder.ChatBuilderV2; | ||
import com.velocitypowered.proxy.protocol.packet.chat.keyed.KeyedPlayerCommand; | ||
import de.cubeattack.neoprotect.velocity.NeoProtectVelocity; | ||
import de.cubeattack.neoprotect.velocity.unsign.message.PacketReceiveEvent; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public final class KeyedCommandListener implements CommandHandler<KeyedPlayerCommand> { | ||
|
||
private final VelocityServer proxyServer; | ||
|
||
@Inject | ||
public KeyedCommandListener(final NeoProtectVelocity plugin) { | ||
this.proxyServer = (VelocityServer) plugin.getProxy(); | ||
proxyServer.getEventManager().register(plugin, PacketReceiveEvent.class, this::onCommand); | ||
} | ||
|
||
public void onCommand(final PacketReceiveEvent event) { | ||
if (!(event.getPacket() instanceof KeyedPlayerCommand)) { | ||
return; | ||
} | ||
|
||
KeyedPlayerCommand packet = (KeyedPlayerCommand) event.getPacket(); | ||
|
||
final ConnectedPlayer player = (ConnectedPlayer) event.getPlayer(); | ||
if (checkConnectionFailed(player)) return; | ||
|
||
event.setResult(ResultedEvent.GenericResult.denied()); | ||
final String commandExecuted = packet.getCommand(); | ||
|
||
queueCommandResult(proxyServer, player, commandEvent -> { | ||
final CommandExecuteEvent.CommandResult result = commandEvent.getResult(); | ||
if (result == CommandExecuteEvent.CommandResult.denied()) { | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
final String commandToRun = result.getCommand().orElse(commandExecuted); | ||
if (result.isForwardToServer()) { | ||
ChatBuilderV2 write = player.getChatBuilderFactory() | ||
.builder() | ||
.setTimestamp(packet.getTimestamp()) | ||
.asPlayer(player); | ||
|
||
if (commandToRun.equals(commandExecuted)) { | ||
return CompletableFuture.completedFuture(packet); | ||
} else { | ||
write.message("/" + commandToRun); | ||
} | ||
return CompletableFuture.completedFuture(write.toServer()); | ||
} | ||
|
||
return runCommand(proxyServer, player, commandToRun, hasRun -> { | ||
if (hasRun) return null; | ||
|
||
if (commandToRun.equals(packet.getCommand())) { | ||
return packet; | ||
} | ||
|
||
return player.getChatBuilderFactory() | ||
.builder() | ||
.setTimestamp(packet.getTimestamp()) | ||
.asPlayer(player) | ||
.message("/" + commandToRun) | ||
.toServer(); | ||
}); | ||
}, packet.getCommand(), packet.getTimestamp()); | ||
} | ||
|
||
public Class<KeyedPlayerCommand> packetClass() { | ||
return KeyedPlayerCommand.class; | ||
} | ||
|
||
@Override | ||
public void handlePlayerCommandInternal(KeyedPlayerCommand keyedPlayerCommand) { | ||
|
||
} | ||
|
||
public static boolean checkConnectionFailed(final ConnectedPlayer player) { | ||
try { | ||
player.ensureAndGetCurrentServer().ensureConnected(); | ||
return false; | ||
} catch (final IllegalStateException e) { | ||
return true; | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/de/cubeattack/neoprotect/velocity/unsign/command/SessionCommandListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package de.cubeattack.neoprotect.velocity.unsign.command; | ||
|
||
import com.google.inject.Inject; | ||
import com.velocitypowered.api.event.ResultedEvent; | ||
import com.velocitypowered.api.event.command.CommandExecuteEvent; | ||
import com.velocitypowered.proxy.VelocityServer; | ||
import com.velocitypowered.proxy.connection.client.ConnectedPlayer; | ||
import com.velocitypowered.proxy.protocol.packet.chat.CommandHandler; | ||
import com.velocitypowered.proxy.protocol.packet.chat.session.SessionPlayerCommand; | ||
import de.cubeattack.neoprotect.velocity.NeoProtectVelocity; | ||
import de.cubeattack.neoprotect.velocity.unsign.message.PacketReceiveEvent; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public final class SessionCommandListener implements CommandHandler<SessionPlayerCommand> { | ||
|
||
private final VelocityServer proxyServer; | ||
|
||
@Inject | ||
public SessionCommandListener(NeoProtectVelocity plugin) { | ||
this.proxyServer = (VelocityServer) plugin.getProxy(); | ||
this.proxyServer.getEventManager().register(plugin, PacketReceiveEvent.class, this::onCommand); | ||
} | ||
|
||
|
||
public void onCommand(final PacketReceiveEvent event) { | ||
if (!(event.getPacket() instanceof SessionPlayerCommand)) { | ||
return; | ||
} | ||
|
||
SessionPlayerCommand packet = (SessionPlayerCommand) event.getPacket(); | ||
|
||
final ConnectedPlayer player = (ConnectedPlayer) event.getPlayer(); | ||
if (checkConnectionFailed(player)) return; | ||
|
||
event.setResult(ResultedEvent.GenericResult.denied()); | ||
final String commandExecuted = packet.getCommand(); | ||
|
||
queueCommandResult(proxyServer, player, commandEvent -> { | ||
final CommandExecuteEvent.CommandResult result = commandEvent.getResult(); | ||
if (result == CommandExecuteEvent.CommandResult.denied()) { | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
final String commandToRun = result.getCommand().orElse(commandExecuted); | ||
if (result.isForwardToServer()) { | ||
if (commandToRun.equals(commandExecuted)) { | ||
return CompletableFuture.completedFuture(packet); | ||
} else { | ||
return CompletableFuture.completedFuture(player.getChatBuilderFactory() | ||
.builder() | ||
.setTimestamp(packet.getTimeStamp()) | ||
.asPlayer(player) | ||
.message("/" + commandToRun) | ||
.toServer()); | ||
} | ||
} | ||
|
||
return runCommand(proxyServer, player, commandToRun, hasRun -> { | ||
if (hasRun) return null; | ||
|
||
if (commandToRun.equals(commandExecuted)) { | ||
return packet; | ||
} else { | ||
return player.getChatBuilderFactory() | ||
.builder() | ||
.setTimestamp(packet.getTimeStamp()) | ||
.asPlayer(player) | ||
.message("/" + commandToRun) | ||
.toServer(); | ||
} | ||
}); | ||
}, commandExecuted, packet.getTimeStamp()); | ||
} | ||
|
||
@Override | ||
public Class<SessionPlayerCommand> packetClass() { | ||
return SessionPlayerCommand.class; | ||
} | ||
|
||
@Override | ||
public void handlePlayerCommandInternal(SessionPlayerCommand sessionPlayerCommand) { | ||
// noop | ||
} | ||
|
||
public boolean checkConnectionFailed(final ConnectedPlayer player) { | ||
try { | ||
player.ensureAndGetCurrentServer().ensureConnected(); | ||
return false; | ||
} catch (final IllegalStateException e) { | ||
return true; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../velocity/messageunsign/JoinListener.java → ...velocity/unsign/message/JoinListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ity/messageunsign/PacketReceiveEvent.java → ...ty/unsign/message/PacketReceiveEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...y/messageunsign/PlayerChannelHandler.java → .../unsign/message/PlayerChannelHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ty/messageunsign/SessionChatListener.java → ...y/unsign/message/SessionChatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters