2015-10-19 17:43:46 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
2013-07-30 01:09:20 +00:00
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
2013-07-30 01:09:20 +00:00
|
|
|
import java.util.EnumMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
|
|
|
import net.pravian.aero.component.service.AbstractService;
|
2013-07-30 01:09:20 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.World;
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public class GameRuleHandler extends AbstractService<TotalFreedomMod>
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
private final EnumMap<TFM_GameRule, TFM_GameRule_Value> rules = new EnumMap<TFM_GameRule, TFM_GameRule_Value>(TFM_GameRule.class);
|
2013-07-30 01:09:20 +00:00
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public GameRuleHandler(TotalFreedomMod plugin)
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
super(plugin);
|
|
|
|
|
2013-07-30 01:09:20 +00:00
|
|
|
for (TFM_GameRule gameRule : TFM_GameRule.values())
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
rules.put(gameRule, gameRule.getDefaultValue());
|
2013-07-30 01:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
@Override
|
|
|
|
protected void onStart()
|
|
|
|
{
|
|
|
|
setGameRule(TFM_GameRule.DO_DAYLIGHT_CYCLE, !ConfigEntry.DISABLE_NIGHT.getBoolean(), false);
|
|
|
|
setGameRule(TFM_GameRule.DO_FIRE_TICK, ConfigEntry.ALLOW_FIRE_SPREAD.getBoolean(), false);
|
|
|
|
setGameRule(TFM_GameRule.DO_MOB_LOOT, false, false);
|
|
|
|
setGameRule(TFM_GameRule.DO_MOB_SPAWNING, !ConfigEntry.MOB_LIMITER_ENABLED.getBoolean(), false);
|
|
|
|
setGameRule(TFM_GameRule.DO_TILE_DROPS, false, false);
|
|
|
|
setGameRule(TFM_GameRule.MOB_GRIEFING, false, false);
|
|
|
|
setGameRule(TFM_GameRule.NATURAL_REGENERATION, true, false);
|
|
|
|
commitGameRules();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop()
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public void setGameRule(TFM_GameRule gameRule, boolean value)
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
setGameRule(gameRule, value, true);
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public void setGameRule(TFM_GameRule gameRule, boolean value, boolean doCommit)
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
rules.put(gameRule, TFM_GameRule_Value.fromBoolean(value));
|
2013-07-30 01:09:20 +00:00
|
|
|
if (doCommit)
|
|
|
|
{
|
|
|
|
commitGameRules();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public void commitGameRules()
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
List<World> worlds = Bukkit.getWorlds();
|
2015-10-19 17:43:46 +00:00
|
|
|
Iterator<Map.Entry<TFM_GameRule, TFM_GameRule_Value>> it = rules.entrySet().iterator();
|
2013-07-30 01:09:20 +00:00
|
|
|
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())
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FUtil.setWorldTime(world, 6000L);
|
2013-07-30 01:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static enum TFM_GameRule
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static enum TFM_GameRule_Value
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
return (this.value.equals(TFM_GameRule_Value.TRUE.value));
|
2013-07-30 01:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static TFM_GameRule_Value fromBoolean(boolean in)
|
|
|
|
{
|
|
|
|
return (in ? TFM_GameRule_Value.TRUE : TFM_GameRule_Value.FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|