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

WIP - Refactored config handling + Added /sf reload #2822

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
58fe5ac
Some progress
TheBusyBiscuit Jan 30, 2021
36cceb8
Merge branch 'master' of https://github.com/Slimefun/Slimefun4.git into
TheBusyBiscuit Feb 12, 2021
dc77c95
Added /sf reload
TheBusyBiscuit Feb 12, 2021
e663251
Finished reload command
TheBusyBiscuit Feb 12, 2021
4a4e9a3
Merge branch 'master' of https://github.com/Slimefun/Slimefun4.git in…
TheBusyBiscuit Feb 13, 2021
3e2f250
Addded temporary redirection methods (deprecated)
TheBusyBiscuit Feb 13, 2021
ce83312
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Mar 10, 2021
629d38e
Merge branch 'master' of https://github.com/Slimefun/Slimefun4.git in…
TheBusyBiscuit Mar 13, 2021
776bfaa
Refactoring
TheBusyBiscuit Mar 13, 2021
807e5f7
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Mar 13, 2021
2a9c099
Fixed merge conflict
TheBusyBiscuit Mar 13, 2021
3b8ee0b
Updated reload command to support the new item settings method
TheBusyBiscuit Mar 13, 2021
15e09af
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Mar 14, 2021
847b230
Update CHANGELOG.md
TheBusyBiscuit Mar 14, 2021
c97410b
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Mar 19, 2021
218253f
Update src/main/java/io/github/thebusybiscuit/slimefun4/core/config/S…
TheBusyBiscuit Mar 19, 2021
bc1714d
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Mar 21, 2021
25d8460
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Mar 25, 2021
2137c26
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Mar 28, 2021
b8e1826
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Apr 9, 2021
22b7751
Merge branch 'master' into chore/config-rewrite
TheBusyBiscuit Apr 10, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* Added "2 magma blocks -> slime block" recipe to the Freezer
* Added configurable enchantment level limit for both auto enchanter and auto disenchanter
* (API) Added AutoEnchantEvent
* Added /sf reload

