Created
November 19, 2011 03:14
-
-
Save sk89q/1378371 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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