Skip to content

Commit

Permalink
- 使用新的方法来替换 Chunk 中的 tileEntityPosQueue.
Browse files Browse the repository at this point in the history
  • Loading branch information
KasumiNova committed Nov 2, 2024
1 parent d1df60f commit 5a319af
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {

// Project properties
group = "github.kasuminova.stellarcore"
version = "1.5.14"
version = "1.5.15"

// Set the toolchain version to decouple the Java we run Gradle with from the Java used to compile and run the mod
java {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package github.kasuminova.stellarcore.common.util;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
import java.util.Spliterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import java.util.function.Predicate;

@SuppressWarnings("StandardVariableNames")
public class FakeConcurrentLinkedQueue<E> extends ConcurrentLinkedQueue<E> {

private final Queue<E> internal;

public FakeConcurrentLinkedQueue(final Collection<? extends E> c, @Nonnull final Queue<E> internal) {
this.internal = internal;
}

public FakeConcurrentLinkedQueue(@Nonnull final Queue<E> internal) {
this.internal = internal;
}

@Override
public void forEach(final Consumer<? super E> action) {
internal.forEach(action);
}

@Override
public void clear() {
internal.clear();
}

@Override
public boolean retainAll(final Collection<?> c) {
return internal.retainAll(c);
}

@Override
public boolean removeAll(final Collection<?> c) {
return internal.removeAll(c);
}

@Override
public boolean removeIf(final Predicate<? super E> filter) {
return internal.removeIf(filter);
}

@Override
public Spliterator<E> spliterator() {
return internal.spliterator();
}

@Nonnull
@Override
public Iterator<E> iterator() {
return internal.iterator();
}

@Nonnull
@Override
public <T> T[] toArray(final T[] a) {
return internal.toArray(a);
}

@Nonnull
@Override
public Object[] toArray() {
return internal.toArray();
}

@Override
public String toString() {
return internal.toString();
}

@Override
public boolean addAll(final Collection<? extends E> c) {
return internal.addAll(c);
}

@Override
public boolean remove(final Object o) {
return internal.remove(o);
}

@Override
public boolean contains(final Object o) {
return internal.contains(o);
}

@Override
public int size() {
return internal.size();
}

@Override
public boolean isEmpty() {
return internal.isEmpty();
}

@Override
public E peek() {
return internal.peek();
}

@Override
public E poll() {
return internal.poll();
}

@Override
public boolean offer(final E element) {
return internal.offer(element);
}

@Override
public boolean add(final E element) {
return internal.add(element);
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package github.kasuminova.stellarcore.mixin.minecraft.chunktequeue;

import github.kasuminova.stellarcore.common.util.FakeConcurrentLinkedQueue;
import github.kasuminova.stellarcore.shaded.org.jctools.queues.MpscUnboundedXaddArrayQueue;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.Chunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

@SuppressWarnings("MethodMayBeStatic")
@Mixin(Chunk.class)
public class MixinChunk {

@Unique
private final Queue<BlockPos> stellar_core$tileEntityPosQueue = new MpscUnboundedXaddArrayQueue<>(16);

@Redirect(
method = "<init>(Lnet/minecraft/world/World;II)V",
at = @At(
Expand All @@ -27,44 +23,8 @@ public class MixinChunk {
)
)
private ConcurrentLinkedQueue<BlockPos> redirectNewQueue() {
// Deallocate the queue.
return null;
}

@Redirect(
method = "getTileEntity",
at = @At(
value = "INVOKE",
target = "Ljava/util/concurrent/ConcurrentLinkedQueue;add(Ljava/lang/Object;)Z",
remap = false
)
)
private boolean redirectGetTileEntityQueueAdd(final ConcurrentLinkedQueue<BlockPos> _inst, final Object element) {
return stellar_core$tileEntityPosQueue.add((BlockPos) element);
}

@Redirect(
method = "onTick",
at = @At(
value = "INVOKE",
target = "Ljava/util/concurrent/ConcurrentLinkedQueue;isEmpty()Z",
remap = false
)
)
private boolean redirectOnTickQueueIsEmpty(final ConcurrentLinkedQueue<BlockPos> _inst) {
return stellar_core$tileEntityPosQueue.isEmpty();
}

@Redirect(
method = "onTick",
at = @At(
value = "INVOKE",
target = "Ljava/util/concurrent/ConcurrentLinkedQueue;poll()Ljava/lang/Object;",
remap = false
)
)
private Object redirectOnTickQueuePoll(final ConcurrentLinkedQueue<BlockPos> _inst) {
return stellar_core$tileEntityPosQueue.poll();
// Use new queue type.
return new FakeConcurrentLinkedQueue<>(new MpscUnboundedXaddArrayQueue<>(16));
}

}

0 comments on commit 5a319af

Please sign in to comment.