Skip to content

Commit

Permalink
Add support for customising flags (#273)
Browse files Browse the repository at this point in the history
* Add ability to customize flags from config

* Update Config-Files.md with new flags.yml

* Add API methods for flags, tweak method names

* Add API methods for flags, tweak method names

* docs: Add note about flags.yml to Legacy Migration
  • Loading branch information
WiIIiam278 authored Jun 25, 2023
1 parent c4a9394 commit 3b92ad7
Show file tree
Hide file tree
Showing 19 changed files with 583 additions and 180 deletions.
12 changes: 12 additions & 0 deletions bukkit/src/main/java/net/william278/husktowns/BukkitHuskTowns.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class BukkitHuskTowns extends JavaPlugin implements HuskTowns, PluginMess
private Locales locales;
private Roles roles;
private Presets presets;
private Flags flags;
private Levels levels;
private Server server;
private Database database;
Expand Down Expand Up @@ -239,6 +240,17 @@ public void setRulePresets(@NotNull Presets presets) {
this.presets = presets;
}

@Override
@NotNull
public Flags getFlags() {
return flags;
}

@Override
public void setFlags(@NotNull Flags flags) {
this.flags = flags;
}

@Override
@NotNull
public Levels getLevels() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ public ContextSet estimatePotentialContexts() {

private void setContextsFromRules(@NotNull ContextConsumer consumer, Rules wilderness) {
consumer.accept(ContextKey.CAN_PLAYER_BUILD.getKey(plugin), wilderness
.cancelOperation(Operation.Type.BLOCK_BREAK) ? "false" : "true");
.cancelOperation(Operation.Type.BLOCK_BREAK, plugin().getFlags()) ? "false" : "true");
consumer.accept(ContextKey.CAN_PLAYER_OPEN_CONTAINERS.getKey(plugin), wilderness
.cancelOperation(Operation.Type.CONTAINER_OPEN) ? "false" : "true");
.cancelOperation(Operation.Type.CONTAINER_OPEN, plugin().getFlags()) ? "false" : "true");
consumer.accept(ContextKey.CAN_PLAYER_INTERACT.getKey(plugin), wilderness
.cancelOperation(Operation.Type.BLOCK_INTERACT) ? "false" : "true");
.cancelOperation(Operation.Type.BLOCK_INTERACT, plugin().getFlags()) ? "false" : "true");
}
}

Expand Down
8 changes: 7 additions & 1 deletion common/src/main/java/net/william278/husktowns/HuskTowns.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public interface HuskTowns extends TaskRunner, EventDispatcher, AdvancementTrack

void setRulePresets(@NotNull Presets presets);

@NotNull
Flags getFlags();

void setFlags(@NotNull Flags flags);

@NotNull
Levels getLevels();

Expand Down Expand Up @@ -194,7 +199,7 @@ default Optional<Member> getUserTown(@NotNull User user) throws IllegalStateExce
final int weight = town.getMembers().get(user.getUuid());
return Optional.of(getRoles().fromWeight(weight)
.map(role -> new Member(user, town, role))
.orElseThrow(() -> new IllegalStateException("No role found for weight " + weight)));
.orElseThrow(() -> new IllegalStateException("No role found for weight \"" + weight + "\"")));
});
}

Expand Down Expand Up @@ -319,6 +324,7 @@ default void loadConfig() throws RuntimeException {
setSettings(Annotaml.create(new File(getDataFolder(), "config.yml"), Settings.class).get());
setRoles(Annotaml.create(new File(getDataFolder(), "roles.yml"), Roles.class).get());
setRulePresets(Annotaml.create(new File(getDataFolder(), "rules.yml"), Presets.class).get());
setFlags(Annotaml.create(new File(getDataFolder(), "flags.yml"), Flags.class).get());
setLevels(Annotaml.create(new File(getDataFolder(), "levels.yml"), new Levels()).get());
setLocales(Annotaml.create(new File(getDataFolder(), "messages-" + getSettings().getLanguage() + ".yml"),
Annotaml.create(Locales.class, getResource("locales/" + getSettings().getLanguage() + ".yml")).get()).get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import net.william278.husktowns.user.SavedUser;
import net.william278.husktowns.user.User;
import net.william278.husktowns.util.Validator;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
Expand All @@ -49,6 +48,7 @@ public interface IHuskTownsAPI {
*
* @return The {@link HuskTowns} instance
*/
@ApiStatus.Internal
@NotNull
HuskTowns getPlugin();

Expand Down Expand Up @@ -83,11 +83,47 @@ default void setAdvancements(@NotNull Advancement advancements) throws IllegalSt
* Get the Town {@link Advancement Advancements} used by the plugin.
*
* @return The root {@link Advancement} used by the plugin, if advancements are enabled.
* @since 2.4
*/
default Optional<Advancement> getAdvancements() {
return getPlugin().getAdvancements();
}

/**
* Get the set of {@link Flag}s in use by the plugin.
*
* @return The set of {@link Flag}s currently in use
* @since 2.5
*/
@Unmodifiable
default Set<Flag> getFlagSet() {
return getPlugin().getFlags().getFlagSet();
}

/**
* Get the {@link Flag} with the given name.
*
* @param name The name of the {@link Flag} to get
* @return The {@link Flag} with the given name, if it exists
* @since 2.5
*/
default Optional<Flag> getFlag(@NotNull String name) {
return getPlugin().getFlags().getFlag(name);
}

/**
* Register one or more {@link Flag}s to be used by the plugin.
*
* @param flags The {@link Flag}s to register
* @since 2.5
*/
default void registerFlags(@NotNull Flag... flags) {
final Set<Flag> flagSet = new LinkedHashSet<>(getFlagSet());
flagSet.addAll(Arrays.asList(flags));
getPlugin().getFlags().setFlags(flagSet);
}


/**
* Get a list of {@link ClaimWorld}s on this server. If you want to get the {@link ClaimWorld}s on every server,
* see {@link #getAllClaimWorlds()}.
Expand Down Expand Up @@ -304,7 +340,7 @@ default void createAdminClaimAt(@NotNull OnlineUser actor, @NotNull Position pos
*/
default void deleteClaimAt(@NotNull OnlineUser actor, @NotNull Chunk chunk, @NotNull World world) throws IllegalArgumentException {
final TownClaim townClaim = getClaimAt(chunk, world)
.orElseThrow(() -> new IllegalArgumentException("No claim exists at " + chunk));
.orElseThrow(() -> new IllegalArgumentException("No claim exists at: " + chunk));
getPlugin().runAsync(() -> getPlugin().getManager().claims().deleteClaimData(actor, townClaim, world));
}

Expand All @@ -330,7 +366,7 @@ default void deleteClaimAt(@NotNull OnlineUser actor, @NotNull Position position
*/
default void updateClaim(@NotNull TownClaim claim, @NotNull World world) throws IllegalArgumentException {
final ClaimWorld claimWorld = getClaimWorld(world)
.orElseThrow(() -> new IllegalArgumentException(world + " is not claimable"));
.orElseThrow(() -> new IllegalArgumentException("World \"" + world.getName() + "\" is not claimable"));
getPlugin().runAsync(() -> {
if (claim.isAdminClaim(getPlugin())) {
return;
Expand Down
Loading

0 comments on commit 3b92ad7

Please sign in to comment.