diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/BetterShardsAPI.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/BetterShardsAPI.java index 93d155a..21657b9 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/BetterShardsAPI.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/BetterShardsAPI.java @@ -138,4 +138,23 @@ public static void randomSpawnPlayer(String server, UUID player) { public static ConfigurationSection getConfigurationSection(JavaPlugin plugin, UUID uuid) { return CustomWorldNBTStorage.getWorldNBTStorage().getConfigurationSection(uuid, plugin); } + + /** + * Registers a portal with BetterShards. + * This should be called for every custom portal you have. + * @param plugin_id The plugin specific id that your portal uses. + * @param plugin The plugin that is registering this portal. + * @param portal The portal Class. + * @param name The name of the portalType that will show up to players. + */ + public static void registerPortal(int plugin_id, String plugin, Class portal, String name) { + BetterShardsPlugin.getDatabaseManager().addPortalType(plugin_id, plugin); + // Now we get the generated id from bettershards. + int real_id = BetterShardsPlugin.getDatabaseManager().getPortalID(plugin, plugin_id); + BetterShardsPlugin.getPortalManager().getPortalFactory().registerPortal(real_id, portal, name); + } + + public static int getPortalID(int pluginId, String plugin) { + return BetterShardsPlugin.getDatabaseManager().getPortalID(plugin, pluginId); + } } diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/BetterShardsPlugin.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/BetterShardsPlugin.java index e4ebd08..4d480b9 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/BetterShardsPlugin.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/BetterShardsPlugin.java @@ -40,6 +40,15 @@ public void onEnable(){ db = new DatabaseManager(); pm = new PortalsManager(); pm.loadPortalsManager(); + Bukkit.getScheduler().runTask(this, new Runnable() { + + @Override + public void run() { + // Delay load so that other plugins can register with this plugin. + pm.loadPortalsFromServer(); + } + + }); CustomWorldNBTStorage.setWorldNBTStorage(); combatManager = new CombatTagManager(getServer()); randomSpawn = new RandomSpawnManager(); diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/command/commands/CreatePortal.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/command/commands/CreatePortal.java index 698bad4..5cc9ba4 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/command/commands/CreatePortal.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/command/commands/CreatePortal.java @@ -16,6 +16,7 @@ import vg.civcraft.mc.bettershards.portal.portals.CuboidPortal; import vg.civcraft.mc.bettershards.portal.portals.WorldBorderPortal; import vg.civcraft.mc.civmodcore.command.PlayerCommand; +import vg.civcraft.mc.mercury.MercuryAPI; public class CreatePortal extends PlayerCommand { @@ -45,19 +46,29 @@ else if (g.getMissingSelection() == GridLocation.RIGHTSELECTION) else if (g.getMissingSelection() == GridLocation.LEFTSELECTION) return sendPlayerMessage(p, ChatColor.RED + "Your primary selection has not been chosen.", true); Portal portal = null; - if (args.length == 1 || args[1].equalsIgnoreCase("cuboid")) { - portal = new CuboidPortal(args[0], g.getLeftClickLocation(), g.getRightClickLocation(), null, true); - } - else if (args [1].equalsIgnoreCase("worldborder") || args [1].equalsIgnoreCase("wb")) { - LocationWrapper firstLoc = new LocationWrapper(g.getLeftClickLocation()); - LocationWrapper secondLoc = new LocationWrapper(g.getRightClickLocation()); - portal = new WorldBorderPortal(args[0],null, true, firstLoc, secondLoc); - } - else if (args [1].equalsIgnoreCase("circle") || args[1].equalsIgnoreCase("circular")) { - portal = new CircularPortal(args [0], null, true, g.getLeftClickLocation(), g.getRightClickLocation()); + if (args.length < 1) { + String[] names = BetterShardsPlugin.getPortalManager().getPortalFactory().getAllPortalNames(); + StringBuilder portals = new StringBuilder(); + for (String x : names) { + portals.append(x); + portals.append(" "); + } + return sendPlayerMessage(p, ChatColor.RED + "You must specify a portaltype, portal types are: " + portals.toString(), true); } + if (pm.getPortal(args[0]) != null) return sendPlayerMessage(p, ChatColor.RED + "That portal name already exists.", true); + + + Class clazz = BetterShardsPlugin.getPortalManager().getPortalFactory().getPortal(args[1]); + portal = BetterShardsPlugin.getPortalManager().getPortalFactory().buildPortal(clazz); + portal.setName(args[0]); + portal.setIsOnCurrentServer(true); + portal.setServerName(MercuryAPI.serverName()); + portal.setFirstLocation(new LocationWrapper(g.getLeftClickLocation())); + portal.setSecondLocation(new LocationWrapper(g.getRightClickLocation())); + portal.valuesPopulated(); + pm.createPortal(portal); String m = ChatColor.GREEN + "You have successfully created the portal " + args[0] + ".\n" + "To add a connection use the command /bsj
"; diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/command/commands/JoinPortal.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/command/commands/JoinPortal.java index f76746c..32ed3df 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/command/commands/JoinPortal.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/command/commands/JoinPortal.java @@ -40,7 +40,7 @@ public boolean execute(CommandSender sender, String[] args) { if (!one.getClass().equals(two.getClass())) { return sendPlayerMessage(p, ChatColor.RED + "You can not join portals of a different type", true); } - one.setPartnerPortal(two); + one.setPartnerPortal(two.getName()); String m = "%s has been set as Portal %s partner."; sender.sendMessage(ChatColor.GREEN + String.format(m, two.getName(), one.getName())); return true; diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/database/DatabaseManager.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/database/DatabaseManager.java index b7b5f45..6b47818 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/database/DatabaseManager.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/database/DatabaseManager.java @@ -40,6 +40,7 @@ import vg.civcraft.mc.bettershards.misc.LocationWrapper; import vg.civcraft.mc.bettershards.misc.TeleportInfo; import vg.civcraft.mc.bettershards.portal.Portal; +import vg.civcraft.mc.bettershards.portal.PortalFactory; import vg.civcraft.mc.bettershards.portal.portals.CircularPortal; import vg.civcraft.mc.bettershards.portal.portals.CuboidPortal; import vg.civcraft.mc.bettershards.portal.portals.WorldBorderPortal; @@ -101,6 +102,11 @@ public class DatabaseManager { private static final String getAllBedLocation = "select * from player_beds;"; private static final String removeBedLocation = "delete from player_beds where uuid = ?;"; + private static final String addPortalType = "insert ignore into portalVersion (" + + "plugin_name, portal_plugin_id) values (?, ?);"; + private static final String getPortalType = "select portal_id from portalVersion " + + "where plugin_name = ? and portal_plugin_id = ?;"; + private String cleanupLocks; private BukkitTask lockCleanup; @@ -221,6 +227,11 @@ public Boolean call() { + "inv_id INT NOT NULL," + "last_upd TIMESTAMP NOT NULL DEFAULT NOW()," + "PRIMARY KEY (uuid, inv_id));"); + this.db.registerMigration(3, false, "CREATE TABLE IF NOT EXISTS portalVersion(" + + "portal_id INT NOT NULL AUTO_INCREMENT," + + "plugin_name VARCHAR(36) NOT NULL," + + "portal_plugin_id INT NOT NULL," + + "PRIMARY KEY (plugin_name, portal_plugin_id));"); } @CivConfigs({ @@ -419,7 +430,7 @@ public void addPortalData(Portal portal, Portal connection){ PreparedStatement addPortalData = connect.prepareStatement(DatabaseManager.addPortalData);) { addPortalData.setString(1, portal.getName()); addPortalData.setString(2, serverName); - addPortalData.setInt(3, portal.specialId); + addPortalData.setInt(3, portal.getPortalID()); String name = null; if (connection != null) name = connection.getName(); @@ -785,22 +796,20 @@ private Portal getPortalData(String name, LocationWrapper first, LocationWrapper String serverName = set.getString("server_name"); String partner = set.getString("partner_id"); boolean currentServer = serverName.equals(MercuryAPI.serverName()); - switch (specialId) { - case 0: - CuboidPortal p = new CuboidPortal(name, first.getFakeLocation(), second.getFakeLocation(), partner, currentServer); - p.setServerName(serverName); - return p; - case 1: - WorldBorderPortal wb = new WorldBorderPortal(name, partner, currentServer, first, second); - wb.setServerName(serverName); - return wb; - case 2: - CircularPortal cp = new CircularPortal(name, partner, currentServer, first.getFakeLocation(), second.getFakeLocation()); - cp.setServerName(serverName); - return cp; - default: - return null; - } + + PortalFactory factory = BetterShardsPlugin.getPortalManager().getPortalFactory(); + Class clazz = factory.getPortal(specialId); + + Portal p = factory.buildPortal(clazz); + p.setName(name); + p.setIsOnCurrentServer(true); + p.setServerName(MercuryAPI.serverName()); + p.setFirstLocation(first); + p.setSecondLocation(second); + p.setPartnerPortal(partner); + p.valuesPopulated(); + + return p; } catch (SQLException e) { logger.log(Level.SEVERE, "Failed to getPortalData for {0}", name); logger.log(Level.SEVERE, "Failed to getPortalData, exception:", e); @@ -1019,6 +1028,64 @@ public void removeBed(UUID uuid) { logger.log(Level.SEVERE, "Failed to removeBed, exception:", e); } } + + /** + * This method is used to register with BetterShards what portal_id should + * be associated with each portal so when saving occurs each portal specific id + * is respected. + * This method should be called for each portal type that exists. + * So what should happen is in each plugin you give your custom portal it's own + * specific id that is internally recognized and tracked. From there you will pass + * that id and your plugin name to this method and it will create an id that can be + * used to pass in your constructor for your portal objects. + * After calling this method refer to the + * {@link #getPortalID(String, int)} method for getting what id to pass to + * your custom portal constructors. + * This method can be called at every start and if an id is already present it will + * fail silently. + * @param id The id that will be used internally in your plugin to reference each + * portal type. + * @param plugin Your plugin's name. + */ + public void addPortalType(int id, String plugin) { + try (Connection connection = db.getConnection(); + PreparedStatement addPortalLoc = connection.prepareStatement(DatabaseManager.addPortalType)){ + addPortalLoc.setString(1, plugin); + addPortalLoc.setInt(2, id); + addPortalLoc.execute(); + } + catch (SQLException e) { + logger.log(Level.SEVERE, "Add PortalType DB failure: ", e); + } + } + + /** + * This method is used to get an id that will be used in each portal's constructor. + * This id is important because it is used to map what kind of portal is being saved + * in the database. + * To use this method you must first call {@link #addPortalType(int, String)} in + * order to generate an id that can be used. + * @param plugin The plugin that is generating an id. + * @param id The plugin specific id that is used to reference what id + * should be returned based on what kind of portal it is. + * @return Should return an id that should be passed in your portal subclass constructor. + */ + public int getPortalID(String plugin, int id) { + int type_id = -1; + try (Connection connection = db.getConnection(); + PreparedStatement addPortalLoc = connection.prepareStatement(DatabaseManager.getPortalType)){ + addPortalLoc.setString(1, plugin); + addPortalLoc.setInt(2, id); + ResultSet set = addPortalLoc.executeQuery(); + if (!set.next()) + return -1; + type_id = set.getInt(1); + } + catch (SQLException e) { + logger.log(Level.SEVERE, "Get PortalType DB failure: ", e); + } + return type_id; + } private void shortTrace() { StackTraceElement[] ste = Thread.currentThread().getStackTrace(); diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/listeners/BetterShardsListener.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/listeners/BetterShardsListener.java index 23e348a..5bc6c11 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/listeners/BetterShardsListener.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/listeners/BetterShardsListener.java @@ -56,6 +56,9 @@ import vg.civcraft.mc.bettershards.misc.PlayerStillDeadException; import vg.civcraft.mc.bettershards.misc.TeleportInfo; import vg.civcraft.mc.bettershards.portal.Portal; +import vg.civcraft.mc.bettershards.portal.portals.CircularPortal; +import vg.civcraft.mc.bettershards.portal.portals.CuboidPortal; +import vg.civcraft.mc.bettershards.portal.portals.WorldBorderPortal; import vg.civcraft.mc.civmodcore.Config; import vg.civcraft.mc.civmodcore.annotations.CivConfig; import vg.civcraft.mc.civmodcore.annotations.CivConfigType; diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/listeners/MercuryListener.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/listeners/MercuryListener.java index c69c262..51573f1 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/listeners/MercuryListener.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/listeners/MercuryListener.java @@ -148,7 +148,7 @@ else if (content[0].equals("portal")) { Portal p2 = portalManager.getPortal(content[3]); if (p1 == null || p2 == null) return; - p1.setPartnerPortal(p2); + p1.setPartnerPortal(p2.getName()); } else if (content[1].equals("remove")) { Portal p1 = portalManager.getPortal(content[2]); diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/manager/PortalsManager.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/manager/PortalsManager.java index 884ec95..650da34 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/manager/PortalsManager.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/manager/PortalsManager.java @@ -14,10 +14,15 @@ import org.bukkit.World; import org.bukkit.entity.Player; +import vg.civcraft.mc.bettershards.BetterShardsAPI; import vg.civcraft.mc.bettershards.BetterShardsPlugin; import vg.civcraft.mc.bettershards.database.DatabaseManager; import vg.civcraft.mc.bettershards.external.MercuryManager; import vg.civcraft.mc.bettershards.portal.Portal; +import vg.civcraft.mc.bettershards.portal.PortalFactory; +import vg.civcraft.mc.bettershards.portal.portals.CircularPortal; +import vg.civcraft.mc.bettershards.portal.portals.CuboidPortal; +import vg.civcraft.mc.bettershards.portal.portals.WorldBorderPortal; public class PortalsManager { @@ -25,15 +30,17 @@ public class PortalsManager { private Map portals; private Map arrivedPlayers = new ConcurrentHashMap(); private final long portalCoolDown = 10000L; //10 seconds + private PortalFactory factory; public PortalsManager() { super(); portals = new HashMap(); + factory = new PortalFactory(); registerParticleRunnable(); } public void loadPortalsManager() { - loadPortalsFromServer(); + generatePortalIds(); removeTeleportedPlayers(); autoSaveTimer(); } @@ -175,4 +182,17 @@ public void run() { } }, 4L, 4L); } + + /** + * This method is used in order to generate ids for our portals. + */ + private void generatePortalIds() { + BetterShardsAPI.registerPortal(0, BetterShardsPlugin.getInstance().getName(), CuboidPortal.class, "Cuboid"); + BetterShardsAPI.registerPortal(1, BetterShardsPlugin.getInstance().getName(), WorldBorderPortal.class, "WorldBorder"); + BetterShardsAPI.registerPortal(2, BetterShardsPlugin.getInstance().getName(), CircularPortal.class, "Circle"); + } + + public PortalFactory getPortalFactory() { + return factory; + } } diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/Portal.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/Portal.java index 3c3a9ac..8d2d5d2 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/Portal.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/Portal.java @@ -1,76 +1,82 @@ package vg.civcraft.mc.bettershards.portal; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import vg.civcraft.mc.bettershards.BetterShardsPlugin; -import vg.civcraft.mc.bettershards.database.DatabaseManager; +import vg.civcraft.mc.bettershards.misc.LocationWrapper; public abstract class Portal { - protected Portal connection; + protected LocationWrapper first, second; + protected String connection; protected String serverName; protected String name; - private boolean isOnCurrentServer; // Set to false if not on current server - protected DatabaseManager db; + protected boolean isOnCurrentServer; // Set to false if not on current server private boolean isDirty = false; - public final int specialId; protected static final int PARTICLE_RANGE = 4; protected static final int PARTICLE_SIGHT_RANGE = 16; - public Portal(String name, final String con, boolean isOnCurrentServer, int specialId) { - Bukkit.getScheduler().scheduleSyncDelayedTask(BetterShardsPlugin.getInstance(), new Runnable() { - - @Override - public void run() { - connection = BetterShardsPlugin.getPortalManager().getPortal(con); - } - - }); - this.name = name; - this.isOnCurrentServer = isOnCurrentServer; - db = BetterShardsPlugin.getDatabaseManager(); - this.specialId = specialId; + public Portal() { + } public Portal getPartnerPortal() { - return connection; + return BetterShardsPlugin.getPortalManager().getPortal(connection); } public String getName() { return name; } - public void setName(String name) { + public Portal setName(String name) { this.name = name; setDirty(true); + return this; } - public void setPartnerPortal(Portal connection) { + public Portal setPartnerPortal(String connection) { this.connection = connection; setDirty(true); + return this; } public String getServerName() { return serverName; } - public void setServerName(String serverName) { + public Portal setServerName(String serverName) { this.serverName = serverName; setDirty(true); + return this; } public boolean isOnCurrentServer() { return isOnCurrentServer; } + + public Portal setIsOnCurrentServer(boolean value) { + isOnCurrentServer = value; + return this; + } public boolean isDirty() { return isDirty; } - public void setDirty(boolean dirty) { + public Portal setDirty(boolean dirty) { isDirty = dirty; + return this; + } + + public Portal setFirstLocation(LocationWrapper loc) { + first = loc; + return this; + } + + public Portal setSecondLocation(LocationWrapper loc) { + second = loc; + return this; } /** @@ -86,5 +92,18 @@ public void setDirty(boolean dirty) { public abstract void teleport(Player p); public abstract void showParticles(Player p); + + public abstract String getTypeName(); + + /** + * This method should be called after a portal is populated with its necessary values. + */ + public abstract void valuesPopulated(); + + /** + * Get the id of the portal. + * @return + */ + public abstract int getPortalID(); } \ No newline at end of file diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/PortalFactory.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/PortalFactory.java new file mode 100644 index 0000000..ff927de --- /dev/null +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/PortalFactory.java @@ -0,0 +1,41 @@ +package vg.civcraft.mc.bettershards.portal; + +import java.util.HashMap; +import java.util.Map; + +public class PortalFactory { + + private Map> portals = new HashMap>(); + private Map portalsNames = new HashMap(); + + public E buildPortal(Class clazz) { + try { + E p = (E) clazz.newInstance(); + return p; + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + public void registerPortal(int real_id, Class clazz, String name) { + portals.put(real_id, clazz); + portalsNames.put(name, real_id); + } + + public String[] getAllPortalNames() { + return (String[]) portalsNames.keySet().toArray(); + } + + public Class getPortal(int id) { + return portals.get(id); + } + + public Class getPortal(String name) { + return portals.get(portalsNames.get(name)); + } +} diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/CircularPortal.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/CircularPortal.java index f3864d0..e39c27a 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/CircularPortal.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/CircularPortal.java @@ -1,45 +1,40 @@ package vg.civcraft.mc.bettershards.portal.portals; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Location; import org.bukkit.entity.Player; import vg.civcraft.mc.bettershards.BetterShardsAPI; +import vg.civcraft.mc.bettershards.BetterShardsPlugin; import vg.civcraft.mc.bettershards.events.PlayerChangeServerReason; import vg.civcraft.mc.bettershards.misc.PlayerStillDeadException; import vg.civcraft.mc.bettershards.portal.Portal; public class CircularPortal extends Portal { - private Location first, second, center; + private Location center; private double range; + + private Location actualFirst, actualSecond; + + private static int id = -1; - public CircularPortal(String name, final String con, - boolean isOnCurrentServer, Location first, - Location second) { - super(name, con, isOnCurrentServer, 2); - this.first = first; - this.second = second; - if (isOnCurrentServer) { - center = new Location(first.getWorld(), - (first.getX() + second.getX()) / 2, - (first.getY() + second.getY()) / 2, - (first.getZ() + second.getZ()) / 2); - range = getXZDistance(first); - } + protected CircularPortal() { + } public boolean inPortal(Location loc) { - double y1 = first.getY(); - double y2 = second.getY(); + double y1 = actualFirst.getY(); + double y2 = actualSecond.getY(); return getXZDistance(loc) < range && ((loc.getY() >= y1 && loc.getY() <= y2) || (loc.getY() <= y1 && loc.getY() >= y2)); } public Location getFirst() { - return first; + return actualFirst; } public Location getSecond() { - return second; + return actualSecond; } private double getXZDistance(Location loc) { @@ -51,7 +46,7 @@ private double getXZDistance(Location loc) { public Location findSpawnLocation() { double xScale = Math.random(); double zScale = Math.random(); - Location loc = new Location(first.getWorld(), xScale * range + center.getX(), center.getY(), zScale * range + center.getZ()); + Location loc = new Location(Bukkit.getWorld(first.getActualWorld()), xScale * range + center.getX(), center.getY(), zScale * range + center.getZ()); if (!inPortal(loc)) { //could be in the edges outside the circle return findSpawnLocation(); @@ -70,7 +65,8 @@ public void teleport(Player p) { Double xScale = (loc.getX() - center.getX()) / range; Double zScale = (loc.getZ() - center.getZ()) / range; try { - BetterShardsAPI.connectPlayer(p, connection, + BetterShardsAPI.connectPlayer(p, + BetterShardsPlugin.getPortalManager().getPortal(connection), PlayerChangeServerReason.PORTAL, xScale, zScale); } catch (PlayerStillDeadException e) { e.printStackTrace(); @@ -83,8 +79,8 @@ public void showParticles(Player p) { //- 16 so players see particles even if they are slightly out of range if (getXZDistance(loc) - PARTICLE_SIGHT_RANGE < range) { //ensure player is in y range - int upperBound = Math.max(first.getBlockY(), second.getBlockY()); - int lowerBound = Math.min(first.getBlockY(), second.getBlockY()); + int upperBound = Math.max(first.getFakeLocation().getBlockY(), second.getFakeLocation().getBlockY()); + int lowerBound = Math.min(first.getFakeLocation().getBlockY(), second.getFakeLocation().getBlockY()); if (upperBound + PARTICLE_SIGHT_RANGE >= loc.getBlockY() && lowerBound - PARTICLE_SIGHT_RANGE <= loc.getBlockY()) { int y; if (loc.getY() >= upperBound) { @@ -110,4 +106,32 @@ public void showParticles(Player p) { } } } + + @Override + public String getTypeName() { + return "Circle"; + } + + @Override + public void valuesPopulated() { + if (isOnCurrentServer) { + center = new Location(Bukkit.getWorld(first.getActualWorld()), + (first.getFakeLocation().getX() + second.getFakeLocation().getX()) / 2, + (first.getFakeLocation().getY() + second.getFakeLocation().getY()) / 2, + (first.getFakeLocation().getZ() + second.getFakeLocation().getZ()) / 2); + range = getXZDistance(first.getFakeLocation()); + actualFirst = new Location(Bukkit.getWorld(first.getActualWorld()), first.getFakeLocation().getBlockX(), + first.getFakeLocation().getBlockY(), first.getFakeLocation().getBlockZ()); + actualSecond = new Location(Bukkit.getWorld(second.getActualWorld()), second.getFakeLocation().getBlockX(), + second.getFakeLocation().getBlockY(), second.getFakeLocation().getBlockZ()); + } + } + + @Override + public int getPortalID() { + if (id == -1) { + id = BetterShardsPlugin.getDatabaseManager().getPortalID(BetterShardsPlugin.getInstance().getName(), 2); + } + return id; + } } diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/CuboidPortal.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/CuboidPortal.java index 2ad080b..e4b6a73 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/CuboidPortal.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/CuboidPortal.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import vg.civcraft.mc.bettershards.BetterShardsAPI; +import vg.civcraft.mc.bettershards.BetterShardsPlugin; import vg.civcraft.mc.bettershards.events.PlayerChangeServerReason; import vg.civcraft.mc.bettershards.misc.PlayerStillDeadException; import vg.civcraft.mc.bettershards.portal.Portal; @@ -14,11 +15,11 @@ public class CuboidPortal extends Portal { protected Location first; // This should be the location of the first block protected Location second; + + private static int id = -1; - public CuboidPortal(String name, Location first, Location second, String connection, boolean isOnCurrentServer) { - super(name, connection, isOnCurrentServer, 0); - this.first = first; - this.second = second; + public CuboidPortal() { + } public Location getFirst() { @@ -78,12 +79,13 @@ private boolean validSpawn(World world, double x, double y, double z) { public void teleport(Player p) { if (connection == null) return; - if (connection.getServerName().equals(BetterShardsAPI.getServerName())) { - p.teleport(connection.findSpawnLocation()); + Portal portal = BetterShardsPlugin.getPortalManager().getPortal(connection); + if (portal.getServerName().equals(BetterShardsAPI.getServerName())) { + p.teleport(portal.findSpawnLocation()); return; } try { - BetterShardsAPI.connectPlayer(p, connection, + BetterShardsAPI.connectPlayer(p, portal, PlayerChangeServerReason.PORTAL); } catch (PlayerStillDeadException e) { e.printStackTrace(); @@ -94,4 +96,22 @@ public void teleport(Player p) { public void showParticles(Player p) { //TODO } + + @Override + public String getTypeName() { + return "Cuboid"; + } + + @Override + public void valuesPopulated() { + // Not needed. + } + + @Override + public int getPortalID() { + if (id == -1) { + id = BetterShardsPlugin.getDatabaseManager().getPortalID(BetterShardsPlugin.getInstance().getName(), 0); + } + return id; + } } diff --git a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/WorldBorderPortal.java b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/WorldBorderPortal.java index 3a8e23e..8faab9a 100644 --- a/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/WorldBorderPortal.java +++ b/BetterShardsBukkit/src/main/java/vg/civcraft/mc/bettershards/portal/portals/WorldBorderPortal.java @@ -29,49 +29,16 @@ public class WorldBorderPortal extends Portal { private double arcLength; private double particleIncrement; private static List ignoreMaterials; + + private static int id = -1; /** * So without complication everything needlessly, note that all border begin/ends * should be listed in clockwise order. If you fail to adhere to this, the border * will instead be everything you meant to be outside the border. */ - public WorldBorderPortal(String name, String connection, - boolean isOnCurrentServer, - LocationWrapper first, LocationWrapper second) { - super(name, connection, isOnCurrentServer, 1); - this.mapCenter = new Location(first.getFakeLocation().getWorld(), 0, 0, 0); - this.first = first; - this.second = second; - - double fRadius = getXZDistance(first.getFakeLocation()); - double sRadius = getXZDistance(second.getFakeLocation()); - this.wbRange = Math.min(fRadius, sRadius); - - this.fAngle = getAdjustedAngle(first.getFakeLocation()); - this.sAngle = getAdjustedAngle(second.getFakeLocation()); - - this.arcLength = (fAngle == sAngle) ? 2 * Math.PI : - (fAngle > sAngle) ? 2 * Math.PI - fAngle + sAngle : - sAngle - fAngle; - //(circumference in blocks) * (percentage of circumference the portal takes up) - //= (wbRange * 2 * PI) * (arcLength / (PI * 2)) - //= wbRange * arcLength - double blocksInPortal = arcLength * wbRange; - this.particleIncrement = 1.0 / blocksInPortal; - List ignoreMats = BetterShardsPlugin.getInstance().GetConfig().get("randomspawn.ignoreMaterials").getStringList(); - if (ignoreMaterials == null) { - ignoreMaterials = new ArrayList(); - for(String ign : ignoreMats) { - try { - Material m = Material.valueOf(ign); - BetterShardsPlugin.getInstance().info("Ignoring " + m.toString() + " for portal spawning"); - ignoreMaterials.add(m); - } - catch (IllegalArgumentException e) { - BetterShardsPlugin.getInstance().warning("The portal spawn ignore material specified as " + ign + " is not valid. It was ignored"); - } - } - } + public WorldBorderPortal() { + } public LocationWrapper getFirst() { @@ -182,13 +149,14 @@ public Location calculateSpawnLocation(double arcPosition) { public void teleport(Player p) { if (connection == null) return; + Portal portal = BetterShardsPlugin.getPortalManager().getPortal(connection); Double relativeArcPosition = getArcPosition(p.getLocation()); - if (connection.getServerName().equals(BetterShardsAPI.getServerName())) { - p.teleport(((WorldBorderPortal)connection).calculateSpawnLocation(relativeArcPosition)); + if (portal.getServerName().equals(BetterShardsAPI.getServerName())) { + p.teleport(((WorldBorderPortal)portal).calculateSpawnLocation(relativeArcPosition)); return; } try { - BetterShardsAPI.connectPlayer(p, connection, + BetterShardsAPI.connectPlayer(p, portal, PlayerChangeServerReason.PORTAL, relativeArcPosition); } catch (PlayerStillDeadException e) { // TODO Auto-generated catch block @@ -221,4 +189,52 @@ public void showParticles(Player p) { } } + + @Override + public String getTypeName() { + return "WorldBorder"; + } + + @Override + public void valuesPopulated() { + this.mapCenter = new Location(first.getFakeLocation().getWorld(), 0, 0, 0); + + double fRadius = getXZDistance(first.getFakeLocation()); + double sRadius = getXZDistance(second.getFakeLocation()); + this.wbRange = Math.min(fRadius, sRadius); + + this.fAngle = getAdjustedAngle(first.getFakeLocation()); + this.sAngle = getAdjustedAngle(second.getFakeLocation()); + + this.arcLength = (fAngle == sAngle) ? 2 * Math.PI : + (fAngle > sAngle) ? 2 * Math.PI - fAngle + sAngle : + sAngle - fAngle; + //(circumference in blocks) * (percentage of circumference the portal takes up) + //= (wbRange * 2 * PI) * (arcLength / (PI * 2)) + //= wbRange * arcLength + double blocksInPortal = arcLength * wbRange; + this.particleIncrement = 1.0 / blocksInPortal; + List ignoreMats = BetterShardsPlugin.getInstance().GetConfig().get("randomspawn.ignoreMaterials").getStringList(); + if (ignoreMaterials == null) { + ignoreMaterials = new ArrayList(); + for(String ign : ignoreMats) { + try { + Material m = Material.valueOf(ign); + BetterShardsPlugin.getInstance().info("Ignoring " + m.toString() + " for portal spawning"); + ignoreMaterials.add(m); + } + catch (IllegalArgumentException e) { + BetterShardsPlugin.getInstance().warning("The portal spawn ignore material specified as " + ign + " is not valid. It was ignored"); + } + } + } + } + + @Override + public int getPortalID() { + if (id == -1) { + id = BetterShardsPlugin.getDatabaseManager().getPortalID(BetterShardsPlugin.getInstance().getName(), 1); + } + return id; + } } diff --git a/pom.xml b/pom.xml index 0f40c18..5773f40 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ https://github.com/Civcraft/BetterShards/ - 1.5.0 + 1.5.01 UTF-8 UTF-8 1.8