Skip to content

Commit

Permalink
added blocking of console commands if the player is not authorized
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisIndenbom committed Dec 20, 2021
1 parent 5d4789b commit 7121528
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
Expand All @@ -28,6 +29,10 @@

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class PlayerListener implements Listener
{
Expand All @@ -37,6 +42,8 @@ public class PlayerListener implements Listener
private final boolean kick;
private final long authTime;

private final List<String> VALID_COMMANDS = this.getCommandsList("/login ", "/l ", "/log ", "/register ", "/r ", "/reg ", "/change_password ");

private final MessageSender messageSender = new MessageSender();

public PlayerListener(CyberAuth plugin, FileConfiguration messages, boolean kick, long authTime)
Expand Down Expand Up @@ -72,14 +79,26 @@ public void onPlayerQuit(@NotNull PlayerQuitEvent event)
}

@EventHandler
public void onPlayerInteract(@NotNull PlayerInteractEvent event)
public void onPlayerChat(@NotNull PlayerChatEvent event)
{
// check that player is authorized
if (!userIsAuth(event.getPlayer())) event.setCancelled(true);
}

@EventHandler
public void onPlayerChat(@NotNull PlayerChatEvent event)
public void onPlayerCommandPreprocess(@NotNull PlayerCommandPreprocessEvent event)
{
if (userIsAuth(event.getPlayer())) return;

for (String validCommand : this.VALID_COMMANDS)
if (event.getMessage().contains(validCommand)) return;

this.messageSender.sendMessage(event.getPlayer(), this.messages.getString("error.not_logged_in"));
event.setCancelled(true);
}

@EventHandler
public void onPlayerInteract(@NotNull PlayerInteractEvent event)
{
// check that player is authorized
if (!userIsAuth(event.getPlayer())) event.setCancelled(true);
Expand Down Expand Up @@ -134,7 +153,7 @@ public void onPlayerDropItem(@NotNull PlayerDropItemEvent event)
}

@EventHandler
public void onEntityDamageByEntity(@NotNull EntityDamageByEntityEvent event)
public void onEntityDamageByPlayer(@NotNull EntityDamageByEntityEvent event)
{
if (!event.getDamager().getType().equals(EntityType.PLAYER)) return;

Expand All @@ -144,7 +163,7 @@ public void onEntityDamageByEntity(@NotNull EntityDamageByEntityEvent event)
}

@EventHandler
public void onEntityDamage(@NotNull EntityDamageEvent event)
public void onPlayerDamage(@NotNull EntityDamageEvent event)
{
if (!event.getEntityType().equals(EntityType.PLAYER)) return;

Expand Down Expand Up @@ -194,4 +213,9 @@ public void run()
}
}.runTaskTimer(this.plugin, 10, 200);
}

private List<String> getCommandsList(String... commands)
{
return new ArrayList<>(Arrays.stream(commands).toList());
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: CyberAuth
version: 1.0.2
version: 1.0.3
main: com.denisindenbom.cyberauth.CyberAuth

prefix: CyberAuth
Expand Down

0 comments on commit 7121528

Please sign in to comment.