Skip to content

Commit

Permalink
implementing dynamic command registration
Browse files Browse the repository at this point in the history
* fix for overriding commands
  • Loading branch information
Machine-Maker committed Jul 18, 2020
1 parent fe8f07c commit 6005ea5
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ protected void registerCommands(BaseModuleCommand<?> command) {
this.plugin.commandManager.registerCommand(command);
}

@Deprecated
protected void unregisterCommands(BaseCommand command) {
this.plugin.commandManager.unregisterCommand(command);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/machinemaker/vanillatweaks/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum Lang {
PREFIX("&8[&9V&1T&8] &r"),
ERROR_PREFIX("&8[&4V&cT&8] &r"),

RELOAD("&aConfiguration/Modules/Lang reloaded."),
RELOAD("&aConfiguration/Modules/Lang reloaded. &cIf you are overriding commands from this plugin with commands from another plugin, you should do a full reload of the server."),
NOT_ENABLED("&cThis module is not enabled!"),

SCOREBOARD_ON("&aTurned on %board% scoreboard."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ public class CoordinatesHUD extends BaseModule {

final List<Player> enabled = Lists.newArrayList();
private HUDRunnable runnable;
private final Commands commands = new Commands(this);

public CoordinatesHUD(VanillaTweaks plugin) {
super(plugin, config -> config.coordinatesHud);
this.registerCommands(new Commands(this));
}

@Override
public void register() {
this.registerCommands(commands);
runnable = new HUDRunnable();
runnable.runTaskTimer(this.plugin, 1L, 2L);
}

@Override
public void unregister() {
this.unregisterCommands(commands);
runnable.cancel();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class DurabilityPing extends BaseModule implements Listener {

private final Config config = new Config();
private final Map<UUID, Long> cooldownMap = Maps.newHashMap();
private final Commands commands = new Commands(this);
final NamespacedKey PING = new NamespacedKey(this.plugin, "ping");

public DurabilityPing(VanillaTweaks plugin) {
super(plugin, config -> config.durabilityPing);
config.init(plugin, new File(plugin.getDataFolder(), "durabilityping"));
this.registerCommands(new Commands(this));
}

@EventHandler
Expand All @@ -55,11 +55,13 @@ public void onPlayerLeave(PlayerQuitEvent event) {

@Override
public void register() {
this.registerCommands(commands);
this.registerEvents(this);
}

@Override
public void unregister() {
this.unregisterCommands(commands);
this.unregisterEvents(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

public class KillEmptyBoats extends BaseModule {

private final Commands commands = new Commands(this);

public KillEmptyBoats(VanillaTweaks plugin) {
super(plugin, config -> config.killEmptyBoats);
this.registerCommands(new Commands(this));
}

@Override
public void register() {

this.registerCommands(commands);
}

@Override
public void unregister() {

this.unregisterCommands(commands);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class MobCounting extends BaseModule implements Listener {
final Scoreboard board;
final Objective objective;
boolean isCounting = false;
private final Commands commands = new Commands(this);

public MobCounting(VanillaTweaks vanillaTweaks) {
super(vanillaTweaks, config -> config.countMobDeaths);
this.registerCommands(new Commands(this));
board = Objects.requireNonNull(Bukkit.getScoreboardManager()).getNewScoreboard();
objective = board.registerNewObjective("mobDeathCount", "dummy", ChatColor.GOLD + "No. Mob Deaths");
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
Expand All @@ -38,11 +38,13 @@ public void onEntityDeath(EntityDeathEvent event) {

@Override
public void register() {
this.registerCommands(commands);
this.registerEvents(this);
}

@Override
public void unregister() {
this.unregisterCommands(commands);
this.unregisterEvents(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
public class NetherPortalCoords extends BaseModule {

final Config config = new Config();
private final Commands commands = new Commands(this);

public NetherPortalCoords(VanillaTweaks plugin) {
super(plugin, config -> config.netherPortalCoords);
config.init(plugin, new File(plugin.getDataFolder(), "netherportalcoords"));
this.registerCommands(new Commands(this));
}

@Override
public void register() {

this.registerCommands(commands);
}

@Override
public void unregister() {

this.unregisterCommands(commands);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

public class PillagerTools extends BaseModule implements Listener {

Config config = new Config();
final Config config = new Config();
private final Commands commands = new Commands(this);

public PillagerTools(VanillaTweaks plugin) {
super(plugin, config -> config.pillagerTools);
config.init(plugin, new File(plugin.getDataFolder(), "pillagertools"));
plugin.commandManager.getCommandCompletions().registerStaticCompletion("pillagertools/toggles", Arrays.stream(ToggleOption.values()).map(ToggleOption::name).map(String::toLowerCase).collect(Collectors.toSet()));
this.registerCommands(new Commands(this));
}

@EventHandler
Expand All @@ -46,11 +46,13 @@ public void onStatusEffectChange(EntityPotionEffectEvent event) {

@Override
public void register() {
this.registerCommands(commands);
this.registerEvents(this);
}

@Override
public void unregister() {
this.unregisterCommands(commands);
this.unregisterEvents(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class SetHome extends BaseModule {

final Config config = new Config();

final Commands commands = new Commands(this);
final Map<UUID, SetHomeInfo> homeMap;

public SetHome(VanillaTweaks plugin) {
Expand All @@ -23,7 +23,6 @@ public SetHome(VanillaTweaks plugin) {
File configDir = new File(plugin.getDataFolder(), "sethome");
config.init(plugin, configDir);
plugin.configManager.createConfig("sethome/homes", "homes.yml", configDir);
this.registerCommands(new Commands(this));
homeMap = Maps.newHashMap();
}

Expand All @@ -33,6 +32,7 @@ private YamlConfiguration getConfig() {

@Override
public void register() {
this.registerCommands(commands);
ConfigurationSection section = getConfig().getConfigurationSection("players");
if (section != null) {
section.getKeys(false).forEach(uuid -> homeMap.put(UUID.fromString(uuid), section.getObject(uuid, SetHomeInfo.class)));
Expand All @@ -41,6 +41,7 @@ public void register() {

@Override
public void unregister() {
this.plugin.commandManager.unregisterCommand(commands);
homeMap.forEach((uuid, info) -> getConfig().set("players." + uuid.toString(), info));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@

public class SpawningSpheres extends BaseModule {

private final Commands commands = new Commands(this);

public SpawningSpheres(VanillaTweaks plugin) {
super(plugin, config -> config.spawningSpheres);
plugin.commandManager.getCommandCompletions().registerStaticCompletion("ss/colors", Arrays.stream(Color.values()).map(Color::name).collect(Collectors.toSet()));
this.registerCommands(new Commands(this));
}

@Override
public void register() {

this.registerCommands(commands);
}

@Override
public void unregister() {

this.unregisterCommands(commands);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@

public class SpectatorNightVision extends BaseModule {

private final Commands commands = new Commands(this);

public SpectatorNightVision(VanillaTweaks plugin) {
super(plugin, config -> config.spectatorConduitPower || config.spectatorNightVision);
this.plugin.commandManager.getCommandConditions().addCondition("gamemode", c -> {
if (!c.getIssuer().isPlayer()) throw new ConditionFailedException("Must be run by a player");
if (c.getIssuer().getPlayer().getGameMode() != GameMode.valueOf(c.getConfig()))
throw new ConditionFailedException("Command must be run in gamemode: " + c.getConfig());
});
this.registerCommands(new Commands(this));
}

@Override
public void register() { }
public void register() {
this.registerCommands(commands);
}

@Override
public void unregister() { }
public void unregister() {
this.unregisterCommands(commands);
}
}
4 changes: 3 additions & 1 deletion src/main/java/me/machinemaker/vanillatweaks/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class Tag extends BaseModule implements Listener {
private Team colorTeam;

private TagRunnable runnable;
private final Commands commands = new Commands(this);

public Tag(VanillaTweaks plugin) {
super(plugin, config -> config.tag);
config.init(plugin, new File(plugin.getDataFolder(), "tag"));
ItemMeta meta = tagItem.getItemMeta();
meta.setDisplayName(ChatColor.RESET.toString() + ChatColor.YELLOW + "Tag!");
tagItem.setItemMeta(meta);
this.registerCommands(new Commands(this));
colorTeam = Bukkit.getScoreboardManager().getMainScoreboard().getTeam("tag/redcolor");
if (colorTeam == null) {
colorTeam = Bukkit.getScoreboardManager().getMainScoreboard().registerNewTeam("tag/redcolor");
Expand Down Expand Up @@ -90,13 +90,15 @@ void removeAsIt(Player player) {

@Override
public void register() {
this.registerCommands(commands);
this.registerEvents(this);
this.runnable = new TagRunnable(this);
this.runnable.runTaskTimer(this.plugin, 0L, 5L);
}

@Override
public void unregister() {
this.unregisterCommands(commands);
this.unregisterEvents(this);
this.runnable.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public class ThunderShrine extends BaseModule implements Listener {

final Set<Entity> shrineLocations = Sets.newHashSet();

private final Commands commands = new Commands(this);

public ThunderShrine(VanillaTweaks plugin) {
super(plugin, config -> config.thunderShrine);
this.registerCommands(new Commands(this));
}

@Override
public void register() {
this.registerCommands(commands);
runnable = new ShrineRunnable(this);
effectRunnable = new EffectRunnable(this);
loadRunnable = new EntityLoadRunnable();
Expand All @@ -42,6 +44,7 @@ public void register() {

@Override
public void unregister() {
this.unregisterCommands(commands);
runnable.cancel();
effectRunnable.cancel();
loadRunnable.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TrackRawStats extends BaseModule {

Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
private final Set<IStat> statTypes = Sets.newHashSet();
private final Commands commands = new Commands(this);

public TrackRawStats(VanillaTweaks plugin) {
super(plugin, config -> config.trackRawStats);
Expand All @@ -37,13 +38,11 @@ public TrackRawStats(VanillaTweaks plugin) {
if (type == null) return Sets.newHashSet();
return type.objectiveMap.keySet();
});

this.registerCommands(new Commands(this));

}

@Override
public void register() {
this.registerCommands(commands);
for (StatType type : StatType.values()) {
statTypes.addAll(type.stats);
type.stats.forEach(stat -> {
Expand All @@ -62,6 +61,7 @@ public void register() {

@Override
public void unregister() {
this.unregisterCommands(commands);
for (StatType statType : StatType.values()) {
statType.objectiveMap.values().forEach(Objective::unregister);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

public class WorkstationHighlights extends BaseModule {

private final Commands commands = new Commands(this);

public WorkstationHighlights(VanillaTweaks plugin) {
super(plugin, config -> config.workstationHighlights);
this.registerCommands(new Commands(this));
}

@Override
public void register() {

this.registerCommands(commands);
}

@Override
public void unregister() {

this.unregisterCommands(commands);
}
}

0 comments on commit 6005ea5

Please sign in to comment.