Skip to content

Commit

Permalink
Merge pull request #1 from RobotoRaccoon/master
Browse files Browse the repository at this point in the history
Fix teleport exploits
  • Loading branch information
selfslaughter authored Apr 23, 2020
2 parents 1f90bb8 + 8daedf2 commit 33042dc
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
target/
lib/
/bin/

.idea/
*.iml
14 changes: 10 additions & 4 deletions src/main/java/nl/Steffion/BlockHunt/ArenaHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public static void playerJoinArena(Player player, String arenaname) {
return;
}
}

boolean canWarp = PlayerHandler.teleport(player, arena.lobbyWarp);
if (!canWarp) {
MessageManager.sendFMessage(player, ConfigC.error_teleportFailed);
return;
}

System.out.println("[BlockHunt] " + player.getName() + " has joined " + arenaname);
arena.playersInArena.add(player);
JoinArenaEvent event = new JoinArenaEvent(player, arena);
Expand All @@ -107,7 +114,6 @@ public static void playerJoinArena(Player player, String arenaname) {

MemoryStorage.pData.put(player, pad);

player.teleport(arena.lobbyWarp);
player.setGameMode(GameMode.SURVIVAL);
for (PotionEffect pe : player.getActivePotionEffects()) {
player.removePotionEffect(pe.getType());
Expand Down Expand Up @@ -267,7 +273,7 @@ public static void playerLeaveArena(Player player, boolean message, boolean clea
}
seeker.getInventory().clear();
arena.seekers.add(seeker);
seeker.teleport(arena.seekersWarp);
PlayerHandler.teleport(seeker, arena.seekersWarp);
MemoryStorage.seekertime.put(seeker, arena.waitingTimeSeeker);
seeker.setWalkSpeed(0.3F);

Expand Down Expand Up @@ -300,7 +306,7 @@ public static void playerLeaveArena(Player player, boolean message, boolean clea
player.setHealth(pad.pHealth);
player.setFoodLevel(pad.pFood);
player.addPotionEffects(pad.pPotionEffects);
player.teleport(arena.spawnWarp);
PlayerHandler.teleport(player, arena.spawnWarp);
player.setGameMode(pad.pGameMode);
player.setAllowFlight(pad.pFlying);
if (player.getAllowFlight()) {
Expand Down Expand Up @@ -434,7 +440,7 @@ public static void stopArena(Arena arena,String cause, ConfigC message) {
arena.timer = 0;
arena.playersInArena.clear();
}

public static boolean noPlayersInArenas() {
// Check if there are any players in any arena (quick way to early exit for event handlers)
for (Arena arena : MemoryStorage.arenaList) {
Expand Down
26 changes: 8 additions & 18 deletions src/main/java/nl/Steffion/BlockHunt/BlockHunt.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
import nl.Steffion.BlockHunt.Arena.ArenaState;
import nl.Steffion.BlockHunt.Listeners.*;
import nl.Steffion.BlockHunt.PermissionsC.Permissions;
import nl.Steffion.BlockHunt.Commands.CMDcreate;
import nl.Steffion.BlockHunt.Commands.CMDhelp;
Expand All @@ -23,18 +24,6 @@
import nl.Steffion.BlockHunt.Commands.CMDstart;
import nl.Steffion.BlockHunt.Commands.CMDtokens;
import nl.Steffion.BlockHunt.Commands.CMDwand;
import nl.Steffion.BlockHunt.Listeners.OnBlockBreakEvent;
import nl.Steffion.BlockHunt.Listeners.OnBlockPlaceEvent;
import nl.Steffion.BlockHunt.Listeners.OnEntityDamageByEntityEvent;
import nl.Steffion.BlockHunt.Listeners.OnEntityDamageEvent;
import nl.Steffion.BlockHunt.Listeners.OnFoodLevelChangeEvent;
import nl.Steffion.BlockHunt.Listeners.OnInventoryClickEvent;
import nl.Steffion.BlockHunt.Listeners.OnInventoryCloseEvent;
import nl.Steffion.BlockHunt.Listeners.OnPlayerDropItemEvent;
import nl.Steffion.BlockHunt.Listeners.OnPlayerInteractEvent;
import nl.Steffion.BlockHunt.Listeners.OnPlayerMoveEvent;
import nl.Steffion.BlockHunt.Listeners.OnPlayerQuitEvent;
import nl.Steffion.BlockHunt.Listeners.OnSignChangeEvent;
import nl.Steffion.BlockHunt.Managers.CommandManager;
import nl.Steffion.BlockHunt.Managers.ConfigManager;
import nl.Steffion.BlockHunt.Managers.MessageManager;
Expand Down Expand Up @@ -129,6 +118,7 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new OnPlayerDropItemEvent(), this);
getServer().getPluginManager().registerEvents(new OnPlayerInteractEvent(), this);
getServer().getPluginManager().registerEvents(new OnPlayerMoveEvent(), this);
getServer().getPluginManager().registerEvents(new OnPlayerTeleportEvent(), this);
getServer().getPluginManager().registerEvents(new OnPlayerQuitEvent(), this);
getServer().getPluginManager().registerEvents(new OnSignChangeEvent(), this);

Expand Down Expand Up @@ -249,7 +239,7 @@ public void onEnable() {
if (!arena.seekers.contains(seeker)) {
ArenaHandler.sendFMessage(arena, ConfigC.normal_ingameSeekerChoosen, "seeker-" + seeker.getName());
arena.seekers.add(seeker);
seeker.teleport(arena.seekersWarp);
PlayerHandler.teleport(seeker, arena.seekersWarp);
seeker.getInventory().clear();
seeker.updateInventory();
seeker.setWalkSpeed(0.3F);
Expand All @@ -273,7 +263,7 @@ public void onEnable() {

MiscDisguise disguise = new MiscDisguise(DisguiseType.FALLING_BLOCK, block.getType());
DisguiseAPI.disguiseToAll(arenaPlayer, disguise);
arenaPlayer.teleport(arena.hidersWarp);
PlayerHandler.teleport(arenaPlayer, arena.hidersWarp);
ItemStack blockCount = new ItemStack(block.getType(), 5);
arenaPlayer.getInventory().setItem(8, blockCount);
arenaPlayer.getInventory().setHelmet(new ItemStack(block));
Expand Down Expand Up @@ -307,7 +297,7 @@ public void onEnable() {
if (MemoryStorage.seekertime.get(player) != null) {
MemoryStorage.seekertime.put(player, MemoryStorage.seekertime.get(player) - 1);
if (MemoryStorage.seekertime.get(player) <= 0) {
player.teleport(arena.hidersWarp);
PlayerHandler.teleport(player, arena.hidersWarp);
MemoryStorage.seekertime.remove(player);
ArenaHandler.sendFMessage(arena, ConfigC.normal_ingameSeekerSpawned, "playername-" + player.getName());
}
Expand Down Expand Up @@ -461,12 +451,12 @@ public void onDisable() {

/**
* Args to String. Makes 1 string.
*
*
* @param input
* String list which should be converted to a string.
* @param startArg
* Start on this length.
*
*
* @return The converted string.
*/
public static String stringBuilder(String[] input, int startArg) {
Expand Down Expand Up @@ -547,7 +537,7 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe

/**
* Short a String for like the Scoreboard title.
*
*
* @param string
* String to be shorten.
* @param maxLenght
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/nl/Steffion/BlockHunt/ConfigC.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ public enum ConfigC {
"%TAG%ENo player found with the name '%A%playername%%E'!", MemoryStorage.messages), error_tokensUnknownsetting("%TAG%E'%A%option%%E' is not a known option!", MemoryStorage.messages), error_shopNeedMoreTokens(
"%TAG%EYou need more tokens before you can buy this item.", MemoryStorage.messages), error_shopMaxSeekersReached(
"%TAG%ESorry, the maximum amount of seekers has been reached!", MemoryStorage.messages), error_shopMaxHidersReached(
"%TAG%ESorry, the maximum amount of hiders has been reached!", MemoryStorage.messages);
"%TAG%ESorry, the maximum amount of hiders has been reached!", MemoryStorage.messages),
error_teleportFailed("%TAG%EUnable to teleport you into the arena.", MemoryStorage.messages),
error_teleportBlocked("%TAG%EYou cannot teleport while playing!", MemoryStorage.messages);

public Object value;
public ConfigManager config;
public String location;

/**
* Makes an object from the list above.
*
*
* @param value
* Setting in the config file.
* @param config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
import java.util.List;

import me.libraryaddict.disguise.DisguiseAPI;
import nl.Steffion.BlockHunt.Arena;
import nl.Steffion.BlockHunt.*;
import nl.Steffion.BlockHunt.Arena.ArenaState;
import nl.Steffion.BlockHunt.ArenaHandler;
import nl.Steffion.BlockHunt.BlockHunt;
import nl.Steffion.BlockHunt.ConfigC;
import nl.Steffion.BlockHunt.MemoryStorage;
import nl.Steffion.BlockHunt.Managers.MessageManager;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -148,7 +144,7 @@ public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
} else {
DisguiseAPI.undisguiseToAll(player);
MemoryStorage.seekertime.put(player, arena.waitingTimeSeeker);
player.teleport(arena.seekersWarp);
PlayerHandler.teleport(player, arena.seekersWarp);
player.setGameMode(GameMode.SURVIVAL);
player.setWalkSpeed(0.3F);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package nl.Steffion.BlockHunt.Listeners;

import nl.Steffion.BlockHunt.Arena;
import nl.Steffion.BlockHunt.ArenaHandler;
import nl.Steffion.BlockHunt.*;
import nl.Steffion.BlockHunt.Arena.ArenaState;
import nl.Steffion.BlockHunt.BlockHunt;
import nl.Steffion.BlockHunt.MemoryStorage;

import org.bukkit.Effect;
import org.bukkit.Location;
Expand Down Expand Up @@ -36,7 +33,7 @@ public void onPlayerMoveEvent(PlayerMoveEvent event) {
Location loc = player.getLocation();
player.playEffect(loc, Effect.ENDER_SIGNAL, null);
player.playSound(loc, Sound.ENTITY_GHAST_SHOOT, 1, 1);
player.teleport(arena.hidersWarp);
PlayerHandler.teleport(player, arena.hidersWarp);
return;
}
double maxX = Math.max(arena.pos1.getX(), arena.pos2.getX());
Expand All @@ -51,32 +48,32 @@ public void onPlayerMoveEvent(PlayerMoveEvent event) {
//event.setCancelled(true);
player.playEffect(loc, Effect.ENDER_SIGNAL, null);
player.playSound(loc, Sound.ENTITY_GHAST_SHOOT, 1, 1);
player.teleport(arena.hidersWarp);
PlayerHandler.teleport(player, arena.hidersWarp);
} else if (loc.getBlockX() < minX) {
//event.setCancelled(true);
player.playEffect(loc, Effect.ENDER_SIGNAL, null);
player.playSound(loc, Sound.ENTITY_GHAST_SHOOT, 1, 1);
player.teleport(arena.hidersWarp);
PlayerHandler.teleport(player, arena.hidersWarp);
} else if (loc.getBlockZ() > maxZ) {
//event.setCancelled(true);
player.playEffect(loc, Effect.ENDER_SIGNAL, null);
player.playSound(loc, Sound.ENTITY_GHAST_SHOOT, 1, 1);
player.teleport(arena.hidersWarp);
PlayerHandler.teleport(player, arena.hidersWarp);
} else if (loc.getBlockZ() < minZ) {
//event.setCancelled(true);
player.playEffect(loc, Effect.ENDER_SIGNAL, null);
player.playSound(loc, Sound.ENTITY_GHAST_SHOOT, 1, 1);
player.teleport(arena.hidersWarp);
PlayerHandler.teleport(player, arena.hidersWarp);
} else if (loc.getBlockY() > maxY) {
//event.setCancelled(true);
player.playEffect(loc, Effect.ENDER_SIGNAL, null);
player.playSound(loc, Sound.ENTITY_GHAST_SHOOT, 1, 1);
player.teleport(arena.hidersWarp);
PlayerHandler.teleport(player, arena.hidersWarp);
} else if (loc.getBlockY() < minY) {
//event.setCancelled(true);
player.playEffect(loc, Effect.ENDER_SIGNAL, null);
player.playSound(loc, Sound.ENTITY_GHAST_SHOOT, 1, 1);
player.teleport(arena.hidersWarp);
PlayerHandler.teleport(player, arena.hidersWarp);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nl.Steffion.BlockHunt.Listeners;

import nl.Steffion.BlockHunt.ConfigC;
import nl.Steffion.BlockHunt.Managers.MessageManager;
import nl.Steffion.BlockHunt.MemoryStorage;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;

public class OnPlayerTeleportEvent implements Listener {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerTeleportEvent(PlayerTeleportEvent event) {
Player player = event.getPlayer();
if (MemoryStorage.pData.get(player) != null) {
handlePlayer(event, player);
}
}

/**
* Handle a player that is in an arena
* @param event PlayerTeleportEvent
* @param player Player
*/
private void handlePlayer(PlayerTeleportEvent event, Player player) {
Location storedLoc = MemoryStorage.teleportLoc.remove(player);
if (storedLoc == null || event.getTo().distanceSquared(storedLoc) > 1) {
MessageManager.sendFMessage(player, ConfigC.error_teleportBlocked);
event.setCancelled(true);
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/nl/Steffion/BlockHunt/MemoryStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import nl.Steffion.BlockHunt.Managers.CommandManager;
Expand Down Expand Up @@ -59,4 +60,6 @@ public class MemoryStorage {
public static HashMap<Player, Location> moveLoc = new HashMap<>();
public static HashMap<Player, Location> hiddenLoc = new HashMap<>();
public static HashMap<Player, Boolean> hiddenLocWater = new HashMap<>();

public static Map<Player, Location> teleportLoc = new HashMap<>();
}
11 changes: 11 additions & 0 deletions src/main/java/nl/Steffion/BlockHunt/PlayerHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nl.Steffion.BlockHunt;

import org.bukkit.Location;
import org.bukkit.entity.Player;

public class PlayerHandler {
public static boolean teleport(Player player, Location location) {
MemoryStorage.teleportLoc.put(player, location);
return player.teleport(location);
}
}
4 changes: 3 additions & 1 deletion src/main/resources/lang/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ error-tokensPlayerNotOnline: '%TAG%ENo player found with the name ''%A%playernam
error-tokensUnknownsetting: '%TAG%E''%A%option%%E'' is not a known option!'
error-shopNeedMoreTokens: '%TAG%EYou need more tokens before you can buy this item.'
error-shopMaxSeekersReached: '%TAG%ESorry, the maximum amount of seekers has been reached!'
error-shopMaxHidersReached: '%TAG%ESorry, the maximum amount of hiders has been reached!'
error-shopMaxHidersReached: '%TAG%ESorry, the maximum amount of hiders has been reached!'
error-teleportFailed: '%TAG%EUnable to teleport you into the arena.'
error-teleportBlocked: '%TAG%EYou cannot teleport while playing!'
4 changes: 3 additions & 1 deletion src/main/resources/lang/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ error-tokensPlayerNotOnline: '%TAG%ENo player found with the name ''%A%playernam
error-tokensUnknownsetting: '%TAG%E''%A%option%%E'' is not a known option!'
error-shopNeedMoreTokens: '%TAG%EYou need more tokens before you can buy this item.'
error-shopMaxSeekersReached: '%TAG%ESorry, the maximum amount of seekers has been reached!'
error-shopMaxHidersReached: '%TAG%ESorry, the maximum amount of hiders has been reached!'
error-shopMaxHidersReached: '%TAG%ESorry, the maximum amount of hiders has been reached!'
error-teleportFailed: '%TAG%EUnable to teleport you into the arena.'
error-teleportBlocked: '%TAG%EYou cannot teleport while playing!'
2 changes: 2 additions & 0 deletions src/test/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,5 @@ error:
shopNeedMoreTokens: '%TAG%EYou need more tokens before you can buy this item.'
shopMaxSeekersReached: '%TAG%ESorry, the maximum amount of seekers has been reached!'
shopMaxHidersReached: '%TAG%ESorry, the maximum amount of hiders has been reached!'
teleportFailed: '%TAG%EUnable to teleport you into the arena.'
teleportBlocked: '%TAG%EYou cannot teleport while playing!'

0 comments on commit 33042dc

Please sign in to comment.