Skip to content

Commit

Permalink
坐标相关内容修改
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Nov 4, 2024
1 parent 52c168a commit 19b0a55
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 112 deletions.
24 changes: 4 additions & 20 deletions src/main/java/dev/dubhe/gugle/carpet/commands/HereCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import dev.dubhe.gugle.carpet.GcaSetting;
import dev.dubhe.gugle.carpet.tools.PosUtils;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class HereCommand {
public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Expand All @@ -34,18 +27,9 @@ public static int execute(@NotNull CommandContext<CommandSourceStack> context) {
if (!source.isPlayer()) return 0;
ServerPlayer player = source.getPlayer();
if (player == null) return 0;
server.getPlayerList().broadcastSystemMessage(HereCommand.playerPos(player), false);
for (MutableComponent component : PosUtils.playerPos(player)) {
server.getPlayerList().broadcastSystemMessage(component, false);
}
return 1;
}

public static @NotNull MutableComponent playerPos(@NotNull ServerPlayer player) {
player.addEffect(new MobEffectInstance(MobEffects.GLOWING, 200, 0, true, false));
Vec3 position = player.position();
ResourceKey<Level> dimension = player.level().dimension();
String name = player.getGameProfile().getName();
List<MutableComponent> pos = LocCommand.pos("Shared Location", position.x, position.y, position.z, dimension);
MutableComponent component = Component.literal("%s at".formatted(name)).append(" ").append(pos.get(0));
if (pos.size() > 2) component.append("->").append(pos.get(2));
return component;
}
}
92 changes: 5 additions & 87 deletions src/main/java/dev/dubhe/gugle/carpet/commands/LocCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
import com.mojang.brigadier.arguments.LongArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import dev.dubhe.gugle.carpet.GcaExtension;
import dev.dubhe.gugle.carpet.GcaSetting;
import dev.dubhe.gugle.carpet.tools.FilesUtil;
import dev.dubhe.gugle.carpet.tools.PosUtils;
import dev.dubhe.gugle.carpet.tools.IdGenerator;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.DimensionArgument;
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
Expand All @@ -29,7 +26,6 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -48,14 +44,6 @@ public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispa
.then(
Commands.argument("desc", StringArgumentType.greedyString())
.executes(LocCommand::add)
.then(
Commands.argument("pos", Vec3Argument.vec3())
.executes(LocCommand::add)
.then(
Commands.argument("dim", DimensionArgument.dimension())
.executes(LocCommand::add)
)
)
)
)
.then(
Expand Down Expand Up @@ -97,21 +85,8 @@ public static int add(CommandContext<CommandSourceStack> context) {
CommandSourceStack source = context.getSource();
long id = IdGenerator.nextId();
String desc = StringArgumentType.getString(context, "desc");
Vec3 pos;
try {
pos = Vec3Argument.getVec3(context, "pos");
} catch (IllegalArgumentException ignored) {
pos = source.getPosition();
}
ResourceKey<Level> dim;
try {
dim = DimensionArgument.getDimension(context, "dim").dimension();
} catch (IllegalArgumentException ignored) {
dim = source.getLevel().dimension();
} catch (CommandSyntaxException e) {
GcaExtension.LOGGER.error(e.getMessage(), e);
dim = source.getLevel().dimension();
}
Vec3 pos = source.getPosition();
ResourceKey<Level> dim = source.getLevel().dimension();
LOC_POINT.map.put(id, new LocPoint(id, desc, pos.x, pos.y, pos.z, dim));
LOC_POINT.save();
source.sendSuccess(() -> Component.literal("Loc %s is added.".formatted(desc)), false);
Expand Down Expand Up @@ -189,7 +164,7 @@ public static int list(CommandContext<CommandSourceStack> context) {
.applyFormat(ChatFormatting.GRAY)
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(Long.toString(locPoint.id))))
);
List<MutableComponent> pos = LocCommand.pos(locPoint.desc, locPoint.x, locPoint.y, locPoint.z, locPoint.dimType);
List<MutableComponent> pos = PosUtils.pos(locPoint.desc, locPoint.x, locPoint.y, locPoint.z, locPoint.dimType);
MutableComponent info = Component.literal("[i]").withStyle(
Style.EMPTY
.applyFormat(ChatFormatting.YELLOW)
Expand Down Expand Up @@ -234,7 +209,7 @@ public static int info(CommandContext<CommandSourceStack> context) {
} else {
dimType = Component.literal(point.dimType.location().toString());
}
List<MutableComponent> pos = LocCommand.pos(point.desc, point.x, point.y, point.z, point.dimType);
List<MutableComponent> pos = PosUtils.pos(point.desc, point.x, point.y, point.z, point.dimType);
List<Component> result = new ArrayList<>();
result.add(Component.literal("==================").withStyle(ChatFormatting.YELLOW));
result.add(Component.literal("Loc Point: ").append(desc));
Expand All @@ -247,63 +222,6 @@ public static int info(CommandContext<CommandSourceStack> context) {
return result;
}

public static @NotNull @Unmodifiable List<MutableComponent> pos(String desc, double x, double y, double z, @NotNull ResourceKey<Level> dimension) {
MutableComponent pos = Component.literal("[%.2f, %.2f, %.2f]".formatted(x, y, z)).withStyle(
Style.EMPTY
.applyFormat(
dimension == Level.OVERWORLD ?
ChatFormatting.GREEN :
dimension == Level.NETHER ?
ChatFormatting.RED :
dimension == Level.END ?
ChatFormatting.LIGHT_PURPLE :
ChatFormatting.AQUA
)
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(dimension.location().toString())))
);
double scale = 0;
ResourceKey<Level> toDimension = Level.END;
if (dimension == Level.NETHER) {
scale = 8;
toDimension = Level.OVERWORLD;
} else if (dimension == Level.OVERWORLD) {
scale = 0.125;
toDimension = Level.NETHER;
}
MutableComponent toPos = Component.literal("[%.2f, %.2f, %.2f]".formatted(x * scale, y, z * scale)).withStyle(
Style.EMPTY
.applyFormat(
dimension == Level.OVERWORLD ?
ChatFormatting.RED :
dimension == Level.NETHER ?
ChatFormatting.GREEN :
ChatFormatting.AQUA
)
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(toDimension.location().toString())))
);
return scale > 0 ?
List.of(pos, xaero(desc, x, y, z, dimension), toPos, xaero(desc, x * scale, y, z * scale, toDimension)) :
List.of(pos, xaero(desc, x, y, z, dimension));
}

