Skip to content

Commit

Permalink
More options and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jul 1, 2024
1 parent 0706ad8 commit 0965e62
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 20 deletions.
11 changes: 5 additions & 6 deletions plugin/src/main/java/org/battleplugins/arena/BattleArena.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,6 @@ public <T extends Arena> void registerArena(Plugin plugin, String name, Class<T>
throw new RuntimeException("Failed to instantiate arena " + arenaClass.getName(), e);
}
});

if (plugin != this) {
this.info("Registered arena {} from plugin {}.", name, plugin.getName());

this.loadArenaLoaders(plugin.getDataFolder().toPath().resolve("arenas"));
}
}

/**
Expand All @@ -346,6 +340,11 @@ public <T extends Arena> void registerArena(Plugin plugin, String name, Class<T>
ArenaConfigParser.registerFactory(arenaClass, arenaFactory);

this.arenaTypes.put(name, new ArenaType(plugin, arenaClass));
if (plugin != this) {
this.info("Registered arena {} from plugin {}.", name, plugin.getName());

this.loadArenaLoaders(plugin.getDataFolder().toPath().resolve("arenas"));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ protected List<String> onVerifyTabComplete(String arg, Class<?> parameter) {
return super.onVerifyTabComplete(arg, parameter);
}

@Override
protected String onGetUsageString(Class<?> parameter) {
return switch (parameter.getSimpleName().toLowerCase()) {
case "arena" -> "<arena> ";
case "competition" -> "<map> "; // Best name for player-facing values
case "competitionmap" -> "<map> ";
default -> super.onGetUsageString(parameter);
};
}

@Override
public final void sendHeader(CommandSender sender) {
Messages.HEADER.sendCentered(sender, this.arena.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.battleplugins.arena.BattleArena;
import org.battleplugins.arena.config.ItemStackParser;
import org.battleplugins.arena.config.ParseException;
Expand Down Expand Up @@ -128,17 +129,15 @@ public final boolean onCommand(CommandSender sender, Command command, String lab
}

private void registerCommands() {
for (Method method : this.getClass().getDeclaredMethods()) {
if (method.isAnnotationPresent(ArenaCommand.class)) {
ArenaCommand arenaCommand = method.getAnnotation(ArenaCommand.class);
for (Method method : MethodUtils.getMethodsWithAnnotation(this.getClass(), ArenaCommand.class, true, true)) {
ArenaCommand arenaCommand = method.getAnnotation(ArenaCommand.class);

CommandWrapper wrapper = new CommandWrapper(this, method, this.getUsage(method));
for (String cmd : arenaCommand.commands()) {
Set<CommandWrapper> wrappers = this.commandMethods.getOrDefault(cmd, new HashSet<>());
wrappers.add(wrapper);
CommandWrapper wrapper = new CommandWrapper(this, method, this.getUsage(method));
for (String cmd : arenaCommand.commands()) {
Set<CommandWrapper> wrappers = this.commandMethods.getOrDefault(cmd, new HashSet<>());
wrappers.add(wrapper);

this.commandMethods.put(cmd, wrappers);
}
this.commandMethods.put(cmd, wrappers);
}
}
}
Expand Down Expand Up @@ -587,15 +586,18 @@ private String getUsageString(Class<?> parameter, @Nullable Argument argument) {
return "<" + argument.name() + "> ";
}

String usageString = this.onGetUsageString(parameter);
if (usageString != null) {
return usageString;
}

return switch (parameter.getSimpleName().toLowerCase()) {
case "string[]" -> "[string...] ";
case "int", "double", "float" -> "<number> ";
case "boolean" -> "<true|false> ";
case "material" -> "<material> ";
case "player", "offlineplayer" -> "<player> ";
case "world" -> "<world> ";
case "arena" -> "<arena> ";
case "competition" -> "<map> "; // Best name for player-facing values
default -> {
for (SubCommandExecutor subCommandExecutor : this.subCommandExecutors) {
String usage = subCommandExecutor.getUsageString(parameter);
Expand All @@ -619,6 +621,10 @@ private String getUsageString(Class<?> parameter, @Nullable Argument argument) {
};
}

protected String onGetUsageString(Class<?> parameter) {
return null;
}

@NotNull
@Override
public final List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;

class OptionsListener<T extends Competition<T>> implements ArenaListener, CompetitionLike<T> {
Expand All @@ -28,6 +29,10 @@ public void onBlockBreak(BlockBreakEvent event) {
if (!this.competition.option(ArenaOptionType.BLOCK_BREAK).map(BooleanArenaOption::isEnabled).orElse(true)) {
event.setCancelled(true);
}

if (!this.competition.option(ArenaOptionType.BLOCK_DROPS).map(BooleanArenaOption::isEnabled).orElse(true)) {
event.setDropItems(false);
}
}

@ArenaEventHandler(priority = EventPriority.LOWEST)
Expand All @@ -37,6 +42,13 @@ public void onBlockPlace(BlockPlaceEvent event) {
}
}

@ArenaEventHandler(priority = EventPriority.LOWEST)
public void onDropItem(PlayerDropItemEvent event) {
if (!this.competition.option(ArenaOptionType.ITEM_DROPS).map(BooleanArenaOption::isEnabled).orElse(true)) {
event.setCancelled(true);
}
}

@ArenaEventHandler(priority = EventPriority.LOWEST)
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;

import java.time.Duration;
import java.util.HashSet;
import java.util.Set;

public class VictoryPhase<T extends LiveCompetition<T>> extends LiveCompetitionPhase<T> {
@ArenaOption(name = "duration", description = "The number of seconds to remain in the victory condition.", required = true)
private int duration;
private Duration duration;

private BukkitTask durationTask;

Expand All @@ -31,7 +32,7 @@ public void onStart() {
this.durationTask = Bukkit.getScheduler().runTaskLater(
this.competition.getArena().getPlugin(),
this::advanceToNextPhase,
this.duration * 20L
this.duration.toMillis() / 50
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -233,7 +234,9 @@ private static void populateType(@Nullable Path sourceFile, Field field, ArenaOp
// Unknown object! Let's try to parse it
ConfigurationSection configurationSection = configuration.getConfigurationSection(name);
if (configurationSection == null) {
if (configuration.contains(name)) {
if (configuration.get(name) instanceof Map<?, ?> map) {
configurationSection = toMemorySection((Map<String, Object>) map);
} else if (configuration.contains(name)) {
throw new ParseException("Invalid object " + name + " in configuration section " + configuration.getName())
.cause(ParseException.Cause.INVALID_TYPE)
.context("Option name", arenaOption.name())
Expand Down Expand Up @@ -514,6 +517,12 @@ private static List<ConfigurationSection> toMemorySections(List<?> list) {
return sections;
}

private static ConfigurationSection toMemorySection(Map<String, Object> map) {
MemoryConfiguration memoryConfig = new MemoryConfiguration();
memoryConfig.addDefaults(map);
return memoryConfig;
}

public interface Parser<T> {
T parse(Object object) throws ParseException;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public final class ArenaEditorWizards {
Spawns spawns = new Spawns(ctx.getWaitroomSpawn(), ctx.getSpectatorSpawn(), teamSpawns);

LiveCompetitionMap map = ctx.getArena().getMapFactory().create(ctx.getMapName(), ctx.getArena(), ctx.getMapType(), ctx.getPlayer().getWorld().getName(), bounds, spawns);
map.postProcess(); // Call post process to ensure all data is loaded

BattleArena.getInstance().addArenaMap(ctx.getArena(), map);

// If our competition is a match, create it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public void registerEvents(ArenaListener listener) {
if (ArenaPlayerEvent.class.isAssignableFrom(eventClass)) {
try {
method.invoke(eventListener, event, ((ArenaPlayerEvent) event).getArenaPlayer());
return;
} catch (Exception e) {
throw new EventException(e, "Error executing ArenaPlayerEvent: " + eventClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public final class ArenaOptionType<T extends ArenaOption> {

public static final ArenaOptionType<BooleanArenaOption> BLOCK_BREAK = new ArenaOptionType<>("block-break", BooleanArenaOption::new);
public static final ArenaOptionType<BooleanArenaOption> BLOCK_PLACE = new ArenaOptionType<>("block-place", BooleanArenaOption::new);
public static final ArenaOptionType<BooleanArenaOption> BLOCK_DROPS = new ArenaOptionType<>("block-drops", BooleanArenaOption::new);
public static final ArenaOptionType<BooleanArenaOption> BLOCK_INTERACT = new ArenaOptionType<>("block-interact", BooleanArenaOption::new);
public static final ArenaOptionType<BooleanArenaOption> ITEM_DROPS = new ArenaOptionType<>("item-drops", BooleanArenaOption::new);
public static final ArenaOptionType<BooleanArenaOption> KEEP_INVENTORY = new ArenaOptionType<>("keep-inventory", BooleanArenaOption::new);
public static final ArenaOptionType<BooleanArenaOption> KEEP_EXPERIENCE = new ArenaOptionType<>("keep-experience", BooleanArenaOption::new);

Expand Down

0 comments on commit 0965e62

Please sign in to comment.