Getting ready for 1.8 GameModes.

This setup will allow different worlds to be setup under different gamemodes, it handles per player modes during teleportation to and from Worlds.
This commit is contained in:
Rigby 2011-09-09 12:25:21 +01:00
parent 69b143ada6
commit 9384f96549
4 changed files with 85 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import java.util.List;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
@ -80,6 +81,8 @@ public class MVWorld {
private Boolean pvp; // Does this World allow PVP?
private Boolean fakepvp; // Should this world have fakePVP on? (used for PVP zones)
private GameMode gameMode = GameMode.SURVIVAL;
private String respawnWorld; // Contains the name of the World to respawn the player to
private List<Integer> blockBlacklist; // Contain a list of Blocks which we won't allow on this World.
@ -137,6 +140,8 @@ public class MVWorld {
this.setCurrency(config.getInt("worlds." + this.name + ".entryfee.currency", -1));
this.getMobExceptions();
this.setGameMode(config.getString("worlds." + this.name + ".gamemode", GameMode.SURVIVAL.toString()));
this.setSpawnInMemory(config.getBoolean("worlds." + this.name + ".keepspawninmemory", true));
this.getWorldBlacklist().addAll(config.getStringList("worlds." + this.name + ".worldblacklist", new ArrayList<String>()));
@ -418,6 +423,15 @@ public class MVWorld {
} catch (Exception e) {
}
}
if (name.equalsIgnoreCase("gamemode") && name.equalsIgnoreCase("mode")) {
try {
GameMode mode = GameMode.valueOf(value);
return this.setGameMode(mode);
} catch (Exception e) {
}
}
try {
boolean boolValue = Boolean.parseBoolean(value);
return this.setVariable(name, boolValue);
@ -629,6 +643,26 @@ public class MVWorld {
}
}
private boolean setGameMode(String strMode) {
GameMode mode = GameMode.SURVIVAL;
try {
mode = GameMode.valueOf(strMode);
} catch (Exception e) {
}
return this.setGameMode(mode);
}
private boolean setGameMode(GameMode mode) {
this.gameMode = mode;
config.setProperty("worlds." + this.name + ".gamemode", this.gameMode.toString());
saveConfig();
return true;
}
public GameMode getGameMode() {
return this.gameMode;
}
public boolean getWeatherEnabled() {
return this.allowWeather;
}

View File

@ -123,6 +123,7 @@ public class InfoCommand extends MultiverseCommand {
message.add(new FancyHeader("More World Settings", colors));
message.add(new FancyMessage("Weather: ", world.getWeatherEnabled() + "", colors));
message.add(new FancyMessage("Keep spawn in memory: ", world.getKeepSpawnInMemory() + "", colors));
message.add(new FancyMessage("Game Mode:", world.getGameMode().toString(), colors));
message.add(new FancyHeader("PVP Settings", colors));
message.add(new FancyMessage("Multiverse Setting: ", world.getPvp().toString(), colors));
message.add(new FancyMessage("Bukkit Setting: ", world.getCBWorld().getPVP() + "", colors));

View File

@ -21,7 +21,7 @@ enum Action {
// Color == Aliascolor
enum SetProperties {
alias, animals, monsters, pvp, scaling, aliascolor, color, respawn, currency, curr, price, scale, spawnmemory, memory, weather, storm
alias, animals, monsters, pvp, scaling, aliascolor, color, respawn, currency, curr, price, scale, spawnmemory, memory, weather, storm, gamemode, mode
}
public class ModifyCommand extends MultiverseCommand {

View File

@ -2,9 +2,11 @@ package com.onarandombox.MultiverseCore.listeners;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
@ -67,8 +69,10 @@ public class MVPlayerListener extends PlayerListener {
if (!this.worldManager.isMVWorld(world.getName())) {
return;
}
if(event.isBedSpawn() && this.plugin.getConfig().getBoolean("bedrespawn", true)) {
// Handle the Players GameMode setting for the new world.
this.handleGameMode(event.getPlayer(), event.getRespawnLocation().getWorld());
this.plugin.log(Level.FINE, "Spawning " + event.getPlayer().getName() + " at their bed");
return;
}
@ -92,6 +96,9 @@ public class MVPlayerListener extends PlayerListener {
MVRespawnEvent respawnEvent = new MVRespawnEvent(respawnLocation, event.getPlayer(), "compatability");
this.plugin.getServer().getPluginManager().callEvent(respawnEvent);
event.setRespawnLocation(respawnEvent.getPlayersRespawnLocation());
// Handle the Players GameMode setting for the new world.
this.handleGameMode(event.getPlayer(), respawnEvent.getPlayersRespawnLocation().getWorld());
}
private Location getMostAccurateRespawnLocation(World w) {
@ -110,6 +117,8 @@ public class MVPlayerListener extends PlayerListener {
event.getPlayer().sendMessage("or you can create new ones with " + ChatColor.GOLD + "/mvcreate");
event.getPlayer().sendMessage("If you just wanna see all of the Multiverse Help, type: " + ChatColor.GREEN + "/mv");
}
// Handle the Players GameMode setting for the new world.
this.handleGameMode(event.getPlayer(), event.getPlayer().getWorld());
}
@Override
@ -141,6 +150,9 @@ public class MVPlayerListener extends PlayerListener {
}
// Only check payments if it's a different world:
if (!event.getTo().getWorld().equals(event.getFrom().getWorld())) {
// Handle the Players GameMode setting for the new world.
this.handleGameMode(event.getPlayer(), toWorld);
// If the player does not have to pay, return now.
if (toWorld.isExempt(event.getPlayer())) {
return;
@ -153,4 +165,40 @@ public class MVPlayerListener extends PlayerListener {
}
}
}
// FOLLOWING 2 Methods and Private class handle Per Player GameModes.
private void handleGameMode(Player player, World world) {
MVWorld mvWorld = this.worldManager.getMVWorld(world.getName());
if (mvWorld != null) {
this.handleGameMode(player, mvWorld);
}
}
private void handleGameMode(Player player, MVWorld world) {
// We perform this task one tick later to MAKE SURE that the player actually reaches the
// destination world, otherwise we'd be changing the player mode if they havent moved anywhere.
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new HandleGameMode(player, world), 1L);
}
/**
* The following private class is used to handle player game mode changes within a scheduler.
*/
private class HandleGameMode implements Runnable {
private Player player;
private MVWorld world;
private HandleGameMode(Player player, MVWorld world) {
this.player = player;
this.world = world;
}
@Override
public void run() {
// Check that the player is in the new world and they haven't been teleported elsewhere or the event cancelled.
if (player.getWorld().getName().equals(world.getCBWorld().getName())) {
player.setGameMode(world.getGameMode());
}
}
}
}