#### Changes
* Changed item order in guide for the Villager Rune and Nether Goo (All runes are now grouped together)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,24 @@ protected String getErrorMessage() {
* This method is called by a {@link SlimefunItem} which wants to load its {@link ItemSetting}
* from the {@link Config} file.
*
*
* @return Whether the value was successfully updated or misconfigured
*/
@SuppressWarnings("unchecked")
public void reload() {
public boolean reload() {
Validate.notNull(item, "Cannot apply settings for a non-existing SlimefunItem");

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

if (defaultValue.getClass().isInstance(configuredValue)) {
// 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 @@ -164,6 +168,7 @@ public void reload() {
"\n" + getErrorMessage()
);
// @formatter:on
return false;
}
} else {
this.value = defaultValue;
Expand All @@ -177,6 +182,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 @@ -27,6 +27,7 @@
import io.github.thebusybiscuit.cscorelib2.config.Config;
import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
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 @@ -64,19 +65,12 @@ public final class SlimefunRegistry {
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 backwardsCompatibility;
private boolean automaticallyLoadItems;
private boolean enableResearches;
private boolean freeCreativeResearches;
private boolean researchFireworks;
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 @@ -94,25 +88,18 @@ public final class SlimefunRegistry {
private final Map<Class<? extends ItemHandler>, Set<ItemHandler>> globalItemHandlers = new HashMap<>();
private final Map<String, SlimefunBlockHandler> blockHandlers = new HashMap<>();

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

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");
guides.put(SlimefunGuideMode.SURVIVAL_MODE, new SurvivalSlimefunGuide(showVanillaRecipes));
guides.put(SlimefunGuideMode.SURVIVAL_MODE, new SurvivalSlimefunGuide());
guides.put(SlimefunGuideMode.CHEAT_MODE, new CheatSheetSlimefunGuide());

Config cfg = SlimefunPlugin.getConfigManager().getPluginConfig();
researchRanks.addAll(cfg.getStringList("research-ranks"));

backwardsCompatibility = cfg.getBoolean("options.backwards-compatibility");
freeCreativeResearches = cfg.getBoolean("researches.free-in-creative-mode");
researchFireworks = cfg.getBoolean("researches.enable-fireworks");
logDuplicateBlockEntries = cfg.getBoolean("options.log-duplicate-block-entries");
talismanActionBarMessages = cfg.getBoolean("talismans.use-actionbar");
}

/**
Expand All @@ -127,29 +114,6 @@ public boolean isAutoLoadingEnabled() {
return automaticallyLoadItems;
}

/**
* This method returns whether backwards-compatibility is enabled.
* Backwards compatibility allows Slimefun to recognize items from older versions but comes
* at a huge performance cost.
*
* @return Whether backwards compatibility is enabled
*/
public boolean isBackwardsCompatible() {
return backwardsCompatibility;
}

/**
* This method sets the status of backwards compatibility.
* Backwards compatibility allows Slimefun to recognize items from older versions but comes
* at a huge performance cost.
*
* @param compatible
* Whether backwards compatibility should be enabled
*/
public void setBackwardsCompatible(boolean compatible) {
backwardsCompatibility = compatible;
}

/**
* This method will make any {@link SlimefunItem} which is registered automatically
* call {@link SlimefunItem#load()}.
Expand Down Expand Up @@ -220,26 +184,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;
}

/**
* This method returns a {@link List} of every enabled {@link MultiBlock}.
*
Expand Down Expand Up @@ -354,14 +298,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 @@ -377,4 +313,30 @@ 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 SlimefunPlugin.getConfigManager().isFreeCreativeResearchingEnabled();
}

/**
* This has been moved.
* Our metrics module accesses this though.
*
* @deprecated Please use {@link SlimefunConfigManager#isBackwardsCompatible()}
*
* @return backwards compat?
*/
@Deprecated
public boolean isBackwardsCompatible() {
return SlimefunPlugin.getConfigManager().isBackwardsCompatible();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ private void giveItem(CommandSender sender, Player p, SlimefunItem sfItem, Strin
if (amount > 0) {
SlimefunPlugin.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 CustomItem(sfItem.getItem(), amount));
if (SlimefunPlugin.getCfg().getBoolean("options.drop-excess-sf-give-items") && !excess.isEmpty()) {

// Check whether we should drop excess items to the ground
if (SlimefunPlugin.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,41 @@
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.SlimefunPlugin;

class ReloadCommand extends SubCommand {

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

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Nonnull;

import io.github.thebusybiscuit.slimefun4.core.commands.SlimefunCommand;
import io.github.thebusybiscuit.slimefun4.core.commands.SubCommand;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
Expand All @@ -20,7 +22,8 @@ public final class SlimefunSubCommands {

private SlimefunSubCommands() {}

public static Collection<SubCommand> getAllCommands(SlimefunCommand cmd) {
@Nonnull
public static Collection<SubCommand> getAllCommands(@Nonnull SlimefunCommand cmd) {
SlimefunPlugin plugin = cmd.getPlugin();
List<SubCommand> commands = new LinkedList<>();

Expand All @@ -38,6 +41,7 @@ public static Collection<SubCommand> getAllCommands(SlimefunCommand cmd) {
commands.add(new DebugFishCommand(plugin, cmd));
commands.add(new BackpackCommand(plugin, cmd));
commands.add(new ChargeCommand(plugin, cmd));
commands.add(new ReloadCommand(plugin, cmd));

return commands;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void onExecute(@Nonnull CommandSender sender, @Nonnull String[] args) {

addJavaVersion(builder);

if (SlimefunPlugin.getRegistry().isBackwardsCompatible()) {
if (SlimefunPlugin.getConfigManager().isBackwardsCompatible()) {
// @formatter:off
HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(
"Backwards compatibility has a negative impact on performance!\n" +
Expand Down
Loading