Skip to content

Commit

Permalink
big command update
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Jan 2, 2024
1 parent 1ca08fe commit 9d1aa5b
Show file tree
Hide file tree
Showing 24 changed files with 230 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.logging.LogUtils;
import io.github.lukegrahamlandry.tribes.commands.util.DeityArgumentType;
import io.github.lukegrahamlandry.tribes.commands.util.OfflinePlayerArgumentType;
import io.github.lukegrahamlandry.tribes.commands.util.TribeArgumentType;
import io.github.lukegrahamlandry.tribes.config.Config;
import io.github.lukegrahamlandry.tribes.init.*;
Expand Down Expand Up @@ -40,7 +41,8 @@ public TribesMain() {
BannerInit.setup();

ArgumentTypes.register("tribe", TribeArgumentType.class, new EmptyArgumentSerializer<>(TribeArgumentType::tribe));
ArgumentTypes.register("deity", DeityArgumentType.class, new EmptyArgumentSerializer<>(DeityArgumentType::tribe));
ArgumentTypes.register("deity", DeityArgumentType.class, new EmptyArgumentSerializer<>(DeityArgumentType::deity));
ArgumentTypes.register("maybe_offline_player", OfflinePlayerArgumentType.class, new EmptyArgumentSerializer<>(OfflinePlayerArgumentType::offlinePlayerID));

// event listeners
eventBus.addListener(this::setup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,19 @@ public class AdminCommands {
.then(Commands.literal("load").executes(AdminCommands::loadData))
.then(Commands.literal("delete")
.then(Commands.argument("name", StringArgumentType.greedyString())
.executes(AdminCommands::handleDelete))
.executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_TRIBE.getText(), false);
return 0;
}))
.executes(AdminCommands::handleDelete)
)
)
.then(Commands.literal("rename")
.then(Commands.argument("tribe", TribeArgumentType.tribe())
.then(Commands.argument("name", StringArgumentType.string())
.executes(AdminCommands::handleRename))
.executes(ctx -> {
ctx.getSource().sendSuccess(new TextComponent("choose a new name for " + StringArgumentType.getString(ctx, "name")), false);
ctx.getSource().sendSuccess(new TextComponent("Choose a new name for " + StringArgumentType.getString(ctx, "name")), false);
return 0;
}))
.executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_TRIBE.getText(), false);
return 0;
}))
;
})
)
);
}

private static int handleRename(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.lukegrahamlandry.tribes.api.tribe.Relation;
import io.github.lukegrahamlandry.tribes.commands.util.TribeArgumentType;
import io.github.lukegrahamlandry.tribes.tribe_data.*;
import io.github.lukegrahamlandry.tribes.tribe_data.Tribe;
import io.github.lukegrahamlandry.tribes.tribe_data.TribeHelper;
import io.github.lukegrahamlandry.tribes.tribe_data.TribeSuccessType;
import io.github.lukegrahamlandry.tribes.tribe_data.TribesManager;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.world.entity.player.Player;
Expand All @@ -16,10 +19,6 @@ public class AllyTribeCommand {
return Commands.literal("ally")
.then(Commands.argument("tribe", TribeArgumentType.tribe())
.executes(AllyTribeCommand::handleAlly)
).executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_TRIBE.getText(), false);
return 0;
}
);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.lukegrahamlandry.tribes.commands.util.OfflinePlayerArgumentType;
import io.github.lukegrahamlandry.tribes.tribe_data.*;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.world.entity.player.Player;

import java.util.UUID;

public class BanPlayerCommand {
public static ArgumentBuilder<CommandSourceStack, ?> register() {
return Commands.literal("ban")
.then(Commands.argument("player", EntityArgument.player())
.executes(BanPlayerCommand::handleBan)
).executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_PLAYER.getText(), false);
return 0;
}
.then(Commands.argument("player", OfflinePlayerArgumentType.offlinePlayerID())
.executes(ctx -> {
var uuid = OfflinePlayerArgumentType.getOfflinePlayer(ctx, "offlinePlayer");
return handleBan(ctx, uuid);
})
);

}

