Skip to content

Commit

Permalink
Better safe y determining and offset value in config
Browse files Browse the repository at this point in the history
  • Loading branch information
SocketByte committed Apr 4, 2018
1 parent efa86b8 commit 5d0e8b0
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public static ServerController getServerController(int serverId) {
return null;
}

public static ServerController getCurrentServer() {
return getServerController(getServerId());
}

public static Logger log() {
return getInstance().getLogger();
}
Expand Down Expand Up @@ -237,7 +241,7 @@ public static void ready() {
World world = Bukkit.getWorlds().get(0);

WorldBorder worldBorder = world.getWorldBorder();
worldBorder.setSize((config.border - 25) * 2); // im genius XD
worldBorder.setSize(config.border * 2);
worldBorder.setCenter(0, 0);
log().info("Ready!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class ServerController {

public int id;
public int offset;
public int port;
public String name;
public int minX;
Expand All @@ -14,6 +15,7 @@ public class ServerController {
public String toString() {
return "ServerController{" +
"id=" + id +
", offset=" + offset +
", port=" + port +
", name='" + name + '\'' +
", minX=" + minX +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import pl.socketbyte.opensectors.linker.packet.PacketPlayerTransfer;
import pl.socketbyte.opensectors.linker.packet.PacketUpdatePlayerSession;
import pl.socketbyte.opensectors.linker.packet.serializable.SerializablePotionEffect;
import pl.socketbyte.opensectors.linker.util.NetworkManager;
import pl.socketbyte.opensectors.linker.util.PlayerInfoHolder;
import pl.socketbyte.opensectors.linker.util.Serializer;
import pl.socketbyte.opensectors.linker.util.Util;
import pl.socketbyte.opensectors.linker.util.*;

import java.io.IOException;
import java.util.UUID;
Expand Down Expand Up @@ -105,10 +102,9 @@ public void onPlayerJoin(PlayerJoinEvent event) {
StackTraceHandler.handle(OpenSectorLinker.class, e, StackTraceSeverity.ERROR);
}
SerializablePotionEffect[] potionEffects = packet.getPotionEffects();
Location destination = new Location(player.getWorld(), packet.getX(), packet.getY(), packet.getZ(),
packet.getYaw(), packet.getPitch());
Location valid = Util.getValidLocation(destination, packet.getY());
player.teleport(valid);

SafeTeleport safeTeleport = new SafeTeleport(player);
safeTeleport.teleport(packet.getX(), packet.getZ(), packet.getY(), packet.getYaw(), packet.getPitch());

for (PotionEffect effect : player.getActivePotionEffects())
player.removePotionEffect(effect.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ private void sendTransferRequest(Player player, Sector to) {
ServerController current = SectorManager.INSTANCE.getSectorMap()
.get(OpenSectorLinker.getServerId())
.getServerController();
//int[] destination = Util.getDestinationWithOffset(current,
// to.getServerController(), x, z);
//System.out.println((destination[0] - x) + ", " + (destination[1] - z));

PlayerTransferHolder.getTransfering().add(player.getUniqueId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.Vector;
import pl.socketbyte.opensectors.linker.OpenSectorLinker;
import pl.socketbyte.opensectors.linker.json.controllers.ServerController;

public class Sector {

private final ServerController serverController;
private World world;

private int offset;

private int minX;
private int minZ;
private int maxX;
Expand All @@ -19,12 +22,13 @@ public class Sector {
private Location lower;
private Location upper;

public Sector(ServerController serverController, int minX, int minZ, int maxX, int maxZ) {
public Sector(ServerController serverController, int minX, int minZ, int maxX, int maxZ, int offset) {
this.world = Bukkit.getWorlds().get(0);
this.minX = minX;
this.minZ = minZ;
this.maxX = maxX;
this.maxZ = maxZ;
this.minX = minX - offset;
this.minZ = minZ - offset;
this.maxX = maxX - offset;
this.maxZ = maxZ - offset;
this.offset = offset;
this.serverController = serverController;
setPositions();
}
Expand Down Expand Up @@ -56,7 +60,8 @@ public double howClose(Location location) {
double distSouth = Math.abs(maxZ - z);
double distX = (distWest < distEast) ? distWest : distEast;
double distZ = (distNorth < distSouth) ? distNorth : distSouth;
return distX > distZ ? distZ : distX;
double distance = distX > distZ ? distZ : distX;
return distance + OpenSectorLinker.getCurrentServer().offset;
}

public boolean isIn(Location location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void load() {

for (int i = 0; i < sectors; i++) {
ServerController controller = controllers[i];
int id = controller.id * 2;
Sector sector = new Sector(controller, controller.minX - id, controller.minZ - id,
controller.maxX - id, controller.maxZ - id);
int offset = controller.offset;
Sector sector = new Sector(controller, controller.minX, controller.minZ,
controller.maxX, controller.maxZ, offset);

sectorMap.put(controller.id, sector);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pl.socketbyte.opensectors.linker.util;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

import java.util.Stack;

public class SafeTeleport {

private final Player player;
private final World world;

public SafeTeleport(Player player) {
this.player = player;
this.world = player.getWorld();
}

public void teleport(int x, int z, int originalY, float yaw, float pitch) {
int y = determineSafeY(x, z);

if (originalY > y)
y = originalY;

Location location = new Location(world, x, y, z, yaw, pitch);
player.teleport(location);
}

private int determineSafeY(int x, int z) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < 256; i++) {
Block block = world.getBlockAt(x, i, z);
Material type = block.getType();

if (type == Material.AIR) {
if (stack.isEmpty())
stack.push(i);
}
else if (!stack.isEmpty()
&& (type == Material.LEAVES
|| type == Material.LEAVES_2))
stack.pop();
}
return stack.pop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,4 @@ public static List<String> fixColors(List<String> texts) {
strings.add(Util.fixColors(str));
return strings;
}

private static boolean isLocationValid(Location loc) {
Material mat = loc.getBlock().getType();
return (mat == Material.GRASS) || (mat == Material.SAND) || (mat == Material.DIRT)
|| (mat == Material.GRAVEL) || (mat == Material.STONE) || (mat == Material.WATER);
}

public static Location getValidLocation(Location loc, int y) {
for (int i = 0; i < 256; i++) {
loc.setY(loc.getWorld().getHighestBlockYAt(loc.getBlockX(), loc.getBlockZ()));
if (isLocationValid(new Location(loc.getWorld(), loc.getX(), loc.getY() - 1, loc.getZ()))) {
loc.add(0, 3, 0);
if (y > (loc.getBlockY() + 2))
loc.setY(y);

return loc;
}
}
return loc;
}

public static int[] getDestinationWithOffset(ServerController current, ServerController next, int x, int z) {
int distWest = Math.abs((next.maxX) - current.maxX);
int distEast = Math.abs((next.minX) - current.minX);
int distNorth = Math.abs((next.minZ) - current.minZ);
int distSouth = Math.abs((next.maxZ) - current.maxZ);
int dirX = (distWest < distEast) ? -8 : 8;
int dirZ = (distNorth < distSouth) ? -8 : 8;
int distX = (distWest < distEast) ? distWest : distEast;
int distZ = (distNorth < distSouth) ? distNorth : distSouth;
return distX < distZ ? new int[] { x, z + dirZ } : new int[] { x + dirX, z };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class ServerController {

public int id;
public int offset;
public int port;
public String name;
public int minX;
Expand All @@ -14,6 +15,7 @@ public class ServerController {
public String toString() {
return "ServerController{" +
"id=" + id +
", offset=" + offset +
", port=" + port +
", name='" + name + '\'' +
", minX=" + minX +
Expand Down
9 changes: 7 additions & 2 deletions OpenSectorSystem/src/main/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"serverControllers": [
{
"id": 0,
"offset": 0,
"port": 25566,
"name": "center",
"minX": -250.0,
Expand All @@ -28,6 +29,7 @@
},
{
"id": 1,
"offset": 2,
"port": 25567,
"name": "n",
"minX": -250.0,
Expand All @@ -37,6 +39,7 @@
},
{
"id": 2,
"offset": 3,
"port": 25568,
"name": "s",
"minX": -2000.0,
Expand All @@ -46,17 +49,19 @@
},
{
"id": 3,
"offset": 4,
"port": 25569,
"name": "w",
"name": "e",
"minX": 250.0,
"minZ": -250.0,
"maxX": 2000.0,
"maxZ": 2000.0
},
{
"id": 4,
"offset": 5,
"port": 25570,
"name": "e",
"name": "w",
"minX": -2000.0,
"minZ": -2000.0,
"maxX": -250.0,
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ ANY of location scaling systems or similar currently.
"serverControllers": [
{
"id": 0,
"offset": 0,
"port": 25566,
"name": "center",
"minX": -250.0,
Expand All @@ -138,6 +139,7 @@ ANY of location scaling systems or similar currently.
},
{
"id": 1,
"offset": 2,
"port": 25567,
"name": "n",
"minX": -250.0,
Expand All @@ -147,6 +149,7 @@ ANY of location scaling systems or similar currently.
},
{
"id": 2,
"offset": 3,
"port": 25568,
"name": "s",
"minX": -2000.0,
Expand All @@ -156,17 +159,19 @@ ANY of location scaling systems or similar currently.
},
{
"id": 3,
"offset": 4,
"port": 25569,
"name": "w",
"name": "e",
"minX": 250.0,
"minZ": -250.0,
"maxX": 2000.0,
"maxZ": 2000.0
},
{
"id": 4,
"offset": 5,
"port": 25570,
"name": "e",
"name": "w",
"minX": -2000.0,
"minZ": -2000.0,
"maxX": -250.0,
Expand Down

0 comments on commit 5d0e8b0

Please sign in to comment.