Skip to content

Commit

Permalink
Implements feature to disable Border per GameMode (#47)
Browse files Browse the repository at this point in the history
* Implements feature to disable Border per GameMode

Implements #39 as previously users were not able to disable it per gamemode. I have seen this was the way it was done in other addons, so I just implemented it here as well

* Remove reference to Challenges

Co-authored-by: tastybento <tastybento@users.noreply.github.com>
  • Loading branch information
wellnesscookie and tastybento committed Jan 3, 2021
1 parent cc95efe commit fb73ec6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
51 changes: 46 additions & 5 deletions src/main/java/world/bentobox/border/Border.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world.bentobox.border;

import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.border.commands.IslandBorderCommand;
import world.bentobox.border.listeners.PlayerBorder;
import world.bentobox.border.listeners.PlayerListener;
Expand All @@ -9,17 +10,40 @@ public final class Border extends Addon {

private final PlayerBorder playerBorder = new PlayerBorder(this);

private Settings settings;

private boolean hooked;

@Override
public void onLoad() {
// Save default config.yml
this.saveDefaultConfig();
// Load the plugin's config
this.loadSettings();
}

@Override
public void onEnable() {
// Register listeners
registerListener(new PlayerListener(this));
registerListener(playerBorder);


// Register commands
getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {
log("Border hooking into " + gameModeAddon.getDescription().getName());
gameModeAddon.getPlayerCommand().ifPresent(c -> new IslandBorderCommand(this, c, "border"));

if (!this.settings.getDisabledGameModes().contains(
gameModeAddon.getDescription().getName())) {

hooked = true;

log("Border hooking into " + gameModeAddon.getDescription().getName());
gameModeAddon.getPlayerCommand().ifPresent(c -> new IslandBorderCommand(this, c, "border"));
}
});

if (hooked) {
// Register listeners
registerListener(new PlayerListener(this));
registerListener(playerBorder);
}
}

@Override
Expand All @@ -34,4 +58,21 @@ public PlayerBorder getPlayerBorder() {
return playerBorder;
}

/**
* This method loads addon configuration settings in memory.
*/
private void loadSettings() {
this.settings = new Config<>(this, Settings.class).loadConfigObject();

if (this.settings == null) {
// Disable
this.logError("Challenges settings could not load! Addon disabled.");
this.setState(State.DISABLED);
}
}

public Settings getSettings() {
return this.settings;
}

}
37 changes: 37 additions & 0 deletions src/main/java/world/bentobox/border/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package world.bentobox.border;

import world.bentobox.bentobox.api.configuration.ConfigComment;
import world.bentobox.bentobox.api.configuration.ConfigEntry;
import world.bentobox.bentobox.api.configuration.ConfigObject;
import world.bentobox.bentobox.api.configuration.StoreAt;

import java.util.HashSet;
import java.util.Set;

@StoreAt(filename = "config.yml", path = "addons/Border")
public class Settings implements ConfigObject {

@ConfigComment("")
@ConfigComment("This list stores GameModes in which Border addon should not work.")
@ConfigComment("To disable addon it is necessary to write its name in new line that starts with -. Example:")
@ConfigComment("disabled-gamemodes:")
@ConfigComment(" - BSkyBlock")
@ConfigEntry(path = "disabled-gamemodes")
private Set<String> disabledGameModes = new HashSet<>();

/**
* @param disabledGameModes new disabledGameModes value.
*/
public void setDisabledGameModes(Set<String> disabledGameModes)
{
this.disabledGameModes = disabledGameModes;
}

/**
* @return disabledGameModes value.
*/
public Set<String> getDisabledGameModes()
{
return this.disabledGameModes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public void onVehicleMove(VehicleMoveEvent e) {
* @param island - island
*/
public void showBarrier(Player player, Island island) {

if (addon.getSettings().getDisabledGameModes().contains(island.getGameMode()))
return;

if (!User.getInstance(player).getMetaData(BORDER_STATE).map(MetaDataValue::asBoolean).orElse(false)) {
return;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# This list stores GameModes in which the addon should not work.
# To disable the addon it is necessary to write its name in new line that starts with -. Example:
# disabled-gamemodes:
# - BSkyBlock
disabled-gamemodes: []

0 comments on commit fb73ec6

Please sign in to comment.