Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx1G
minecraft_version=1.21.11
loader_version=0.18.4
# Mod Properties
mod_version=1.5
mod_version=1.5.1
maven_group=org.AndrewElizabeth
archives_base_name=tpc-fabric
# Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Constants {
public static final String MOD_ID = "teleport_commands_fabric";
public static final String ASSETS_ID = "teleport_commands_fabric";
public static final String MOD_NAME = "Teleport Commands Fabric";
public static final String VERSION = "1.5";
public static final String VERSION = "1.5.1";
public static final int CONFIG_VERSION = 2;
public static final int STORAGE_VERSION = 3;
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;

public class TeleportCommands implements ModInitializer {
public static String MOD_LOADER;
Expand All @@ -39,6 +40,10 @@ public void onInitialize() {

net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
org.AndrewElizabeth.teleportcommandsfabric.storage.StorageManager.forceSaveOnShutdown();
TeleportCommands.SERVER = null;
});
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
registerCommands(dispatcher);
});
MOD_LOADER = "Fabric";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.AndrewElizabeth.teleportcommandsfabric.commands.common;

import org.AndrewElizabeth.teleportcommandsfabric.Constants;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;

import static org.AndrewElizabeth.teleportcommandsfabric.utils.TranslationHelper.getTranslatedText;

public final class CommandExecutionSupport {
@FunctionalInterface
public interface CommandAction {
void run() throws Exception;
}

private CommandExecutionSupport() {
}

public static void send(ServerPlayer player, String key, ChatFormatting... formatting) {
player.displayClientMessage(getTranslatedText(key, player).withStyle(formatting), true);
}

public static void sendInvalidPage(ServerPlayer player, int requestedPage, int totalPages) {
player.displayClientMessage(
getTranslatedText("commands.teleport_commands.common.invalidPage", player,
Component.literal(String.valueOf(requestedPage)),
Component.literal(String.valueOf(totalPages)))
.withStyle(ChatFormatting.RED),
true);
}

public static int execute(ServerPlayer player, String errorLogMessage, String errorTranslationKey,
CommandAction action) {
try {
action.run();
return 0;
} catch (Exception e) {
Constants.LOGGER.error(errorLogMessage, e);
player.displayClientMessage(
getTranslatedText(errorTranslationKey, player)
.withStyle(ChatFormatting.RED, ChatFormatting.BOLD),
true);
return 1;
}
}

public static int executeSilently(String errorLogMessage, CommandAction action) {
try {
action.run();
return 0;
} catch (Exception e) {
Constants.LOGGER.error(errorLogMessage, e);
return 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.AndrewElizabeth.teleportcommandsfabric.commands.common;

import org.AndrewElizabeth.teleportcommandsfabric.common.NamedLocation;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;

import static org.AndrewElizabeth.teleportcommandsfabric.utils.TranslationHelper.getTranslatedText;

public final class CommandUiSupport {
private CommandUiSupport() {
}

public static void appendNameLine(MutableComponent message, String name, MutableComponent... suffixes) {
message.append("\n");
message.append(Component.literal(" - " + name).withStyle(ChatFormatting.AQUA));
for (MutableComponent suffix : suffixes) {
if (suffix == null) {
continue;
}
message.append(" ").append(suffix);
}
}

public static void appendLocationLine(MutableComponent message, ServerPlayer player, NamedLocation location) {
String coords = String.format("[X%d Y%d Z%d]", location.getX(), location.getY(), location.getZ());
String dimension = String.format(" [%s]", location.getWorldString());

message.append("\n");
message.append(Component.literal(" | ").withStyle(ChatFormatting.AQUA))
.append(Component.literal(coords)
.withStyle(ChatFormatting.LIGHT_PURPLE)
.withStyle(style -> style.withClickEvent(
new ClickEvent.CopyToClipboard(
String.format("X%d Y%d Z%d", location.getX(), location.getY(),
location.getZ()))))
.withStyle(style -> style.withHoverEvent(
new HoverEvent.ShowText(
getTranslatedText("commands.teleport_commands.common.hoverCopy", player)))))
.append(Component.literal(dimension)
.withStyle(ChatFormatting.DARK_PURPLE)
.withStyle(style -> style
.withClickEvent(new ClickEvent.CopyToClipboard(location.getWorldString())))
.withStyle(style -> style.withHoverEvent(
new HoverEvent.ShowText(
getTranslatedText("commands.teleport_commands.common.hoverCopy", player)))));
}

public static MutableComponent translatedButton(ServerPlayer player, String key, ChatFormatting color,
ClickEvent clickEvent) {
MutableComponent component = getTranslatedText(key, player).withStyle(color);
if (clickEvent != null) {
component = component.withStyle(style -> style.withClickEvent(clickEvent));
}
return component;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.AndrewElizabeth.teleportcommandsfabric.commands.common;

import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;

import java.util.List;

public final class PagedListCommandSupport {
@FunctionalInterface
public interface EmptyHandler {
void run();
}

@FunctionalInterface
public interface InvalidPageHandler {
void run(int requestedPage, int totalPages);
}

@FunctionalInterface
public interface PageRenderer<T> {
MutableComponent render(List<T> pageEntries, int currentPage, int totalPages);
}

@FunctionalInterface
public interface PagePickerRenderer {
MutableComponent render(int currentPage, int totalPages);
}

private PagedListCommandSupport() {
}

public static <T> void displayPage(ServerPlayer player, List<T> entries, int page, EmptyHandler onEmpty,
InvalidPageHandler onInvalidPage, PageRenderer<T> renderer) {
if (entries.isEmpty()) {
onEmpty.run();
return;
}

int totalPages = PaginationCommandSupport.getTotalPages(entries.size());
if (!PaginationCommandSupport.isValidPage(page, totalPages)) {
onInvalidPage.run(page, totalPages);
return;
}

player.displayClientMessage(
renderer.render(PaginationCommandSupport.getPageEntries(entries, page), page, totalPages),
false);
}

public static <T> void displayPagePicker(ServerPlayer player, List<T> entries, int currentPage,
EmptyHandler onEmpty,
InvalidPageHandler onInvalidPage, PagePickerRenderer renderer) {
if (entries.isEmpty()) {
onEmpty.run();
return;
}

int totalPages = PaginationCommandSupport.getTotalPages(entries.size());
if (!PaginationCommandSupport.isValidPage(currentPage, totalPages)) {
onInvalidPage.run(currentPage, totalPages);
return;
}

player.displayClientMessage(renderer.render(currentPage, totalPages), false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package org.AndrewElizabeth.teleportcommandsfabric.commands.common;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;

import java.util.List;

import static org.AndrewElizabeth.teleportcommandsfabric.utils.TranslationHelper.getTranslatedText;

public final class PaginationCommandSupport {
public static final int PAGE_SIZE = 4;
private static final int PAGE_PICKER_COLUMNS = 8;

private PaginationCommandSupport() {
}

public static int getTotalPages(int totalItems) {
return Math.max(1, (totalItems + PAGE_SIZE - 1) / PAGE_SIZE);
}

public static boolean isValidPage(int page, int totalPages) {
return page >= 1 && page <= totalPages;
}

public static <T> List<T> getPageEntries(List<T> entries, int page) {
int fromIndex = (page - 1) * PAGE_SIZE;
int toIndex = Math.min(fromIndex + PAGE_SIZE, entries.size());
return entries.subList(fromIndex, toIndex);
}

public static MutableComponent buildHeader(ServerPlayer player, String titleKey, int currentPage, int totalPages) {
MutableComponent header = Component.literal("========== ").withStyle(ChatFormatting.DARK_GRAY);
header.append(getTranslatedText(titleKey, player).withStyle(ChatFormatting.YELLOW, ChatFormatting.BOLD));
header.append(Component.literal(" (").withStyle(ChatFormatting.DARK_GRAY));
header.append(getTranslatedText(
"commands.teleport_commands.common.page",
player,
Component.literal(String.valueOf(currentPage)),
Component.literal(String.valueOf(totalPages))).withStyle(ChatFormatting.GOLD));
header.append(Component.literal(") ==========").withStyle(ChatFormatting.DARK_GRAY));
return header;
}

public static MutableComponent buildNavigation(ServerPlayer player, int currentPage, int totalPages,
String listCommand, String jumpCommand) {
MutableComponent navigation = Component.empty();
navigation.append("\n");
navigation.append(buildNavButton(player, "commands.teleport_commands.common.first",
currentPage > 1 ? listCommand + " 1" : null));
navigation.append(" ");
navigation.append(buildNavButton(player, "commands.teleport_commands.common.prev",
currentPage > 1 ? listCommand + " " + (currentPage - 1) : null));
navigation.append(" ");

int startPage = Math.max(1, currentPage - 1);
int endPage = Math.min(totalPages, startPage + 2);
startPage = Math.max(1, endPage - 2);
for (int page = startPage; page <= endPage; page++) {
if (page > startPage) {
navigation.append(" ");
}
navigation.append(buildPageButton(page, currentPage, listCommand));
}

navigation.append(" ");
navigation.append(buildNavButton(player, "commands.teleport_commands.common.jump",
jumpCommand + " " + currentPage));
navigation.append(" ");
navigation.append(buildNavButton(player, "commands.teleport_commands.common.next",
currentPage < totalPages ? listCommand + " " + (currentPage + 1) : null));
navigation.append(" ");
navigation.append(buildNavButton(player, "commands.teleport_commands.common.last",
currentPage < totalPages ? listCommand + " " + totalPages : null));
return navigation;
}

public static MutableComponent buildPagePicker(ServerPlayer player, String titleKey, int currentPage,
int totalPages,
String listCommand) {
MutableComponent picker = Component.empty();
picker.append(getTranslatedText(
"commands.teleport_commands.common.pagePickerTitle",
player,
getTranslatedText(titleKey, player),
Component.literal(String.valueOf(currentPage)),
Component.literal(String.valueOf(totalPages)))
.withStyle(ChatFormatting.YELLOW, ChatFormatting.BOLD));

for (int page = 1; page <= totalPages; page++) {
if ((page - 1) % PAGE_PICKER_COLUMNS == 0) {
picker.append("\n");
} else {
picker.append(" ");
}
picker.append(buildPageButton(page, currentPage, listCommand));
}

return picker;
}

private static MutableComponent buildNavButton(ServerPlayer player, String translationKey, String command) {
MutableComponent button = getTranslatedText(translationKey, player);
if (command == null) {
return button.withStyle(ChatFormatting.DARK_GRAY);
}
return button.withStyle(ChatFormatting.AQUA)
.withStyle(style -> style.withClickEvent(new ClickEvent.RunCommand(command)));
}

private static MutableComponent buildPageButton(int page, int currentPage, String listCommand) {
MutableComponent button = Component.literal("[" + page + "]");
if (page == currentPage) {
return button.withStyle(ChatFormatting.GOLD, ChatFormatting.BOLD);
}
return button.withStyle(ChatFormatting.GREEN)
.withStyle(style -> style.withClickEvent(new ClickEvent.RunCommand(listCommand + " " + page)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.AndrewElizabeth.teleportcommandsfabric.commands.common;

import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import org.AndrewElizabeth.teleportcommandsfabric.Constants;

import java.util.concurrent.CompletableFuture;

public final class SuggestionCommandSupport {
@FunctionalInterface
public interface SuggestionSupplier {
Iterable<String> get() throws Exception;
}

private SuggestionCommandSupport() {
}

public static CompletableFuture<Suggestions> suggest(SuggestionsBuilder builder, String errorLogMessage,
SuggestionSupplier supplier) {
try {
for (String suggestion : supplier.get()) {
if (suggestion == null || suggestion.isBlank()) {
continue;
}
builder.suggest(suggestion);
}
} catch (Exception e) {
Constants.LOGGER.error(errorLogMessage, e);
}
return builder.buildFuture();
}
}
Loading
Loading