Resolve arenas by name using slugs.

This commit is a minor refactor of how arenas are resolved by name,
similar to the previous arena class resolution refactoring.

The difference this time around is that there is an ArenaMaster method
that does most of the work for a lot of different areas of the plugin,
`getArenaWithName(String)`. This method is called from well over 30
different places in the code base, making it a cornerstone of all arena
resolution. Luckily, it is called primarily from places that shouldn't
care _how_ an arena is resolved.

Affected by this commit are all commands that resolve arenas by name,
including all those not listed in the diff, because of the change to
`getArenaWithName(String)` on ArenaMaster.

Commands that can tab complete arena name arguments now complete slugs
instead of config names.
This commit is contained in:
Andreas Troelsen
2020-11-03 19:58:30 +01:00
parent fdb84dfaf4
commit 0f93d8ac05
11 changed files with 22 additions and 20 deletions
@@ -8,6 +8,7 @@ import com.garbagemule.MobArena.framework.ArenaMaster;
import com.garbagemule.MobArena.things.InvalidThingInputString;
import com.garbagemule.MobArena.things.Thing;
import com.garbagemule.MobArena.util.JoinInterruptTimer;
import com.garbagemule.MobArena.util.Slugs;
import com.garbagemule.MobArena.util.config.ConfigUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -232,8 +233,9 @@ public class ArenaMasterImpl implements ArenaMaster
}
public Arena getArenaWithName(Collection<Arena> arenas, String configName) {
String slug = Slugs.create(configName);
for (Arena arena : arenas)
if (arena.configName().equalsIgnoreCase(configName))
if (arena.getSlug().equalsIgnoreCase(slug))
return arena;
return null;
}
@@ -67,8 +67,8 @@ public class DisableCommand implements Command
List<Arena> arenas = am.getArenas();
return arenas.stream()
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.map(Arena::configName)
.filter(arena -> arena.getSlug().startsWith(prefix))
.map(Arena::getSlug)
.collect(Collectors.toList());
}
}
@@ -67,8 +67,8 @@ public class EnableCommand implements Command
List<Arena> arenas = am.getArenas();
return arenas.stream()
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.map(Arena::configName)
.filter(arena -> arena.getSlug().startsWith(prefix))
.map(Arena::getSlug)
.collect(Collectors.toList());
}
}
@@ -117,9 +117,9 @@ public class ForceCommand implements Command
List<Arena> arenas = am.getArenas();
return arenas.stream()
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.filter(arena -> arena.getSlug().startsWith(prefix))
.filter(arena -> start != arena.isRunning())
.map(Arena::configName)
.map(Arena::getSlug)
.collect(Collectors.toList());
}
}
@@ -69,8 +69,8 @@ public class EditArenaCommand implements Command
List<Arena> arenas = am.getArenas();
return arenas.stream()
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.map(Arena::configName)
.filter(arena -> arena.getSlug().startsWith(prefix))
.map(Arena::getSlug)
.collect(Collectors.toList());
}
}
@@ -51,8 +51,8 @@ public class RemoveArenaCommand implements Command
List<Arena> arenas = am.getArenas();
return arenas.stream()
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.map(Arena::configName)
.filter(arena -> arena.getSlug().startsWith(prefix))
.map(Arena::getSlug)
.collect(Collectors.toList());
}
}
@@ -120,9 +120,9 @@ public class SettingCommand implements Command {
String prefix = args[0].toLowerCase();
return arenas.stream()
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.filter(arena -> arena.getSlug().startsWith(prefix))
.filter(arena -> !arena.isRunning())
.map(Arena::configName)
.map(Arena::getSlug)
.collect(Collectors.toList());
}
@@ -101,8 +101,8 @@ public class SetupCommand implements Command, Listener {
List<Arena> arenas = am.getArenas();
return arenas.stream()
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.map(Arena::configName)
.filter(arena -> arena.getSlug().startsWith(prefix))
.map(Arena::getSlug)
.collect(Collectors.toList());
}
@@ -94,9 +94,9 @@ public class JoinCommand implements Command
return arenas.stream()
.filter(Arena::isEnabled)
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.filter(arena -> arena.getSlug().startsWith(prefix))
.filter(arena -> arena.getRegion().isSetup())
.map(Arena::configName)
.map(Arena::getSlug)
.collect(Collectors.toList());
}
}
@@ -88,9 +88,9 @@ public class SpecCommand implements Command
return arenas.stream()
.filter(Arena::isEnabled)
.filter(arena -> arena.configName().toLowerCase().startsWith(prefix))
.filter(arena -> arena.getSlug().startsWith(prefix))
.filter(arena -> arena.getRegion().isSetup())
.map(Arena::configName)
.map(Arena::getSlug)
.collect(Collectors.toList());
}
}