public static @NotNull MutableComponent xaero(String desc, double x, double y, double z, @NotNull ResourceKey<Level> dimType) {
int color = dimType == Level.OVERWORLD ? 10 :
dimType == Level.NETHER ? 12 :
dimType == Level.END ? 13 : 11;
return Component.literal(
"xaero-waypoint:%s:%s:%.0f:%.0f:%.0f:%d:false:0:Internal-%s-waypoints"
.formatted(
desc,
desc.substring(0, 1),
x,
y,
z,
color,
dimType.location().getPath()
)
);
}

public record LocPoint(
long id,
String desc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.dubhe.gugle.carpet.GcaSetting;
import dev.dubhe.gugle.carpet.tools.PosUtils;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.NotNull;

Expand All @@ -25,7 +27,9 @@ public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispa

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = EntityArgument.getPlayer(context, "player");
context.getSource().sendSuccess(() -> HereCommand.playerPos(player), false);
for (MutableComponent component : PosUtils.playerPos(player)) {
context.getSource().sendSuccess(() -> component, false);
}
return 1;
}
}
5 changes: 2 additions & 3 deletions src/main/java/dev/dubhe/gugle/carpet/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import dev.dubhe.gugle.carpet.GcaExtension;
import dev.dubhe.gugle.carpet.GcaSetting;
import dev.dubhe.gugle.carpet.tools.GcaUtils;
import dev.dubhe.gugle.carpet.api.tools.text.ComponentTranslate;
import dev.dubhe.gugle.carpet.tools.*;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -50,7 +49,7 @@ private InteractionResult interactOn(Entity entity, @NotNull Player player, Inte
return InteractionResult.CONSUME;
}
} else {
if ((GcaSetting.openFakePlayerInventory || GcaUtils.openFakePlayerEnderChest(player)) && entity instanceof EntityPlayerMPFake fakePlayer) {
if ((GcaSetting.openFakePlayerInventory || SettingUtils.openFakePlayerEnderChest(player)) && entity instanceof EntityPlayerMPFake fakePlayer) {
// 打开物品栏
InteractionResult result = this.openInventory(player, fakePlayer);
if (result != InteractionResult.PASS) {
Expand All @@ -67,7 +66,7 @@ private InteractionResult openInventory(@NotNull Player player, EntityPlayerMPFa
SimpleMenuProvider provider;
if (player.isShiftKeyDown()) {
// 打开末影箱
if (GcaUtils.openFakePlayerEnderChest(player)) {
if (SettingUtils.openFakePlayerEnderChest(player)) {
provider = new SimpleMenuProvider(
(i, inventory, p) -> ChestMenu.sixRows(
i, inventory,
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/dev/dubhe/gugle/carpet/tools/PosUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dev.dubhe.gugle.carpet.tools;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.util.List;

public class PosUtils {
public static @NotNull MutableComponent xaero(String desc, double x, double y, double z, @NotNull ResourceKey<Level> dimType) {
int color = dimType == Level.OVERWORLD ? 10 :
dimType == Level.NETHER ? 12 :
dimType == Level.END ? 13 : 11;
return Component.literal(
"xaero-waypoint:%s:%s:%.0f:%.0f:%.0f:%d:false:0:Internal-%s-waypoints"
.formatted(
desc,
desc.substring(0, 1),
x,
y,
z,
color,
dimType.location().getPath()
)
);
}

public static @NotNull @Unmodifiable List<MutableComponent> pos(String desc, double x, double y, double z, @NotNull ResourceKey<Level> dimension) {
MutableComponent pos = Component.literal("[%.1f, %.1f, %.1f]".formatted(x, y, z)).withStyle(
Style.EMPTY
.applyFormat(
dimension == Level.OVERWORLD ?
ChatFormatting.GREEN :
dimension == Level.NETHER ?
ChatFormatting.RED :
dimension == Level.END ?
ChatFormatting.LIGHT_PURPLE :
ChatFormatting.AQUA
)
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(dimension.location().toString())))
);
double scale = 0;
ResourceKey<Level> toDimension = Level.END;
if (dimension == Level.NETHER) {
scale = 8;
toDimension = Level.OVERWORLD;
} else if (dimension == Level.OVERWORLD) {
scale = 0.125;
toDimension = Level.NETHER;
}
MutableComponent toPos = Component.literal("[%.1f, %.1f, %.1f]".formatted(x * scale, y, z * scale)).withStyle(
Style.EMPTY
.applyFormat(
dimension == Level.OVERWORLD ?
ChatFormatting.RED :
dimension == Level.NETHER ?
ChatFormatting.GREEN :
ChatFormatting.AQUA
)
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(toDimension.location().toString())))
);
return scale > 0 ?
List.of(pos, xaero(desc, x, y, z, dimension), toPos, xaero(desc, x * scale, y, z * scale, toDimension)) :
List.of(pos, xaero(desc, x, y, z, dimension));
}

public static @NotNull @Unmodifiable List<MutableComponent> playerPos(@NotNull ServerPlayer player) {
player.addEffect(new MobEffectInstance(MobEffects.GLOWING, 200, 0, true, false));
Vec3 position = player.position();
ResourceKey<Level> dimension = player.level().dimension();
String name = player.getGameProfile().getName();
List<MutableComponent> pos = PosUtils.pos("Shared Location", position.x, position.y, position.z, dimension);
MutableComponent component = Component.literal("%s at".formatted(name)).append(" ").append(pos.get(0));
if (pos.size() > 2) component.append("->").append(pos.get(2));
return List.of(component, pos.get(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Items;

public class GcaUtils {
public class SettingUtils {
public static boolean openFakePlayerEnderChest(Player player) {
if ("true".equals(GcaSetting.openFakePlayerEnderChest)) return true;
return "ender_chest".equals(GcaSetting.openFakePlayerEnderChest) &&
Expand Down

0 comments on commit 19b0a55

Please sign in to comment.