Added TFM_GameRuleHandler for easy setting of game rule parameters.

This commit is contained in:
Steven Lawson 2013-07-29 21:09:20 -04:00
parent 3f61e05609
commit be8314cf63
8 changed files with 142 additions and 37 deletions

View File

@ -1,6 +1,6 @@
#Sun, 28 Jul 2013 20:48:17 -0400 #Mon, 29 Jul 2013 20:32:38 -0400
program.VERSION=2.21 program.VERSION=2.21
program.BUILDNUM=355 program.BUILDNUM=370
program.BUILDDATE=07/28/2013 08\:48 PM program.BUILDDATE=07/29/2013 08\:32 PM

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit! #Build Number for ANT. Do not edit!
#Sun Jul 28 20:48:17 EDT 2013 #Mon Jul 29 20:32:38 EDT 2013
build.number=356 build.number=371

View File

@ -53,6 +53,7 @@ blocked_commands:
- n:b:/time:Server-side time changing is disabled. Please use /ptime to set your own personal time. - n:b:/time:Server-side time changing is disabled. Please use /ptime to set your own personal time.
- n:b:/md:This server now uses DisguiseCraft instead of MobDisguise. Type /d to disguise and /u to undisguise. - n:b:/md:This server now uses DisguiseCraft instead of MobDisguise. Type /d to disguise and /u to undisguise.
- n:b:/gamemode:Use /creative and /survival to set your gamemode. - n:b:/gamemode:Use /creative and /survival to set your gamemode.
- n:b:/gamerule:_
- n:b:/ban:_ - n:b:/ban:_
- n:b:/pardon:_ - n:b:/pardon:_
- n:b:/toggledownfall:_ - n:b:/toggledownfall:_

View File

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod.Commands; package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_GameRuleHandler;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -17,16 +18,9 @@ public class Command_firespread extends TFM_Command
return false; return false;
} }
if (args[0].equalsIgnoreCase("on")) playerMsg("Fire spread is now " + ((TotalFreedomMod.allowFireSpread = !args[0].equalsIgnoreCase("off")) ? "enabled" : "disabled") + ".");
{
TotalFreedomMod.allowFireSpread = true; TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_FIRE_TICK, TotalFreedomMod.allowFireSpread);
playerMsg("Fire spread is now enabled.");
}
else
{
TotalFreedomMod.allowFireSpread = false;
playerMsg("Fire spread is now disabled.");
}
return true; return true;
} }

View File

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod.Commands; package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_GameRuleHandler;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -74,6 +75,8 @@ public class Command_moblimiter extends TFM_Command
playerMsg("Moblimiter is disabled. No mob restrictions are in effect."); playerMsg("Moblimiter is disabled. No mob restrictions are in effect.");
} }
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_MOB_SPAWNING, !TotalFreedomMod.mobLimiterEnabled);
return true; return true;
} }
} }

View File

