Skip to content

Commit

Permalink
v0.21.0 - Add ability to teleport to another player's home...
Browse files Browse the repository at this point in the history
Intended for admin use.
  • Loading branch information
John-Paul-R committed Mar 12, 2022
1 parent 61a3161 commit 2076c54
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 56 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ loader_version=0.13.3
fabric_version=0.47.10+1.18.2

# Mod Properties
mod_name = Essential Commands
mod_id = essential_commands
mod_version = 0.20.4
maven_group = com.fibermc
archives_base_name = essential_commands
mod_name = Essential Commands
mod_id = essential_commands
mod_version = 0.21.0
maven_group = com.fibermc
archives_base_name = essential_commands

# Dependencies
permissions_api_version=0.1-SNAPSHOT
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/fibermc/essentialcommands/ECPerms.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.fibermc.essentialcommands;

import me.lucko.fabric.api.permissions.v0.PermissionCheckEvent;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.fabric.api.util.TriState;
import net.minecraft.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Predicate;
Expand All @@ -25,6 +23,7 @@ public static final class Registry {
public static final String tpdeny = "essentialcommands.tpdeny";
public static final String home_set = "essentialcommands.home.set";
public static final String home_tp = "essentialcommands.home.tp";
public static final String home_tp_others = "essentialcommands.home_tp_others";
public static final String home_delete = "essentialcommands.home.delete";
public static final String warp_set = "essentialcommands.warp.set";
public static final String warp_tp = "essentialcommands.warp.tp";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.fibermc.essentialcommands.commands.*;
import com.fibermc.essentialcommands.commands.bench.*;
import com.fibermc.essentialcommands.commands.suggestions.*;
import com.fibermc.essentialcommands.commands.suggestions.ListSuggestion;
import com.fibermc.essentialcommands.commands.suggestions.NicknamePlayersSuggestion;
import com.fibermc.essentialcommands.commands.suggestions.TeleportResponseSuggestion;
import com.fibermc.essentialcommands.commands.suggestions.WarpSuggestion;
import com.fibermc.essentialcommands.util.EssentialsXParser;
import com.fibermc.essentialcommands.util.TextUtil;
import com.mojang.brigadier.CommandDispatcher;
Expand All @@ -11,6 +14,7 @@
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.TextArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
Expand Down Expand Up @@ -90,6 +94,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, b
LiteralArgumentBuilder<ServerCommandSource> homeBuilder = CommandManager.literal("home");
LiteralArgumentBuilder<ServerCommandSource> homeSetBuilder = CommandManager.literal("set");
LiteralArgumentBuilder<ServerCommandSource> homeTpBuilder = CommandManager.literal("tp");
LiteralArgumentBuilder<ServerCommandSource> homeTpOtherBuilder = CommandManager.literal("tp_other");
LiteralArgumentBuilder<ServerCommandSource> homeDeleteBuilder = CommandManager.literal("delete");
LiteralArgumentBuilder<ServerCommandSource> homeListBuilder = CommandManager.literal("list");

Expand All @@ -102,27 +107,34 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, b
.requires(ECPerms.require(ECPerms.Registry.home_tp, 0))
.executes(new HomeCommand()::runDefault)
.then(argument("home_name", StringArgumentType.word())
.suggests(HomeSuggestion.suggestedStrings())
.suggests(HomeCommand.Suggestion.suggestedStrings)
.executes(new HomeCommand()));

homeTpOtherBuilder
.requires(ECPerms.require(ECPerms.Registry.home_tp_others, 4))
.then(argument("target_player", EntityArgumentType.player())
.then(argument("home_name", StringArgumentType.word())
.suggests(HomeTeleportOtherCommand.Suggestion.suggestedStrings)
.executes(new HomeTeleportOtherCommand())));

homeDeleteBuilder
.requires(ECPerms.require(ECPerms.Registry.home_delete, 0))
.then(argument("home_name", StringArgumentType.word())
.suggests(HomeSuggestion.suggestedStrings())
.suggests(HomeCommand.Suggestion.suggestedStrings)
.executes(new HomeDeleteCommand()));

homeListBuilder
.requires(ECPerms.require(ECPerms.Registry.home_tp, 0))
.executes(ListCommandFactory.create(
ECText.getInstance().get("cmd.home.list.start"),
"home tp",
HomeSuggestion::getSuggestionEntries
));
HomeCommand.Suggestion::getSuggestionEntries));

