Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify and deduplicate PaperweightGetBlocks#update logic #3055

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
if (!this.initialised) {
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,13 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
if (!this.initialised) {
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,13 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
if (!this.initialised) {
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,13 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
if (!this.initialised) {
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,13 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
if (!this.initialised) {
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,13 @@ public BukkitImplAdapter<net.minecraft.nbt.Tag> getParent() {
return parent;
}

@Override
protected void ensureInit() {
if (!this.initialised) {
init();
}
}

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