Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Commit

Permalink
Changes -> Added Unsign command
Browse files Browse the repository at this point in the history
  • Loading branch information
EinfacheSache committed Aug 30, 2023
1 parent 5b5b26c commit b448e2a
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public Logger getLogger() {
@Override
public ArrayList<String> getPlugins() {
ArrayList<String> plugins = new ArrayList<>();
getProxy().getPluginManager().getPlugins().forEach(p -> plugins.add(p.getDescription().getName().orElseThrow(null)));
getProxy().getPluginManager().getPlugins().forEach(p -> plugins.add(p.getDescription().getName().orElse("NAME_NOT_FOUND:" + p.getDescription())));
return plugins;
}

Expand All @@ -149,6 +149,6 @@ public PluginType getPluginType() {

@Override
public String getPluginVersion() {
return proxy.getPluginManager().ensurePluginContainer(this).getDescription().getVersion().orElse("");
return proxy.getPluginManager().ensurePluginContainer(this).getDescription().getVersion().orElse("PLUGIN_VERSION_NOT_FOUND");
}
}
8 changes: 6 additions & 2 deletions src/main/java/de/cubeattack/neoprotect/velocity/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import de.cubeattack.neoprotect.velocity.listener.ChatListener;
import de.cubeattack.neoprotect.velocity.listener.DisconnectListener;
import de.cubeattack.neoprotect.velocity.listener.LoginListener;
import de.cubeattack.neoprotect.velocity.messageunsign.JoinListener;
import de.cubeattack.neoprotect.velocity.messageunsign.SessionChatListener;
import de.cubeattack.neoprotect.velocity.proxyprotocol.ProxyProtocol;
import de.cubeattack.neoprotect.velocity.unsign.command.KeyedCommandListener;
import de.cubeattack.neoprotect.velocity.unsign.command.SessionCommandListener;
import de.cubeattack.neoprotect.velocity.unsign.message.JoinListener;
import de.cubeattack.neoprotect.velocity.unsign.message.SessionChatListener;

public class Startup {

Expand All @@ -28,5 +30,7 @@ private void register(NeoProtectVelocity instance) {
em.register(instance, new LoginListener(instance));
em.register(instance, new DisconnectListener(instance));
em.register(instance, new SessionChatListener(instance));
em.register(instance, new KeyedCommandListener(instance));
em.register(instance, new SessionCommandListener(instance));
}
}
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;
}
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubeattack.neoprotect.velocity.messageunsign;
package de.cubeattack.neoprotect.velocity.unsign.message;

import com.velocitypowered.api.event.Continuation;
import com.velocitypowered.api.event.EventTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubeattack.neoprotect.velocity.messageunsign;
package de.cubeattack.neoprotect.velocity.unsign.message;

import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.proxy.Player;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubeattack.neoprotect.velocity.messageunsign;
package de.cubeattack.neoprotect.velocity.unsign.message;

import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.proxy.Player;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubeattack.neoprotect.velocity.messageunsign;
package de.cubeattack.neoprotect.velocity.unsign.message;

import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.Subscribe;
Expand Down

0 comments on commit b448e2a

Please sign in to comment.