Skip to content

Instantly share code, notes, and snippets.

@sk89q
Created November 19, 2011 03:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sk89q/1378371 to your computer and use it in GitHub Desktop.
Save sk89q/1378371 to your computer and use it in GitHub Desktop.
package com.sk89q.skcraft;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.event.player.PlayerPortalEvent;
import com.sk89q.rebar.AbstractComponent;
import com.sk89q.rebar.BukkitEvent;
import com.sk89q.rebar.Rebar;
import com.sk89q.rebar.util.ChatUtil;
import com.sk89q.rebar.util.config.ConfigurationBase;
import com.sk89q.rebar.util.config.Setting;
import com.sk89q.rebar.util.config.SettingBase;
public class SplitNether extends AbstractComponent {
private static final Logger logger = createLogger(SplitNether.class);
private World upperWorld;
private World lowerWorld;
private World netherWorld;
public void initialize() {
LocalConfiguration config = configure(new LocalConfiguration());
if (config.isConfigured()) {
upperWorld = Rebar.server().getWorld(config.upperWorld);
lowerWorld = Rebar.server().getWorld(config.lowerWorld);
netherWorld = Rebar.server().getWorld(config.netherWorld);
if (upperWorld != null && lowerWorld != null && netherWorld != null) {
Rebar.getInstance().registerEvents(new PlayerListener());
logger.info("SplitNether loaded properly.");
} else {
logger.severe("SplitNether failed to detect the worlds correctly.");
}
} else {
logger.warning("SplitNether is not properly configured!");
}
}
public void shutdown() {
}
@SettingBase("split-nether")
private static class LocalConfiguration extends ConfigurationBase {
@Setting("upper-world") public String upperWorld;
@Setting("lower-world") public String lowerWorld;
@Setting("nether-world") public String netherWorld;
public boolean isConfigured() {
return upperWorld != null && lowerWorld != null && netherWorld != null;
}
}
private class PlayerListener extends org.bukkit.event.player.PlayerListener {
@Override
@BukkitEvent(type = Type.PLAYER_PORTAL, priority = Priority.Highest)
public void onPlayerPortal(PlayerPortalEvent event) {
if (event.isCancelled()) return;
if (!event.useTravelAgent()) return;
Player player = event.getPlayer();
Location from = event.getFrom();
if (from.getWorld().equals(netherWorld)) {
if (from.getY() > 64) { // Upper world
event.setTo(new Location(upperWorld, from.getX() * 8, (from.getY() - 64) * 2, from.getZ() * 8));
} else { // Lower world
event.setTo(new Location(lowerWorld, from.getX() * 8, from.getY() * 2, from.getZ() * 8));
}
} else if (from.getWorld().equals(upperWorld)) {
event.setTo(new Location(netherWorld, from.getX() / 8, (from.getY() / 2) + 64, from.getZ() / 8));
} else if (from.getWorld().equals(lowerWorld)) {
event.setTo(new Location(netherWorld, from.getX() / 8, (from.getY() / 2), from.getZ() / 8));
} else {
ChatUtil.error(player, "Uh oh! SplitNether does not handle this case!");
event.setCancelled(true);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment