Add global gamerules

This commit is contained in:
Telesphoreo 2022-03-20 20:05:50 -05:00
parent 580f87f28b
commit d0b89d995c
4 changed files with 67 additions and 51 deletions

View File

@ -9,6 +9,7 @@ import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import java.util.List; import java.util.List;
import java.util.Locale;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -44,8 +45,17 @@ public class DebugCMD extends PlexCommand
{ {
for (World world : Bukkit.getWorlds()) for (World world : Bukkit.getWorlds())
{ {
PlexUtils.commitGameRules(world); PlexUtils.commitGlobalGameRules(world);
PlexLog.debug("Set gamerules for world: " + world.getName()); PlexLog.log("Set global gamerules for world: " + world.getName());
}
for (String world : plugin.config.getConfigurationSection("worlds").getKeys(false))
{
World bukkitWorld = Bukkit.getWorld(world);
if (bukkitWorld != null)
{
PlexUtils.commitSpecificGameRules(bukkitWorld);
PlexLog.log("Set specific gamerules for world: " + world.toLowerCase(Locale.ROOT));
}
} }
return mmString("<aqua>Re-applied game all the game rules!"); return mmString("<aqua>Re-applied game all the game rules!");
} }

View File

@ -3,6 +3,7 @@ package dev.plex.services.impl;
import dev.plex.services.AbstractService; import dev.plex.services.AbstractService;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import java.util.Locale;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
@ -18,8 +19,17 @@ public class GameRuleService extends AbstractService
{ {
for (World world : Bukkit.getWorlds()) for (World world : Bukkit.getWorlds())
{ {
PlexUtils.commitGameRules(world); PlexUtils.commitGlobalGameRules(world);
PlexLog.debug("Set gamerules for world: " + world.getName()); PlexLog.log("Set global gamerules for world: " + world.getName());
}
for (String world : plugin.config.getConfigurationSection("worlds").getKeys(false))
{
World bukkitWorld = Bukkit.getWorld(world);
if (bukkitWorld != null)
{
PlexUtils.commitSpecificGameRules(bukkitWorld);
PlexLog.log("Set specific gamerules for world: " + world.toLowerCase(Locale.ROOT));
}
} }
} }

View File

@ -239,9 +239,23 @@ public class PlexUtils extends PlexBase
} }
} }
public static <T> void commitGameRules(World world) public static <T> void commitGlobalGameRules(World world)
{
for (String s : Plex.get().config.getStringList("global_gamerules"))
{
readGameRules(world, s);
}
}
public static <T> void commitSpecificGameRules(World world)
{ {
for (String s : Plex.get().config.getStringList("worlds." + world.getName().toLowerCase(Locale.ROOT) + ".gameRules")) for (String s : Plex.get().config.getStringList("worlds." + world.getName().toLowerCase(Locale.ROOT) + ".gameRules"))
{
readGameRules(world, s);
}
}
private static <T> void readGameRules(World world, String s)
{ {
String gameRule = s.split(";")[0]; String gameRule = s.split(";")[0];
T value = (T)s.split(";")[1]; T value = (T)s.split(";")[1];
@ -256,11 +270,9 @@ public class PlexUtils extends PlexBase
PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value)); PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value));
} }
} }
}
public static <T> Object check(T value) public static <T> Object check(T value)
{ {
if (value.toString().equalsIgnoreCase("true") || value.toString().equalsIgnoreCase("false")) if (value.toString().equalsIgnoreCase("true") || value.toString().equalsIgnoreCase("false"))
{ {
return Boolean.parseBoolean(value.toString()); return Boolean.parseBoolean(value.toString());

View File

@ -47,14 +47,10 @@ data:
password: "" password: ""
# See https://plex.us.org/docs/customization/config#worlds for documentation # See https://plex.us.org/docs/customization/config#worlds for documentation
worlds: # These gamerules apply to all worlds on the server
flatlands: global_gamerules:
name: "Flatlands" - "doWeatherCycle;true"
permission: "plex.world.flatlands" - "doDaylightCycle;true"
noEdit: "&cYou can't edit this world!"
gameRules:
- "doWeatherCycle;false"
- "doDaylightCycle;false"
- "doMobSpawning;false" - "doMobSpawning;false"
- "keepInventory;true" - "keepInventory;true"
- "doFireTick;false" - "doFireTick;false"
@ -66,6 +62,16 @@ worlds:
- "announceAdvancements;false" - "announceAdvancements;false"
- "showDeathMessages;false" - "showDeathMessages;false"
- "sendCommandFeedback;false" - "sendCommandFeedback;false"
worlds:
flatlands:
name: "Flatlands"
permission: "plex.world.flatlands"
noEdit: "&cYou can't edit this world!"
gameRules:
# The gamerules here override the global gamerules
- "doWeatherCycle;false"
- "doDaylightCycle;false"
parameters: parameters:
grass_block: 1 grass_block: 1
dirt: 32 dirt: 32
@ -80,17 +86,6 @@ worlds:
gameRules: gameRules:
- "doWeatherCycle;false" - "doWeatherCycle;false"
- "doDaylightCycle;false" - "doDaylightCycle;false"
- "doMobSpawning;false"
- "keepInventory;true"
- "doFireTick;false"
- "doMobLoot;false"
- "mobGriefing;false"
- "doTileDrops;false"
- "commandBlockOutput;false"
- "naturalRegeneration;true"
- "announceAdvancements;false"
- "showDeathMessages;false"
- "sendCommandFeedback;false"
parameters: parameters:
grass_block: 1 grass_block: 1
dirt: 32 dirt: 32
@ -105,17 +100,6 @@ worlds:
gameRules: gameRules:
- "doWeatherCycle;false" - "doWeatherCycle;false"
- "doDaylightCycle;false" - "doDaylightCycle;false"
- "doMobSpawning;false"
- "keepInventory;true"
- "doFireTick;false"
- "doMobLoot;false"
- "mobGriefing;false"
- "doTileDrops;false"
- "commandBlockOutput;false"
- "naturalRegeneration;true"
- "announceAdvancements;false"
- "showDeathMessages;false"
- "sendCommandFeedback;false"
parameters: parameters:
grass_block: 1 grass_block: 1
dirt: 32 dirt: 32