Skip to content

Commit

Permalink
More 1.16.5 tweaks and fixes
Browse files Browse the repository at this point in the history
Implemented the Vanilla Chat format for "/shrug", "/tableflip", "/mainhand", "/offhand", etc.. if "chat.modify" is disabled in the config
Added custom command suggestions that display nicely formatted tooltips
- "/home" names will now show the world and coordinate that the waystone is located at
- "/protection" subcommands will now show descriptions for permissions and settings effecting a claim
Added StructureBuilder options for Waystones, to build them out of different materials depending on the biome
Added check to prevent players from making waystones on the main End island
Moved the check of building waystones to prevent the server from stealing a players money
Fixed an issue properly coloring in the name of players names when sleeping, if using a gradient color
Added additional options to EnumArgumentTypes for commands
  • Loading branch information
GStefanowich committed Jun 16, 2021
1 parent 7e07943 commit 24627c1
Show file tree
Hide file tree
Showing 18 changed files with 431 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,59 @@
import net.TheElm.project.interfaces.BoolEnums;
import net.minecraft.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.EnumSet;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public class EnumArgumentType<E extends Enum<E>> {
public static final DynamicCommandExceptionType INVALID_COMPONENT_EXCEPTION = new DynamicCommandExceptionType((object_1) -> new TranslatableText("argument.component.invalid", object_1));

private final EnumSet<E> enumValues;
private final Function<E, String> nameFormatter;
private final Function<E, Text> tooltipFormatter;

private EnumArgumentType( final Class<E> enumClass ) {
this.enumValues = EnumSet.allOf( enumClass );
private EnumArgumentType(@NotNull final Class<E> enumClass) {
this(enumClass, null);
}
private EnumArgumentType(@NotNull final Class<E> enumClass, @Nullable Function<E, Text> tooltips) {
this(enumClass, tooltips, (enumValue) -> enumValue.name().toLowerCase());
}
private EnumArgumentType(@NotNull final Class<E> enumClass, @Nullable Function<E, Text> tooltips, @NotNull Function<E, String> names) {
this.enumValues = EnumSet.allOf(enumClass);
this.nameFormatter = names;
this.tooltipFormatter = tooltips;
}

public static <T extends Enum<T>> T getEnum(Class<T> tClass, String search) throws CommandSyntaxException {
return EnumSet.allOf( tClass ).stream().filter((enumValue) -> {
return enumValue.name().equalsIgnoreCase( search );
return EnumSet.allOf(tClass).stream().filter((enumValue) -> {
return enumValue.name().equalsIgnoreCase(search);
}).findFirst().orElseThrow(() -> INVALID_COMPONENT_EXCEPTION.create(search));
}

public <S> CompletableFuture<Suggestions> suggests(CommandContext<S> context, SuggestionsBuilder builder) {
return CommandSource.suggestMatching(
this.enumValues.stream()
.filter((val) -> ((!(val instanceof BoolEnums)) || ((BoolEnums)val).isEnabled()))
.map((enumValue) -> enumValue.name().toLowerCase()),
builder
);
public @NotNull <S> CompletableFuture<Suggestions> suggests(@NotNull CommandContext<S> context, @NotNull SuggestionsBuilder builder) {
final String remaining = builder.getRemaining().toLowerCase(Locale.ROOT);
this.enumValues.stream()
.filter((eVal) -> ((!(eVal instanceof BoolEnums)) || ((BoolEnums)eVal).isEnabled()))
.filter((eName) -> CommandSource.method_27136(remaining, eName.name().toLowerCase(Locale.ROOT)))
.forEach((eVal) -> {
Text text = this.tooltipFormatter != null ? this.tooltipFormatter.apply(eVal) : null;
builder.suggest(this.nameFormatter.apply(eVal), text);
});
return builder.buildFuture();
}
public static @NotNull <E extends Enum<E>> SuggestionProvider<ServerCommandSource> enumerate(@NotNull Class<E> claimRanksClass) {
return new EnumArgumentType<>(claimRanksClass)::suggests;
}
public static @NotNull <E extends Enum<E>> SuggestionProvider<ServerCommandSource> enumerate(@NotNull Class<E> claimRanksClass, @NotNull Function<E, Text> tooltips) {
return new EnumArgumentType<>(claimRanksClass, tooltips)::suggests;
}
public static <E extends Enum<E>> SuggestionProvider<ServerCommandSource> enumerate(Class<E> claimRanksClass) {
return new EnumArgumentType<>( claimRanksClass )::suggests;
public static @NotNull <E extends Enum<E>> SuggestionProvider<ServerCommandSource> enumerate(@NotNull Class<E> claimRanksClass, @NotNull Function<E, Text> tooltips, @NotNull Function<E, String> names) {
return new EnumArgumentType<>(claimRanksClass, tooltips, names)::suggests;
}
}
16 changes: 8 additions & 8 deletions src/main/java/net/TheElm/project/commands/ClaimCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
// Set a friends rank
.then(CommandManager.literal("set")
.then(CommandManager.argument("rank", StringArgumentType.word())
.suggests(EnumArgumentType.enumerate(ClaimRanks.class))
.suggests(EnumArgumentType.enumerate(ClaimRanks.class, ClaimRanks::rankSetterDescription))
.then(CommandManager.argument("friend", GameProfileArgumentType.gameProfile())
.suggests(CommandUtils::getAllPlayerNames)
.executes(ClaimCommand::addRank)
Expand Down Expand Up @@ -306,7 +306,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
)
)
.then(CommandManager.literal("leave")
.requires((source) -> ClaimCommand.sourceInTown( source ) && ClaimCommand.sourceNotMayor( source ))
.requires((source) -> ClaimCommand.sourceInTown(source) && ClaimCommand.sourceNotMayor(source))
.executes(ClaimCommand::playerPartsTown)
)
.then(CommandManager.literal("set")
Expand Down Expand Up @@ -348,15 +348,15 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
// Update claim permissions
.then(CommandManager.literal("permissions")
.then(CommandManager.argument("permission", StringArgumentType.word())
.suggests(EnumArgumentType.enumerate(ClaimPermissions.class))
.suggests(EnumArgumentType.enumerate(ClaimPermissions.class, ClaimPermissions::getDescription))
.then(CommandManager.argument("rank", StringArgumentType.word())
.suggests(EnumArgumentType.enumerate(ClaimRanks.class))
.suggests(EnumArgumentType.enumerate(ClaimRanks.class, ClaimRanks::rankNormalDescription))
.executes(ClaimCommand::updateSetting)
)
)
.then(CommandManager.literal("*")
.then(CommandManager.argument( "rank", StringArgumentType.word())
.suggests(EnumArgumentType.enumerate(ClaimRanks.class))
.suggests(EnumArgumentType.enumerate(ClaimRanks.class, ClaimRanks::rankNormalDescription))
.executes(ClaimCommand::updateSettings)
)
)
Expand All @@ -368,7 +368,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
// Chunk settings
.then(CommandManager.literal("settings")
.then(CommandManager.argument("setting", StringArgumentType.word())
.suggests(EnumArgumentType.enumerate(ClaimSettings.class))
.suggests(EnumArgumentType.enumerate(ClaimSettings.class, ClaimSettings::getDescription))
.then(CommandManager.argument("bool", BoolArgumentType.bool())
.executes(ClaimCommand::updateBoolean)
)
Expand Down Expand Up @@ -1040,8 +1040,8 @@ private static CompletableFuture<Suggestions> listTownInvites(@NotNull CommandCo

private static int updateSetting(@NotNull CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
// Get enums
ClaimPermissions permissions = EnumArgumentType.getEnum( ClaimPermissions.class, StringArgumentType.getString( context, "permission" ) );
ClaimRanks rank = EnumArgumentType.getEnum( ClaimRanks.class, StringArgumentType.getString( context, "rank" ) );
ClaimPermissions permissions = EnumArgumentType.getEnum(ClaimPermissions.class, StringArgumentType.getString(context, "permission"));
ClaimRanks rank = EnumArgumentType.getEnum(ClaimRanks.class, StringArgumentType.getString(context, "rank"));

// Get the player
ServerPlayerEntity player = context.getSource().getPlayer();
Expand Down
34 changes: 20 additions & 14 deletions src/main/java/net/TheElm/project/commands/MiscCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -92,23 +93,28 @@ public static int playerSendsMessageAndData(@NotNull ServerPlayerEntity player,
return MiscCommands.playerSendsMessageAndData(player, message, new LiteralText( main ));
}
public static int playerSendsMessageAndData(@NotNull ServerPlayerEntity player, @NotNull String message, @NotNull Text main) {
// Create the player display for chat
MutableText text = PlayerNameUtils.getPlayerChatDisplay( player, ((PlayerChat) player).getChatRoom() )
.append(new LiteralText( ": " ).formatted(Formatting.GRAY));

// Append the users message
if ( !"".equals( message ) )
text.append( message )
.append( " " );

// Append the main information
text.append( main );
MutableText text;
if (SewConfig.get(SewConfig.CHAT_MODIFY)) {
// Create the player display for chat
text = PlayerNameUtils.getPlayerChatDisplay(player, ((PlayerChat) player).getChatRoom())
.append(new LiteralText(": ").formatted(Formatting.GRAY));

// Append the users message
if (!"".equals(message))
text.append(message)
.append(" ");

// Append the main information
text.append(main);
} else {
text = new TranslatableText("chat.type.text", player.getDisplayName(), main);
}

// Send to all players
MessageUtils.sendTo(
((PlayerChat) player).getChatRoom(),
player,
text
((PlayerChat) player).getChatRoom(),
player,
text
);

return Command.SINGLE_SUCCESS;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/net/TheElm/project/commands/PermissionCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ public static void register(@NotNull CommandDispatcher<ServerCommandSource> disp
.requires(CommandUtils.isEnabledAnd(SewConfig.HANDLE_PERMISSIONS, OpLevels.CHEATING))
.then(CommandManager.literal("help")
.then(CommandManager.argument("permission", StringArgumentType.word())
.suggests( ArgumentSuggestions::suggestNodes)
.executes( PermissionCommand::nodeInfo )
.suggests(ArgumentSuggestions::suggestNodes)
.executes(PermissionCommand::nodeInfo )
)
)
.then(CommandManager.literal("add")
.then(CommandManager.argument("rank", StringArgumentType.word())
.suggests( ArgumentSuggestions::suggestRanks)
.suggests(ArgumentSuggestions::suggestRanks)
.then(CommandManager.argument("permission", StringArgumentType.word())
.suggests( ArgumentSuggestions::suggestNodes)
.executes( PermissionCommand::addNodeToRank )
.suggests(ArgumentSuggestions::suggestNodes)
.executes(PermissionCommand::addNodeToRank )
)
.executes( PermissionCommand::addRank )
.executes(PermissionCommand::addRank)
)
)
.then(CommandManager.literal("remove")
.then(CommandManager.argument("rank", StringArgumentType.word())
.suggests( ArgumentSuggestions::suggestRanks)
.suggests(ArgumentSuggestions::suggestRanks)
.then(CommandManager.argument("permission", StringArgumentType.word())
.suggests( ArgumentSuggestions::suggestNodes)
.executes( PermissionCommand::delNodeFromRank )
.suggests(ArgumentSuggestions::suggestNodes)
.executes(PermissionCommand::delNodeFromRank )
)
.executes( PermissionCommand::delRank )
.executes(PermissionCommand::delRank)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import net.TheElm.project.utilities.*;
import net.TheElm.project.utilities.WarpUtils.Warp;
import net.TheElm.project.utilities.text.MessageUtils;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.GameProfileArgumentType;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -273,13 +272,13 @@ private static int setHomePrimaryCommand(@NotNull CommandContext<ServerCommandSo

private static @NotNull CompletableFuture<Suggestions> playerHomeNames(@NotNull CommandContext<ServerCommandSource> context, @NotNull SuggestionsBuilder builder) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
return CommandSource.suggestMatching(WarpUtils.getWarpNames(source.getPlayer()), builder);
return WarpUtils.buildSuggestions(source.getPlayer(), builder);
}
private static @NotNull CompletableFuture<Suggestions> playerHomeNamesOfPlayer(@NotNull CommandContext<ServerCommandSource> context, @NotNull SuggestionsBuilder builder) throws CommandSyntaxException {
Collection<GameProfile> profiles = GameProfileArgumentType.getProfileArgument(context, "player");
GameProfile target = profiles.stream().findAny()
.orElseThrow(GameProfileArgumentType.UNKNOWN_PLAYER_EXCEPTION::create);
return CommandSource.suggestMatching(WarpUtils.getWarpNames(target.getId()), builder);
return WarpUtils.buildSuggestions(target.getId(), builder);
}

private static int tpaCommand(@NotNull CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Expand Down Expand Up @@ -418,7 +417,7 @@ private static int sendEntitiesToEnd(@NotNull Collection<? extends Entity> entit
return Command.SINGLE_SUCCESS;
}

private static void feedback(@NotNull PlayerEntity porter, @NotNull PlayerEntity target, @Nullable String location) {
public static void feedback(@NotNull PlayerEntity porter, @NotNull PlayerEntity target, @Nullable String location) {
TeleportsCommand.feedback(porter, target.getGameProfile(), location);
}
public static void feedback(@NotNull PlayerEntity porter, @NotNull GameProfile target, @Nullable String location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import net.TheElm.project.utilities.TranslatableServerSide;
import net.TheElm.project.utilities.WarpUtils;
import net.TheElm.project.utilities.text.MessageUtils;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.GameProfileArgumentType;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -225,12 +224,12 @@ private static int sendPlayersToLocation(@NotNull CommandContext<ServerCommandSo

private static @NotNull CompletableFuture<Suggestions> getPlayerEntityLocations(@NotNull CommandContext<ServerCommandSource> context, @NotNull SuggestionsBuilder builder) throws CommandSyntaxException {
ServerPlayerEntity player = EntityArgumentType.getPlayer(context, "player");
return CommandSource.suggestMatching(WarpUtils.getWarpNames(player), builder);
return WarpUtils.buildSuggestions(player, builder);
}
private static @NotNull CompletableFuture<Suggestions> getPlayersToLocations(@NotNull CommandContext<ServerCommandSource> context, @NotNull SuggestionsBuilder builder) throws CommandSyntaxException {
Collection<GameProfile> profiles = GameProfileArgumentType.getProfileArgument(context, "to");
GameProfile target = profiles.stream().findAny()
.orElseThrow(GameProfileArgumentType.UNKNOWN_PLAYER_EXCEPTION::create);
return CommandSource.suggestMatching(WarpUtils.getWarpNames(target.getId()), builder);
return WarpUtils.buildSuggestions(target.getId(), builder);
}
}
40 changes: 27 additions & 13 deletions src/main/java/net/TheElm/project/enums/ClaimPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,41 @@

package net.TheElm.project.enums;

import net.TheElm.project.utilities.CasingUtils;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;

public enum ClaimPermissions {

CREATURES( ClaimRanks.PASSIVE ), // Harm passive mods
HARVEST( ClaimRanks.ALLY ), // Harvest plants
BLOCKS( ClaimRanks.ALLY ), // Break or Place blocks
STORAGE( ClaimRanks.ALLY ), // Access storage containers
DOORS( ClaimRanks.PASSIVE ), // Open doors and gates
PICKUP( ClaimRanks.ALLY ), // Pickup entities
RIDING( ClaimRanks.ALLY ), // Ride carts and animals
WARP( ClaimRanks.OWNER ), // Warp to the players warp location
TRADING( ClaimRanks.PASSIVE ), // Trade with villagers
CRAFTING( ClaimRanks.PASSIVE ) // Open crafting benches
CREATURES(ClaimRanks.PASSIVE, "Harm or kill passive mobs"), // Harm passive mods
HARVEST(ClaimRanks.ALLY, "Harvest crops that are fully grown"), // Harvest plants
BLOCKS(ClaimRanks.ALLY, "Place or break any block"), // Break or Place blocks
STORAGE(ClaimRanks.ALLY, "Access shared inventories: chests, hoppers, furnaces, etc"), // Access storage containers
DOORS(ClaimRanks.PASSIVE, "Open or close doors"), // Open doors and gates
PICKUP(ClaimRanks.ALLY, "Pickup dropped items"), // Pickup entities
RIDING(ClaimRanks.ALLY, "Ride on horses and llamas"), // Ride carts and animals
WARP(ClaimRanks.OWNER, "Teleport to waystones"), // Warp to the players warp location
TRADING(ClaimRanks.PASSIVE, "Make trades with villagers"), // Trade with villagers
CRAFTING(ClaimRanks.PASSIVE, "Access non-shared inventories: crafting tables, looms, etc") // Open crafting benches
;

private final ClaimRanks defaultRank;
private final @NotNull ClaimRanks defaultRank;
private final @NotNull Text description;

ClaimPermissions(ClaimRanks defaultRank) {
ClaimPermissions(@NotNull ClaimRanks defaultRank, @NotNull String description) {
this.defaultRank = defaultRank;
this.description = new LiteralText(description)
.append(" (Default: ")
.append(new LiteralText(CasingUtils.Sentence(this.defaultRank.name()))
.formatted(this.defaultRank.getColor()))
.append(")");
}

public ClaimRanks getDefault() {
public @NotNull Text getDescription() {
return this.description;
}
public @NotNull ClaimRanks getDefault() {
return this.defaultRank;
}
}
Loading

0 comments on commit 24627c1

Please sign in to comment.