Skip to content

Commit

Permalink
Added identity and tags to WS sessions, added more info to highlight …
Browse files Browse the repository at this point in the history
…packets
  • Loading branch information
LatvianModder committed Aug 31, 2024
1 parent 6dec951 commit e806423
Show file tree
Hide file tree
Showing 19 changed files with 333 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DynamicOps;
import dev.latvian.mods.kubejs.plugin.KubeJSPlugin;
import dev.latvian.mods.kubejs.plugin.KubeJSPlugins;
Expand Down Expand Up @@ -300,18 +301,21 @@ static StringBuilder mapToString(StringBuilder builder, DynamicOps<Tag> ops, Dat
boolean first = true;

for (var comp : map) {
var id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(comp.type());
var codec = comp.type().codec();

if (id == null || codec == null) {
continue;
}

if (first) {
first = false;
} else {
builder.append(',');
}

var id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(comp.type());
var optional = comp.encodeValue(ops).result();

if (id != null && !optional.isEmpty()) {
builder.append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString()).append('=').append(optional.get());
}
var value = codec == Codec.BOOL ? comp.value() : codec.encodeStart(ops, Cast.to(comp.value())).getOrThrow();
builder.append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString()).append('=').append(value);
}

builder.append(']');
Expand All @@ -324,21 +328,24 @@ static StringBuilder patchToString(StringBuilder builder, DynamicOps<Tag> ops, D
boolean first = true;

for (var comp : patch.entrySet()) {
var id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(comp.getKey());
var codec = comp.getKey().codec();

if (id == null || codec == null) {
continue;
}

if (first) {
first = false;
} else {
builder.append(',');
}

var id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(comp.getKey());

if (id != null) {
if (comp.getValue().isPresent()) {
var value = comp.getKey().codecOrThrow().encodeStart(ops, Cast.to(comp.getValue().get())).result().get();
builder.append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString()).append('=').append(value);
} else {
builder.append('!').append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString());
}
if (comp.getValue().isPresent()) {
var value = codec == Codec.BOOL ? comp.getValue().get() : codec.encodeStart(ops, Cast.to(comp.getValue().get())).result().get();
builder.append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString()).append('=').append(value);
} else {
builder.append('!').append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString());
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/core/BlockStateKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import dev.latvian.mods.kubejs.web.RelativeURL;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.core.BlockPos;
Expand All @@ -21,8 +21,6 @@
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;

import java.util.Map;

@RemapPrefixForJS("kjs$")
public interface BlockStateKJS extends RegistryObjectKJS<Block>, Replaceable {
@Override
Expand Down Expand Up @@ -75,8 +73,7 @@ default Object replaceThisWith(Context cx, Object with) {
return with instanceof BlockState state ? state : with instanceof Block block ? block.defaultBlockState() : cx.jsToJava(with, BlockWrapper.STATE_TYPE_INFO);
}

default String kjs$getWebIconURL(int size) {
var url = "/img/" + size + "/block/" + ID.url(kjs$getIdLocation());
return LocalWebServer.getURL(url, Map.of());
default RelativeURL kjs$getWebIconURL(int size) {
return new RelativeURL("/img/" + size + "/block/" + ID.url(kjs$getIdLocation()));
}
}
6 changes: 3 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.WithCodec;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import dev.latvian.mods.kubejs.web.RelativeURL;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.SpecialEquality;
import net.minecraft.core.Holder;
Expand Down Expand Up @@ -145,9 +145,9 @@ default boolean matches(Context cx, FluidIngredient ingredient, boolean exact) {
return ingredient.test(kjs$self());
}

default String kjs$getWebIconURL(DynamicOps<Tag> ops, int size) {
default RelativeURL kjs$getWebIconURL(DynamicOps<Tag> ops, int size) {
var url = "/img/" + size + "/fluid/" + ID.url(kjs$getIdLocation());
var c = DataComponentWrapper.patchToString(new StringBuilder(), ops, DataComponentWrapper.visualPatch(kjs$self().getComponentsPatch())).toString();
return LocalWebServer.getURL(url, c.equals("[]") ? Map.of() : Map.of("components", c.substring(1, c.length() - 1)));
return new RelativeURL(url, c.equals("[]") ? Map.of() : Map.of("components", c.substring(1, c.length() - 1)));
}
}
4 changes: 4 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ public interface ItemKJS extends IngredientSupplierKJS, RegistryObjectKJS<Item>
default ItemStackKey kjs$getTypeItemStackKey() {
throw new NoMixinException();
}

default void kjs$setCanRepair(boolean repairable) {
throw new NoMixinException();
}
}
6 changes: 3 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.WithCodec;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import dev.latvian.mods.kubejs.web.RelativeURL;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import dev.latvian.mods.rhino.util.ReturnsSelf;
Expand Down Expand Up @@ -279,9 +279,9 @@ default boolean matches(Context cx, ItemLike itemLike, boolean exact) {
return kjs$self().getItem() == itemLike.asItem();
}

default String kjs$getWebIconURL(DynamicOps<Tag> ops, int size) {
default RelativeURL kjs$getWebIconURL(DynamicOps<Tag> ops, int size) {
var url = "/img/" + size + "/item/" + ID.url(kjs$getIdLocation());
var c = DataComponentWrapper.patchToString(new StringBuilder(), ops, DataComponentWrapper.visualPatch(kjs$self().getComponentsPatch())).toString();
return LocalWebServer.getURL(url, c.equals("[]") ? Map.of() : Map.of("components", c.substring(1, c.length() - 1)));
return new RelativeURL(url, c.equals("[]") ? Map.of() : Map.of("components", c.substring(1, c.length() - 1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,9 @@ private void hurtEnemy(ItemStack itemStack, LivingEntity livingEntity, LivingEnt

return kjs$typeItemStackKey;
}

@Override
@Accessor("canRepair")
@Mutable
public abstract void kjs$setCanRepair(boolean repairable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,9 @@ public void setTier(Consumer<MutableToolTier> c) {
public void setNameKey(String key) {
item.kjs$setNameKey(key);
}

public void disableRepair() {
item.kjs$setCanRepair(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.kubedex;

import dev.latvian.mods.kubejs.component.DataComponentWrapper;
import dev.latvian.mods.kubejs.net.WebServerUpdateNBTPayload;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.OrderedCompoundTag;
Expand All @@ -9,16 +10,35 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.StringTag;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.network.PacketDistributor;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class KubedexPayloadHandler {
private static ListTag sortedTagList(Stream<? extends TagKey<?>> stream) {
return stream
.map(TagKey::location)
.sorted(ResourceLocation::compareNamespaced)
.map(ResourceLocation::toString)
.map(StringTag::valueOf)
.collect(ListTag::new, ListTag::add, ListTag::addAll);
}

public static void block(ServerPlayer player, BlockPos pos) {
var registries = player.server.registryAccess();
var blockState = player.level().getBlockState(pos);
Expand Down Expand Up @@ -61,7 +81,7 @@ public static void block(ServerPlayer player, BlockPos pos) {
}
}

PacketDistributor.sendToPlayer(player, new WebServerUpdateNBTPayload("highlight/block", Optional.of(payload)));
PacketDistributor.sendToPlayer(player, new WebServerUpdateNBTPayload("highlight/block", "highlight", Optional.of(payload)));
}
}

Expand All @@ -87,7 +107,7 @@ public static void entity(ServerPlayer player, int entityId) {
payload.put("data", new CompoundTag());
}

PacketDistributor.sendToPlayer(player, new WebServerUpdateNBTPayload("highlight/entity", Optional.of(payload)));
PacketDistributor.sendToPlayer(player, new WebServerUpdateNBTPayload("highlight/entity", "highlight", Optional.of(payload)));
}
}

Expand All @@ -113,9 +133,55 @@ public static void itemStacks(ServerPlayer player, Collection<ItemStack> stacks)
var payload = new ListTag();

for (var stack : stacks) {
payload.add(ItemStack.CODEC.encodeStart(ops, stack).result().get());
var tag = new OrderedCompoundTag();
tag.putString("string", stack.kjs$toItemString0(ops));
tag.put("item", ItemStack.CODEC.encodeStart(ops, stack).result().get());
tag.put("name", ComponentSerialization.FLAT_CODEC.encodeStart(ops, stack.getHoverName()).getOrThrow());
tag.putString("icon", stack.kjs$getWebIconURL(ops, 64).toString());

var patch = stack.getComponentsPatch();

if (!patch.isEmpty()) {
tag.putString("component_string", DataComponentWrapper.patchToString(new StringBuilder(), ops, patch).toString());
}

var itemTagList = sortedTagList(stack.getItemHolder().tags());

if (!itemTagList.isEmpty()) {
tag.put("tags", itemTagList);
}

if (stack.getItem() instanceof BlockItem blockItem && blockItem.getBlock() != Blocks.AIR) {
var blockTagList = sortedTagList(blockItem.getBlock().builtInRegistryHolder().tags());

if (!blockTagList.isEmpty()) {
tag.put("block_tags", blockTagList);
}
}

if (stack.getItem() instanceof BucketItem bucket && bucket.content != Fluids.EMPTY) {
var fluidTagList = sortedTagList(bucket.content.builtInRegistryHolder().tags());

if (!fluidTagList.isEmpty()) {
tag.put("fluid_tags", fluidTagList);
}
}

if (stack.getItem() instanceof SpawnEggItem egg) {
var entityType = egg.getType(stack);

if (entityType != null) {
var entityTagList = sortedTagList(entityType.builtInRegistryHolder().tags());

if (!entityTagList.isEmpty()) {
tag.put("entity_tags", entityTagList);
}
}
}

payload.add(tag);
}

PacketDistributor.sendToPlayer(player, new WebServerUpdateNBTPayload("highlight/items", Optional.of(payload)));
PacketDistributor.sendToPlayer(player, new WebServerUpdateNBTPayload("highlight/items", "highlight", Optional.of(payload)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import net.neoforged.neoforge.network.handling.IPayloadContext;
import org.jetbrains.annotations.Nullable;

public record WebServerUpdateJSONPayload(String event, @Nullable JsonElement payload) implements CustomPacketPayload {
public record WebServerUpdateJSONPayload(String event, String requiredTag, @Nullable JsonElement payload) implements CustomPacketPayload {
public static final StreamCodec<ByteBuf, WebServerUpdateJSONPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.STRING_UTF8, WebServerUpdateJSONPayload::event,
ByteBufCodecs.STRING_UTF8, WebServerUpdateJSONPayload::requiredTag,
KubeJSCodecs.JSON_ELEMENT_STREAM_CODEC, WebServerUpdateJSONPayload::payload,
WebServerUpdateJSONPayload::new
);
Expand All @@ -23,6 +24,6 @@ public Type<?> type() {
}

public void handle(IPayloadContext ctx) {
KubeJSWeb.broadcastUpdate("server/" + event, () -> payload);
KubeJSWeb.broadcastUpdate("server/" + event, requiredTag, () -> payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
import dev.latvian.mods.kubejs.web.local.KubeJSWeb;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.CollectionTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.network.handling.IPayloadContext;

import java.util.Optional;

public record WebServerUpdateNBTPayload(String event, Optional<Tag> payload) implements CustomPacketPayload {
public record WebServerUpdateNBTPayload(String event, String requiredTag, Optional<Tag> payload) implements CustomPacketPayload {
public static final StreamCodec<ByteBuf, WebServerUpdateNBTPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.STRING_UTF8, WebServerUpdateNBTPayload::event,
ByteBufCodecs.STRING_UTF8, WebServerUpdateNBTPayload::requiredTag,
ByteBufCodecs.optional(ByteBufCodecs.TAG), WebServerUpdateNBTPayload::payload,
WebServerUpdateNBTPayload::new
);
Expand All @@ -28,15 +30,19 @@ public Type<?> type() {
}

public void handle(IPayloadContext ctx) {
if (KubeJSWeb.UPDATES.sessions().isEmpty() && event.equals("highlight/items")) {
var ops = ctx.player().level().registryAccess().createSerializationContext(NbtOps.INSTANCE);
int count = KubeJSWeb.broadcastUpdate("server/" + event, requiredTag, () -> payload.map(tag -> NbtOps.INSTANCE.convertTo(JsonOps.INSTANCE, tag)).orElse(null));

if (count == 0 && event.equals("highlight/items")) {
for (var e : (CollectionTag<?>) payload.get()) {
var stack = ItemStack.CODEC.decode(NbtOps.INSTANCE, e).result().get().getFirst();
KubeJS.LOGGER.info("[Highlighted Item] " + stack.kjs$toItemString0(ops));
var t = (CompoundTag) e;
KubeJS.LOGGER.info("[Highlighted Item] " + t.getString("string"));

if (t.get("tags") instanceof ListTag l) {
for (var tag : l) {
KubeJS.LOGGER.info("[Highlighted Item] Item Tag: #" + tag.getAsString());
}
}
}
} else {
KubeJSWeb.broadcastUpdate("server/" + event, () -> payload.map(tag -> NbtOps.INSTANCE.convertTo(JsonOps.INSTANCE, tag)).orElse(null));
}
}
}
20 changes: 10 additions & 10 deletions src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,15 @@ private <T> T reduceRecipesAsync(Context cx, RecipeFilter filter, Function<Strea
}

public void forEachRecipe(Context cx, RecipeFilter filter, Consumer<KubeRecipe> consumer) {
recipeStream(cx, filter).forEach(consumer);
if (filter instanceof IDFilter id) {
var r = originalRecipes.get(id.id);

if (r != null && !r.removed) {
consumer.accept(r);
}
} else {
recipeStream(cx, filter).forEach(consumer);
}
}

public int countRecipes(Context cx, RecipeFilter filter) {
Expand All @@ -486,15 +494,7 @@ public Collection<ResourceLocation> findRecipeIds(Context cx, RecipeFilter filte
}

public void remove(Context cx, RecipeFilter filter) {
if (filter instanceof IDFilter id) {
var r = originalRecipes.get(id.id);

if (r != null) {
r.remove();
}
} else {
forEachRecipe(cx, filter, KubeRecipe::remove);
}
forEachRecipe(cx, filter, KubeRecipe::remove);
}

public void replaceInput(Context cx, RecipeFilter filter, ReplacementMatchInfo match, Object with) {
Expand Down
Loading

0 comments on commit e806423

Please sign in to comment.