LiteralCommandNode<ServerCommandSource> homeNode = homeBuilder
.requires(ECPerms.requireAny(ECPerms.Registry.Group.home_group, 0))
.build();
homeNode.addChild(homeTpBuilder.build());
homeNode.addChild(homeTpOtherBuilder.build());
homeNode.addChild(homeSetBuilder.build());
homeNode.addChild(homeDeleteBuilder.build());
homeNode.addChild(homeListBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
import com.fibermc.essentialcommands.PlayerData;
import com.fibermc.essentialcommands.PlayerTeleporter;
import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess;
import com.fibermc.essentialcommands.commands.suggestions.ListSuggestion;
import com.fibermc.essentialcommands.types.MinecraftLocation;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class HomeCommand implements Command<ServerCommandSource> {

public HomeCommand() {
}
public HomeCommand() {}

@Override
public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Expand All @@ -33,26 +37,39 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
return exec(senderPlayerData, homeName);
}

public int runDefault(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
PlayerData playerData = ((ServerPlayerEntityAccess) context.getSource().getPlayer()).getEcPlayerData();
private static PlayerData getTargetPlayerData(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return ((ServerPlayerEntityAccess)context.getSource().getPlayer()).getEcPlayerData();
}

public static String getSoleHomeName(PlayerData playerData) throws CommandSyntaxException {
Set<String> homeNames = playerData.getHomeNames();
if (homeNames.size() > 1) {
throw CommandUtil.createSimpleException(ECText.getInstance().getText("cmd.home.tp.error.shortcut_more_than_one"));
} else if (homeNames.size() < 1) {
} else if (homeNames.isEmpty()) {
throw CommandUtil.createSimpleException(ECText.getInstance().getText("cmd.home.tp.error.shortcut_none_exist"));
}

return homeNames.stream().findAny().get();
}

public int runDefault(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
PlayerData playerData = ((ServerPlayerEntityAccess) context.getSource().getPlayer()).getEcPlayerData();

return exec(
playerData,
homeNames.stream().findAny().get()
playerData,
getSoleHomeName(playerData)
);
}

private int exec(PlayerData senderPlayerData, String homeName) throws CommandSyntaxException {
private static int exec(PlayerData senderPlayerData, String homeName) throws CommandSyntaxException {
return exec(senderPlayerData, senderPlayerData, homeName);
}

public static int exec(PlayerData senderPlayerData, PlayerData targetPlayerData, String homeName) throws CommandSyntaxException {
int out;

//Get home location
MinecraftLocation loc = senderPlayerData.getHomeLocation(homeName);
MinecraftLocation loc = targetPlayerData.getHomeLocation(homeName);

// Teleport & chat message
if (loc != null) {
Expand All @@ -66,4 +83,25 @@ private int exec(PlayerData senderPlayerData, String homeName) throws CommandSyn

return out;
}

public static class Suggestion {
//Brigader Suggestions
public static final SuggestionProvider<ServerCommandSource> suggestedStrings
= ListSuggestion.ofContext(Suggestion::getSuggestionsList);

/**
* Gets a list of suggested strings to be used with Brigader
*/
public static List<String> getSuggestionsList(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return new ArrayList<>(HomeCommand.getTargetPlayerData(context).getHomeNames());
}

/**
* Gets a set of suggestion entries to be used with ListCommandFactory
*/
public static Set<Map.Entry<String, MinecraftLocation>> getSuggestionEntries(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return HomeCommand.getTargetPlayerData(context).getHomeEntries();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.fibermc.essentialcommands.commands;

import com.fibermc.essentialcommands.PlayerData;
import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess;
import com.fibermc.essentialcommands.commands.suggestions.ListSuggestion;
import com.fibermc.essentialcommands.types.MinecraftLocation;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.ServerCommandSource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class HomeTeleportOtherCommand extends HomeCommand implements Command<ServerCommandSource> {

public HomeTeleportOtherCommand() {
}

@Override
public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
PlayerData senderPlayerData = ((ServerPlayerEntityAccess) context.getSource().getPlayer()).getEcPlayerData();
String homeName = StringArgumentType.getString(context, "home_name");

return HomeCommand.exec(senderPlayerData, getTargetPlayerData(context), homeName);
}

private static PlayerData getTargetPlayerData(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return ((ServerPlayerEntityAccess) EntityArgumentType.getPlayer(context, "target_player")).getEcPlayerData();
}

public int runDefault(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var senderPlayerData = ((ServerPlayerEntityAccess) context.getSource().getPlayer()).getEcPlayerData();
var targetPlayerData = getTargetPlayerData(context);

return HomeCommand.exec(
senderPlayerData,
targetPlayerData,
HomeCommand.getSoleHomeName(targetPlayerData)
);
}

public static class Suggestion {
//Brigader Suggestions
public static final SuggestionProvider<ServerCommandSource> suggestedStrings
= ListSuggestion.ofContext(HomeTeleportOtherCommand.Suggestion::getSuggestionsList);

/**
* Gets a list of suggested strings to be used with Brigader
*/
public static List<String> getSuggestionsList(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return new ArrayList<>(HomeTeleportOtherCommand.getTargetPlayerData(context).getHomeNames());
}

/**
* Gets a set of suggestion entries to be used with ListCommandFactory
*/
public static Set<Map.Entry<String, MinecraftLocation>> getSuggestionEntries(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return HomeTeleportOtherCommand.getTargetPlayerData(context).getHomeEntries();
}

}
}

This file was deleted.

0 comments on commit 2076c54

Please sign in to comment.