Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@
import net.minecraft.ChatFormatting;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.protocol.Packet;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import org.slf4j.Logger;

import java.io.IOException;
Expand All @@ -43,6 +50,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import static net.earthcomputer.clientcommands.command.arguments.PacketTypeArgument.*;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;
Expand All @@ -59,6 +67,11 @@ public static void disable() {
private static final SimpleCommandExceptionType ALREADY_LISTENING_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.clisten.add.failed"));
private static final SimpleCommandExceptionType NOT_LISTENING_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.clisten.remove.failed"));

private static final Component SQUARE_BRACKET = Component.literal("]");
private static final Component CURLY_BRACKET = Component.literal("}");
private static final Component EQUALS = Component.literal("=");
private static final Component SEPARATOR = ComponentUtils.DEFAULT_NO_STYLE_SEPARATOR;

private static final Logger LOGGER = LogUtils.getLogger();

private static final Set<ResourceLocation> packets = new HashSet<>();
Expand Down Expand Up @@ -198,38 +211,48 @@ private static Component serializeInner(Object object, Set<Object> seen, int dep
case Message message -> Component.translationArg(message);
case Collection<?> collection -> {
MutableComponent component = Component.literal("[");
component.append(collection.stream().map(e -> serialize(e, seen, depth + 1).copy()).reduce((l, r) -> l.append(", ").append(r)).orElse(Component.empty()));
yield component.append("]");
component.append(collection.stream().map(e -> asMutable(serialize(e, seen, depth + 1))).reduce((l, r) -> l.append(SEPARATOR).append(r)).orElseGet(Component::empty));
yield component.append(SQUARE_BRACKET);
}
case Map<?, ?> map -> {
MutableComponent component = Component.literal("{");
component.append(map.entrySet().stream().map(e -> serialize(e.getKey(), seen, depth + 1).copy().append("=").append(serialize(e.getValue(), seen, depth + 1))).reduce((l, r) -> l.append(", ").append(r)).orElse(Component.empty()));
yield component.append("}");
component.append(map.entrySet().stream().map(e -> asMutable(serialize(e.getKey(), seen, depth + 1)).append(EQUALS).append(serialize(e.getValue(), seen, depth + 1))).reduce((l, r) -> l.append(SEPARATOR).append(r)).orElseGet(Component::empty));
yield component.append(CURLY_BRACKET);
}
case Registry<?> registry -> Component.translationArg(registry.key().location());
case ResourceKey<?> resourceKey -> {
MutableComponent component = Component.literal("{");
component.append("registry=").append(serialize(resourceKey.registry(), seen, depth + 1)).append(", ");
component.append("registry=").append(serialize(resourceKey.registry(), seen, depth + 1)).append(SEPARATOR);
component.append("location=").append(serialize(resourceKey.location(), seen, depth + 1));
yield component.append("}");
yield component.append(CURLY_BRACKET);
}
case Holder<?> holder -> {
MutableComponent component = Component.literal("{");
component.append("kind=").append(serialize(holder.kind().name(), seen, depth + 1)).append(", ");
component.append("kind=").append(serialize(holder.kind().name(), seen, depth + 1)).append(SEPARATOR);
component.append("value=").append(serialize(holder.value(), seen, depth + 1));
yield component.append("}");
yield component.append(CURLY_BRACKET);
}
case BlockState state -> {
MutableComponent component = asMutable(serialize(state.getBlock(), seen, depth));
if (!state.getProperties().isEmpty()) {
component.append(state.getProperties().stream().map(property -> property.getName() + "=" + getProperty(state, property)).collect(Collectors.joining(", ", "[", "]")));
}
yield component;
}
case Block block -> Component.literal(BuiltInRegistries.BLOCK.getKey(block).toString());
case Item item -> Component.literal(BuiltInRegistries.ITEM.getKey(item).toString());
case EntityType<?> entityType -> Component.literal(BuiltInRegistries.ENTITY_TYPE.getKey(entityType).toString());
default -> {
if (object.getClass().isArray()) {
MutableComponent component = Component.literal("[");
int lengthMinusOne = Array.getLength(object) - 1;
if (lengthMinusOne < 0) {
yield component.append("]");
yield component.append(SQUARE_BRACKET);
}
for (int i = 0; i < lengthMinusOne; i++) {
component.append(serialize(Array.get(object, i), seen, depth + 1)).append(", ");
component.append(serialize(Array.get(object, i), seen, depth + 1)).append(SEPARATOR);
}
yield component.append(serialize(Array.get(object, lengthMinusOne), seen, depth + 1)).append("]");
yield component.append(serialize(Array.get(object, lengthMinusOne), seen, depth + 1)).append(SQUARE_BRACKET);
}

String className = object.getClass().getName().replace(".", "/");
Expand Down Expand Up @@ -258,13 +281,21 @@ private static Component serializeInner(Object object, Set<Object> seen, int dep
}
}
})
.reduce((l, r) -> l.append(", ").append(r))
.orElse(Component.empty()));
yield component.append("}");
.reduce((l, r) -> l.append(SEPARATOR).append(r))
.orElseGet(Component::empty));
yield component.append(CURLY_BRACKET);
}
};
}

private static MutableComponent asMutable(Component component) {
return component instanceof MutableComponent mutable ? mutable : component.copy();
}

private static <T extends Comparable<T>> String getProperty(BlockState state, Property<T> property) {
return property.getName(state.getValue(property));
}

public enum PacketFlow {
SERVERBOUND,
CLIENTBOUND,
Expand Down