Remove BlockData.java

This commit is contained in:
TheMode 2021-06-11 17:33:18 +02:00
parent 64907ebb34
commit 7079225045
6 changed files with 12 additions and 377 deletions

View File

@ -41,8 +41,6 @@ public interface Block extends ProtocolObject, TagReadable, BlockConstants {
return Registry.block(this);
}
@NotNull BlockData getData();
default boolean compare(@NotNull Block block, @NotNull Comparator comparator) {
return comparator.test(this, block);
}
@ -74,15 +72,15 @@ public interface Block extends ProtocolObject, TagReadable, BlockConstants {
}
default boolean isAir() {
return getData().isAir();
return registry().isAir();
}
default boolean isSolid() {
return getData().isSolid();
return registry().isSolid();
}
default boolean isLiquid() {
return getData().isLiquid();
return registry().isLiquid();
}

View File

@ -1,116 +0,0 @@
package net.minestom.server.instance.block;
import net.minestom.server.item.Material;
import net.minestom.server.map.MapColors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface BlockData {
// Block properties
/**
* Gets the blast (explosion) resistance of a {@link Block}
* @return a double denoting the blast resistance.
*/
double getExplosionResistance();
/**
* Gets the corresponding {@link Material} of a {@link Block}
* @return the corresponding {@link Material} or null if not applicable.
*/
@Nullable Material getCorrespondingItem();
/**
* Gets the friction value of a {@link Block}
* @return a double denoting the friction.
*/
double getFriction();
/**
* Gets the speed factor of a {@link Block}
* @return a double denoting the speed factor.
*/
double getSpeedFactor();
/**
* Gets the jump factor of a {@link Block}
* @return a double denoting the jump factor.
*/
double getJumpFactor();
/**
* Checks if a {@link Block} is a block entity.
* @return a boolean, true when a Block is a block entity, false otherwise.
*/
boolean isBlockEntity();
// State properties
/**
* Gets the hardness (destroy speed) of a {@link Block}.
* @return a double denoting the hardness.
*/
double getHardness();
/**
* Gets the light level emitted by a {@link Block}
* @return an int representing the light emission.
*/
int getLightEmission();
/**
* Checks if a {@link Block} is occluding.
* @return a boolean, true if a Block is occluding, false otherwise.
*/
boolean isOccluding();
/**
* Gets the piston push reaction of a {@link Block}
* @return a {@link String} containing the push reaction of a Block.
*/
String getPushReaction(); // TODO: Dedicated object?
/**
* Checks if a {@link Block} is blocking motion
* @return a boolean, true if a Block is blocking motion, false otherwise.
*/
boolean isBlockingMotion();
/**
* Checks if a {@link Block} is flammable.
* @return a boolean, true if a Block is flammable, false otherwise.
*/
boolean isFlammable();
/**
* Checks if a {@link Block} is an instance of air.
* @return a boolean, true if a Block is air, false otherwise.
*/
boolean isAir();
/**
* Checks if a {@link Block} is an instance of a fluid.
* @return a boolean, true if a Block is a liquid, false otherwise.
*/
boolean isLiquid();
/**
* Checks if a {@link Block} is replaceable.
* @return a boolean, true if a Block is replaceable, false otherwise.
*/
boolean isReplaceable();
/**
* Checks if a {@link Block} is solid.
* @return a boolean, true if a Block is solid, false otherwise.
*/
boolean isSolid();
/**
* Checks if a {@link Block} is solid and blocking.
* @return a boolean, true if a Block is solid and blocking, false otherwise.
*/
boolean isSolidBlocking();
/**
* Gets the corresponding {@link MapColors} of a {@link Block}
* @return the corresponding {@link MapColors}.
*/
@NotNull MapColors getMapColor();
/**
* Gets the piston bounding box of a {@link Block}
* @return a {@link String} containing the bounding box of a Block.
*/
String getBoundingBox(); // TODO: Dedicated object?
}

View File

@ -1,170 +0,0 @@
package net.minestom.server.instance.block;
import net.minestom.server.item.Material;
import net.minestom.server.map.MapColors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Supplier;
class BlockDataImpl implements BlockData {
private final double explosionResistance;
private final @NotNull Supplier<@NotNull Material> item;
private final double friction;
private final double speedFactor;
private final double jumpFactor;
private final double hardness;
private final boolean blockEntity;
private final int lightEmission;
private final boolean occluding;
private final String pushReaction; // TODO: Dedicated object?
private final boolean blockingMotion;
private final boolean flammable;
private final boolean air;
private final boolean liquid;
private final boolean replaceable;
private final boolean solid;
private final boolean solidBlocking;
private final @NotNull MapColors mapColor;
private final String boundingBox; // TODO: Dedicated object?
BlockDataImpl(
double explosionResistance,
@NotNull Supplier<@NotNull Material> item,
double friction,
double speedFactor,
double jumpFactor,
boolean blockEntity,
double hardness,
int lightEmission,
boolean occluding,
String pushReaction,
boolean blockingMotion,
boolean flammable,
boolean air,
boolean liquid,
boolean replaceable,
boolean solid,
boolean solidBlocking,
@NotNull MapColors mapColor,
String boundingBox
) {
this.explosionResistance = explosionResistance;
this.item = item;
this.friction = friction;
this.speedFactor = speedFactor;
this.jumpFactor = jumpFactor;
this.hardness = hardness;
this.blockEntity = blockEntity;
this.lightEmission = lightEmission;
this.occluding = occluding;
this.pushReaction = pushReaction;
this.blockingMotion = blockingMotion;
this.air = air;
this.flammable = flammable;
this.liquid = liquid;
this.replaceable = replaceable;
this.solid = solid;
this.solidBlocking = solidBlocking;
this.mapColor = mapColor;
this.boundingBox = boundingBox;
}
@Override
public double getExplosionResistance() {
return explosionResistance;
}
@Override
public @Nullable Material getCorrespondingItem() {
return item.get();
}
@Override
public double getFriction() {
return friction;
}
@Override
public double getSpeedFactor() {
return speedFactor;
}
@Override
public double getJumpFactor() {
return jumpFactor;
}
@Override
public double getHardness() {
return hardness;
}
@Override
public boolean isBlockEntity() {
return blockEntity;
}
@Override
public int getLightEmission() {
return lightEmission;
}
@Override
public boolean isOccluding() {
return occluding;
}
@Override
public String getPushReaction() {
return pushReaction;
}
@Override
public boolean isBlockingMotion() {
return blockingMotion;
}
@Override
public boolean isFlammable() {
return flammable;
}
@Override
public boolean isAir() {
return air;
}
@Override
public boolean isLiquid() {
return liquid;
}
@Override
public boolean isReplaceable() {
return replaceable;
}
@Override
public boolean isSolid() {
return solid;
}
@Override
public boolean isSolidBlocking() {
return solidBlocking;
}
@Override
public @NotNull MapColors getMapColor() {
return mapColor;
}
@Override
public String getBoundingBox() {
return boundingBox;
}
}

