Update some incorrect code missing from last commit

This commit is contained in:
MattBDev 2020-03-11 18:43:21 -04:00
parent 6f9babe55e
commit 85b65669bd
2 changed files with 32 additions and 26 deletions

View File

@ -61,9 +61,9 @@ public class QueryTool implements BlockTool {
builder.append(TextComponent.of(block.getBlockType().getName(), TextColor.YELLOW)); builder.append(TextComponent.of(block.getBlockType().getName(), TextColor.YELLOW));
builder.append(TextComponent.of(" (" + block + ") ", TextColor.GRAY) builder.append(TextComponent.of(" (" + block + ") ", TextColor.GRAY)
.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TranslatableComponent.of("worldedit.tool.info.blockstate.hover")))); .hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TranslatableComponent.of("worldedit.tool.info.blockstate.hover"))));
final OptionalInt internalId = BlockStateIdAccess.getBlockStateId(block.toImmutableState()); final int internalId = BlockStateIdAccess.getBlockStateId(block.toImmutableState());
if (internalId.isPresent()) { if (BlockStateIdAccess.isValidInternalId(internalId)) {
builder.append(TextComponent.of(" (" + internalId.getAsInt() + ") ", TextColor.DARK_GRAY) builder.append(TextComponent.of(" (" + internalId+ ") ", TextColor.DARK_GRAY)
.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TranslatableComponent.of("worldedit.tool.info.internalid.hover")))); .hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TranslatableComponent.of("worldedit.tool.info.internalid.hover"))));
} }
final int[] legacy = LegacyMapper.getInstance().getLegacyFromBlock(block.toImmutableState()); final int[] legacy = LegacyMapper.getInstance().getLegacyFromBlock(block.toImmutableState());

View File