public static int handleBan(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
public static int handleBan(CommandContext<CommandSourceStack> context, UUID target) throws CommandSyntaxException {
Player playerBanning = context.getSource().getPlayerOrException();
Player playerToBan = EntityArgument.getPlayer(context, "player");
var server = context.getSource().getServer();

Tribe tribe = TribesManager.getTribeOf(playerBanning.getUUID());
Expand All @@ -34,13 +33,13 @@ public static int handleBan(CommandContext<CommandSourceStack> context) throws C
return 0;
}

var result = tribe.banPlayer(playerBanning.getUUID(), playerToBan.getUUID());
var result = tribe.banPlayer(playerBanning.getUUID(), target);
if (!result.success()) {
context.getSource().sendFailure(result.error().getText());
return 0;
}

TribeHelper.broadcastMessage(tribe, TribeSuccessType.BAN_PLAYER, playerBanning, server, playerToBan);
TribeHelper.broadcastMessage(tribe, TribeSuccessType.BAN_PLAYER, playerBanning, server, OfflinePlayerArgumentType.getPlayerName(target));

return Command.SINGLE_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static int handleClaim(CommandContext<CommandSourceStack> source) throws
Tribe tribe = TribesManager.getTribeOf(player.getUUID());

if (tribe == null) {
source.getSource().sendSuccess(TribeError.YOU_NOT_IN_TRIBE.getText(), true);
return Command.SINGLE_SUCCESS;
source.getSource().sendFailure(TribeError.YOU_NOT_IN_TRIBE.getText());
return 0;
}

var result = tribe.claimChunk(getChunk(player), player.getUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ConfirmCommand {
return Commands.literal("confirm").executes(ConfirmCommand::handleConfirm);
}

public static void add(Player player, IConfirmAction action){
public static void add(Player player, IConfirmAction action) {
player.displayClientMessage(TribeSuccessType.MUST_CONFIRM.getBlueText(), false);
CONFIRM_ACTIONS.put(player.getUUID(), action);
}
Expand All @@ -31,12 +31,13 @@ public static int handleConfirm(CommandContext<CommandSourceStack> source) throw


IConfirmAction action = CONFIRM_ACTIONS.get(player.getUUID());
if (action == null){
source.getSource().sendSuccess(TribeError.NO_CONFIRM.getText(), true);
} else {
action.call();
if (action == null) {
source.getSource().sendFailure(TribeError.NO_CONFIRM.getText());
return 0;
}

action.call();

return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.lukegrahamlandry.tribes.commands.util.TribeArgumentType;
import io.github.lukegrahamlandry.tribes.tribe_data.Tribe;
import io.github.lukegrahamlandry.tribes.tribe_data.TribeError;
import io.github.lukegrahamlandry.tribes.tribe_data.TribeSuccessType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
Expand All @@ -16,20 +15,14 @@ public class CountTribeCommand {
return Commands.literal("count")
.then(Commands.argument("tribe", TribeArgumentType.tribe())
.executes(CountTribeCommand::handleCount)
).executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_TRIBE.getText(), false);
return 0;
}
);

}

public static int handleCount(CommandContext<CommandSourceStack> source) throws CommandSyntaxException {
Tribe tribe = TribeArgumentType.getTribe(source, "tribe");

if (tribe != null) {
source.getSource().sendSuccess(TribeSuccessType.COUNT_TRIBE.getBlueText(tribe, tribe.getMemberCount(), tribe.getTribeTier()), true);
}
source.getSource().sendSuccess(TribeSuccessType.COUNT_TRIBE.getBlueText(tribe, tribe.getMemberCount(), tribe.getTribeTier()), true);

return Command.SINGLE_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.github.lukegrahamlandry.tribes.tribe_data.TribeError;
import io.github.lukegrahamlandry.tribes.tribe_data.TribeSuccessType;
import io.github.lukegrahamlandry.tribes.tribe_data.TribesManager;
import net.minecraft.commands.CommandSourceStack;
Expand All @@ -27,11 +26,6 @@ public static LiteralArgumentBuilder<CommandSourceStack> register() {

return Command.SINGLE_SUCCESS;
})
).executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_MISSING.getText(), false);
return 0;
}
);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ public class DeityCommands {
.then(Commands.literal("list").executes(DeityCommands::handleList))
.then(Commands.literal("banner").executes(DeityCommands::createBanner))
.then(Commands.literal("choose")
.then(Commands.argument("deity", DeityArgumentType.tribe())
.then(Commands.argument("deity", DeityArgumentType.deity())
.executes(DeityCommands::handleChoose))
.executes(ctx -> {
ctx.getSource().sendFailure(TribeError.ARG_DEITY.getText());
return 0;
}))
.then(Commands.literal("describe")
.then(Commands.argument("deity", DeityArgumentType.tribe())
.executes(DeityCommands::handleDescribe))
.executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_DEITY.getText(), false);
return 0;
}))
;
.then(Commands.argument("deity", DeityArgumentType.deity())
.executes(DeityCommands::handleDescribe)
)
);
}

