Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd46f1c
commit 5d53f15
Showing
4 changed files
with
219 additions
and
30 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
138 changes: 138 additions & 0 deletions
138
src/main/java/com/onarandombox/MultiverseNetherPortals/listeners/MVNPEntityListener.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,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."); | ||
} | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
src/main/java/com/onarandombox/MultiverseNetherPortals/utils/MVLinkChecker.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,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; | ||
} | ||
} |