Skip to content

Commit

Permalink
Add Portal rejection system
Browse files Browse the repository at this point in the history
  • Loading branch information
fernferret committed Nov 27, 2011
1 parent cd46f1c commit 5d53f15
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 30 deletions.
Expand Up @@ -8,6 +8,7 @@
import com.onarandombox.MultiverseNetherPortals.commands.ShowLinkCommand;
import com.onarandombox.MultiverseNetherPortals.commands.UnlinkCommand;
import com.onarandombox.MultiverseNetherPortals.listeners.MVNPConfigReloadListener;
import com.onarandombox.MultiverseNetherPortals.listeners.MVNPEntityListener;
import com.onarandombox.MultiverseNetherPortals.listeners.MVNPPlayerListener;
import com.onarandombox.MultiverseNetherPortals.listeners.MVNPPluginListener;
import com.pneumaticraft.commandhandler.CommandHandler;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class MultiverseNetherPortals extends JavaPlugin implements MVPlugin {
private Map<String, String> endLinkMap;
protected CommandHandler commandHandler;
private final static int requiresProtocol = 9;
private MVNPEntityListener entityListener;

@Override
public void onEnable() {
Expand Down Expand Up @@ -71,11 +73,13 @@ public void onEnable() {

this.pluginListener = new MVNPPluginListener(this);
this.playerListener = new MVNPPlayerListener(this);
this.entityListener = new MVNPEntityListener(this);
this.customListener = new MVNPConfigReloadListener(this);
// Register the PLUGIN_ENABLE Event as we will need to keep an eye out for the Core Enabling if we don't find it initially.
this.getServer().getPluginManager().registerEvent(Type.PLUGIN_ENABLE, this.pluginListener, Priority.Normal, this);
this.getServer().getPluginManager().registerEvent(Type.PLAYER_PORTAL, this.playerListener, Priority.Normal, this);
this.getServer().getPluginManager().registerEvent(Type.CUSTOM_EVENT, this.customListener, Priority.Normal, this);
this.getServer().getPluginManager().registerEvent(Type.ENTITY_PORTAL_ENTER, this.entityListener, Priority.Monitor, this);

log.info(logPrefix + "- Version " + this.getDescription().getVersion() + " Enabled - By " + getAuthors());

Expand Down
@@ -0,0 +1,138 @@
package com.onarandombox.MultiverseNetherPortals.listeners;

import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.utils.PermissionTools;
import com.onarandombox.MultiverseNetherPortals.MultiverseNetherPortals;
import com.onarandombox.MultiverseNetherPortals.utils.MVLinkChecker;
import com.onarandombox.MultiverseNetherPortals.utils.MVNameChecker;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityListener;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.util.Vector;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

/**
* Multiverse 2
*
* @author fernferret
*/
public class MVNPEntityListener extends EntityListener {

private MultiverseNetherPortals plugin;
private MVNameChecker nameChecker;
private MVLinkChecker linkChecker;
private MVWorldManager worldManager;
private PermissionTools pt;
private int cooldown = 250;
private Map<String, Date> playerErrors;

public MVNPEntityListener(MultiverseNetherPortals plugin) {
this.plugin = plugin;
this.nameChecker = new MVNameChecker(this.plugin);
this.linkChecker = new MVLinkChecker(this.plugin);
this.worldManager = this.plugin.getCore().getMVWorldManager();
this.pt = new PermissionTools(this.plugin.getCore());
this.playerErrors = new HashMap<String, Date>();
}

protected void shootPlayer(Player p, Block block, String type) {
this.playerErrors.put(p.getName(), new Date());
double myconst = 2;
double newVecX = 0;
double newVecZ = 0;
// Determine portal axis:
BlockFace face = p.getLocation().getBlock().getFace(block);
if (block.getRelative(BlockFace.EAST).getType() == Material.PORTAL || block.getRelative(BlockFace.WEST).getType() == Material.PORTAL) {
System.out.println("East/West");
if (p.getLocation().getX() < block.getLocation().getX()) {
newVecX = -1 * myconst;
} else {
newVecX = 1 * myconst;
}
} else {
//NOrth/South
System.out.println("N/S");
if (p.getLocation().getZ() < block.getLocation().getZ()) {
newVecZ = -1 * myconst;
} else {
newVecZ = 1 * myconst;
}
}
p.teleport(p.getLocation().clone().add(newVecX,.2,newVecZ));
System.out.println(new Vector(newVecX, .6, newVecZ));
p.setVelocity(new Vector(newVecX, .6, newVecZ));
}

@Override
public void onEntityPortalEnter(EntityPortalEnterEvent event) {
if (!(event.getEntity() instanceof Player)) {
return;
}
Player p = (Player) event.getEntity();
if (this.playerErrors.containsKey(p.getName())) {
Date lastTry = this.playerErrors.get(p.getName());
if (lastTry.getTime() + this.cooldown > new Date().getTime()) {
return;
}
this.playerErrors.remove(p.getName());
}

String type = "end";
if (event.getLocation().getBlock().getType() == Material.PORTAL) {
type = "nether";

}
String currentWorld = event.getLocation().getWorld().getName();
String linkedWorld = this.plugin.getWorldLink(event.getLocation().getWorld().getName(), type);
Location currentLocation = event.getLocation();
Location eventLocation = event.getLocation().clone();
Location toLocation = null;

if (linkedWorld != null) {
toLocation = this.linkChecker.findNewTeleportLocation(currentLocation, linkedWorld, p);
} else if (this.nameChecker.isValidNetherName(currentWorld)) {
toLocation = this.linkChecker.findNewTeleportLocation(currentLocation, this.nameChecker.getNormalName(currentWorld), p);
} else {
toLocation = this.linkChecker.findNewTeleportLocation(currentLocation, this.nameChecker.getNetherName(currentWorld), p);
}

if (toLocation == null) {
this.shootPlayer(p, eventLocation.getBlock(), type);
p.sendMessage("This portal goes nowhere!");
return;
}
MultiverseWorld fromWorld = this.worldManager.getMVWorld(p.getLocation().getWorld().getName());
MultiverseWorld toWorld = this.worldManager.getMVWorld(toLocation.getWorld().getName());
if (fromWorld.getCBWorld().equals(toWorld.getCBWorld())) {
// The player is Portaling to the same world.
this.plugin.log(Level.FINER, "Player '" + p.getName() + "' is portaling to the same world.");
return;
}
if (!pt.playerHasMoneyToEnter(fromWorld, toWorld, p, p, false)) {
System.out.println("BOOM");
this.shootPlayer(p, eventLocation.getBlock(), type);
this.plugin.log(Level.FINE, "Player '" + p.getName() + "' was DENIED ACCESS to '" + toWorld.getCBWorld().getName() +
"' because they don't have the FUNDS required to enter.");
return;
}
if (MultiverseCore.EnforceAccess) {
if (!pt.playerCanGoFromTo(fromWorld, toWorld, p, p)) {
this.shootPlayer(p, eventLocation.getBlock(), type);
this.plugin.log(Level.FINE, "Player '" + p.getName() + "' was DENIED ACCESS to '" + toWorld.getCBWorld().getName() +
"' because they don't have: multiverse.access." + toWorld.getCBWorld().getName());
}
} else {
this.plugin.log(Level.FINE, "Player '" + p.getName() + "' was allowed to go to '" + toWorld.getCBWorld().getName() + "' because enforceaccess is off.");
}
}
}
Expand Up @@ -6,6 +6,7 @@
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.utils.PermissionTools;
import com.onarandombox.MultiverseNetherPortals.MultiverseNetherPortals;
import com.onarandombox.MultiverseNetherPortals.utils.MVLinkChecker;
import com.onarandombox.MultiverseNetherPortals.utils.MVNameChecker;
import org.bukkit.Location;
import org.bukkit.Material;
Expand All @@ -19,6 +20,7 @@ public class MVNPPlayerListener extends PlayerListener {

private MultiverseNetherPortals plugin;
private MVNameChecker nameChecker;
private MVLinkChecker linkChecker;
private MVWorldManager worldManager;
private PermissionTools pt;

Expand All @@ -27,6 +29,7 @@ public MVNPPlayerListener(MultiverseNetherPortals plugin) {
this.nameChecker = new MVNameChecker(plugin);
this.worldManager = this.plugin.getCore().getMVWorldManager();
this.pt = new PermissionTools(this.plugin.getCore());
this.linkChecker = new MVLinkChecker(this.plugin);
}

@Override
Expand All @@ -35,17 +38,16 @@ public void onPlayerPortal(PlayerPortalEvent event) {
String currentWorld = currentLocation.getWorld().getName();
String type = "end";
if (event.getFrom().getBlock().getType() == Material.PORTAL) {
System.out.println("Normal!");
type = "nether";
}
String linkedWorld = this.plugin.getWorldLink(currentWorld, type);

if (linkedWorld != null) {
this.getNewTeleportLocation(event, currentLocation, linkedWorld);
this.linkChecker.getNewTeleportLocation(event, currentLocation, linkedWorld);
} else if (this.nameChecker.isValidNetherName(currentWorld)) {
this.getNewTeleportLocation(event, currentLocation, this.nameChecker.getNormalName(currentWorld));
this.linkChecker.getNewTeleportLocation(event, currentLocation, this.nameChecker.getNormalName(currentWorld));
} else {
this.getNewTeleportLocation(event, currentLocation, this.nameChecker.getNetherName(currentWorld));
this.linkChecker.getNewTeleportLocation(event, currentLocation, this.nameChecker.getNetherName(currentWorld));
}

if (event.getTo() == null || event.getFrom() == null) {
Expand All @@ -59,7 +61,7 @@ public void onPlayerPortal(PlayerPortalEvent event) {
this.plugin.log(Level.FINER, "Player '" + event.getPlayer().getName() + "' is portaling to the same world.");
return;
}
event.setCancelled(!pt.playerHasMoneyToEnter(fromWorld, toWorld, event.getPlayer(), event.getPlayer()));
event.setCancelled(!pt.playerHasMoneyToEnter(fromWorld, toWorld, event.getPlayer(), event.getPlayer(), true));
if (event.isCancelled()) {
this.plugin.log(Level.FINE, "Player '" + event.getPlayer().getName() + "' was DENIED ACCESS to '" + event.getTo().getWorld().getName() +
"' because they don't have the FUNDS required to enter.");
Expand All @@ -76,29 +78,4 @@ public void onPlayerPortal(PlayerPortalEvent event) {
}
}

private void getNewTeleportLocation(PlayerPortalEvent event, Location fromLocation, String worldstring) {
MultiverseWorld tpto = this.worldManager.getMVWorld(worldstring);
if (tpto != null && this.plugin.getCore().getMVPerms().canEnterWorld(event.getPlayer(), tpto) && this.worldManager.isMVWorld(fromLocation.getWorld().getName())) {
// Set the output location to the same XYZ coords but different world
double toScaling = this.worldManager.getMVWorld(tpto.getName()).getScaling();
double fromScaling = this.worldManager.getMVWorld(event.getFrom().getWorld().getName()).getScaling();

fromLocation = this.getScaledLocation(fromLocation, fromScaling, toScaling);
fromLocation.setWorld(tpto.getCBWorld());
event.setTo(fromLocation);
} else {
this.plugin.log(Level.WARNING, "Looks like " + worldstring + " does not exist. Whoops on your part!");
this.plugin.log(Level.WARNING, "You should check your Multiverse-NetherPortals configs!!");
// Set the event to redirect back to the same portal
// otherwise they sit in the jelly stuff forever!
event.setTo(fromLocation);
}
}

private Location getScaledLocation(Location fromLocation, double fromScaling, double toScaling) {
double scaling = fromScaling / toScaling;
fromLocation.setX(fromLocation.getX() * scaling);
fromLocation.setZ(fromLocation.getZ() * scaling);
return fromLocation;
}
}
@@ -0,0 +1,70 @@
package com.onarandombox.MultiverseNetherPortals.utils;

import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseNetherPortals.MultiverseNetherPortals;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerPortalEvent;

import java.util.logging.Level;

/**
* Multiverse 2
*
* @author fernferret
*/
public class MVLinkChecker {
private MultiverseNetherPortals plugin;
private MVWorldManager worldManager;

public MVLinkChecker(MultiverseNetherPortals plugin) {
this.plugin = plugin;
this.worldManager = this.plugin.getCore().getMVWorldManager();
}

public Location findNewTeleportLocation(Location fromLocation, String worldstring, Player p) {
MultiverseWorld tpto = this.worldManager.getMVWorld(worldstring);
if (tpto != null && this.plugin.getCore().getMVPerms().canEnterWorld(p, tpto) && this.worldManager.isMVWorld(fromLocation.getWorld().getName())) {
// Set the output location to the same XYZ coords but different world
double toScaling = this.worldManager.getMVWorld(tpto.getName()).getScaling();
double fromScaling = this.worldManager.getMVWorld(fromLocation.getWorld().getName()).getScaling();

fromLocation = this.getScaledLocation(fromLocation, fromScaling, toScaling);
fromLocation.setWorld(tpto.getCBWorld());
return fromLocation;
} else {
this.plugin.log(Level.WARNING, "Looks like " + worldstring + " does not exist. Whoops on your part!");
this.plugin.log(Level.WARNING, "You should check your Multiverse-NetherPortals configs!!");
// Set the event to redirect back to the same portal
// otherwise they sit in the jelly stuff forever!
return null;
}
}

public void getNewTeleportLocation(PlayerPortalEvent event, Location fromLocation, String worldstring) {
MultiverseWorld tpto = this.worldManager.getMVWorld(worldstring);
if (tpto != null && this.plugin.getCore().getMVPerms().canEnterWorld(event.getPlayer(), tpto) && this.worldManager.isMVWorld(fromLocation.getWorld().getName())) {
// Set the output location to the same XYZ coords but different world
double toScaling = this.worldManager.getMVWorld(tpto.getName()).getScaling();
double fromScaling = this.worldManager.getMVWorld(event.getFrom().getWorld().getName()).getScaling();

fromLocation = this.getScaledLocation(fromLocation, fromScaling, toScaling);
fromLocation.setWorld(tpto.getCBWorld());
event.setTo(fromLocation);
} else {
this.plugin.log(Level.WARNING, "Looks like " + worldstring + " does not exist. Whoops on your part!");
this.plugin.log(Level.WARNING, "You should check your Multiverse-NetherPortals configs!!");
// Set the event to redirect back to the same portal
// otherwise they sit in the jelly stuff forever!
event.setTo(fromLocation);
}
}

private Location getScaledLocation(Location fromLocation, double fromScaling, double toScaling) {
double scaling = fromScaling / toScaling;
fromLocation.setX(fromLocation.getX() * scaling);
fromLocation.setZ(fromLocation.getZ() * scaling);
return fromLocation;
}
}

0 comments on commit 5d53f15

Please sign in to comment.