Skip to content

Commit

Permalink
refactor: Avoid listNotOwned state by adopting Vanilla's lambda style…
Browse files Browse the repository at this point in the history
… of registering commands, remove dependency on command code in menu, simplify collection of applicable waystones #758
  • Loading branch information
BlayTheNinth committed Dec 14, 2023
1 parent 119c74f commit a358275
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 155 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,91 @@
package net.blay09.mods.waystones.command;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.blay09.mods.balm.api.command.BalmCommands;
import net.minecraft.commands.CommandSourceStack;
import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.comparator.WaystoneComparators;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.selector.EntitySelector;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.server.level.ServerPlayer;

import static net.minecraft.commands.Commands.argument;

public class ModCommands {
public static void initialize(BalmCommands commands) {
commands.register(dispatcher -> dispatcher.register(LiteralArgumentBuilder.<CommandSourceStack>literal("waystones")
commands.register(dispatcher -> dispatcher.register(Commands.literal("waystones")
.requires(source -> source.isPlayer() && source.hasPermission(2))
.then(LiteralArgumentBuilder.<CommandSourceStack>literal("count")
.then(Commands.literal("count")
.then(argument("player", EntityArgument.player()).executes(new CountWaystonesCommand())))
.then(LiteralArgumentBuilder.<CommandSourceStack>literal("owned")
.then(argument("player", EntityArgument.player()).executes(new ListWaystonesCommand(false))))
.then(LiteralArgumentBuilder.<CommandSourceStack>literal("activated")
.then(argument("player", EntityArgument.player()).executes(new ListWaystonesCommand(true))))
.then(LiteralArgumentBuilder.<CommandSourceStack>literal("gui")
.then(Commands.literal("list")
.then(argument("player", EntityArgument.player()).executes(ctx -> {
final var caller = ctx.getSource().getPlayerOrException();
final var target = ctx.getArgument("player", EntitySelector.class).findSinglePlayer(ctx.getSource());
final var waystones = PlayerWaystoneManager.getWaystones(target)
.stream()
.filter(it -> it.isOwner(target))
.sorted(WaystoneComparators.forAdminInspection(caller, target))
.toList();
ctx.getSource().sendSystemMessage(Component.translatable("commands.waystones.list.header", target.getScoreboardName()));
for (var waystone : waystones) {
ctx.getSource().sendSystemMessage(componentForWaystoneList(caller, target, waystone));
}
final var result = Component.translatable("commands.waystones.list.footer", waystones.size());
ctx.getSource().sendSuccess(() -> result, false);
return waystones.size();
})
.then(Commands.literal("all").executes(ctx -> {
final var caller = ctx.getSource().getPlayerOrException();
final var target = ctx.getArgument("player", EntitySelector.class).findSinglePlayer(ctx.getSource());
final var waystones = PlayerWaystoneManager.getWaystones(target)
.stream()
.sorted(WaystoneComparators.forAdminInspection(caller, target))
.toList();
ctx.getSource().sendSystemMessage(Component.translatable("commands.waystones.list.all.header", target.getScoreboardName()));
for (var waystone : waystones) {
ctx.getSource().sendSystemMessage(componentForWaystoneList(caller, target, waystone));
}
final var ownedCount = waystones.stream().filter(it -> it.isOwner(target)).count();
final var result = Component.translatable("commands.waystones.list.all.footer", waystones.size(), ownedCount);
ctx.getSource().sendSuccess(() -> result, false);
return waystones.size();
}))))
.then(Commands.literal("gui")
.then(argument("player", EntityArgument.player()).executes(new OpenPlayerWaystonesGuiCommand())))
));
}

private static Component componentForWaystoneList(ServerPlayer caller, ServerPlayer target, IWaystone waystone) {
final var waystoneDimensionId = waystone.getDimension().location();
final var waystonePos = waystone.getPos();
Component location;
if (waystone.getDimension() != caller.level().dimension()) {
location = Component.translatable("commands.waystones.list.in_dimension", waystoneDimensionId);
} else {
final var distance = (int) caller.position().distanceTo(waystonePos.getCenter());
location = Component.translatable("commands.waystones.list.at_distance", distance);
}

final var suggestedCommand = String.format("/execute in %s run teleport %d %d %d",
waystoneDimensionId,
waystonePos.getX(),
waystonePos.getY(),
waystonePos.getZ());

final var coordinates = Component.translatable("commands.waystones.list.coordinates", waystonePos.getX(), waystonePos.getY(), waystonePos.getZ())
.withStyle(ChatFormatting.YELLOW)
.withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestedCommand)));

final var waystoneName = Component.literal(waystone.getName())
.withStyle(ChatFormatting.GREEN)
.withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestedCommand)));

