Skip to content

Commit

Permalink
Simplify NMSAdapter#createPalette methods (#3036)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell authored Dec 23, 2024
1 parent 0cf9813 commit c8f1984
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
}

private synchronized boolean init() {
if (ibdToStateOrdinal != null && ibdToStateOrdinal[1] != 0) {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
}
ibdToStateOrdinal = new char[BlockTypesCache.states.length]; // size
ordinalToIbdID = new int[ibdToStateOrdinal.length]; // size
for (int i = 0; i < ibdToStateOrdinal.length; i++) {
ibdToOrdinal = new int[BlockTypesCache.states.length]; // size
ordinalToIbdID = new int[ibdToOrdinal.length]; // size
for (int i = 0; i < ibdToOrdinal.length; i++) {
BlockState blockState = BlockTypesCache.states[i];
PaperweightBlockMaterial material = (PaperweightBlockMaterial) blockState.getMaterial();
int id = Block.BLOCK_STATE_REGISTRY.getId(material.getState());
char ordinal = blockState.getOrdinalChar();
ibdToStateOrdinal[id] = ordinal;
ibdToOrdinal[id] = ordinal;
ordinalToIbdID[ordinal] = id;
}
Map<String, List<Property<?>>> properties = new HashMap<>();
Expand Down Expand Up @@ -365,18 +365,18 @@ public BlockState adapt(net.minecraft.world.level.block.state.BlockState blockSt
public char adaptToChar(net.minecraft.world.level.block.state.BlockState blockState) {
int id = Block.BLOCK_STATE_REGISTRY.getId(blockState);
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
try {
init();
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
} catch (ArrayIndexOutOfBoundsException e1) {
LOGGER.error("Attempted to convert {} with ID {} to char. ibdToStateOrdinal length: {}. Defaulting to air!",
blockState.getBlock(), Block.BLOCK_STATE_REGISTRY.getId(blockState), ibdToStateOrdinal.length, e1
LOGGER.error("Attempted to convert {} with ID {} to char. ibdToOrdinal length: {}. Defaulting to air!",
blockState.getBlock(), Block.BLOCK_STATE_REGISTRY.getId(blockState), ibdToOrdinal.length, e1
);
return BlockTypesCache.ReservedIDs.AIR;
}
Expand All @@ -385,28 +385,28 @@ public char adaptToChar(net.minecraft.world.level.block.state.BlockState blockSt

public char ibdIDToOrdinal(int id) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
init();
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
}

@Override
public char[] getIbdToStateOrdinal() {
protected int[] getIbdToOrdinal() {
if (initialised) {
return ibdToStateOrdinal;
return ibdToOrdinal;
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal;
return ibdToOrdinal;
}
init();
return ibdToStateOrdinal;
return ibdToOrdinal;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.function.IntFunction;

import static java.lang.invoke.MethodType.methodType;
import static net.minecraft.core.registries.Registries.BIOME;
Expand Down Expand Up @@ -420,7 +420,7 @@ public static LevelChunkSection newChunkSection(

public static LevelChunkSection newChunkSection(
final int layer,
final Function<Integer, char[]> get,
final IntFunction<char[]> get,
char[] set,
CachedBukkitAdapter adapter,
Registry<Biome> biomeRegistry,
Expand All @@ -436,9 +436,9 @@ public static LevelChunkSection newChunkSection(
try {
int num_palette;
if (get == null) {
num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null);
num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter);
} else {
num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null);
num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter);
}

int bitsPerEntry = MathMan.log2nlz(num_palette - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
}

private synchronized boolean init() {
if (ibdToStateOrdinal != null && ibdToStateOrdinal[1] != 0) {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
}
ibdToStateOrdinal = new char[BlockTypesCache.states.length]; // size
ordinalToIbdID = new int[ibdToStateOrdinal.length]; // size
for (int i = 0; i < ibdToStateOrdinal.length; i++) {
ibdToOrdinal = new int[BlockTypesCache.states.length]; // size
ordinalToIbdID = new int[ibdToOrdinal.length]; // size
for (int i = 0; i < ibdToOrdinal.length; i++) {
BlockState blockState = BlockTypesCache.states[i];
PaperweightBlockMaterial material = (PaperweightBlockMaterial) blockState.getMaterial();
int id = Block.BLOCK_STATE_REGISTRY.getId(material.getState());
char ordinal = blockState.getOrdinalChar();
ibdToStateOrdinal[id] = ordinal;
ibdToOrdinal[id] = ordinal;
ordinalToIbdID[ordinal] = id;
}
Map<String, List<Property<?>>> properties = new HashMap<>();
Expand Down Expand Up @@ -364,18 +364,18 @@ public BlockState adapt(net.minecraft.world.level.block.state.BlockState blockSt
public char adaptToChar(net.minecraft.world.level.block.state.BlockState blockState) {
int id = Block.BLOCK_STATE_REGISTRY.getId(blockState);
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
try {
init();
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
} catch (ArrayIndexOutOfBoundsException e1) {
LOGGER.error("Attempted to convert {} with ID {} to char. ibdToStateOrdinal length: {}. Defaulting to air!",
blockState.getBlock(), Block.BLOCK_STATE_REGISTRY.getId(blockState), ibdToStateOrdinal.length, e1
LOGGER.error("Attempted to convert {} with ID {} to char. ibdToOrdinal length: {}. Defaulting to air!",
blockState.getBlock(), Block.BLOCK_STATE_REGISTRY.getId(blockState), ibdToOrdinal.length, e1
);
return BlockTypesCache.ReservedIDs.AIR;
}
Expand All @@ -384,28 +384,28 @@ public char adaptToChar(net.minecraft.world.level.block.state.BlockState blockSt

public char ibdIDToOrdinal(int id) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
init();
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
}

@Override
public char[] getIbdToStateOrdinal() {
public int[] getIbdToOrdinal() {
if (initialised) {
return ibdToStateOrdinal;
return ibdToOrdinal;
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal;
return ibdToOrdinal;
}
init();
return ibdToStateOrdinal;
return ibdToOrdinal;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.function.IntFunction;

import static java.lang.invoke.MethodType.methodType;
import static net.minecraft.core.registries.Registries.BIOME;
Expand Down Expand Up @@ -420,7 +420,7 @@ public static LevelChunkSection newChunkSection(

public static LevelChunkSection newChunkSection(
final int layer,
final Function<Integer, char[]> get,
final IntFunction<char[]> get,
char[] set,
CachedBukkitAdapter adapter,
Registry<Biome> biomeRegistry,
Expand All @@ -436,9 +436,9 @@ public static LevelChunkSection newChunkSection(
try {
int num_palette;
if (get == null) {
num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null);
num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter);
} else {
num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null);
num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter);
}

int bitsPerEntry = MathMan.log2nlz(num_palette - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,17 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
}

private synchronized boolean init() {
if (ibdToStateOrdinal != null && ibdToStateOrdinal[1] != 0) {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
}
ibdToStateOrdinal = new char[BlockTypesCache.states.length]; // size
ordinalToIbdID = new int[ibdToStateOrdinal.length]; // size
for (int i = 0; i < ibdToStateOrdinal.length; i++) {
ibdToOrdinal = new int[BlockTypesCache.states.length]; // size
ordinalToIbdID = new int[ibdToOrdinal.length]; // size
for (int i = 0; i < ibdToOrdinal.length; i++) {
BlockState blockState = BlockTypesCache.states[i];
PaperweightBlockMaterial material = (PaperweightBlockMaterial) blockState.getMaterial();
int id = Block.BLOCK_STATE_REGISTRY.getId(material.getState());
char ordinal = blockState.getOrdinalChar();
ibdToStateOrdinal[id] = ordinal;
ibdToOrdinal[id] = ordinal;
ordinalToIbdID[ordinal] = id;
}
Map<String, List<Property<?>>> properties = new HashMap<>();
Expand Down Expand Up @@ -374,18 +374,18 @@ public BlockState adapt(net.minecraft.world.level.block.state.BlockState blockSt
public char adaptToChar(net.minecraft.world.level.block.state.BlockState blockState) {
int id = Block.BLOCK_STATE_REGISTRY.getId(blockState);
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
try {
init();
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
} catch (ArrayIndexOutOfBoundsException e1) {
LOGGER.error("Attempted to convert {} with ID {} to char. ibdToStateOrdinal length: {}. Defaulting to air!",
blockState.getBlock(), Block.BLOCK_STATE_REGISTRY.getId(blockState), ibdToStateOrdinal.length, e1
LOGGER.error("Attempted to convert {} with ID {} to char. ibdToOrdinal length: {}. Defaulting to air!",
blockState.getBlock(), Block.BLOCK_STATE_REGISTRY.getId(blockState), ibdToOrdinal.length, e1
);
return BlockTypesCache.ReservedIDs.AIR;
}
Expand All @@ -394,28 +394,28 @@ public char adaptToChar(net.minecraft.world.level.block.state.BlockState blockSt

public char ibdIDToOrdinal(int id) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
init();
return ibdToStateOrdinal[id];
return (char) ibdToOrdinal[id];
}
}

@Override
public char[] getIbdToStateOrdinal() {
public int[] getIbdToOrdinal() {
if (initialised) {
return ibdToStateOrdinal;
return ibdToOrdinal;
}
synchronized (this) {
if (initialised) {
return ibdToStateOrdinal;
return ibdToOrdinal;
}
init();
return ibdToStateOrdinal;
return ibdToOrdinal;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.fastasyncworldedit.core.math.IntPair;
import com.fastasyncworldedit.core.util.MathMan;
import com.fastasyncworldedit.core.util.TaskManager;
import com.mojang.datafixers.util.Either;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
import com.sk89q.worldedit.bukkit.adapter.Refraction;
Expand Down Expand Up @@ -77,12 +76,11 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.function.IntFunction;

import static java.lang.invoke.MethodType.methodType;
import static net.minecraft.core.registries.Registries.BIOME;
Expand Down Expand Up @@ -415,7 +413,7 @@ public static LevelChunkSection newChunkSection(

public static LevelChunkSection newChunkSection(
final int layer,
final Function<Integer, char[]> get,
final IntFunction<char[]> get,
char[] set,
CachedBukkitAdapter adapter,
Registry<Biome> biomeRegistry,
Expand All @@ -431,9 +429,9 @@ public static LevelChunkSection newChunkSection(
try {
int num_palette;
if (get == null) {
num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null);
num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter);
} else {
num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null);
num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter);
}

int bitsPerEntry = MathMan.log2nlz(num_palette - 1);
Expand Down
Loading

0 comments on commit c8f1984

Please sign in to comment.