Skip to content

Commit

Permalink
Simplify and deduplicate PaperweightGetBlocks#update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell committed Jan 2, 2025
1 parent e0db69c commit f809c6c
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
init();
}

private synchronized boolean init() {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,13 +966,8 @@ public char[] update(int layer, char[] data, boolean aggressive) {
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
return data;
}
if (data != null && data.length != 4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
}
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096 || data.length != 4096) {
data = new char[4096]; // new array, will be populated below
}
Semaphore lock = PaperweightPlatformAdapter.applyLock(section);
synchronized (lock) {
Expand Down Expand Up @@ -1001,38 +996,24 @@ public char[] update(int layer, char[] data, boolean aggressive) {
num_palette = palette.getSize();
} else {
// The section's palette is the global block palette.
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char ordinal = adapter.ibdIDToOrdinal(paletteVal);
data[i] = ordinal;
}
adapter.mapFromGlobalPalette(data);
return data;
}

char[] paletteToOrdinal = FaweCache.INSTANCE.PALETTE_TO_BLOCK_CHAR.get();
try {
if (num_palette != 1) {
if (num_palette == 1) {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
} else {
for (int i = 0; i < num_palette; i++) {
char ordinal = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = ordinal;
}
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char val = paletteToOrdinal[paletteVal];
if (val == Character.MAX_VALUE) {
val = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = val;
}
data[i] = val;
}
} else {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
adapter.mapWithPalette(data, paletteToOrdinal);
}
} finally {
for (int i = 0; i < num_palette; i++) {
paletteToOrdinal[i] = Character.MAX_VALUE;
}
Arrays.fill(paletteToOrdinal, 0, num_palette, Character.MAX_VALUE);
}
return data;
} catch (IllegalAccessException | InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
init();
}

private synchronized boolean init() {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,13 +964,8 @@ public char[] update(int layer, char[] data, boolean aggressive) {
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
return data;
}
if (data != null && data.length != 4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
}
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096 || data.length != 4096) {
data = new char[4096]; // new array, will be populated below
}
Semaphore lock = PaperweightPlatformAdapter.applyLock(section);
synchronized (lock) {
Expand Down Expand Up @@ -999,38 +994,24 @@ public char[] update(int layer, char[] data, boolean aggressive) {
num_palette = palette.getSize();
} else {
// The section's palette is the global block palette.
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char ordinal = adapter.ibdIDToOrdinal(paletteVal);
data[i] = ordinal;
}
adapter.mapFromGlobalPalette(data);
return data;
}

char[] paletteToOrdinal = FaweCache.INSTANCE.PALETTE_TO_BLOCK_CHAR.get();
try {
if (num_palette != 1) {
if (num_palette == 1) {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
} else {
for (int i = 0; i < num_palette; i++) {
char ordinal = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = ordinal;
}
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char val = paletteToOrdinal[paletteVal];
if (val == Character.MAX_VALUE) {
val = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = val;
}
data[i] = val;
}
} else {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
adapter.mapWithPalette(data, paletteToOrdinal);
}
} finally {
for (int i = 0; i < num_palette; i++) {
paletteToOrdinal[i] = Character.MAX_VALUE;
}
Arrays.fill(paletteToOrdinal, 0, num_palette, Character.MAX_VALUE);
}
return data;
} catch (IllegalAccessException | InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
init();
}

private synchronized boolean init() {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,13 +967,8 @@ public char[] update(int layer, char[] data, boolean aggressive) {
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
return data;
}
if (data != null && data.length != 4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
}
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096 || data.length != 4096) {
data = new char[4096]; // new array, will be populated below
}
Semaphore lock = PaperweightPlatformAdapter.applyLock(section);
synchronized (lock) {
Expand Down Expand Up @@ -1002,38 +997,24 @@ public char[] update(int layer, char[] data, boolean aggressive) {
num_palette = palette.getSize();
} else {
// The section's palette is the global block palette.
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char ordinal = adapter.ibdIDToOrdinal(paletteVal);
data[i] = ordinal;
}
adapter.mapFromGlobalPalette(data);
return data;
}

