mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-08 20:03:31 +01:00
Remove legacy methods
This commit is contained in:
parent
3324fd1e21
commit
2d57235198
@ -5,7 +5,6 @@ import net.minestom.server.registry.Registry;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.tag.TagReadable;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.math.IntRange;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
@ -82,26 +81,20 @@ public interface Block extends ProtocolObject, TagReadable, BlockConstants {
|
||||
return compare(block, Comparator.ID);
|
||||
}
|
||||
|
||||
static @Nullable Block fromNamespaceId(@NotNull NamespaceID namespaceID) {
|
||||
return BlockRegistry.fromNamespaceId(namespaceID);
|
||||
static @Nullable Block fromNamespaceId(@NotNull String namespaceID) {
|
||||
return BlockRegistry.get(namespaceID);
|
||||
}
|
||||
|
||||
static @Nullable Block fromNamespaceId(@NotNull String namespaceID) {
|
||||
return fromNamespaceId(NamespaceID.from(namespaceID));
|
||||
static @Nullable Block fromNamespaceId(@NotNull NamespaceID namespaceID) {
|
||||
return fromNamespaceId(namespaceID.asString());
|
||||
}
|
||||
|
||||
static @Nullable Block fromStateId(short stateId) {
|
||||
return BlockRegistry.fromStateId(stateId);
|
||||
return BlockRegistry.getState(stateId);
|
||||
}
|
||||
|
||||
static @Nullable Block fromBlockId(int blockId) {
|
||||
return BlockRegistry.fromBlockId(blockId);
|
||||
}
|
||||
|
||||
static void register(@NotNull NamespaceID namespaceID, @NotNull Block block,
|
||||
@NotNull IntRange range,
|
||||
@NotNull Block.Supplier blockSupplier) {
|
||||
BlockRegistry.register(namespaceID, block, range, blockSupplier);
|
||||
return BlockRegistry.getId(blockId);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
|
@ -1,215 +1,13 @@
|
||||
package net.minestom.server.instance.block;
|
||||
|
||||
import net.minestom.server.registry.Registry;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.math.IntRange;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
@Deprecated
|
||||
class BlockImpl implements Block {
|
||||
|
||||
private NamespaceID namespaceID;
|
||||
private int blockId;
|
||||
private short minStateId, stateId;
|
||||
private List<BlockProperty<?>> properties;
|
||||
protected BlockImpl original = null;
|
||||
private LinkedHashMap<BlockProperty<?>, Object> propertiesMap;
|
||||
private BlockHandler handler;
|
||||
private NBTCompound compound;
|
||||
|
||||
private BlockImpl() {
|
||||
}
|
||||
|
||||
private BlockImpl(NamespaceID namespaceID,
|
||||
int blockId,
|
||||
short minStateId, short stateId,
|
||||
List<BlockProperty<?>> properties,
|
||||
LinkedHashMap<BlockProperty<?>, Object> propertiesMap,
|
||||
NBTCompound compound) {
|
||||
this.namespaceID = namespaceID;
|
||||
this.blockId = blockId;
|
||||
this.minStateId = minStateId;
|
||||
this.stateId = stateId;
|
||||
this.properties = properties;
|
||||
this.propertiesMap = propertiesMap;
|
||||
this.compound = compound;
|
||||
}
|
||||
|
||||
private BlockImpl(NamespaceID namespaceID,
|
||||
int blockId, short minStateId, short stateId,
|
||||
List<BlockProperty<?>> properties,
|
||||
LinkedHashMap<BlockProperty<?>, Object> propertiesMap) {
|
||||
this(namespaceID, blockId, minStateId, stateId, properties, propertiesMap, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
||||
return tag.read(compound);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTag(@NotNull Tag<?> tag) {
|
||||
return compound.containsKey(tag.getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> Block withTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
if ((compound == null || compound.getKeys().isEmpty()) && value == null) {
|
||||
// No change
|
||||
return this;
|
||||
}
|
||||
|
||||
// Apply tag
|
||||
NBTCompound compound = Objects.requireNonNullElseGet(this.compound, NBTCompound::new);
|
||||
tag.write(compound, value);
|
||||
if (compound.getKeys().isEmpty()) {
|
||||
compound = null;
|
||||
}
|
||||
return withNbt(compound);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Block withNbt(@Nullable NBTCompound compound) {
|
||||
var block = shallowClone();
|
||||
block.compound = compound;
|
||||
return block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
||||
var block = shallowClone();
|
||||
block.handler = handler;
|
||||
return block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getProperty(@NotNull String property) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable NBTCompound getNbt() {
|
||||
return compound != null ? compound.deepClone() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull NamespaceID getNamespaceId() {
|
||||
return namespaceID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, String> createPropertiesMap() {
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
propertiesMap.forEach((blockProperty, o) -> properties.put(blockProperty.getName(), o.toString()));
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Registry.BlockEntry registry() {
|
||||
return BlockRegistry.getState(stateId).registry(); // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable BlockHandler getHandler() {
|
||||
return handler;
|
||||
}
|
||||
|
||||
private @NotNull BlockImpl shallowClone() {
|
||||
var block = new BlockImpl();
|
||||
block.namespaceID = namespaceID;
|
||||
block.blockId = blockId;
|
||||
block.minStateId = minStateId;
|
||||
block.stateId = stateId;
|
||||
block.properties = properties;
|
||||
block.original = original;
|
||||
block.propertiesMap = propertiesMap;
|
||||
block.handler = handler;
|
||||
block.compound = compound;
|
||||
return block;
|
||||
}
|
||||
|
||||
protected static BlockImpl create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId,
|
||||
short defaultStateId, List<BlockProperty<?>> properties) {
|
||||
var block = new BlockImpl(namespaceID, blockId, minStateId, defaultStateId,
|
||||
properties, computeMap(minStateId, defaultStateId, properties));
|
||||
block.original = block;
|
||||
Block.register(namespaceID, block,
|
||||
new IntRange((int) minStateId, (int) maxStateId), requestedStateId -> {
|
||||
var requestedBlock = new BlockImpl(namespaceID, blockId, minStateId, requestedStateId,
|
||||
properties, computeMap(minStateId, requestedStateId, properties));
|
||||
requestedBlock.original = block;
|
||||
return requestedBlock;
|
||||
});
|
||||
return block;
|
||||
}
|
||||
|
||||
private static short computeId(short id, List<BlockProperty<?>> properties,
|
||||
LinkedHashMap<BlockProperty<?>, Object> propertiesMap) {
|
||||
int[] factors = computeFactors(properties);
|
||||
int index = 0;
|
||||
for (var entry : propertiesMap.entrySet()) {
|
||||
var property = entry.getKey();
|
||||
var value = entry.getValue();
|
||||
if (value != null) {
|
||||
var values = property.getPossibleValues();
|
||||
id += values.indexOf(value) * factors[index++];
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
private static LinkedHashMap<BlockProperty<?>, Object> computeMap(short minStateId, short stateId, List<BlockProperty<?>> properties) {
|
||||
// Computes a filled property map from a state id
|
||||
|
||||
LinkedHashMap<BlockProperty<?>, Object> result = new LinkedHashMap<>();
|
||||
int[] factors = computeFactors(properties);
|
||||
short deltaId = (short) (stateId - minStateId);
|
||||
int index = 0;
|
||||
for (var property : properties) {
|
||||
final var possibilities = property.getPossibleValues();
|
||||
final int factor = factors[index++];
|
||||
|
||||
// TODO optimize
|
||||
int value = 0;
|
||||
int valueIndex = 0;
|
||||
while (value < deltaId) {
|
||||
if (value + factor > deltaId)
|
||||
break;
|
||||
value += factor;
|
||||
valueIndex++;
|
||||
}
|
||||
deltaId -= value;
|
||||
|
||||
result.put(property, possibilities.get(valueIndex));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int[] computeFactors(List<BlockProperty<?>> properties) {
|
||||
final int size = properties.size();
|
||||
int[] result = new int[size];
|
||||
int factor = 1;
|
||||
ListIterator<BlockProperty<?>> li = properties.listIterator(properties.size());
|
||||
// Iterate in reverse.
|
||||
int i = size;
|
||||
while (li.hasPrevious()) {
|
||||
var property = li.previous();
|
||||
result[--i] = factor;
|
||||
factor *= property.getPossibleValues().size();
|
||||
}
|
||||
|
||||
return result;
|
||||
class BlockImpl {
|
||||
protected static Block create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId,
|
||||
short defaultStateId, List<BlockProperty<?>> properties) {
|
||||
return BlockRegistry.get(namespaceID.asString());
|
||||
}
|
||||
}
|
@ -1,20 +1,13 @@
|
||||
package net.minestom.server.instance.block;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectSortedMap;
|
||||
import it.unimi.dsi.fastutil.shorts.Short2ObjectAVLTreeMap;
|
||||
import it.unimi.dsi.fastutil.shorts.Short2ObjectSortedMap;
|
||||
import net.minestom.server.registry.Registry;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.math.IntRange;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
class BlockRegistry {
|
||||
|
||||
@ -27,10 +20,6 @@ class BlockRegistry {
|
||||
// Block namespace -> properties map to block access
|
||||
private static final Map<String, PropertyEntry> BLOCK_PROPERTY_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
private static final Map<NamespaceID, Block> namespaceMap = new HashMap<>();
|
||||
private static final Int2ObjectSortedMap<Block> blockSet = new Int2ObjectAVLTreeMap<>();
|
||||
private static final Short2ObjectSortedMap<Block.Supplier> stateSet = new Short2ObjectAVLTreeMap<>();
|
||||
|
||||
private static class PropertyEntry {
|
||||
private final Map<Map<String, String>, Block> propertyMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
@ -46,6 +35,8 @@ class BlockRegistry {
|
||||
final JsonObject stateObject = blockObject.remove("states").getAsJsonObject();
|
||||
blockObject.remove("properties");
|
||||
|
||||
blockObject.addProperty("namespace", blockNamespace);
|
||||
|
||||
retrieveState(blockNamespace, blockObject, stateObject);
|
||||
final int defaultState = blockObject.get("defaultStateId").getAsInt();
|
||||
final Block defaultBlock = getState(defaultState);
|
||||
@ -110,26 +101,7 @@ class BlockRegistry {
|
||||
return entry.propertyMap.get(properties);
|
||||
}
|
||||
|
||||
public static synchronized @Nullable Block fromNamespaceId(@NotNull NamespaceID namespaceID) {
|
||||
return namespaceMap.get(namespaceID);
|
||||
}
|
||||
|
||||
public static synchronized @Nullable Block fromStateId(short stateId) {
|
||||
Block.Supplier supplier = stateSet.get(stateId);
|
||||
if (supplier == null) {
|
||||
return null;
|
||||
}
|
||||
return supplier.get(stateId);
|
||||
}
|
||||
|
||||
public static synchronized @Nullable Block fromBlockId(int blockId) {
|
||||
return blockSet.get(blockId);
|
||||
}
|
||||
|
||||
public static synchronized void register(@NotNull NamespaceID namespaceID, @NotNull Block block,
|
||||
@NotNull IntRange range, @NotNull Block.Supplier blockSupplier) {
|
||||
namespaceMap.put(namespaceID, block);
|
||||
IntStream.range(range.getMinimum(), range.getMaximum() + 1).forEach(value -> stateSet.put((short) value, blockSupplier));
|
||||
blockSet.put(block.getId(), block);
|
||||
public static @Nullable Block getProperties(Block block, Map<String, String> properties) {
|
||||
return getProperties(block.getNamespaceId().asString(), properties);
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ class BlockTest implements Block {
|
||||
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
|
||||
var properties = new HashMap<>(this.properties);
|
||||
properties.put(property, value);
|
||||
return Objects.requireNonNull(BlockRegistry.getProperties(getNamespaceId().asString(), properties));
|
||||
return Objects.requireNonNull(BlockRegistry.getProperties(this, properties));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -70,7 +70,7 @@ class BlockTest implements Block {
|
||||
|
||||
@Override
|
||||
public @Nullable NBTCompound getNbt() {
|
||||
return compound.deepClone();
|
||||
return compound != null ? compound.deepClone() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user