2015-10-19 17:43:46 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
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;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
2013-07-30 01:09:20 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.World;
|
|
|
|
|
2016-03-01 16:47:01 +00:00
|
|
|
public class GameRuleHandler extends FreedomService
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
private final Map<GameRule, Boolean> rules = new EnumMap<>(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);
|
|
|
|
|
2016-02-29 20:48:17 +00:00
|
|
|
for (GameRule gameRule : GameRule.values())
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
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()
|
|
|
|
{
|
2016-02-29 20:48:17 +00:00
|
|
|
setGameRule(GameRule.DO_DAYLIGHT_CYCLE, !ConfigEntry.DISABLE_NIGHT.getBoolean(), false);
|
|
|
|
setGameRule(GameRule.DO_FIRE_TICK, ConfigEntry.ALLOW_FIRE_SPREAD.getBoolean(), false);
|
|
|
|
setGameRule(GameRule.DO_MOB_LOOT, false, false);
|
|
|
|
setGameRule(GameRule.DO_MOB_SPAWNING, !ConfigEntry.MOB_LIMITER_ENABLED.getBoolean(), false);
|
|
|
|
setGameRule(GameRule.DO_TILE_DROPS, false, false);
|
|
|
|
setGameRule(GameRule.MOB_GRIEFING, false, false);
|
2016-03-01 16:47:01 +00:00
|
|
|
setGameRule(GameRule.COMMAND_BLOCK_OUTPUT, false);
|
2016-02-29 20:48:17 +00:00
|
|
|
setGameRule(GameRule.NATURAL_REGENERATION, true, false);
|
2018-01-07 19:49:38 +00:00
|
|
|
setGameRule(GameRule.KEEP_INVENTORY, true, false);
|
2018-07-26 05:22:19 +00:00
|
|
|
setGameRule(GameRule.ANNOUNCE_ADVANCEMENTS, false, false);
|
2015-10-19 17:43:46 +00:00
|
|
|
commitGameRules();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop()
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-29 20:48:17 +00:00
|
|
|
public void setGameRule(GameRule gameRule, boolean value)
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
setGameRule(gameRule, value, true);
|
|
|
|
}
|
|
|
|
|
2016-02-29 20:48:17 +00:00
|
|
|
public void setGameRule(GameRule gameRule, boolean value, boolean doCommit)
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
2016-02-29 20:48:17 +00:00
|
|
|
rules.put(gameRule, 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();
|
2016-02-29 20:48:17 +00:00
|
|
|
Iterator<Map.Entry<GameRule, Boolean>> it = rules.entrySet().iterator();
|
2013-07-30 01:09:20 +00:00
|
|
|
while (it.hasNext())
|
|
|
|
{
|
2016-02-29 20:48:17 +00:00
|
|
|
|
|
|
|
Map.Entry<GameRule, Boolean> gameRuleEntry = it.next();
|
2013-07-30 01:09:20 +00:00
|
|
|
String gameRuleName = gameRuleEntry.getKey().getGameRuleName();
|
|
|
|
String gameRuleValue = gameRuleEntry.getValue().toString();
|
2016-02-29 20:48:17 +00:00
|
|
|
|
2013-07-30 01:09:20 +00:00
|
|
|
for (World world : worlds)
|
|
|
|
{
|
|
|
|
world.setGameRuleValue(gameRuleName, gameRuleValue);
|
2016-02-29 20:48:17 +00:00
|
|
|
if (gameRuleEntry.getKey() == GameRule.DO_DAYLIGHT_CYCLE && !gameRuleEntry.getValue())
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
long time = world.getTime();
|
|
|
|
time -= time % 24000;
|
|
|
|
world.setTime(time + 24000 + 6000);
|
2013-07-30 01:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-29 20:48:17 +00:00
|
|
|
|
2013-07-30 01:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 20:48:17 +00:00
|
|
|
public static enum GameRule
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2016-02-29 20:48:17 +00:00
|
|
|
DO_FIRE_TICK("doFireTick", true),
|
|
|
|
MOB_GRIEFING("mobGriefing", true),
|
|
|
|
KEEP_INVENTORY("keepInventory", false),
|
|
|
|
DO_MOB_SPAWNING("doMobSpawning", true),
|
|
|
|
DO_MOB_LOOT("doMobLoot", true),
|
|
|
|
DO_TILE_DROPS("doTileDrops", true),
|
|
|
|
COMMAND_BLOCK_OUTPUT("commandBlockOutput", true),
|
|
|
|
NATURAL_REGENERATION("naturalRegeneration", true),
|
2018-07-26 05:22:19 +00:00
|
|
|
DO_DAYLIGHT_CYCLE("doDaylightCycle", true),
|
|
|
|
ANNOUNCE_ADVANCEMENTS("announceAdvancements", false);
|
2016-02-29 20:48:17 +00:00
|
|
|
//
|
2013-07-30 01:09:20 +00:00
|
|
|
private final String gameRuleName;
|
2016-02-29 20:48:17 +00:00
|
|
|
private final boolean defaultValue;
|
2013-07-30 01:09:20 +00:00
|
|
|
|
2016-02-29 20:48:17 +00:00
|
|
|
private GameRule(String gameRuleName, boolean defaultValue)
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
this.gameRuleName = gameRuleName;
|
|
|
|
this.defaultValue = defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getGameRuleName()
|
|
|
|
{
|
|
|
|
return gameRuleName;
|
|
|
|
}
|
|
|
|
|
2016-02-29 20:48:17 +00:00
|
|
|
public boolean getDefaultValue()
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|