Plex-FAWE/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/blocks/CharBlocks.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
4.7 KiB
Java
Raw Normal View History

2019-04-28 15:44:59 +00:00
package com.boydti.fawe.beta.implementation.blocks;
2020-01-03 17:02:18 +00:00
import com.boydti.fawe.Fawe;
2019-04-28 15:44:59 +00:00
import com.boydti.fawe.beta.IBlocks;
import com.boydti.fawe.beta.IChunkSet;
2019-10-23 04:23:52 +00:00
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache;
2020-01-03 17:02:18 +00:00
import org.jetbrains.annotations.Range;
2019-10-23 04:23:52 +00:00
2019-10-26 13:21:49 +00:00
public abstract class CharBlocks implements IBlocks {
2019-08-06 15:29:09 +00:00
public static final Section FULL = new Section() {
@Override
public final char[] get(CharBlocks blocks, int layer) {
return blocks.blocks[layer];
}
};
public static final Section EMPTY = new Section() {
@Override
public synchronized final char[] get(CharBlocks blocks, int layer) {
2019-08-06 15:29:09 +00:00
char[] arr = blocks.blocks[layer];
if (arr == null) {
2019-11-07 10:28:17 +00:00
arr = blocks.blocks[layer] = blocks.update(layer, null);
2019-11-17 19:30:28 +00:00
if (arr == null) {
throw new IllegalStateException("Array cannot be null: " + blocks.getClass());
}
2019-08-06 15:29:09 +00:00
} else {
2019-11-07 10:28:17 +00:00
blocks.blocks[layer] = blocks.update(layer, arr);
2019-11-17 20:02:33 +00:00
if (blocks.blocks[layer] == null) {
throw new IllegalStateException("Array cannot be null (update): " + blocks.getClass());
}
}
if (blocks.blocks[layer] != null) {
blocks.sections[layer] = FULL;
2019-08-06 15:29:09 +00:00
}
return arr;
}
};
2019-04-30 16:19:10 +00:00
public final char[][] blocks;
public final Section[] sections;
public CharBlocks() {
blocks = new char[16][];
sections = new Section[16];
2019-08-06 15:29:09 +00:00
for (int i = 0; i < 16; i++) {
sections[i] = EMPTY;
}
2019-04-30 16:19:10 +00:00
}
@Override
2019-08-06 15:29:09 +00:00
public boolean trim(boolean aggressive) {
2019-04-30 16:19:10 +00:00
boolean result = true;
for (int i = 0; i < 16; i++) {
2019-11-17 20:02:33 +00:00
if (sections[i] == EMPTY && blocks[i] != null) {
blocks[i] = null;
2019-04-30 16:19:10 +00:00
} else {
result = false;
}
}
return result;
}
@Override
public boolean trim(boolean aggressive, int layer) {
boolean result = true;
if (sections[layer] == EMPTY && blocks[layer] != null) {
blocks[layer] = null;
} else {
result = false;
}
return result;
}
2019-04-30 16:19:10 +00:00
@Override
public IChunkSet reset() {
2019-08-06 15:29:09 +00:00
for (int i = 0; i < 16; i++) {
sections[i] = EMPTY;
}
return null;
2019-04-30 16:19:10 +00:00
}
2020-01-03 17:02:18 +00:00
public void reset(@Range(from = 0, to = 15) int layer) {
sections[layer] = EMPTY;
2019-05-01 18:19:15 +00:00
}
2019-11-07 10:28:17 +00:00
public char[] update(int layer, char[] data) {
if (data == null) {
return new char[4096];
}
2019-08-06 15:29:09 +00:00
for (int i = 0; i < 4096; i++) {
data[i] = 0;
}
2019-04-30 16:19:10 +00:00
return data;
}
@Override
2020-01-03 17:02:18 +00:00
public boolean hasSection(@Range(from = 0, to = 15) int layer) {
2019-04-30 16:19:10 +00:00
return sections[layer] == FULL;
}
2019-10-23 04:23:52 +00:00
@Override
2020-01-03 17:02:18 +00:00
public char[] load(@Range(from = 0, to = 15) int layer) {
2019-10-23 04:23:52 +00:00
return sections[layer].get(this, layer);
}
@Override
public BlockState getBlock(int x, int y, int z) {
return BlockTypesCache.states[get(x, y, z)];
2019-10-23 04:23:52 +00:00
}
2020-01-26 18:01:16 +00:00
public char get(int x, @Range(from = 0, to = 255) int y, int z) {
2019-05-02 14:45:03 +00:00
final int layer = y >> 4;
2019-08-06 15:29:09 +00:00
final int index = (y & 15) << 8 | z << 4 | x;
2019-04-30 16:19:10 +00:00
return sections[layer].get(this, layer, index);
}
2020-01-26 18:01:16 +00:00
public void set(int x, @Range(from = 0, to = 255) int y, int z, char value) {
2019-05-02 14:45:03 +00:00
final int layer = y >> 4;
2019-08-06 15:29:09 +00:00
final int index = (y & 15) << 8 | z << 4 | x;
try {
set(layer, index, value);
} catch (ArrayIndexOutOfBoundsException exception) {
assert Fawe.imp() != null;
Fawe.imp().debug("Tried Setting Block at x:" + x + ", y:" + y + " , z:" + z);
Fawe.imp().debug("Layer variable was = " + layer);
exception.printStackTrace();
}
2019-05-02 14:45:03 +00:00
}
2019-08-06 15:29:09 +00:00
/*
Section
*/
2020-01-03 17:02:18 +00:00
public final char get(@Range(from = 0, to = 15)int layer, int index) {
2019-05-02 14:45:03 +00:00
return sections[layer].get(this, layer, index);
}
public final void set(@Range(from = 0, to = 15) int layer, int index, char value) throws ArrayIndexOutOfBoundsException {
2019-05-02 14:45:03 +00:00
sections[layer].set(this, layer, index, value);
2019-04-30 16:19:10 +00:00
}
public abstract static class Section {
2019-08-06 15:29:09 +00:00
2020-01-26 18:01:16 +00:00
public abstract char[] get(CharBlocks blocks, @Range(from = 0, to = 15) int layer);
2019-04-30 16:19:10 +00:00
2020-01-26 18:01:16 +00:00
public final char get(CharBlocks blocks, @Range(from = 0, to = 15) int layer, int index) {
2019-04-30 16:19:10 +00:00
return get(blocks, layer)[index];
}
2020-01-26 18:01:16 +00:00
public final void set(CharBlocks blocks, @Range(from = 0, to = 15) int layer, int index, char value) {
2019-05-02 14:45:03 +00:00
get(blocks, layer)[index] = value;
2019-04-30 16:19:10 +00:00
}
}
2019-04-28 15:44:59 +00:00
}