View File

@ -1,35 +1,15 @@
package net.minestom.server.instance.block;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import it.unimi.dsi.fastutil.shorts.Short2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.shorts.Short2ObjectSortedMap;
import net.minestom.server.MinecraftServer;
import net.minestom.server.item.Material;
import net.minestom.server.map.MapColors;
import net.minestom.server.registry.Registries;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
class BlockImpl implements Block {
private static final Logger LOGGER = LoggerFactory.getLogger(BlockImpl.class);
private static final Short2ObjectSortedMap<BlockData> blockData = new Short2ObjectAVLTreeMap<>();
static {
loadBlockData();
}
private NamespaceID namespaceID;
private int blockId;
@ -41,7 +21,6 @@ class BlockImpl implements Block {
private NBTCompound compound;
private BlockImpl() {
}
private BlockImpl(NamespaceID namespaceID,
@ -180,11 +159,6 @@ class BlockImpl implements Block {
return stateId;
}
@Override
public @NotNull BlockData getData() {
return blockData.get(stateId);
}
@Override
public @Nullable BlockHandler getHandler() {
return handler;
@ -276,63 +250,4 @@ class BlockImpl implements Block {
return result;
}
/**
* Loads the {@link BlockData} from the JAR Resources to the Map.
*/
private static void loadBlockData() {
// E.G. 1_16_5_blocks.json
InputStream blocksIS = BlockImpl.class.getResourceAsStream("/minecraft_data/" + MinecraftServer.VERSION_NAME_UNDERSCORED + "_blocks.json");
if (blocksIS == null) {
LOGGER.error("Failed to find blocks.json");
return;
}
// Get map Colors as we will need these
MapColors[] mapColors = MapColors.values();
JsonArray blocks = new Gson().fromJson(new InputStreamReader(blocksIS), JsonArray.class);
for (JsonElement blockEntry : blocks) {
// Load Data
JsonObject block = blockEntry.getAsJsonObject();
double explosionResistance = block.get("explosionResistance").getAsDouble();
double friction = block.get("friction").getAsDouble();
double speedFactor = block.get("speedFactor").getAsDouble();
double jumpFactor = block.get("jumpFactor").getAsDouble();
boolean blockEntity = block.get("blockEntity").getAsBoolean();
final String blockId = block.get("itemId").getAsString();
java.util.function.Supplier<Material> itemSupplier = () -> Registries.getMaterial(blockId);
JsonArray states = block.get("states").getAsJsonArray();
for (JsonElement stateEntry : states) {
// Load Data
JsonObject state = stateEntry.getAsJsonObject();
short stateId = state.get("id").getAsShort();
blockData.put(
stateId,
new BlockDataImpl(
explosionResistance,
itemSupplier,
friction,
speedFactor,
jumpFactor,
blockEntity,
state.get("destroySpeed").getAsDouble(),
state.get("lightEmission").getAsInt(),
state.get("doesOcclude").getAsBoolean(),
state.get("pushReaction").getAsString(),
state.get("blocksMotion").getAsBoolean(),
state.get("isFlammable").getAsBoolean(),
state.get("isAir").getAsBoolean(),
state.get("isLiquid").getAsBoolean(),
state.get("isReplaceable").getAsBoolean(),
state.get("isSolid").getAsBoolean(),
state.get("isSolidBlocking").getAsBoolean(),
mapColors[state.get("mapColorId").getAsInt()],
state.get("boundingBox").getAsString()
)
);
}
}
}
}

View File

@ -56,7 +56,7 @@ public class PlayerDiggingListener {
final boolean instantBreak = player.isCreative() ||
player.isInstantBreak() ||
block.getData().getHardness() == 0;
block.registry().destroySpeed() == 0;
if (instantBreak) {
// No need to check custom block

View File

@ -33,9 +33,17 @@ public class Registry {
return getFloat("jumpFactor");
}
public boolean isAir() {
return getBoolean("air");
}
public boolean isSolid() {
return getBoolean("solid");
}
public boolean isLiquid() {
return getBoolean("liquid");
}
}
public static class Entry {