Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite/command api #696

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
83cb87d
Move Command registration to its own function
Intybyte Aug 20, 2024
91bd35c
Add New API Dependency
Intybyte Aug 20, 2024
ed6ee20
MovecraftCommand adapted to new API
Intybyte Aug 20, 2024
711020f
Shade aikar's ACF
Intybyte Aug 20, 2024
fe417e9
Add description to MovecraftCommand
Intybyte Aug 20, 2024
8c5b9ae
Shade attempt 2
Intybyte Aug 20, 2024
02abbc3
Update command description
Intybyte Aug 21, 2024
cf7d288
Add CraftInfoCommand
Intybyte Aug 21, 2024
c2924ad
Forgot this
Intybyte Aug 21, 2024
2290436
Add CraftReportCommand
Intybyte Aug 21, 2024
3f3d00c
Add CraftTypeCommand pt1
Intybyte Aug 21, 2024
5ee2fad
Add CraftTypeCommand pt2
Intybyte Aug 21, 2024
f59cc64
This wasn't needed
Intybyte Aug 21, 2024
e14dd75
Small change to CraftReportCommand
Intybyte Aug 21, 2024
76a2385
Apply similar changes to CraftInfoCommand
Intybyte Aug 21, 2024
ee05772
Add CruiseCommand pt1
Intybyte Aug 21, 2024
91398c7
Add CruiseCommand pt2
Intybyte Aug 21, 2024
911aa4d
Add CruiseDirection resolver for ACF
Intybyte Aug 21, 2024
1099193
Add CruiseCommand pt3
Intybyte Aug 21, 2024
c4efaf2
Made custom command manager to handle or operations
Intybyte Aug 21, 2024
c669efd
Fix permissions
Intybyte Aug 21, 2024
cf865db
Fix CruiseCommand
Intybyte Aug 21, 2024
ad84cc8
Change to yawLocationCruising
Intybyte Aug 21, 2024
8171ade
Add CraftType context resolver
Intybyte Aug 22, 2024
27b9b8b
Use string parsing
Intybyte Aug 24, 2024
6038679
byebye self
Intybyte Aug 24, 2024
aa6716c
Simplify
Intybyte Aug 24, 2024
72c8843
Simplified Permission check
Intybyte Aug 24, 2024
a254a3a
Permission fixes
Intybyte Aug 25, 2024
c3c7922
Move into command manager
Intybyte Aug 31, 2024
a11c331
Merge remote-tracking branch 'origin/main' into rewrite/commandAPI
Intybyte Aug 31, 2024
2814463
Fixing craftInfo, this requires either restoring self or reverting
Intybyte Aug 31, 2024
0ea58e8
@PreCommand works with inverted logic
Intybyte Aug 31, 2024
fd8ab3b
Remove "self" from CraftInfoCommand
Intybyte Sep 3, 2024
f81e45b
Remove "self" from CraftTypeCommand
Intybyte Sep 3, 2024
a984656
Add ManOverboardCommand
Intybyte Sep 3, 2024
16d3af8
Add PilotCommand pt1
Intybyte Sep 3, 2024
bd710bd
Add PilotCommand pt2
Intybyte Sep 3, 2024
2f76c8d
Add ReleaseCommand pt1
Intybyte Sep 3, 2024
105d21c
Add ReleaseCommand pt2
Intybyte Sep 3, 2024
9284708
Add RotateCommand pt1
Intybyte Sep 3, 2024
f5995c8
Add RotateCommand pt2
Intybyte Sep 3, 2024
bb6f576
Add ScuttleCommand
Intybyte Sep 3, 2024
38aa24e
Merge branch 'refs/heads/main' into rewrite/commandAPI
Intybyte Sep 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Movecraft/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
runtimeOnly(project(":movecraft-v1_21", "reobf"))
implementation(project(":movecraft-api"))
compileOnly("org.yaml:snakeyaml:2.0")
implementation("co.aikar:acf-paper:0.5.1-SNAPSHOT")
}

tasks.shadowJar {
Expand All @@ -25,7 +26,11 @@ tasks.shadowJar {
include(project(":movecraft-v1_18"))
include(project(":movecraft-v1_20"))
include(project(":movecraft-v1_21"))
include(dependency("co.aikar:acf-paper:0.5.1-SNAPSHOT"))
}

relocate("co.aikar.commands", "Movecraft.acf")
relocate("co.aikar.locales", "Movecraft.locales")
Intybyte marked this conversation as resolved.
Show resolved Hide resolved
}