private static int handleChoose(CommandContext<CommandSourceStack> source) throws CommandSyntaxException {
Expand Down Expand Up @@ -88,7 +85,7 @@ private static int handleList(CommandContext<CommandSourceStack> source) {
return Command.SINGLE_SUCCESS;
}

private static int handleDescribe(CommandContext<CommandSourceStack> source) {
private static int handleDescribe(CommandContext<CommandSourceStack> source) throws CommandSyntaxException {
DeitiesManager.DeityData data = DeityArgumentType.getDeity(source, "deity");
if (data != null) {
//FIXME make translatable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,41 @@
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.lukegrahamlandry.tribes.commands.util.OfflinePlayerArgumentType;
import io.github.lukegrahamlandry.tribes.tribe_data.*;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.world.entity.player.Player;

import java.util.UUID;

public class DemotePlayerCommand {
public static ArgumentBuilder<CommandSourceStack, ?> register() {
return Commands.literal("demote")
.then(Commands.argument("player", EntityArgument.player())
.then(Commands.argument("player", OfflinePlayerArgumentType.offlinePlayerID())
.executes(DemotePlayerCommand::handle)
).executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_PLAYER.getText(), false);
return 0;
}
);

}

public static int handle(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Player playerRunning = context.getSource().getPlayerOrException();
Player playerTarget = EntityArgument.getPlayer(context, "player");
UUID target = OfflinePlayerArgumentType.getOfflinePlayer(context, "player");

Tribe tribe = TribesManager.getTribeOf(playerRunning.getUUID());
if (tribe == null) {
context.getSource().sendFailure(TribeError.YOU_NOT_IN_TRIBE.getText());
return 0;
}

var result = tribe.demotePlayer(playerRunning.getUUID(), playerTarget.getUUID());
var result = tribe.demotePlayer(playerRunning.getUUID(), target);

if (!result.success()) {
context.getSource().sendFailure(result.error().getText());
return 0;
}

String rank = tribe.getRankOf(playerTarget.getUUID()).asString();
TribeHelper.broadcastMessage(tribe, TribeSuccessType.DEMOTE, playerRunning, context.getSource().getServer(), playerTarget, rank);
String rank = tribe.getRankOf(target).asString();
TribeHelper.broadcastMessage(tribe, TribeSuccessType.DEMOTE, playerRunning, context.getSource().getServer(), OfflinePlayerArgumentType.getPlayerName(target), rank);

return Command.SINGLE_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,37 @@
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.lukegrahamlandry.tribes.commands.util.OfflinePlayerArgumentType;
import io.github.lukegrahamlandry.tribes.tribe_data.*;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.world.entity.player.Player;

import java.util.UUID;

public class InviteCommands {
public static ArgumentBuilder<CommandSourceStack, ?> register() {
return Commands.literal("invite")
.then(Commands.literal("send").then(Commands.argument("player", EntityArgument.player()).executes(InviteCommands::invitePlayer))).executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_PLAYER.getText(), false);
return 0;
})
.then(Commands.literal("revoke").then(Commands.argument("player", EntityArgument.player()).executes(InviteCommands::uninvitePlayer))).executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_PLAYER.getText(), false);
return 0;
})
.then(Commands.literal("send")
.then(Commands.argument("player", OfflinePlayerArgumentType.offlinePlayerID())
.executes(InviteCommands::invitePlayer)
)
)
.then(Commands.literal("revoke")
.then(Commands.argument("player", OfflinePlayerArgumentType.offlinePlayerID())
.executes(InviteCommands::uninvitePlayer)
)
)
.then(Commands.literal("toggle")
.then(Commands.argument("private", BoolArgumentType.bool()).executes(InviteCommands::setPrivate))
.executes(ctx -> {
ctx.getSource().sendSuccess(TribeError.ARG_MISSING.getText(), false);
return 0;
}));
.then(Commands.argument("private", BoolArgumentType.bool())
.executes(InviteCommands::setPrivate)
)
);
}

