Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor config handling + added /sf reload #3908

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import com.google.common.base.Preconditions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import com.google.common.base.Preconditions;
import com.google.common.base.Preconditions;

import org.apache.commons.lang.Validate;

import io.github.bakedlibs.dough.config.Config;
Expand Down Expand Up @@ -163,18 +164,20 @@ public boolean isType(@Nonnull Class<?> c) {
*
*/
@SuppressWarnings("unchecked")
public void reload() {
Validate.notNull(item, "Cannot apply settings for a non-existing SlimefunItem");
public boolean reload() {
Preconditions.checkNotNull(item, "Cannot apply settings for a non-existing SlimefunItem");

Slimefun.getItemCfg().setDefaultValue(item.getId() + '.' + getKey(), getDefaultValue());
Object configuredValue = Slimefun.getItemCfg().getValue(item.getId() + '.' + getKey());
Config config = Slimefun.getConfigManager().getItemsConfig();
config.setDefaultValue(item.getId() + '.' + getKey(), getDefaultValue());
Object configuredValue = config.getValue(item.getId() + '.' + getKey());

if (defaultValue.getClass().isInstance(configuredValue) || (configuredValue instanceof List && defaultValue instanceof List)) {
// We can do an unsafe cast here, we did an isInstance(...) check before!
T newValue = (T) configuredValue;

if (validateInput(newValue)) {
this.value = newValue;
return true;
} else {
// @formatter:off
item.warn(
Expand All @@ -184,6 +187,7 @@ public void reload() {
"\n" + getErrorMessage()
);
// @formatter:on
return false;
}
} else {
this.value = defaultValue;
Expand All @@ -197,6 +201,7 @@ public void reload() {
"\n Expected \"" + defaultValue.getClass().getSimpleName() + "\" but found: \"" + found + "\""
);
// @formatter:on
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.bakedlibs.dough.config.Config;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import order

import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.World;
Expand Down Expand Up @@ -432,21 +433,23 @@ public void register(@Nonnull SlimefunAddon addon) {
Slimefun.getRegistry().getAllSlimefunItems().add(this);
Slimefun.getRegistry().getSlimefunItemIds().put(id, this);

Config config = Slimefun.getConfigManager().getItemsConfig();

// Items that are "not-configurable" cannot be configured.
if (!(this instanceof NotConfigurable)) {
Slimefun.getItemCfg().setDefaultValue(id + ".enabled", true);
Slimefun.getItemCfg().setDefaultValue(id + ".can-be-used-in-workbenches", useableInWorkbench);
Slimefun.getItemCfg().setDefaultValue(id + ".hide-in-guide", hidden);
Slimefun.getItemCfg().setDefaultValue(id + ".allow-enchanting", enchantable);
Slimefun.getItemCfg().setDefaultValue(id + ".allow-disenchanting", disenchantable);
config.setDefaultValue(id + ".enabled", true);
config.setDefaultValue(id + ".can-be-used-in-workbenches", useableInWorkbench);
config.setDefaultValue(id + ".hide-in-guide", hidden);
config.setDefaultValue(id + ".allow-enchanting", enchantable);
config.setDefaultValue(id + ".allow-disenchanting", disenchantable);

// Load all item settings
for (ItemSetting<?> setting : itemSettings) {
setting.reload();
}
}

if (ticking && !Slimefun.getCfg().getBoolean("URID.enable-tickers")) {
if (ticking && !Slimefun.getConfigManager().getPluginConfig().getBoolean("URID.enable-tickers")) {
state = ItemState.DISABLED;
return;
}
Expand All @@ -457,13 +460,13 @@ public void register(@Nonnull SlimefunAddon addon) {
* Any other settings will remain as default.
*/
state = ItemState.ENABLED;
} else if (Slimefun.getItemCfg().getBoolean(id + ".enabled")) {
} else if (config.getBoolean(id + ".enabled")) {
// The item has been enabled.
state = ItemState.ENABLED;
useableInWorkbench = Slimefun.getItemCfg().getBoolean(id + ".can-be-used-in-workbenches");
hidden = Slimefun.getItemCfg().getBoolean(id + ".hide-in-guide");
enchantable = Slimefun.getItemCfg().getBoolean(id + ".allow-enchanting");
disenchantable = Slimefun.getItemCfg().getBoolean(id + ".allow-disenchanting");
useableInWorkbench = config.getBoolean(id + ".can-be-used-in-workbenches");
hidden = config.getBoolean(id + ".hide-in-guide");
enchantable = config.getBoolean(id + ".allow-enchanting");
disenchantable = config.getBoolean(id + ".allow-disenchanting");
} else if (this instanceof VanillaItem) {
// This item is a vanilla "mock" but was disabled.
state = ItemState.VANILLA_FALLBACK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void unlockResearch(@Nonnull Player p, @Nonnull PlayerProfile profile) {
onFinish(p);

// Check if the Server and the Player have enabled fireworks for researches
if (Slimefun.getRegistry().isResearchFireworkEnabled() && SlimefunGuideSettings.hasFireworksEnabled(p)) {
if (Slimefun.getConfigManager().isResearchFireworkEnabled() && SlimefunGuideSettings.hasFireworksEnabled(p)) {
FireworkUtils.launchRandom(p, 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.bakedlibs.dough.config.Config;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import order

import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -89,7 +90,7 @@ public Research(@Nonnull NamespacedKey key, int id, @Nonnull String defaultName,
* @return Whether this {@link Research} is enabled or not
*/
public boolean isEnabled() {
return Slimefun.getRegistry().isResearchingEnabled() && enabled;
return Slimefun.getConfigManager().isResearchingEnabled() && enabled;
}

/**
Expand Down Expand Up @@ -248,7 +249,7 @@ public boolean canUnlock(@Nonnull Player p) {
return true;
}

boolean creativeResearch = p.getGameMode() == GameMode.CREATIVE && Slimefun.getRegistry().isFreeCreativeResearchingEnabled();
boolean creativeResearch = p.getGameMode() == GameMode.CREATIVE && Slimefun.getConfigManager().isFreeCreativeResearchingEnabled();
return creativeResearch || p.getLevel() >= cost;
}

Expand Down Expand Up @@ -282,10 +283,11 @@ public void unlock(@Nonnull Player p, boolean isInstant, @Nullable Consumer<Play
* Registers this {@link Research}.
*/
public void register() {
Slimefun.getResearchCfg().setDefaultValue("enable-researching", true);
Config pluginConfig = Slimefun.getConfigManager().getPluginConfig();
pluginConfig.setDefaultValue("enable-researching", true);
String path = key.getNamespace() + '.' + key.getKey();

if (Slimefun.getResearchCfg().contains(path + ".enabled") && !Slimefun.getResearchCfg().getBoolean(path + ".enabled")) {
if (pluginConfig.contains(path + ".enabled") && !pluginConfig.getBoolean(path + ".enabled")) {
for (SlimefunItem item : new ArrayList<>(items)) {
if (item != null) {
item.setResearch(null);
Expand All @@ -296,10 +298,10 @@ public void register() {
return;
}

Slimefun.getResearchCfg().setDefaultValue(path + ".cost", getCost());
Slimefun.getResearchCfg().setDefaultValue(path + ".enabled", true);
pluginConfig.setDefaultValue(path + ".cost", getCost());
pluginConfig.setDefaultValue(path + ".enabled", true);

setCost(Slimefun.getResearchCfg().getInt(path + ".cost"));
setCost(pluginConfig.getInt(path + ".cost"));
enabled = true;

Slimefun.getRegistry().getResearches().add(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.collections.KeyMap;
import io.github.bakedlibs.dough.config.Config;
import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.ItemHandler;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.core.config.SlimefunConfigManager;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideMode;
Expand Down Expand Up @@ -63,20 +63,12 @@ public final class SlimefunRegistry {
private final List<Research> researches = new LinkedList<>();
private final List<String> researchRanks = new ArrayList<>();
private final Set<UUID> researchingPlayers = Collections.synchronizedSet(new HashSet<>());

// TODO: Move this all into a proper "config cache" class
private boolean automaticallyLoadItems;
private boolean enableResearches;
private boolean freeCreativeResearches;
private boolean researchFireworks;
private boolean disableLearningAnimation;
private boolean logDuplicateBlockEntries;
private boolean talismanActionBarMessages;

private final Set<String> tickers = new HashSet<>();
private final Set<SlimefunItem> radioactive = new HashSet<>();
private final Set<ItemStack> barterDrops = new HashSet<>();

private boolean automaticallyLoadItems;

private NamespacedKey soulboundKey;
private NamespacedKey itemChargeKey;
private NamespacedKey guideKey;
Expand All @@ -93,26 +85,18 @@ public final class SlimefunRegistry {
private final Map<String, UniversalBlockMenu> universalInventories = new HashMap<>();
private final Map<Class<? extends ItemHandler>, Set<ItemHandler>> globalItemHandlers = new HashMap<>();

public void load(@Nonnull Slimefun plugin, @Nonnull Config cfg) {
public void load(@Nonnull Slimefun plugin) {
Validate.notNull(plugin, "The Plugin cannot be null!");
Validate.notNull(cfg, "The Config cannot be null!");
plugin.getJavaPlugin();

soulboundKey = new NamespacedKey(plugin, "soulbound");
itemChargeKey = new NamespacedKey(plugin, "item_charge");
guideKey = new NamespacedKey(plugin, "slimefun_guide_mode");

boolean showVanillaRecipes = cfg.getBoolean("guide.show-vanilla-recipes");
boolean showHiddenItemGroupsInSearch = cfg.getBoolean("guide.show-hidden-item-groups-in-search");
guides.put(SlimefunGuideMode.SURVIVAL_MODE, new SurvivalSlimefunGuide(showVanillaRecipes, showHiddenItemGroupsInSearch));
guides.put(SlimefunGuideMode.SURVIVAL_MODE, new SurvivalSlimefunGuide());
guides.put(SlimefunGuideMode.CHEAT_MODE, new CheatSheetSlimefunGuide());

researchRanks.addAll(cfg.getStringList("research-ranks"));

freeCreativeResearches = cfg.getBoolean("researches.free-in-creative-mode");
researchFireworks = cfg.getBoolean("researches.enable-fireworks");
disableLearningAnimation = cfg.getBoolean("researches.disable-learning-animation");
logDuplicateBlockEntries = cfg.getBoolean("options.log-duplicate-block-entries");
talismanActionBarMessages = cfg.getBoolean("talismans.use-actionbar");
researchRanks.addAll(Slimefun.getConfigManager().getPluginConfig().getStringList("research-ranks"));
}

/**
Expand Down Expand Up @@ -195,34 +179,6 @@ public List<String> getResearchRanks() {
return researchRanks;
}

public void setResearchingEnabled(boolean enabled) {
enableResearches = enabled;
}

public boolean isResearchingEnabled() {
return enableResearches;
}

public void setFreeCreativeResearchingEnabled(boolean enabled) {
freeCreativeResearches = enabled;
}

public boolean isFreeCreativeResearchingEnabled() {
return freeCreativeResearches;
}

public boolean isResearchFireworkEnabled() {
return researchFireworks;
}

/**
* Returns whether the research learning animations is disabled
*
* @return Whether the research learning animations is disabled
*/
public boolean isLearningAnimationDisabled() {
return disableLearningAnimation;
}

/**
* This method returns a {@link List} of every enabled {@link MultiBlock}.
Expand Down Expand Up @@ -339,14 +295,6 @@ public KeyMap<GEOResource> getGEOResources() {
return geoResources;
}

public boolean logDuplicateBlockEntries() {
return logDuplicateBlockEntries;
}

public boolean useActionbarForTalismans() {
return talismanActionBarMessages;
}

@Nonnull
public NamespacedKey getSoulboundDataKey() {
return soulboundKey;
Expand All @@ -362,4 +310,16 @@ public NamespacedKey getGuideDataKey() {
return guideKey;
}

/**
* This has been moved.
* Our metrics module accesses this though.
*
* @deprecated Please use {@link SlimefunConfigManager#isFreeCreativeResearchingEnabled()}
*
* @return free research in creative?
*/
@Deprecated
public boolean isFreeCreativeResearchingEnabled() {
return Slimefun.getConfigManager().isFreeCreativeResearchingEnabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ private void giveItem(CommandSender sender, Player p, SlimefunItem sfItem, Strin
if (amount > 0) {
Slimefun.getLocalization().sendMessage(p, "messages.given-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount)));
Map<Integer, ItemStack> excess = p.getInventory().addItem(new CustomItemStack(sfItem.getItem(), amount));
if (Slimefun.getCfg().getBoolean("options.drop-excess-sf-give-items") && !excess.isEmpty()) {

// Check whether we should drop excess items to the ground
if (Slimefun.getConfigManager().isExcessCommandItemsDroppingEnabled() && !excess.isEmpty()) {
for (ItemStack is : excess.values()) {
p.getWorld().dropItem(p.getLocation(), is);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.thebusybiscuit.slimefun4.core.commands.subcommands;

import org.bukkit.command.CommandSender;

import io.github.thebusybiscuit.slimefun4.core.commands.SlimefunCommand;
import io.github.thebusybiscuit.slimefun4.core.commands.SubCommand;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

import javax.annotation.Nonnull;

class ReloadCommand extends SubCommand {

ReloadCommand(Slimefun plugin, SlimefunCommand cmd) {
super(plugin, cmd, "reload", false);
}

@Override
protected @Nonnull String getDescription() {
return "commands.reload.description";
}

@Override
public void onExecute(CommandSender sender, @Nonnull String[] args) {
if (sender.hasPermission("slimefun.command.reload")) {
Slimefun.getLocalization().sendMessage(sender, "guide.work-in-progress", true);
boolean reloaded = Slimefun.getConfigManager().reload();
boolean isRestartRequired = Slimefun.getConfigManager(). isRestartRequired();

if (reloaded) {
if (isRestartRequired) {
Slimefun.getLocalization().sendMessage(sender, "commands.reload.restart-required", true);
} else {
Slimefun.getLocalization().sendMessage(sender, "commands.reload.success", true);
}
} else {
Slimefun.getLocalization().sendMessage(sender, "commands.reload.errors", true);
}
} else {
Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected String getDescription() {
@Override
public void onExecute(CommandSender sender, String[] args) {
// Check if researching is even enabled
if (!Slimefun.getRegistry().isResearchingEnabled()) {
if (!Slimefun.getConfigManager().isResearchingEnabled()) {
Slimefun.getLocalization().sendMessage(sender, "messages.researching-is-disabled");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static Collection<SubCommand> getAllCommands(@Nonnull SlimefunCommand cmd
commands.add(new BackpackCommand(plugin, cmd));
commands.add(new ChargeCommand(plugin, cmd));
commands.add(new DebugCommand(plugin, cmd));
commands.add(new ReloadCommand(plugin, cmd));

return commands;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StatsCommand extends SubCommand {
@Override
public void onExecute(CommandSender sender, String[] args) {
// Check if researching is even enabled
if (!Slimefun.getRegistry().isResearchingEnabled()) {
if (!Slimefun.getConfigManager().isResearchingEnabled()) {
Slimefun.getLocalization().sendMessage(sender, "messages.researching-is-disabled");
return;
}
Expand Down
Loading
Loading