tasks.processResources {
Expand Down
61 changes: 50 additions & 11 deletions Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

package net.countercraft.movecraft;

import co.aikar.commands.InvalidCommandArgument;
import io.papermc.paper.datapack.Datapack;
import net.countercraft.movecraft.async.AsyncManager;
import net.countercraft.movecraft.commands.*;
import net.countercraft.movecraft.config.Settings;
import net.countercraft.movecraft.craft.ChunkManager;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.craft.datatag.CraftDataTagRegistry;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.features.contacts.ContactsCommand;
import net.countercraft.movecraft.features.contacts.ContactsManager;
import net.countercraft.movecraft.features.contacts.ContactsSign;
Expand All @@ -46,6 +47,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
Expand Down Expand Up @@ -198,16 +200,7 @@ public void onEnable() {

getServer().getPluginManager().registerEvents(new InteractListener(), this);

getCommand("movecraft").setExecutor(new MovecraftCommand());
getCommand("release").setExecutor(new ReleaseCommand());
getCommand("pilot").setExecutor(new PilotCommand());
getCommand("rotate").setExecutor(new RotateCommand());
getCommand("cruise").setExecutor(new CruiseCommand());
getCommand("craftreport").setExecutor(new CraftReportCommand());
getCommand("manoverboard").setExecutor(new ManOverboardCommand());
getCommand("scuttle").setExecutor(new ScuttleCommand());
getCommand("crafttype").setExecutor(new CraftTypeCommand());
getCommand("craftinfo").setExecutor(new CraftInfoCommand());
initializeCommands();

getServer().getPluginManager().registerEvents(new BlockListener(), this);
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
Expand Down Expand Up @@ -244,6 +237,52 @@ public void onEnable() {
logger.info("[V " + getDescription().getVersion() + "] has been enabled.");
}

private void initializeCommands() {
MovecraftCommandManager movecraftCommandManager = new MovecraftCommandManager(this);
var commandCompletions = movecraftCommandManager.getCommandCompletions();

commandCompletions.registerCompletion("crafttypes", c -> {
Set<CraftType> craftTypes = CraftManager.getInstance().getCraftTypes();
List<String> craftNames = craftTypes.stream().map(type -> type.getStringProperty(CraftType.NAME)).toList();
return craftNames;
});

commandCompletions.registerCompletion("directions", c -> {
var allDirections = CruiseDirection.valuesString();
var allButNone = allDirections.stream().filter(p -> !p.equals("none")).toList();
return allButNone;
});

var commandContexts = movecraftCommandManager.getCommandContexts();

commandContexts.registerContext(CruiseDirection.class, (c) -> {
String data = c.popFirstArg();
return CruiseDirection.fromString(data);
});

commandContexts.registerContext(CraftType.class, (c) -> {
String data = c.popFirstArg();
CraftType type = CraftManager.getInstance().getCraftTypeFromString(data);

if (type == null) {
throw new InvalidCommandArgument("You must supply a craft type!");
}

return type;
});
Intybyte marked this conversation as resolved.
Show resolved Hide resolved

movecraftCommandManager.registerCommand(new MovecraftCommand());
movecraftCommandManager.registerCommand(new CraftInfoCommand());
movecraftCommandManager.registerCommand(new CraftReportCommand());
movecraftCommandManager.registerCommand(new CraftTypeCommand());
movecraftCommandManager.registerCommand(new CruiseCommand());
getCommand("release").setExecutor(new ReleaseCommand());
getCommand("pilot").setExecutor(new PilotCommand());
getCommand("rotate").setExecutor(new RotateCommand());
getCommand("manoverboard").setExecutor(new ManOverboardCommand());
getCommand("scuttle").setExecutor(new ScuttleCommand());
}

@Override
public void onLoad() {
super.onLoad();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package net.countercraft.movecraft.commands;

import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.*;
import co.aikar.commands.bukkit.contexts.OnlinePlayer;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.util.MathUtils;
import net.countercraft.movecraft.util.TopicPaginator;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.Function;

public class CraftInfoCommand implements TabExecutor {
@CommandAlias("craftinfo")
public class CraftInfoCommand extends BaseCommand {
private static final List<Function<Craft,? extends Iterable<String>>> providers = new ArrayList<>();
static {
registerMultiProvider(CraftInfoCommand::allowedBlockProvider);
Expand Down Expand Up @@ -48,51 +48,34 @@ public static void registerProvider(@NotNull Function<Craft, String> provider){
providers.add(provider.andThen(List::of));
}


@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(args.length == 0){
if (!(sender instanceof Player)){
sender.sendMessage("Supply a parameter.");
return true;
}
Craft craft = CraftManager.getInstance().getCraftByPlayer(((Player) sender));
if(craft == null){
sender.sendMessage("You must be piloting a craft.");
return true;
}
craftInfo(sender, craft, 1);
return true;
}
OptionalInt pageQuery;
if (sender instanceof Player && (pageQuery = MathUtils.parseInt(args[0])).isPresent()){
Craft craft = CraftManager.getInstance().getCraftByPlayer(((Player) sender));
if(craft == null){
sender.sendMessage("You must be piloting a craft.");
return true;
}
craftInfo(sender, craft, pageQuery.getAsInt());
return true;
}
var craft = CraftManager.getInstance().getCraftByPlayerName(args[0]);
@Default
@Syntax("[player|self] <page>")
Intybyte marked this conversation as resolved.
Show resolved Hide resolved
@Description("Get information on a piloted craft")
@CommandCompletion("@players")
public static void onCommand(Player player, OnlinePlayer subject, @Default("1") int page) {
var craft = CraftManager.getInstance().getCraftByPlayer(subject.getPlayer());
if (craft == null) {
sender.sendMessage("No player found");
return true;
//maybe no craft found would be more correct
player.sendMessage("No player found");
return;
}
if(args.length > 1){
pageQuery = MathUtils.parseInt(args[1]);
if(pageQuery.isEmpty()){
sender.sendMessage("Parameter " + args[1] + " must be a page number.");
return true;
}
} else {
pageQuery = OptionalInt.of(1);

craftInfo(player, craft, page);
}

@Subcommand("self")
@Syntax("<page>")
public static void selfCraftInfo(Player player, @Default("1") int page) {
Craft craft = CraftManager.getInstance().getCraftByPlayer(player);
if(craft == null){
player.sendMessage("You must be piloting a craft.");
return;
}
craftInfo(sender, craft, pageQuery.getAsInt());
return true;

craftInfo(player, craft, page);
}

public void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft, int page){
public static void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft, int page){
TopicPaginator paginator = new TopicPaginator("Craft Info");
for(var provider : providers){
for(var line : provider.apply(craft)){
Expand All @@ -106,9 +89,4 @@ public void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft
for(String line : paginator.getPage(page))
commandSender.sendMessage(line);
}

@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.countercraft.movecraft.commands;

import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.*;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.craft.PilotedCraft;
Expand All @@ -11,45 +13,38 @@
import net.countercraft.movecraft.util.hitboxes.HitBox;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

public class CraftReportCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] args) {
if (commandSender.getName().equalsIgnoreCase("craftreport"))
return false;
@CommandAlias("craftreport")
@CommandPermission("movecraft.commands|movecraft.commands.craftreport")
public class CraftReportCommand extends BaseCommand {

if (!commandSender.hasPermission("movecraft.commands")
|| !commandSender.hasPermission("movecraft.commands.craftreport")) {
commandSender.sendMessage(Component.empty()
.append(ChatUtils.errorPrefix())
.append(I18nSupport.getInternationalisedComponent("Insufficient Permissions")));
return true;
}
int page;
@Default
@Syntax("<page>")
@Description("Reports on all active craft")
public static void onCommand(CommandSender commandSender, @Default("1") int page) {
// TODO: This is ugly to read, maybe better make a component concatenator method in ChatUtils?
// TODO: Should we keep the error for invalid page or just default to 1 in this case?
Intybyte marked this conversation as resolved.
Show resolved Hide resolved
/*
try {
if (args.length == 0)
page = 1;
else
page = Integer.parseInt(args[0]);
}
catch (NumberFormatException e) {
} catch (NumberFormatException e) {
commandSender.sendMessage(Component.empty()
.append(ChatUtils.commandPrefix())
.append(I18nSupport.getInternationalisedComponent("Paginator - Invalid Page"))
.append(Component.text("\""))
.append(Component.text(args[0]))
.append(Component.text("\"")));
return true;
}
return;
}*/
if (CraftManager.getInstance().isEmpty()) {
commandSender.sendMessage(Component.empty()
.append(ChatUtils.commandPrefix())
.append(I18nSupport.getInternationalisedComponent("Craft Report - None Found")));
return true;
return;
}
ComponentPaginator paginator = new ComponentPaginator(
I18nSupport.getInternationalisedComponent("Craft Report"),
Expand Down Expand Up @@ -87,12 +82,11 @@ else if (craft.getDisabled())
.append(ChatUtils.commandPrefix())
.append(I18nSupport.getInternationalisedComponent("Paginator - Invalid page"))
.append(Component.text(" \""))
.append(Component.text(args[0]))
.append(Component.text(page))
.append(Component.text("\"")));
return true;
return;
}
for (Component line : paginator.getPage(page))
commandSender.sendMessage(line);
return true;
}
}
Loading