Skip to content

Commit

Permalink
52 changed files
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiyou06 committed Jul 30, 2024
1 parent d4ae8a6 commit eea4f8e
Show file tree
Hide file tree
Showing 52 changed files with 3,414 additions and 1,322 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.gensokyoreimagined.nitori.common.ai;

public interface MemoryModificationCounter {

long lithium$getModCount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.gensokyoreimagined.nitori.common.ai;

import net.minecraft.world.entity.ai.behavior.ShufflingList;

import java.util.Iterator;

public interface WeightedListIterable<U> extends Iterable<U> {
/**
* {@inheritDoc}
*/
Iterator<U> iterator();

/**
* Returns an {@link Iterable} over the elements in the {@param list}. This allows code to circumvent the usage
* of streams, providing a speed-up in other areas of the game.
*/
@SuppressWarnings("unchecked")
static <T> Iterable<? extends T> cast(ShufflingList<T> list) {
return ((WeightedListIterable<T>) list);
}

/**
* A wrapper type for an iterator over the entries of a {@link ShufflingList} which de-references the contained
* values for consumers.
*
* @param <U> The value type stored in each list entry
*/
class ListIterator<U> implements Iterator<U> {
private final Iterator<ShufflingList.WeightedEntry<? extends U>> inner;

public ListIterator(Iterator<ShufflingList.WeightedEntry<? extends U>> inner) {
this.inner = inner;
}

@Override
public boolean hasNext() {
return this.inner.hasNext();
}

@Override
public U next() {
return this.inner.next().getData();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.gensokyoreimagined.nitori.common.entity;

import net.minecraft.world.entity.ai.navigation.PathNavigation;

public interface NavigatingEntity {
boolean lithium$isRegisteredToWorld();

void lithium$setRegisteredToWorld(PathNavigation navigation);

PathNavigation lithium$getRegisteredNavigation();

void lithium$updateNavigationRegistration();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package net.gensokyoreimagined.nitori.common.util.collections;

import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

import java.util.*;
import java.util.function.Consumer;

public class MaskedList<E> extends AbstractList<E> {
private final ObjectArrayList<E> allElements;
private final BitSet visibleMask;
private final Object2IntOpenHashMap<E> element2Index;
private final boolean defaultVisibility;
private int numCleared;

public MaskedList(ObjectArrayList<E> allElements, boolean defaultVisibility) {
this.allElements = new ObjectArrayList<>();
this.visibleMask = new BitSet();
this.defaultVisibility = defaultVisibility;
this.element2Index = new Object2IntOpenHashMap<>();
this.element2Index.defaultReturnValue(-1);

this.addAll(allElements);
}

public MaskedList() {
this(new ObjectArrayList<>(), true);
}

public int totalSize() {
return this.allElements.size();
}


public void addOrSet(E element, boolean visible) {
int index = this.element2Index.getInt(element);
if (index != -1) {
this.visibleMask.set(index, visible);
} else {
this.add(element);
this.setVisible(element, visible);
}
}

public void setVisible(E element, final boolean visible) {
int index = this.element2Index.getInt(element);
if (index != -1) {
this.visibleMask.set(index, visible);
}
//ignore when the element is not in the collection
}

@Override
public Iterator<E> iterator() {
return new Iterator<>() {
int nextIndex = 0;
int cachedNext = -1;

@Override
public boolean hasNext() {
return (this.cachedNext = MaskedList.this.visibleMask.nextSetBit(this.nextIndex)) != -1;
}

@Override
public E next() {
int index = this.cachedNext;
this.cachedNext = -1;
this.nextIndex = index + 1;
return MaskedList.this.allElements.get(index);
}
};
}

@Override
public Spliterator<E> spliterator() {
return new Spliterators.AbstractSpliterator<E>(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.NONNULL) {
int nextIndex = 0;

@Override
public boolean tryAdvance(Consumer<? super E> action) {
int index = MaskedList.this.visibleMask.nextSetBit(this.nextIndex);
if (index == -1) {
return false;
}
this.nextIndex = index + 1;
action.accept(MaskedList.this.allElements.get(index));
return true;
}
};
}

@Override
public boolean add(E e) {
int oldIndex = this.element2Index.put(e, this.allElements.size());
if (oldIndex != -1) {
throw new IllegalStateException("MaskedList must not contain duplicates! Trying to add " + e + " but it is already present at index " + oldIndex + ". Current size: " + this.allElements.size());
}
this.visibleMask.set(this.allElements.size(), this.defaultVisibility);
return this.allElements.add(e);
}

@Override
public boolean remove(Object o) {
int index = this.element2Index.removeInt(o);
if (index == -1) {
return false;
}
this.visibleMask.clear(index);
this.allElements.set(index, null);
this.numCleared++;


if (this.numCleared * 2 > this.allElements.size()) {
ObjectArrayList<E> clonedElements = this.allElements.clone();
BitSet clonedVisibleMask = (BitSet) this.visibleMask.clone();
this.allElements.clear();
this.visibleMask.clear();
this.element2Index.clear();
for (int i = 0; i < clonedElements.size(); i++) {
E element = clonedElements.get(i);
int newIndex = this.allElements.size();
this.allElements.add(element);
this.visibleMask.set(newIndex, clonedVisibleMask.get(i));
this.element2Index.put(element, newIndex);
}
this.numCleared = 0;
}
return true;
}

@Override
public E get(int index) {
if (index < 0 || index >= this.size()) {
throw new IndexOutOfBoundsException(index);
}

int i = 0;
while (index >= 0) {
index--;
i = this.visibleMask.nextSetBit(i + 1);
}
return this.allElements.get(i);
}

@Override
public int size() {
return this.visibleMask.cardinality();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.gensokyoreimagined.nitori.common.util.deduplication;

public interface LithiumInternerWrapper<T> {

T lithium$getCanonical(T value);

void lithium$deleteCanonical(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.gensokyoreimagined.nitori.common.world;

import net.minecraft.world.entity.Mob;

public interface ServerWorldExtended {
void lithium$setNavigationActive(Mob mobEntity);

void lithium$setNavigationInactive(Mob mobEntity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package net.gensokyoreimagined.nitori.common.world.blockview;

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.CollisionGetter;
import net.minecraft.world.level.border.WorldBorder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

public record SingleBlockBlockView(BlockState state, BlockPos pos) implements BlockGetter, CollisionGetter {
public static SingleBlockBlockView of(BlockState blockState, BlockPos blockPos) {
return new SingleBlockBlockView(blockState, blockPos.immutable());
}

@Override
public BlockState getBlockState(BlockPos pos) {
if (pos.equals(this.pos())) {
return this.state();
} else {
throw SingleBlockViewException.INSTANCE;
}
}

@Override
public FluidState getFluidState(BlockPos pos) {
if (pos.equals(this.pos())) {
return this.state().getFluidState();
} else {
throw SingleBlockViewException.INSTANCE;
}
}

@Nullable
@Override
public BlockEntity getBlockEntity(BlockPos pos) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public int getHeight() {
throw SingleBlockViewException.INSTANCE;
}

@Override
public int getMinBuildHeight() {
throw SingleBlockViewException.INSTANCE;
}

@Override
public WorldBorder getWorldBorder() {
throw SingleBlockViewException.INSTANCE;
}

@Nullable
@Override
public BlockGetter getChunkForCollisions(int chunkX, int chunkZ) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public boolean isUnobstructed(@Nullable Entity except, VoxelShape shape) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public boolean isUnobstructed(BlockState state, BlockPos pos, CollisionContext context) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public boolean isUnobstructed(Entity entity) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public boolean noCollision(@Nullable Entity entity, AABB box) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public List<VoxelShape> getEntityCollisions(@Nullable Entity entity, AABB box) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public Iterable<VoxelShape> getCollisions(@Nullable Entity entity, AABB box) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public Iterable<VoxelShape> getBlockCollisions(@Nullable Entity entity, AABB box) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public boolean collidesWithSuffocatingBlock(@Nullable Entity entity, AABB box) {
throw SingleBlockViewException.INSTANCE;
}

@Override
public Optional<Vec3> findFreePosition(@Nullable Entity entity, VoxelShape shape, Vec3 target, double x, double y, double z) {
throw SingleBlockViewException.INSTANCE;
}

public static class SingleBlockViewException extends RuntimeException {

public static final SingleBlockViewException INSTANCE = new SingleBlockViewException();

private SingleBlockViewException() {
this.setStackTrace(new StackTraceElement[0]);
}

@Override
public synchronized Throwable fillInStackTrace() {
this.setStackTrace(new StackTraceElement[0]);
return this;
}
}

@javax.annotation.Nullable
@Override
public BlockState getBlockStateIfLoaded(@NotNull BlockPos block) {
throw SingleBlockViewException.INSTANCE;
}

@javax.annotation.Nullable
@Override
public FluidState getFluidIfLoaded(@NotNull BlockPos block) {
throw SingleBlockViewException.INSTANCE;
}
}
Loading

0 comments on commit eea4f8e

Please sign in to comment.