Skip to content

Commit

Permalink
feat: global role and environment setting overrides (closes #190)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddlama committed Mar 19, 2023
1 parent d714de6 commit 556d01d
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.oddlama.vane.util.ItemUtil.name_item;

import java.util.List;
import java.util.function.Consumer;

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
Expand All @@ -16,6 +19,8 @@
import org.oddlama.vane.core.module.Module;
import org.oddlama.vane.core.module.ModuleComponent;

import net.kyori.adventure.text.Component;

public class TranslatedItemStack<T extends Module<T>> extends ModuleComponent<T> {

@ConfigInt(def = 1, min = 0, desc = "The item stack amount.")
Expand Down Expand Up @@ -72,6 +77,12 @@ public ItemStack item(Object... args) {
return name_item(config_material.item(config_amount), lang_name.format(args), lang_lore.format(args));
}

public ItemStack item_transform_lore(Consumer<List<Component>> f_lore, Object... args) {
final var lore = lang_lore.format(args);
f_lore.accept(lore);
return name_item(config_material.item(config_amount), lang_name.format(args), lore);
}

public ItemStack item_amount(int amount, Object... args) {
return name_item(config_material.item(amount), lang_name.format(args), lang_lore.format(args));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.oddlama.vane.regions;

import org.oddlama.vane.annotation.config.ConfigInt;
import org.oddlama.vane.core.module.Context;
import org.oddlama.vane.core.module.ModuleComponent;
import org.oddlama.vane.regions.region.EnvironmentSetting;

public class RegionGlobalEnvironmentOverrides extends ModuleComponent<Regions> {
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether animals can spawn.")
public int config_animals;
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether monsters can spawn.")
public int config_monsters;

@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether explosions can happen.")
public int config_explosions;
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether fire spreads and consumes.")
public int config_fire;
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether pvp is allowed.")
public int config_pvp;

@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether fields can be trampled.")
public int config_trample;
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether vines can grow.")
public int config_vine_growth;

public RegionGlobalEnvironmentOverrides(Context<Regions> context) {
super(context.namespace("global_environment_overrides", "This controls global environment setting overrides for all regions on the server. `0` means no-override, the player-configured values are used normally, `1` force-enables this setting for all regions, `-1` force-disables respectively."));
}

public int get_override(final EnvironmentSetting setting) {
switch (setting) {
case ANIMALS: return config_animals;
case MONSTERS: return config_monsters;
case EXPLOSIONS: return config_explosions;
case FIRE: return config_fire;
case PVP: return config_pvp;
case TRAMPLE: return config_trample;
case VINE_GROWTH: return config_vine_growth;
}
return 0;
}

@Override
public void on_enable() {}

@Override
public void on_disable() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.oddlama.vane.regions;

import org.oddlama.vane.annotation.config.ConfigInt;
import org.oddlama.vane.core.module.Context;
import org.oddlama.vane.core.module.ModuleComponent;
import org.oddlama.vane.regions.region.RoleSetting;

public class RegionGlobalRoleOverrides extends ModuleComponent<Regions> {
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the admin permission. Be careful, this is almost never what you want and may result in immutable regions.")
public int config_admin;
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the build permission.")
public int config_build;
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the use permission.")
public int config_use;
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the container permission.")
public int config_container;
@ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the portal permission.")
public int config_portal;

public RegionGlobalRoleOverrides(Context<Regions> context) {
super(context.namespace("global_role_overrides", "This controls global role setting overrides for all roles in every region on the server. `0` means no-override, the player-configured values are used normally, `1` force-enables this setting for all roles in every region, `-1` force-disables respectively. Force-disable naturally also affects the owner, so be careful!"));
}

public int get_override(final RoleSetting setting) {
switch (setting) {
case ADMIN: return config_admin;
case BUILD: return config_build;
case USE: return config_use;
case CONTAINER: return config_container;
case PORTAL: return config_portal;
}
return 0;
}

@Override
public void on_enable() {}

@Override
public void on_disable() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,13 @@ public class Regions extends Module<Regions> {
public RegionEconomyDelegate economy;
public boolean vane_portals_available = false;

public static RegionGlobalRoleOverrides role_overrides = null;
public static RegionGlobalEnvironmentOverrides environment_overrides = null;

public Regions() {
menus = new RegionMenuGroup(this);
role_overrides = new RegionGlobalRoleOverrides(this);
environment_overrides = new RegionGlobalEnvironmentOverrides(this);

new org.oddlama.vane.regions.commands.Region(this);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.oddlama.vane.regions.menu;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand All @@ -22,6 +24,8 @@
import org.oddlama.vane.regions.region.Role;
import org.oddlama.vane.util.StorageUtil;

import net.kyori.adventure.text.Component;

public class RegionGroupMenu extends ModuleComponent<Regions> {

@LangMessage
Expand Down Expand Up @@ -201,13 +205,7 @@ public Menu create(final RegionGroup group, final Player player) {
add_menu_item_setting(region_group_menu, group, 4, item_setting_info_fire, EnvironmentSetting.FIRE);
add_menu_item_setting(region_group_menu, group, 5, item_setting_info_pvp, EnvironmentSetting.PVP);
add_menu_item_setting(region_group_menu, group, 7, item_setting_info_trample, EnvironmentSetting.TRAMPLE);
add_menu_item_setting(
region_group_menu,
group,
8,
item_setting_info_vine_growth,
EnvironmentSetting.VINE_GROWTH
);
add_menu_item_setting(region_group_menu, group, 8, item_setting_info_vine_growth, EnvironmentSetting.VINE_GROWTH);

region_group_menu.on_natural_close(player2 -> get_module().menus.main_menu.create(player2).open(player2));

Expand Down Expand Up @@ -364,6 +362,11 @@ private void add_menu_item_setting(
2 * 9 + col,
null,
(player, menu, self) -> {
// Prevent toggling when the setting is forced by the server
if (setting.has_override()) {
return ClickResult.ERROR;
}

group.settings().put(setting, !group.get_setting(setting));
mark_persistent_storage_dirty();
menu.update();
Expand All @@ -372,10 +375,17 @@ private void add_menu_item_setting(
) {
@Override
public void item(final ItemStack item) {
final Consumer<List<Component>> maybe_add_forced_hint = (lore) -> {
if (setting.has_override()) {
lore.add(Component.empty());
lore.add(Component.text("FORCED BY SERVER"));
}
};

if (group.get_setting(setting)) {
super.item(item_setting_toggle_on.item());
super.item(item_setting_toggle_on.item_transform_lore(maybe_add_forced_hint));
} else {
super.item(item_setting_toggle_off.item());
super.item(item_setting_toggle_off.item_transform_lore(maybe_add_forced_hint));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.oddlama.vane.regions.menu;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand All @@ -24,6 +26,8 @@
import org.oddlama.vane.util.ItemUtil;
import org.oddlama.vane.util.StorageUtil;

import net.kyori.adventure.text.Component;

public class RoleMenu extends ModuleComponent<Regions> {

@LangMessage
Expand Down Expand Up @@ -370,6 +374,11 @@ private void add_menu_item_setting(
2 * 9 + col,
null,
(player, menu, self) -> {
// Prevent toggling when the setting is forced by the server
if (setting.has_override()) {
return ClickResult.ERROR;
}

if (setting == RoleSetting.ADMIN) {
// Admin setting is immutable
return ClickResult.ERROR;
Expand All @@ -383,10 +392,17 @@ private void add_menu_item_setting(
) {
@Override
public void item(final ItemStack item) {
final Consumer<List<Component>> maybe_add_forced_hint = (lore) -> {
if (setting.has_override()) {
lore.add(Component.empty());
lore.add(Component.text("FORCED BY SERVER"));
}
};

if (role.get_setting(setting)) {
super.item(item_setting_toggle_on.item());
super.item(item_setting_toggle_on.item_transform_lore(maybe_add_forced_hint));
} else {
super.item(item_setting_toggle_off.item());
super.item(item_setting_toggle_off.item_transform_lore(maybe_add_forced_hint));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.oddlama.vane.regions.region;

import org.oddlama.vane.regions.Regions;

public enum EnvironmentSetting {
// Spawning
ANIMALS(true),
Expand All @@ -23,4 +25,12 @@ private EnvironmentSetting(final boolean def) {
public boolean default_value() {
return def;
}

public boolean has_override() {
return get_override() != 0;
}

public int get_override() {
return Regions.environment_overrides.get_override(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public Map<EnvironmentSetting, Boolean> settings() {
}

public boolean get_setting(final EnvironmentSetting setting) {
if (setting.has_override()) {
return setting.get_override() == 1;
}
return settings.getOrDefault(setting, setting.default_value());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import org.oddlama.vane.regions.Regions;

public class Role {

Expand Down Expand Up @@ -86,6 +87,9 @@ public Map<RoleSetting, Boolean> settings() {
}

public boolean get_setting(final RoleSetting setting) {
if (setting.has_override()) {
return setting.get_override() == 1;
}
return settings.getOrDefault(setting, setting.default_value(false));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.oddlama.vane.regions.region;

import org.oddlama.vane.regions.Regions;

public enum RoleSetting {
ADMIN(false, true),
BUILD(false, true),
Expand All @@ -21,4 +23,12 @@ public boolean default_value(final boolean admin) {
}
return def;
}

public boolean has_override() {
return get_override() != 0;
}

public int get_override() {
return Regions.role_overrides.get_override(this);
}
}

0 comments on commit 556d01d

Please sign in to comment.