Skip to content

Commit

Permalink
remove Iterable from ComponentKJS to fix compat issues (use kjs$asIte…
Browse files Browse the repository at this point in the history
…rable to turn it into one if necessary)
  • Loading branch information
MaxNeedsSnacks committed May 18, 2024
1 parent 220391f commit 6c2ac49
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added common/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Component>, JsonSerializable, WrappedJS {
public interface ComponentKJS extends Component, JsonSerializable, WrappedJS {

@Override
default Iterator<Component> iterator() {
throw new NoMixinException();
default Iterable<Component> kjs$asIterable() {
return new Iterable<>() {
@NotNull
@Override
public Iterator<Component> iterator() {
if (!kjs$hasSiblings()) {
return Iterators.forArray(kjs$self());
}

List<Component> 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() {
Expand All @@ -48,6 +73,10 @@ default JsonElement toJsonJS() {
return !getSiblings().isEmpty();
}

default void forEach(Consumer<? super Component> action) {
kjs$asIterable().forEach(action);
}

// region ChatFormatting extensions
default MutableComponent kjs$black() {
return kjs$self().withStyle(ChatFormatting.BLACK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,4 @@ public abstract class MutableComponentMixin implements ComponentKJS {
@HideFromJS
@Shadow
public abstract MutableComponent append(String string);

@Override
public Iterator<Component> iterator() {
if (!kjs$hasSiblings()) {
return UtilsJS.cast(List.of(kjs$self()).iterator());
}

List<Component> 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();
}
}
Binary file added fabric/.DS_Store
Binary file not shown.
Binary file added forge/.DS_Store
Binary file not shown.

0 comments on commit 6c2ac49

Please sign in to comment.