Skip to content

Commit

Permalink
Add permission option to equip classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jul 13, 2024
1 parent 6561ca9 commit f8bdfba
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class ArenaClass implements PostProcessable {

@Scoped
private ArenaClasses classes;
private ClassesConfig classes;

@ArenaOption(name = "name", description = "The name of the class.", required = true)
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Classes implements ArenaModuleInitializer {
public static final ArenaOptionType<BooleanArenaOption> CLASS_EQUIPPING_OPTION = ArenaOptionType.create("class-equipping", BooleanArenaOption::new);
public static final ArenaOptionType<BooleanArenaOption> CLASS_EQUIP_ONLY_SELECTS_OPTION = ArenaOptionType.create("class-equip-only-selects", BooleanArenaOption::new);

private ArenaClasses classes;
private ClassesConfig classes;

@EventHandler
public void onPostInitialize(BattleArenaPostInitializeEvent event) {
Expand All @@ -54,7 +54,7 @@ public void onPostInitialize(BattleArenaPostInitializeEvent event) {

Configuration classesConfig = YamlConfiguration.loadConfiguration(classesPath.toFile());
try {
this.classes = ArenaConfigParser.newInstance(classesPath, ArenaClasses.class, classesConfig, event.getBattleArena());
this.classes = ArenaConfigParser.newInstance(classesPath, ClassesConfig.class, classesConfig, event.getBattleArena());
} catch (ParseException e) {
ParseException.handle(e);

Expand All @@ -76,6 +76,10 @@ public ArenaClass getClass(String name) {
return this.classes.getClasses().get(name);
}

public boolean isRequirePermission() {
return this.classes.isRequirePermission();
}

public Map<String, ArenaClass> getClasses() {
return this.classes.getClasses();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
import java.util.Map;

@DocumentationSource("https://docs.battleplugins.org/books/user-guide/page/classes")
public class ArenaClasses {
public class ClassesConfig {
@Scoped
BattleArena plugin;

@ArenaOption(name = "require-permission", description = "Whether players need specific permission nodes in order to equip classes.", required = true)
private boolean requirePermission;

@ArenaOption(name = "classes", description = "The classes that will be added to the arena.")
private Map<String, ArenaClass> classes;

public boolean isRequirePermission() {
return this.requirePermission;
}

public Map<String, ArenaClass> getClasses() {
return this.classes;
return this.classes == null ? Map.of() : Map.copyOf(this.classes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public void equip(Player player, @Argument(name = "class") ArenaClass arenaClass
return;
}

if (this.module.isRequirePermission()) {
if (!player.hasPermission("battlearena.classes.equip." + arenaClass.getName().toLowerCase())) {
Messages.NO_PERMISSION.send(player);
return;
}
}

// Ensure that the current competition phase allows class equipping
boolean canEquip = arenaPlayer.getCompetition().option(Classes.CLASS_EQUIPPING_OPTION)
.map(BooleanArenaOption::isEnabled)
Expand Down
6 changes: 6 additions & 0 deletions module/classes/src/main/resources/classes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
# -----------------
config-version: 1.0 # The config version, do not change!

# Whether players need specific permission nodes in order to
# equip classes. If set to true, players will need the permission
# battlearena.classes.equip.<class-name> to equip the class.
require-permission: true

# Configuration for classes
classes:
warrior:
name: Warrior
Expand Down

0 comments on commit f8bdfba

Please sign in to comment.