final var entryKey = waystone.isOwner(target) ? "commands.waystones.list.entry.owned" : "commands.waystones.list.entry.activated";
return Component.translatable(entryKey, location, coordinates, waystoneName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.menu.BalmMenuProvider;
import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.comparator.WaystoneComparators;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.blay09.mods.waystones.core.Waystone;
import net.blay09.mods.waystones.menu.WaystoneSelectionMenu;
import net.minecraft.commands.CommandSourceStack;
Expand All @@ -17,10 +18,6 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;

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

public class OpenPlayerWaystonesGuiCommand implements Command<CommandSourceStack> {
@Override
public int run(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
Expand All @@ -29,7 +26,7 @@ public int run(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxExcep
BalmMenuProvider menuProvider = new BalmMenuProvider() {
@Override
public Component getDisplayName() {
return Component.translatable( "container.waystones.waystone_admin_selection", target.getScoreboardName());
return Component.translatable("container.waystones.waystone_admin_selection", target.getScoreboardName());
}

@Override
Expand All @@ -39,10 +36,8 @@ public AbstractContainerMenu createMenu(int i, Inventory playerInventory, Player

@Override
public void writeScreenOpeningData(ServerPlayer player, FriendlyByteBuf buf) {
Map<WaystoneOwnership, List<IWaystone>> all = ListWaystonesCommand.ownedOrActivatedByDistance(player, op);
List<IWaystone> waystones = new ArrayList<>();
waystones.addAll(all.get(WaystoneOwnership.OWNED));
waystones.addAll(all.get(WaystoneOwnership.ACTIVATED));
final var waystones = PlayerWaystoneManager.getWaystones(target);
waystones.sort(WaystoneComparators.forAdminInspection(player, target));
buf.writeInt(waystones.size());
waystones.forEach(w -> Waystone.write(buf, w));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.blay09.mods.waystones.comparator;

import net.blay09.mods.waystones.api.IWaystone;
import net.minecraft.world.entity.player.Player;

import java.util.Comparator;

public class DistanceToPlayerComparator implements Comparator<IWaystone> {

private final Player player;

public DistanceToPlayerComparator(Player player) {
this.player = player;
}

@Override
public int compare(IWaystone o1, IWaystone o2) {
double distance1 = o1.getPos().distSqr(player.blockPosition());
double distance2 = o2.getPos().distSqr(player.blockPosition());
return Double.compare(distance1, distance2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.blay09.mods.waystones.comparator;

import net.blay09.mods.waystones.api.IWaystone;
import net.minecraft.world.entity.player.Player;

import java.util.Comparator;

public class PreferOwnedComparator implements Comparator<IWaystone> {

private final Player owner;

public PreferOwnedComparator(Player owner) {
this.owner = owner;
}

@Override
public int compare(IWaystone o1, IWaystone o2) {
if (o1.isOwner(owner) && !o2.isOwner(owner)) {
return -1;
} else if (!o1.isOwner(owner) && o2.isOwner(owner)) {
return 1;
}

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.blay09.mods.waystones.comparator;

import net.blay09.mods.waystones.api.IWaystone;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;

import java.util.Comparator;

public class PreferSameDimensionComparator implements Comparator<IWaystone> {

private final ResourceKey<Level> dimensionId;

public PreferSameDimensionComparator(ResourceKey<Level> dimensionId) {
this.dimensionId = dimensionId;
}

@Override
public int compare(IWaystone o1, IWaystone o2) {
if (o1.getDimension().equals(dimensionId) && !o2.getDimension().equals(dimensionId)) {
return -1;
} else if (!o1.getDimension().equals(dimensionId) && o2.getDimension().equals(dimensionId)) {
return 1;
}

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.blay09.mods.waystones.comparator;

import net.blay09.mods.waystones.api.IWaystone;
import net.minecraft.world.entity.player.Player;

import java.util.Comparator;

public class WaystoneComparators {
public static Comparator<IWaystone> forAdminInspection(Player caller, Player target) {
return new PreferOwnedComparator(target)
.thenComparing(new PreferSameDimensionComparator(caller.level().dimension())
.thenComparing(new DistanceToPlayerComparator(caller)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.block.SharestoneBlock;
import net.blay09.mods.waystones.command.ListWaystonesCommand;
import net.blay09.mods.waystones.command.WaystoneOwnership;
import net.blay09.mods.waystones.comparator.WaystoneComparators;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.blay09.mods.waystones.core.WarpMode;
import net.blay09.mods.waystones.core.WaystoneManager;
Expand All @@ -18,9 +17,7 @@
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class WaystoneSelectionMenu extends AbstractContainerMenu {
Expand Down Expand Up @@ -76,11 +73,9 @@ public static WaystoneSelectionMenu createSharestoneSelection(MinecraftServer se
return new WaystoneSelectionMenu(ModMenus.sharestoneSelection.get(), WarpMode.SHARESTONE_TO_SHARESTONE, fromWaystone, windowId, waystones);
}

public static WaystoneSelectionMenu createAdminSelection(int windowId, Player op, Player target) {
Map<WaystoneOwnership, List<IWaystone>> all = ListWaystonesCommand.ownedOrActivatedByDistance(target, op);
List<IWaystone> waystones = new ArrayList<>();
waystones.addAll(all.get(WaystoneOwnership.OWNED));
waystones.addAll(all.get(WaystoneOwnership.ACTIVATED));
public static WaystoneSelectionMenu createAdminSelection(int windowId, Player player, Player target) {
final var waystones = PlayerWaystoneManager.getWaystones(target);
waystones.sort(WaystoneComparators.forAdminInspection(player, target));
return new WaystoneSelectionMenu(ModMenus.adminSelection.get(), WarpMode.CUSTOM, null, windowId, waystones);
}
}
Loading

0 comments on commit a358275

Please sign in to comment.