private static int invitePlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Player leader = context.getSource().getPlayerOrException();
Player toInvite = EntityArgument.getPlayer(context, "player");
UUID toInvite = OfflinePlayerArgumentType.getOfflinePlayer(context, "player");

Tribe tribe = TribesManager.getTribeOf(leader.getUUID());

Expand All @@ -48,20 +51,20 @@ private static int invitePlayer(CommandContext<CommandSourceStack> context) thro
context.getSource().sendFailure(TribeError.RANK_TOO_LOW.getText());
return 0;
}
if (TribesManager.getTribeOf(toInvite.getUUID()) != null) {
if (TribesManager.getTribeOf(toInvite) != null) {
context.getSource().sendFailure(TribeError.IN_OTHER_TRIBE.getText());
return 0;
}

tribe.getPendingInvites().add(toInvite.getUUID());
TribeHelper.broadcastMessage(tribe, TribeSuccessType.INVITE_SENT, leader, context.getSource().getServer(), toInvite);
tribe.getPendingInvites().add(toInvite);
TribeHelper.broadcastMessage(tribe, TribeSuccessType.INVITE_SENT, leader, context.getSource().getServer(), OfflinePlayerArgumentType.getPlayerName(toInvite));

return Command.SINGLE_SUCCESS;
}

private static int uninvitePlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Player leader = context.getSource().getPlayerOrException();
Player toInvite = EntityArgument.getPlayer(context, "player");
UUID toInvite = OfflinePlayerArgumentType.getOfflinePlayer(context, "player");

Tribe tribe = TribesManager.getTribeOf(leader.getUUID());

Expand All @@ -74,8 +77,8 @@ private static int uninvitePlayer(CommandContext<CommandSourceStack> context) th
return 0;
}

tribe.getPendingInvites().remove(toInvite.getUUID());
TribeHelper.broadcastMessage(tribe, TribeSuccessType.INVITE_REMOVED, leader, context.getSource().getServer(), toInvite);
tribe.getPendingInvites().remove(toInvite);
TribeHelper.broadcastMessage(tribe, TribeSuccessType.INVITE_REMOVED, leader, context.getSource().getServer(), OfflinePlayerArgumentType.getPlayerName(toInvite));

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class JoinTribeCommand {
public static int handleJoin(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Player player = context.getSource().getPlayerOrException();
Tribe tribe = TribeArgumentType.getTribe(context, "tribe");
if (tribe == null) return 1;

if (playerHasTribe(player.getUUID())) {
context.getSource().sendFailure(TribeError.IN_TRIBE.getText());
Expand Down
Loading

0 comments on commit 9d1aa5b

Please sign in to comment.