-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Avoid listNotOwned state by adopting Vanilla's lambda style…
… of registering commands, remove dependency on command code in menu, simplify collection of applicable waystones #758
- Loading branch information
1 parent
119c74f
commit a358275
Showing
9 changed files
with
180 additions
and
155 deletions.
There are no files selected for viewing
121 changes: 0 additions & 121 deletions
121
shared/src/main/java/net/blay09/mods/waystones/command/ListWaystonesCommand.java
This file was deleted.
Oops, something went wrong.
85 changes: 76 additions & 9 deletions
85
shared/src/main/java/net/blay09/mods/waystones/command/ModCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
shared/src/main/java/net/blay09/mods/waystones/comparator/DistanceToPlayerComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
shared/src/main/java/net/blay09/mods/waystones/comparator/PreferOwnedComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
shared/src/main/java/net/blay09/mods/waystones/comparator/PreferSameDimensionComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
shared/src/main/java/net/blay09/mods/waystones/comparator/WaystoneComparators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.