Skip to content

Commit

Permalink
feat: Lock player on login mode with event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
alt-art committed Nov 26, 2024
1 parent 1dba06f commit e7bf92e
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/altart/telegrambridge/AuthManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.altart.telegrambridge;

import org.bukkit.entity.Player;

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

public class AuthManager {
private final List<String> loggedPlayers = new ArrayList<>();

public boolean isLogged(Player player) {
String nickname = player.getName();
return loggedPlayers.contains(nickname);
}

public void login(String nickname) {
if (!loggedPlayers.contains(nickname)) {
loggedPlayers.add(nickname);
}
}

public void logout(String nickname) {
loggedPlayers.remove(nickname);
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/altart/telegrambridge/TelegramBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.altart.telegrambridge.database.SQLite;
import org.altart.telegrambridge.events.ChatEvent;
import org.altart.telegrambridge.events.GameEvent;
import org.altart.telegrambridge.events.RestrictionEvent;
import org.altart.telegrambridge.utils.Format;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
Expand All @@ -26,6 +27,7 @@ public final class TelegramBridge extends JavaPlugin {
public static Config config;
public static Translations translations;
public static SQLite database;
public static AuthManager authManager;

public static TelegramBot telegramBot;
private BotSession botSession;
Expand All @@ -37,6 +39,7 @@ public void onEnable() {
config = new Config();
translations = new Translations(config.lang);
database = new SQLite();
authManager = new AuthManager();

if (Objects.equals(config.botToken, "YOUR_BOT_TOKEN") || Objects.equals(config.chats.get(0).id, "YOUR_CHAT_ID")) {
log.severe("Please set your bot token and chat id in the config file");
Expand All @@ -55,6 +58,8 @@ public void onEnable() {

Bukkit.getPluginManager().registerEvents(new ChatEvent(), plugin);
Bukkit.getPluginManager().registerEvents(new GameEvent(), plugin);
Bukkit.getPluginManager().registerEvents(new RestrictionEvent(authManager), plugin);

try {
Objects.requireNonNull(getCommand("tbreload")).setExecutor(new ReloadCommand());
PluginCommand replyCommand = Objects.requireNonNull(getCommand("tbreply"));
Expand Down
155 changes: 155 additions & 0 deletions src/main/java/org/altart/telegrambridge/events/RestrictionEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.altart.telegrambridge.events;

import org.altart.telegrambridge.AuthManager;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.*;

import java.util.UUID;

public class RestrictionEvent implements Listener {
private final AuthManager authManager;

public RestrictionEvent(AuthManager authManager) {
this.authManager = authManager;
}

@EventHandler(ignoreCancelled = true)
public void onLoginBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginDisplayCommands(PlayerCommandSendEvent event) {
Player player = event.getPlayer();
if (!authManager.isLogged(player)) {
event.getCommands().clear();
}
}

@EventHandler(ignoreCancelled = true)
public void onLoginCommand(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
if (!authManager.isLogged(player)) {
event.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true)
public void onLoginInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (authManager.isLogged(player)) return;

event.setUseInteractedBlock(Event.Result.DENY);
event.setUseItemInHand(Event.Result.DENY);
event.setCancelled(true);
}

@EventHandler(ignoreCancelled = true)
public void onLoginInteractEntity(PlayerInteractEntityEvent event) {
Player player = event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginInteractEntity(PlayerInteractAtEntityEvent event) {
Player player = event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginInventorySwap(PlayerSwapHandItemsEvent event) {
Player player = event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginInventoryOpen(InventoryOpenEvent event) {
Player player = (Player) event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginItemDamage(PlayerItemDamageEvent event) {
Player player = event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginItemPickup(PlayerPickupItemEvent event) {
Player player = event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginItemDrop(PlayerDropItemEvent event) {
Player player = event.getPlayer();
event.setCancelled(!authManager.isLogged(player));
}

@EventHandler(ignoreCancelled = true)
public void onLoginDamage(EntityDamageEvent event) {
if (event.getEntity() instanceof Player) {
Player player = (Player) event.getEntity();
event.setCancelled(!authManager.isLogged(player));
}
}

@EventHandler(ignoreCancelled = true)
public void onLoginDamage(EntityDamageByEntityEvent event) {
if (event.getEntity() instanceof Player) {
Player player = (Player) event.getEntity();
event.setCancelled(!authManager.isLogged(player));
}
}

@EventHandler(ignoreCancelled = true)
public void onLoginDamage(EntityDamageByBlockEvent event) {
if (event.getEntity() instanceof Player) {
Player player = (Player) event.getEntity();
event.setCancelled(!authManager.isLogged(player));
}
}

@EventHandler(ignoreCancelled = true)
public void onLoginMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (authManager.isLogged(player)) return;

Location to = event.getTo();
if (to == null) {
event.setCancelled(true);
return;
}

Location from = event.getFrom();

if (from.getX() != to.getX() || from.getY() != to.getY() || from.getZ() != to.getZ()) {
event.setCancelled(true);
}
}
}

0 comments on commit e7bf92e

Please sign in to comment.