@ -35,27 +35,30 @@ import it.unimi.dsi.fastutil.objects.ObjectSet;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import javax.annotation.Nonnull;
/** /**
* Int-to-BaseBlock map, but with optimizations for common cases. * Int-to-BaseBlock map, but with optimizations for common cases.
*/ */
class Int2BaseBlockMap extends AbstractInt2ObjectMap<BaseBlock> { class Int2BaseBlockMap extends AbstractInt2ObjectMap<BaseBlock> {
private static boolean hasInt(BlockState b) { /**
return BlockStateIdAccess.getBlockStateId(b).isPresent(); * Given a {@link BaseBlock}, retrieve the internal ID if it's useful,
* i.e. the block has no NBT data.
*
* @param block the block to get the ID for
* @return the internal ID, or {@link BlockStateIdAccess#invalidId()} if not useful
*/
private static int optimizedInternalId(BaseBlock block) {
if (block.hasNbtData()) {
return BlockStateIdAccess.invalidId();
} }
private static boolean isUncommon(BaseBlock block) { return BlockStateIdAccess.getBlockStateId(block.toImmutableState());
return block.hasNbtData() || !hasInt(block.toImmutableState());
}
private static int assumeAsInt(BlockState b) {
return BlockStateIdAccess.getBlockStateId(b)
.orElseThrow(() -> new IllegalStateException("Block state " + b + " did not have an ID"));
} }
private static BaseBlock assumeAsBlock(int id) { private static BaseBlock assumeAsBlock(int id) {
if (id == Integer.MIN_VALUE) { if (!BlockStateIdAccess.isValidInternalId(id)) {
return null; return null;
} }
BlockState state = BlockStateIdAccess.getBlockStateById(id); BlockState state = BlockStateIdAccess.getBlockStateById(id);
@ -65,13 +68,11 @@ class Int2BaseBlockMap extends AbstractInt2ObjectMap<BaseBlock> {
return state.toBaseBlock(); return state.toBaseBlock();
} }
static final Int2BaseBlockMap EMPTY = new Int2BaseBlockMap();
private final Int2IntMap commonMap = new Int2IntOpenHashMap(64, 1f); private final Int2IntMap commonMap = new Int2IntOpenHashMap(64, 1f);
private final Int2ObjectMap<BaseBlock> uncommonMap = new Int2ObjectOpenHashMap<>(1, 1f); private final Int2ObjectMap<BaseBlock> uncommonMap = new Int2ObjectOpenHashMap<>(1, 1f);
{ {
commonMap.defaultReturnValue(Integer.MIN_VALUE); commonMap.defaultReturnValue(BlockStateIdAccess.invalidId());
} }
@Override @Override
@ -83,6 +84,7 @@ class Int2BaseBlockMap extends AbstractInt2ObjectMap<BaseBlock> {
public ObjectSet<Entry<BaseBlock>> int2ObjectEntrySet() { public ObjectSet<Entry<BaseBlock>> int2ObjectEntrySet() {
return new AbstractObjectSet<Entry<BaseBlock>>() { return new AbstractObjectSet<Entry<BaseBlock>>() {
@Override @Override
@Nonnull
public ObjectIterator<Entry<BaseBlock>> iterator() { public ObjectIterator<Entry<BaseBlock>> iterator() {
return new ObjectIterator<Entry<BaseBlock>>() { return new ObjectIterator<Entry<BaseBlock>>() {
@ -122,7 +124,7 @@ class Int2BaseBlockMap extends AbstractInt2ObjectMap<BaseBlock> {
@Override @Override
public BaseBlock get(int key) { public BaseBlock get(int key) {
int oldId = commonMap.get(key); int oldId = commonMap.get(key);
if (oldId == Integer.MIN_VALUE) { if (!BlockStateIdAccess.isValidInternalId(oldId)) {
return uncommonMap.get(key); return uncommonMap.get(key);
} }
return assumeAsBlock(oldId); return assumeAsBlock(oldId);
@ -136,15 +138,17 @@ class Int2BaseBlockMap extends AbstractInt2ObjectMap<BaseBlock> {
@Override @Override
public boolean containsValue(Object v) { public boolean containsValue(Object v) {
BaseBlock block = (BaseBlock) v; BaseBlock block = (BaseBlock) v;
if (isUncommon(block)) { int internalId = optimizedInternalId(block);
if (!BlockStateIdAccess.isValidInternalId(internalId)) {
return uncommonMap.containsValue(block); return uncommonMap.containsValue(block);
} }
return commonMap.containsValue(assumeAsInt(block.toImmutableState())); return commonMap.containsValue(internalId);
} }
@Override @Override
public BaseBlock put(int key, BaseBlock value) { public BaseBlock put(int key, BaseBlock value) {
if (isUncommon(value)) { int internalId = optimizedInternalId(value);
if (!BlockStateIdAccess.isValidInternalId(internalId)) {
BaseBlock old = uncommonMap.put(key, value); BaseBlock old = uncommonMap.put(key, value);
if (old == null) { if (old == null) {
// ensure common doesn't have the entry too // ensure common doesn't have the entry too
@ -153,14 +157,14 @@ class Int2BaseBlockMap extends AbstractInt2ObjectMap<BaseBlock> {
} }
return old; return old;
} }
int oldId = commonMap.put(key, assumeAsInt(value.toImmutableState())); int oldId = commonMap.put(key, internalId);
return assumeAsBlock(oldId); return assumeAsBlock(oldId);
} }
@Override @Override
public BaseBlock remove(int key) { public BaseBlock remove(int key) {
int removed = commonMap.remove(key); int removed = commonMap.remove(key);
if (removed == Integer.MIN_VALUE) { if (!BlockStateIdAccess.isValidInternalId(removed)) {
return uncommonMap.remove(key); return uncommonMap.remove(key);
} }
return assumeAsBlock(removed); return assumeAsBlock(removed);
@ -172,21 +176,23 @@ class Int2BaseBlockMap extends AbstractInt2ObjectMap<BaseBlock> {
iter.hasNext(); ) { iter.hasNext(); ) {
Int2IntMap.Entry next = iter.next(); Int2IntMap.Entry next = iter.next();
BaseBlock value = function.apply(next.getIntKey(), assumeAsBlock(next.getIntValue())); BaseBlock value = function.apply(next.getIntKey(), assumeAsBlock(next.getIntValue()));
if (isUncommon(value)) { int internalId = optimizedInternalId(value);
if (!BlockStateIdAccess.isValidInternalId(internalId)) {
uncommonMap.put(next.getIntKey(), value); uncommonMap.put(next.getIntKey(), value);
iter.remove(); iter.remove();
} else { } else {
next.setValue(assumeAsInt(value.toImmutableState())); next.setValue(internalId);
} }
} }
for (ObjectIterator<Entry<BaseBlock>> iter = Int2ObjectMaps.fastIterator(uncommonMap); for (ObjectIterator<Entry<BaseBlock>> iter = Int2ObjectMaps.fastIterator(uncommonMap);
iter.hasNext(); ) { iter.hasNext(); ) {
Entry<BaseBlock> next = iter.next(); Entry<BaseBlock> next = iter.next();
BaseBlock value = function.apply(next.getIntKey(), next.getValue()); BaseBlock value = function.apply(next.getIntKey(), next.getValue());
if (isUncommon(value)) { int internalId = optimizedInternalId(value);
if (!BlockStateIdAccess.isValidInternalId(internalId)) {
next.setValue(value); next.setValue(value);
} else { } else {
commonMap.put(next.getIntKey(), assumeAsInt(value.toImmutableState())); commonMap.put(next.getIntKey(), internalId);
iter.remove(); iter.remove();
} }
} }