char[] paletteToOrdinal = FaweCache.INSTANCE.PALETTE_TO_BLOCK_CHAR.get();
try {
if (num_palette != 1) {
if (num_palette == 1) {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
} else {
for (int i = 0; i < num_palette; i++) {
char ordinal = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = ordinal;
}
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char val = paletteToOrdinal[paletteVal];
if (val == Character.MAX_VALUE) {
val = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = val;
}
data[i] = val;
}
} else {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
adapter.mapWithPalette(data, paletteToOrdinal);
}
} finally {
for (int i = 0; i < num_palette; i++) {
paletteToOrdinal[i] = Character.MAX_VALUE;
}
Arrays.fill(paletteToOrdinal, 0, num_palette, Character.MAX_VALUE);
}
return data;
} catch (IllegalAccessException | InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
init();
}

private synchronized boolean init() {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,13 +962,8 @@ public char[] update(int layer, char[] data, boolean aggressive) {
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
return data;
}
if (data != null && data.length != 4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
}
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096 || data.length != 4096) {
data = new char[4096]; // new array, will be populated below
}
Semaphore lock = PaperweightPlatformAdapter.applyLock(section);
synchronized (lock) {
Expand Down Expand Up @@ -997,38 +992,24 @@ public char[] update(int layer, char[] data, boolean aggressive) {
num_palette = palette.getSize();
} else {
// The section's palette is the global block palette.
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char ordinal = adapter.ibdIDToOrdinal(paletteVal);
data[i] = ordinal;
}
adapter.mapFromGlobalPalette(data);
return data;
}

char[] paletteToOrdinal = FaweCache.INSTANCE.PALETTE_TO_BLOCK_CHAR.get();
try {
if (num_palette != 1) {
if (num_palette == 1) {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
} else {
for (int i = 0; i < num_palette; i++) {
char ordinal = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = ordinal;
}
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char val = paletteToOrdinal[paletteVal];
if (val == Character.MAX_VALUE) {
val = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = val;
}
data[i] = val;
}
} else {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
adapter.mapWithPalette(data, paletteToOrdinal);
}
} finally {
for (int i = 0; i < num_palette; i++) {
paletteToOrdinal[i] = Character.MAX_VALUE;
}
Arrays.fill(paletteToOrdinal, 0, num_palette, Character.MAX_VALUE);
}
return data;
} catch (IllegalAccessException | InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
init();
}

private synchronized boolean init() {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,8 @@ public char[] update(int layer, char[] data, boolean aggressive) {
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
return data;
}
if (data != null && data.length != 4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
}
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096 || data.length != 4096) {
data = new char[4096]; // new array, will be populated below
}
Semaphore lock = PaperweightPlatformAdapter.applyLock(section);
synchronized (lock) {
Expand Down Expand Up @@ -998,38 +993,24 @@ public char[] update(int layer, char[] data, boolean aggressive) {
num_palette = palette.getSize();
} else {
// The section's palette is the global block palette.
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char ordinal = adapter.ibdIDToOrdinal(paletteVal);
data[i] = ordinal;
}
adapter.mapFromGlobalPalette(data);
return data;
}

char[] paletteToOrdinal = FaweCache.INSTANCE.PALETTE_TO_BLOCK_CHAR.get();
try {
if (num_palette != 1) {
if (num_palette == 1) {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
} else {
for (int i = 0; i < num_palette; i++) {
char ordinal = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = ordinal;
}
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char val = paletteToOrdinal[paletteVal];
if (val == Character.MAX_VALUE) {
val = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = val;
}
data[i] = val;
}
} else {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
adapter.mapWithPalette(data, paletteToOrdinal);
}
} finally {
for (int i = 0; i < num_palette; i++) {
paletteToOrdinal[i] = Character.MAX_VALUE;
}
Arrays.fill(paletteToOrdinal, 0, num_palette, Character.MAX_VALUE);
}
return data;
} catch (IllegalAccessException | InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
init();
}

private synchronized boolean init() {
if (ibdToOrdinal != null && ibdToOrdinal[1] != 0) {
return false;
Expand Down
Loading

0 comments on commit f809c6c

Please sign in to comment.