@ -0,0 +1,118 @@
package me.StevenLawson.TotalFreedomMod;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.World;
public class TFM_GameRuleHandler
{
private static final EnumMap<TFM_GameRule, TFM_GameRule_Value> GAME_RULES = new EnumMap<TFM_GameRule, TFM_GameRule_Value>(TFM_GameRule.class);
static
{
for (TFM_GameRule gameRule : TFM_GameRule.values())
{
GAME_RULES.put(gameRule, gameRule.getDefaultValue());
}
}
private TFM_GameRuleHandler()
{
throw new AssertionError();
}
public static void setGameRule(TFM_GameRule gameRule, boolean value)
{
setGameRule(gameRule, value, true);
}
public static void setGameRule(TFM_GameRule gameRule, boolean value, boolean doCommit)
{
GAME_RULES.put(gameRule, TFM_GameRule_Value.fromBoolean(value));
if (doCommit)
{
commitGameRules();
}
}
public static void commitGameRules()
{
List<World> worlds = Bukkit.getWorlds();
Iterator<Map.Entry<TFM_GameRule, TFM_GameRule_Value>> it = GAME_RULES.entrySet().iterator();
while (it.hasNext())
{
Map.Entry<TFM_GameRule, TFM_GameRule_Value> gameRuleEntry = it.next();
String gameRuleName = gameRuleEntry.getKey().getGameRuleName();
String gameRuleValue = gameRuleEntry.getValue().toString();
for (World world : worlds)
{
world.setGameRuleValue(gameRuleName, gameRuleValue);
if (gameRuleEntry.getKey() == TFM_GameRule.DO_DAYLIGHT_CYCLE && !gameRuleEntry.getValue().toBoolean())
{
TFM_Util.setWorldTime(world, 6000L);
}
}
}
}
public enum TFM_GameRule
{
DO_FIRE_TICK("doFireTick", TFM_GameRule_Value.TRUE),
MOB_GRIEFING("mobGriefing", TFM_GameRule_Value.TRUE),
KEEP_INVENTORY("keepInventory", TFM_GameRule_Value.FALSE),
DO_MOB_SPAWNING("doMobSpawning", TFM_GameRule_Value.TRUE),
DO_MOB_LOOT("doMobLoot", TFM_GameRule_Value.TRUE),
DO_TILE_DROPS("doTileDrops", TFM_GameRule_Value.TRUE),
COMMAND_BLOCK_OUTPUT("commandBlockOutput", TFM_GameRule_Value.TRUE),
NATURAL_REGENERATION("naturalRegeneration", TFM_GameRule_Value.TRUE),
DO_DAYLIGHT_CYCLE("doDaylightCycle", TFM_GameRule_Value.TRUE);
private final String gameRuleName;
private final TFM_GameRule_Value defaultValue;
private TFM_GameRule(String gameRuleName, TFM_GameRule_Value defaultValue)
{
this.gameRuleName = gameRuleName;
this.defaultValue = defaultValue;
}
public String getGameRuleName()
{
return gameRuleName;
}
public TFM_GameRule_Value getDefaultValue()
{
return defaultValue;
}
}
public enum TFM_GameRule_Value
{
TRUE("true"), FALSE("false");
private final String value;
private TFM_GameRule_Value(String value)
{
this.value = value;
}
@Override
public String toString()
{
return this.value;
}
public boolean toBoolean()
{
return (this.value.equals(TFM_GameRule_Value.TRUE.value) ? true : false);
}
public static TFM_GameRule_Value fromBoolean(boolean in)
{
return (in ? TFM_GameRule_Value.TRUE : TFM_GameRule_Value.FALSE);
}
}
}

View File

@ -32,23 +32,6 @@ public class TFM_Heartbeat extends BukkitRunnable
TFM_Util.wipeEntities(!TotalFreedomMod.allowExplosions, false); TFM_Util.wipeEntities(!TotalFreedomMod.allowExplosions, false);
} }
if (TotalFreedomMod.disableNight)
{
try
{
for (World world : server.getWorlds())
{
if (world.getTime() > 12000L)
{
TFM_Util.setWorldTime(world, 1000L);
}
}
}
catch (NullPointerException ex)
{
}
}
if (TotalFreedomMod.disableWeather) if (TotalFreedomMod.disableWeather)
{ {
for (World world : server.getWorlds()) for (World world : server.getWorlds())

View File

@ -6,11 +6,7 @@ import java.io.InputStream;
import java.util.*; import java.util.*;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command; import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader; import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
import me.StevenLawson.TotalFreedomMod.Listener.TFM_BlockListener; import me.StevenLawson.TotalFreedomMod.Listener.*;
import me.StevenLawson.TotalFreedomMod.Listener.TFM_EntityListener;
import me.StevenLawson.TotalFreedomMod.Listener.TFM_PlayerListener;
import me.StevenLawson.TotalFreedomMod.Listener.TFM_ServerListener;
import me.StevenLawson.TotalFreedomMod.Listener.TFM_WeatherListener;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -98,6 +94,16 @@ public class TotalFreedomMod extends JavaPlugin
} }
} }
// Initialize game rules
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_DAYLIGHT_CYCLE, !disableNight, false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_FIRE_TICK, allowFireSpread, false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_MOB_LOOT, false, false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_MOB_SPAWNING, !mobLimiterEnabled, false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.DO_TILE_DROPS, false, false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.MOB_GRIEFING, false, false);
TFM_GameRuleHandler.setGameRule(TFM_GameRuleHandler.TFM_GameRule.NATURAL_REGENERATION, true, false);
TFM_GameRuleHandler.commitGameRules();
if (TotalFreedomMod.protectedAreasEnabled) if (TotalFreedomMod.protectedAreasEnabled)
{ {
TFM_ProtectedArea.loadProtectedAreas(); TFM_ProtectedArea.loadProtectedAreas();