Created
January 8, 2015 05:44
-
-
Save Jikoo/a19edb7abba0d3e71887 to your computer and use it in GitHub Desktop.
Modified version of DirectPortals
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
name: Portals | |
main: com.github.jikoo.portals.Portals | |
version: 1.4 | |
author: xhawk87 | |
authors: [Jikoo] |
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
package com.github.jikoo.portals; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.TravelAgent; | |
import org.bukkit.World; | |
import org.bukkit.block.Block; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.entity.EntityPortalEvent; | |
import org.bukkit.event.player.PlayerPortalEvent; | |
import org.bukkit.plugin.Plugin; | |
public class PortalListener implements Listener { | |
private PortalTravelAgent travelAgent = new PortalTravelAgent(); | |
public void registerEvents(Plugin plugin) { | |
plugin.getServer().getPluginManager().registerEvents(this, plugin); | |
} | |
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) | |
public void onEntityPortal(EntityPortalEvent event) { | |
if (event.useTravelAgent()) { | |
event.setPortalTravelAgent(useDirectTravel(event.getPortalTravelAgent(), | |
event.getFrom(), event.getTo())); | |
} | |
} | |
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) | |
public void onPlayerPortal(PlayerPortalEvent event) { | |
if (event.useTravelAgent()) { | |
event.setPortalTravelAgent(useDirectTravel(event.getPortalTravelAgent(), | |
event.getFrom(), event.getTo())); | |
} | |
} | |
private TravelAgent useDirectTravel(TravelAgent defaultTravelAgent, Location from, Location to) { | |
Block portal = from.getBlock(); | |
if (portal.getType() != Material.PORTAL) { | |
int minX = (int) Math.round(from.getX() - portal.getX() - 1.0D); | |
int maxX = minX + 1; | |
int minZ = (int) Math.round(from.getZ() - portal.getZ() - 1.0D); | |
int maxZ = minZ + 1; | |
label141: for (int dx = minX; dx <= maxX; dx++) { | |
for (int dz = minZ; dz <= maxZ; dz++) { | |
Block near = portal.getRelative(dx, 0, dz); | |
if (near.getType() == Material.PORTAL) { | |
portal = near; | |
break label141; | |
} | |
} | |
} | |
if (portal.getType() != Material.PORTAL) { | |
return defaultTravelAgent; | |
} | |
} | |
Location centre = travelAgent.findPortalCentre(portal); | |
centre.setPitch(from.getPitch()); | |
centre.setYaw(from.getYaw()); | |
if ((centre.getWorld().getEnvironment() == World.Environment.NETHER) | |
&& (to.getWorld().getEnvironment() == World.Environment.NORMAL)) { | |
to.setX(centre.getX() * 8.0D); | |
to.setZ(centre.getZ() * 8.0D); | |
this.travelAgent.setSearchRadius(16); | |
} else if ((centre.getWorld().getEnvironment() == World.Environment.NORMAL) | |
&& (to.getWorld().getEnvironment() == World.Environment.NETHER)) { | |
to.setX(centre.getX() / 8.0D); | |
to.setZ(centre.getZ() / 8.0D); | |
this.travelAgent.setSearchRadius(4); | |
} else { | |
return defaultTravelAgent; | |
} | |
to.setY(centre.getY()); | |
return this.travelAgent; | |
} | |
} |
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
package com.github.jikoo.portals; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class Portals extends JavaPlugin { | |
public void onEnable() { | |
new PortalListener().registerEvents(this); | |
} | |
} |
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
package com.github.jikoo.portals; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.TravelAgent; | |
import org.bukkit.block.Block; | |
public class PortalTravelAgent implements TravelAgent { | |
private static final Material[][][] PORTAL_TEMPLATE = new Material[4][3][5]; | |
static { | |
PORTAL_TEMPLATE[0][1][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[1][1][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[2][1][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[3][1][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[0][1][1] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[0][1][2] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[0][1][3] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[3][1][1] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[3][1][2] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[3][1][3] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[0][1][4] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[1][1][4] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[2][1][4] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[3][1][4] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[1][1][1] = Material.PORTAL; | |
PORTAL_TEMPLATE[1][1][2] = Material.PORTAL; | |
PORTAL_TEMPLATE[1][1][3] = Material.PORTAL; | |
PORTAL_TEMPLATE[2][1][1] = Material.PORTAL; | |
PORTAL_TEMPLATE[2][1][2] = Material.PORTAL; | |
PORTAL_TEMPLATE[2][1][3] = Material.PORTAL; | |
PORTAL_TEMPLATE[0][0][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[1][0][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[2][0][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[3][0][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[0][2][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[1][2][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[2][2][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[3][2][0] = Material.OBSIDIAN; | |
PORTAL_TEMPLATE[0][0][4] = Material.COBBLESTONE; | |
PORTAL_TEMPLATE[1][0][4] = Material.COBBLESTONE; | |
PORTAL_TEMPLATE[2][0][4] = Material.COBBLESTONE; | |
PORTAL_TEMPLATE[3][0][4] = Material.COBBLESTONE; | |
PORTAL_TEMPLATE[0][2][4] = Material.COBBLESTONE; | |
PORTAL_TEMPLATE[1][2][4] = Material.COBBLESTONE; | |
PORTAL_TEMPLATE[2][2][4] = Material.COBBLESTONE; | |
PORTAL_TEMPLATE[3][2][4] = Material.COBBLESTONE; | |
} | |
private int searchRadius = 0; | |
public TravelAgent setSearchRadius(int radius) { | |
this.searchRadius = radius; | |
return this; | |
} | |
public int getSearchRadius() { | |
return this.searchRadius; | |
} | |
public TravelAgent setCreationRadius(int radius) { | |
return this; | |
} | |
public int getCreationRadius() { | |
return 0; | |
} | |
public boolean getCanCreatePortal() { | |
return true; | |
} | |
public Location findOrCreate(Location location) { | |
Location destination = findPortal(location); | |
if (destination == null) { | |
destination = location.clone(); | |
createPortal(destination); | |
} | |
return destination; | |
} | |
public Location findPortal(Location location) { | |
Block block = location.getBlock(); | |
for (int dx = -this.searchRadius; dx <= this.searchRadius; dx++) { | |
for (int dz = -this.searchRadius; dz <= this.searchRadius; dz++) { | |
Block portal = block.getRelative(dx, 0, dz); | |
if (portal.getType() == Material.PORTAL) { | |
Location centre = findPortalCentre(portal); | |
centre.setYaw(location.getYaw()); | |
centre.setYaw(location.getPitch()); | |
return centre; | |
} | |
} | |
} | |
return null; | |
} | |
public boolean createPortal(Location location) { | |
float yaw = location.getYaw(); | |
int xFacing = 0; | |
int zFacing = 0; | |
if (((yaw > -45.0F) && (yaw <= 45.0F)) || ((yaw > 135.0F) && (yaw <= 225.0F))) { | |
xFacing = 1; | |
} else { | |
zFacing = 1; | |
} | |
for (int length = 0; length < PORTAL_TEMPLATE.length; length++) { | |
for (int depth = 0; depth < PORTAL_TEMPLATE[0].length; depth++) { | |
for (int height = 0; height < PORTAL_TEMPLATE[0][0].length; height++) { | |
Material material = PORTAL_TEMPLATE[length][depth][height]; | |
if (material == null) { | |
material = Material.AIR; | |
} | |
int xOffset = xFacing * (length - 1) + zFacing * (depth - 1); | |
int yOffset = height - 1; | |
int zOffset = zFacing * (length - 1) + xFacing * (depth - 1); | |
location.getBlock().getRelative(xOffset, yOffset, zOffset) | |
.setTypeId(material.getId(), false); | |
} | |
} | |
} | |
return true; | |
} | |
public Location findPortalCentre(Block portal) { | |
if (portal.getType() != Material.PORTAL) { | |
throw new IllegalArgumentException("There is no portal at " + portal.toString()); | |
} | |
double minX = 0.0D; | |
double maxX = 0.0D; | |
double minY = 0.0D; | |
double maxY = 0.0D; | |
double minZ = 0.0D; | |
double maxZ = 0.0D; | |
while (portal.getRelative((int) minX - 1, 0, 0).getType() == Material.PORTAL) { | |
minX -= 1.0D; | |
} | |
if (portal.getRelative((int) minX - 1, 0, 0).getType() == Material.OBSIDIAN) { | |
while (portal.getRelative((int) maxX + 1, 0, 0).getType() == Material.PORTAL) { | |
maxX += 1.0D; | |
} | |
if (portal.getRelative((int) minX - 1, 0, 0).getType() != Material.OBSIDIAN) { | |
minX = 0.0D; | |
maxX = 0.0D; | |
} | |
} else { | |
minX = 0.0D; | |
maxX = 0.0D; | |
} | |
while (portal.getRelative(0, (int) minY - 1, 0).getType() == Material.PORTAL) { | |
minY -= 1.0D; | |
} | |
if (portal.getRelative(0, (int) minY - 1, 0).getType() == Material.OBSIDIAN) { | |
while (portal.getRelative(0, (int) maxY + 1, 0).getType() == Material.PORTAL) { | |
maxY += 1.0D; | |
} | |
if (portal.getRelative(0, (int) minY - 1, 0).getType() != Material.OBSIDIAN) { | |
minY = 0.0D; | |
maxY = 0.0D; | |
} | |
} else { | |
minY = 0.0D; | |
maxY = 0.0D; | |
} | |
while (portal.getRelative(0, 0, (int) minZ - 1).getType() == Material.PORTAL) { | |
minZ -= 1.0D; | |
} | |
if (portal.getRelative(0, 0, (int) minZ - 1).getType() == Material.OBSIDIAN) { | |
while (portal.getRelative(0, 0, (int) maxZ + 1).getType() == Material.PORTAL) { | |
maxZ += 1.0D; | |
} | |
if (portal.getRelative(0, 0, (int) minZ - 1).getType() != Material.OBSIDIAN) { | |
minZ = 0.0D; | |
maxZ = 0.0D; | |
} | |
} else { | |
minZ = 0.0D; | |
maxZ = 0.0D; | |
} | |
double x = portal.getX() + (maxX + 1.0D + minX) / 2.0D; | |
double y = portal.getY() + minY; | |
double z = portal.getZ() + (maxZ + 1.0D + minZ) / 2.0D; | |
return new Location(portal.getWorld(), x, y, z); | |
} | |
public void setCanCreatePortal(boolean create) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment