diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..3b16a9669 Binary files /dev/null and b/.DS_Store differ diff --git a/common/.DS_Store b/common/.DS_Store new file mode 100644 index 000000000..5843cfcb7 Binary files /dev/null and b/common/.DS_Store differ diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/ComponentKJS.java b/common/src/main/java/dev/latvian/mods/kubejs/core/ComponentKJS.java index 3bb7d87d7..5a2b2c534 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/ComponentKJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/ComponentKJS.java @@ -1,5 +1,6 @@ package dev.latvian.mods.kubejs.core; +import com.google.common.collect.Iterators; import com.google.gson.JsonElement; import dev.latvian.mods.kubejs.KubeJS; import dev.latvian.mods.kubejs.bindings.TextWrapper; @@ -14,20 +15,44 @@ import net.minecraft.network.chat.HoverEvent; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.function.Consumer; /** * Extensions for components, will be injected into * {@link MutableComponent} at runtime. */ @RemapPrefixForJS("kjs$") -public interface ComponentKJS extends Component, Iterable, JsonSerializable, WrappedJS { +public interface ComponentKJS extends Component, JsonSerializable, WrappedJS { - @Override - default Iterator iterator() { - throw new NoMixinException(); + default Iterable kjs$asIterable() { + return new Iterable<>() { + @NotNull + @Override + public Iterator iterator() { + if (!kjs$hasSiblings()) { + return Iterators.forArray(kjs$self()); + } + + List list = new LinkedList<>(); + list.add(kjs$self()); + + for (var child : getSiblings()) { + if (child instanceof ComponentKJS wrapped) { + wrapped.forEach(list::add); + } else { + list.add(child); + } + } + + return list.iterator(); + } + }; } default MutableComponent kjs$self() { @@ -48,6 +73,10 @@ default JsonElement toJsonJS() { return !getSiblings().isEmpty(); } + default void forEach(Consumer action) { + kjs$asIterable().forEach(action); + } + // region ChatFormatting extensions default MutableComponent kjs$black() { return kjs$self().withStyle(ChatFormatting.BLACK); diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/components/MutableComponentMixin.java b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/components/MutableComponentMixin.java index 1efca75a2..7fa65d6c5 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/components/MutableComponentMixin.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/components/MutableComponentMixin.java @@ -21,24 +21,4 @@ public abstract class MutableComponentMixin implements ComponentKJS { @HideFromJS @Shadow public abstract MutableComponent append(String string); - - @Override - public Iterator iterator() { - if (!kjs$hasSiblings()) { - return UtilsJS.cast(List.of(kjs$self()).iterator()); - } - - List list = new LinkedList<>(); - list.add(kjs$self()); - - for (var child : getSiblings()) { - if (child instanceof ComponentKJS wrapped) { - wrapped.forEach(list::add); - } else { - list.add(child); - } - } - - return list.iterator(); - } } diff --git a/fabric/.DS_Store b/fabric/.DS_Store new file mode 100644 index 000000000..c5e5db271 Binary files /dev/null and b/fabric/.DS_Store differ diff --git a/forge/.DS_Store b/forge/.DS_Store new file mode 100644 index 000000000..13babbd56 Binary files /dev/null and b/forge/.DS_Store differ