Skip to content

Commit

Permalink
change: try to be compatible with mods that use accesswidener on getP…
Browse files Browse the repository at this point in the history
…endingBlockEntity and throw exception on double add
  • Loading branch information
2No2Name committed Sep 20, 2020
1 parent bd89303 commit e45ef60
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap;
import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.BlockPos;

import java.util.*;

@SuppressWarnings("NullableProblems")
public class BlockEntityList implements List<BlockEntity> {
//BlockEntityList does not support double-add of the same object. But it does support multiple at the same position.
//This collection behaves like a set with insertion order. It also provides a position->blockEntity lookup.
Expand Down Expand Up @@ -64,7 +66,14 @@ public <T> T[] toArray(T[] a) {

@Override
public boolean add(BlockEntity blockEntity) {
return this.addNoDoubleAdd(blockEntity, true);
}

private boolean addNoDoubleAdd(BlockEntity blockEntity, boolean exceptionOnDoubleAdd) {
boolean added = this.allBlockEntities.add(blockEntity);
if (!added && exceptionOnDoubleAdd) {
this.throwException(blockEntity);
}

if (added && this.posMap != null) {
long pos = getEntityPos(blockEntity);
Expand All @@ -82,6 +91,10 @@ public boolean add(BlockEntity blockEntity) {
return added;
}

private void throwException(BlockEntity blockEntity) {
throw new IllegalStateException("Lithium BlockEntityList" + (this.posMap != null ? " with posMap" : "") + ": Adding the same BlockEntity object twice: " + blockEntity.toTag(new CompoundTag()));
}

@Override
public boolean remove(Object o) {
if (o instanceof BlockEntity) {
Expand Down Expand Up @@ -212,7 +225,7 @@ private static long getEntityPos(BlockEntity e) {
public boolean addIfAbsent(BlockEntity blockEntity) {
//we are not checking position equality but object/reference equality (like vanilla)
//the hashset prevents double add of the same object
return this.add(blockEntity);
return this.addNoDoubleAdd(blockEntity, false);
}

@SuppressWarnings("unused")
Expand All @@ -239,6 +252,9 @@ public void markRemovedAndRemoveAllAtPosition(BlockPos blockPos) {
}

public BlockEntity getFirstNonRemovedBlockEntityAtPosition(long pos) {
if (this.isEmpty()) {
return null;
}
BlockEntity blockEntity = this.posMap.get(pos);
//usual case: we find no BlockEntity or only one that also is not removed
if (blockEntity == null || !blockEntity.isRemoved()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;
import java.util.function.Supplier;

@SuppressWarnings("OverwriteModifiers")
@Mixin(World.class)
public abstract class WorldMixin implements WorldAccess {
@Shadow
Expand Down Expand Up @@ -71,8 +70,9 @@ private void reinit(MutableWorldProperties properties, RegistryKey<World> regist
* @author JellySquid
* @reason Replace with direct lookup
*/
@SuppressWarnings("OverwriteModifiers")
@Overwrite
private BlockEntity getPendingBlockEntity(BlockPos pos) {
public BlockEntity getPendingBlockEntity(BlockPos pos) {
return this.pendingBlockEntities$lithium.getFirstNonRemovedBlockEntityAtPosition(pos.asLong());
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/lithium.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ accessible class net/minecraft/server/world/ChunkTicketManager$TicketDistanceLev
accessible method net/minecraft/world/ChunkPosDistanceLevelPropagator updateLevel (JIZ)V
accessible method net/minecraft/server/world/ChunkTicket isExpired (J)Z

accessible method net/minecraft/util/shape/VoxelShapes findRequiredBitResolution (DD)I
accessible method net/minecraft/util/shape/VoxelShapes findRequiredBitResolution (DD)I

accessible method me/jellysquid/mods/lithium/mixin/world/block_entity_ticking/WorldMixin getPendingBlockEntity (Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/entity/BlockEntity

0 comments on commit e45ef60

Please sign in to comment.