-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Lock player on login mode with event listener
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 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
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); | ||
} | ||
} |
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
155 changes: 155 additions & 0 deletions
155
src/main/java/org/altart/telegrambridge/events/RestrictionEvent.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,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); | ||
} | ||
} | ||
} |