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.List;
|
|
|
|
import java.util.Map;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
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
|
|
|
{
|
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
|
|
|
|
2020-07-01 01:51:06 +00:00
|
|
|
public GameRuleHandler()
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
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
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStart()
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
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);
|
2019-12-14 03:16:09 +00:00
|
|
|
setGameRule(GameRule.SHOW_DEATH_MESSAGES, false, false);
|
2020-07-12 16:13:41 +00:00
|
|
|
setGameRule(GameRule.SEND_COMMAND_FEEDBACK, false, false);
|
2015-10-19 17:43:46 +00:00
|
|
|
commitGameRules();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
@SuppressWarnings("deprecation")
|
2015-10-19 17:43:46 +00:00
|
|
|
public void commitGameRules()
|
2013-07-30 01:09:20 +00:00
|
|
|
{
|
|
|
|
List<World> worlds = Bukkit.getWorlds();
|
2020-11-09 03:08:44 +00:00
|
|
|
for (Map.Entry<GameRule, Boolean> gameRuleEntry : rules.entrySet())
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:08:44 +00:00
|
|
|
public enum GameRule
|
2013-07-30 01:09:20 +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),
|
2019-12-14 03:16:09 +00:00
|
|
|
ANNOUNCE_ADVANCEMENTS("announceAdvancements", false),
|
2020-07-12 16:13:41 +00:00
|
|
|
SHOW_DEATH_MESSAGES("showDeathMessages", false),
|
|
|
|
SEND_COMMAND_FEEDBACK("sendCommandFeedback", false);
|
2020-11-09 03:08:44 +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
|
|
|
|
2020-12-04 00:28:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 00:28:53 +00:00
|
|
|
}
|