Skip to content

Instantly share code, notes, and snippets.

@heinrichquirit
Created June 8, 2014 11:15
Show Gist options
  • Save heinrichquirit/24394089c9162c71c221 to your computer and use it in GitHub Desktop.
Save heinrichquirit/24394089c9162c71c221 to your computer and use it in GitHub Desktop.
worlds:
world:
x: 0
y: 0
z: 0
yaw: 0
pitch: 0
world_nether:
x: 0
y: 0
z: 0
yaw: 0
pitch: 0
name: SameWorld
main: main.java.net.bigbadcraft.sameworld.SameWorld
version: 1.1.0
commands:
swset:
description: Sets the spawn point for that world
permission: sameworld.set
package main.java.net.bigbadcraft.sameworld;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class SameWorld extends JavaPlugin implements Listener {
private final ChatColor GREEN = ChatColor.GREEN;
private Set<String> allowedWorlds;
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
saveDefaultConfig();
allowedWorlds = getConfig().getConfigurationSection("worlds").getKeys(false);
getCommand("swset").setExecutor(this);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDeath(PlayerRespawnEvent event) {
Player player = event.getPlayer();
if (allowedWorlds.size() > 0 && allowedWorlds.contains(player.getWorld().getName())) {
event.setRespawnLocation(getLocation(player.getWorld().getName()));
player.sendMessage(GREEN + "You respawned in the same world.");
}
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
if (!(sender instanceof Player)) {
return true;
}
Player player = (Player) sender;
Location loc = player.getLocation();
Location underneath = loc.subtract(0.0, 1.0, 0.0);
if (cmd.getName().equalsIgnoreCase("swset")) {
if (args.length == 0) {
if (underneath.getBlock() == null || underneath.getBlock().getType() == Material.AIR) {
player.sendMessage(GREEN + "Cannot save spawn point on nothing.");
return true;
}
saveSpawn(player.getLocation());
player.sendMessage(GREEN + "Successfully saved new spawn point.");
}
}
return true;
}
private void saveSpawn(Location loc) {
reloadConfig();
getConfig().set("worlds."+loc.getWorld().getName()+".x", loc.getBlockX());
getConfig().set("worlds."+loc.getWorld().getName()+".y", loc.getBlockY());
getConfig().set("worlds."+loc.getWorld().getName()+".z", loc.getBlockZ());
getConfig().set("worlds."+loc.getWorld().getName()+".yaw", loc.getYaw());
getConfig().set("worlds."+loc.getWorld().getName()+".pitch", loc.getPitch());
saveConfig();
}
private Location getLocation(String path) {
int x = getConfig().getInt("worlds."+path+".x");
int y = getConfig().getInt("worlds."+path+".y");
int z = getConfig().getInt("worlds."+path+".z");
float yaw = (float) getConfig().getDouble("worlds."+path+".yaw");
float pitch = (float) getConfig().getDouble("worlds."+path+".pitch");
return new Location(Bukkit.getWorld(path), x+0.5D, y, z+0.5D, yaw, pitch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment