3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-20 15:00:11 +01:00

Start using blocks refactor

Dieser Commit ist enthalten in:
AJ Ferguson 2024-05-17 19:09:42 -04:00
Ursprung 91574efdb6
Commit b79a12ea8e
3 geänderte Dateien mit 47 neuen und 58 gelöschten Zeilen

Datei anzeigen

@ -38,12 +38,14 @@ import org.geysermc.erosion.util.BlockPositionIterator;
import org.geysermc.geyser.entity.attribute.GeyserAttributeType; import org.geysermc.geyser.entity.attribute.GeyserAttributeType;
import org.geysermc.geyser.entity.type.LivingEntity; import org.geysermc.geyser.entity.type.LivingEntity;
import org.geysermc.geyser.level.block.BlockStateValues; import org.geysermc.geyser.level.block.BlockStateValues;
import org.geysermc.geyser.level.block.Blocks;
import org.geysermc.geyser.level.block.Fluid; import org.geysermc.geyser.level.block.Fluid;
import org.geysermc.geyser.level.block.property.Properties;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.level.physics.BoundingBox; import org.geysermc.geyser.level.physics.BoundingBox;
import org.geysermc.geyser.level.physics.CollisionManager; import org.geysermc.geyser.level.physics.CollisionManager;
import org.geysermc.geyser.level.physics.Direction; import org.geysermc.geyser.level.physics.Direction;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.type.BlockMapping;
import org.geysermc.geyser.translator.collision.BlockCollision; import org.geysermc.geyser.translator.collision.BlockCollision;
import org.geysermc.geyser.translator.collision.SolidCollision; import org.geysermc.geyser.translator.collision.SolidCollision;
import org.geysermc.geyser.util.BlockUtils; import org.geysermc.geyser.util.BlockUtils;
@ -61,10 +63,10 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
private static final float MIN_VELOCITY = 0.003f; private static final float MIN_VELOCITY = 0.003f;
private static final float CLIMB_SPEED = 0.15f; private static final float CLIMB_SPEED = 0.15f;
private static final Map<String, Vector3f> MOVEMENT_MULTIPLIERS = Map.of( private static final Map<Block, Vector3f> MOVEMENT_MULTIPLIERS = Map.of(
"minecraft:cobweb", Vector3f.from(0.25f, 0.05f, 0.25f), Blocks.COBWEB, Vector3f.from(0.25f, 0.05f, 0.25f),
"minecraft:powder_snow", Vector3f.from(0.9f, 1.5f, 0.9f), Blocks.POWDER_SNOW, Vector3f.from(0.9f, 1.5f, 0.9f),
"minecraft:sweet_berry_bush", Vector3f.from(0.8f, 0.75f, 0.8f) Blocks.SWEET_BERRY_BUSH, Vector3f.from(0.8f, 0.75f, 0.8f)
); );
protected final T vehicle; protected final T vehicle;
@ -146,7 +148,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
switch (fluidHeight.left()) { switch (fluidHeight.left()) {
case WATER -> waterMovement(); case WATER -> waterMovement();
case LAVA -> { case LAVA -> {
if (vehicle.canWalkOnLava() && BlockStateValues.getFluid(getBlockAt(boundingBox.getBottomCenter().toInt())) == Fluid.LAVA) { if (vehicle.canWalkOnLava() && getBlock(boundingBox.getBottomCenter().toInt()) == Blocks.LAVA) {
landMovement(); landMovement();
} else { } else {
lavaMovement(fluidHeight.rightDouble()); lavaMovement(fluidHeight.rightDouble());
@ -171,7 +173,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
if (lavaHeight > 0 && vehicle.getDefinition().entityType() == EntityType.STRIDER) { if (lavaHeight > 0 && vehicle.getDefinition().entityType() == EntityType.STRIDER) {
Vector3i blockPos = boundingBox.getBottomCenter().toInt(); Vector3i blockPos = boundingBox.getBottomCenter().toInt();
if (!CollisionManager.FLUID_COLLISION.isBelow(blockPos.getY(), boundingBox) || BlockStateValues.getFluid(getBlockAt(blockPos.up())) == Fluid.LAVA) { if (!CollisionManager.FLUID_COLLISION.isBelow(blockPos.getY(), boundingBox) || getBlock(boundingBox.getBottomCenter().toInt()) == Blocks.LAVA) {
vehicle.setMotion(vehicle.getMotion().mul(0.5f).add(0, 0.05f, 0)); vehicle.setMotion(vehicle.getMotion().mul(0.5f).add(0, 0.05f, 0));
} else { } else {
vehicle.setOnGround(true); vehicle.setOnGround(true);
@ -213,7 +215,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
Vector3d velocity = Vector3d.ZERO; Vector3d velocity = Vector3d.ZERO;
for (Direction direction : Direction.HORIZONTAL) { for (Direction direction : Direction.HORIZONTAL) {
Vector3i adjacentBlockPos = blockPos.add(direction.getUnitVector()); Vector3i adjacentBlockPos = blockPos.add(direction.getUnitVector());
int adjacentBlockId = getBlockAt(adjacentBlockPos); int adjacentBlockId = getBlockId(adjacentBlockPos);
Fluid adjacentFluid = BlockStateValues.getFluid(adjacentBlockId); Fluid adjacentFluid = BlockStateValues.getFluid(adjacentBlockId);
float fluidHeightDiff = 0; float fluidHeightDiff = 0;
@ -224,7 +226,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
// check if there is a fluid under it // check if there is a fluid under it
BlockCollision adjacentBlockCollision = BlockUtils.getCollision(adjacentBlockId); BlockCollision adjacentBlockCollision = BlockUtils.getCollision(adjacentBlockId);
if (adjacentBlockCollision == null) { if (adjacentBlockCollision == null) {
float adjacentFluidHeight = getLogicalFluidHeight(fluid, getBlockAt(adjacentBlockPos.add(Direction.DOWN.getUnitVector()))); float adjacentFluidHeight = getLogicalFluidHeight(fluid, getBlockId(adjacentBlockPos.add(Direction.DOWN.getUnitVector())));
if (adjacentFluidHeight != -1) { // Only care about same type of fluid if (adjacentFluidHeight != -1) { // Only care about same type of fluid
fluidHeightDiff = getLogicalFluidHeight(fluid, blockId) - (adjacentFluidHeight - MAX_LOGICAL_FLUID_HEIGHT); fluidHeightDiff = getLogicalFluidHeight(fluid, blockId) - (adjacentFluidHeight - MAX_LOGICAL_FLUID_HEIGHT);
} }
@ -243,7 +245,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
if (!flowBlocked) { if (!flowBlocked) {
Vector3i blockPosUp = blockPos.up(); Vector3i blockPosUp = blockPos.up();
for (Direction direction : Direction.HORIZONTAL) { for (Direction direction : Direction.HORIZONTAL) {
flowBlocked = isFlowBlocked(fluid, getBlockAt(blockPosUp.add(direction.getUnitVector()))); flowBlocked = isFlowBlocked(fluid, getBlockId(blockPosUp.add(direction.getUnitVector())));
if (flowBlocked) { if (flowBlocked) {
break; break;
} }
@ -290,11 +292,19 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
return len < 1.0E-4 ? Vector3d.ZERO : Vector3d.from(vec.getX() / len, vec.getY() / len, vec.getZ() / len); return len < 1.0E-4 ? Vector3d.ZERO : Vector3d.from(vec.getX() / len, vec.getY() / len, vec.getZ() / len);
} }
protected int getBlockAt(Vector3i blockPos) { protected int getBlockId(Vector3i blockPos) {
return vehicle.getSession().getGeyser().getWorldManager().getBlockAt(vehicle.getSession(), blockPos); return vehicle.getSession().getGeyser().getWorldManager().getBlockAt(vehicle.getSession(), blockPos);
} }
private float getWorldFluidHeight(Fluid fluidType, int blockId) { protected BlockState getBlockState(Vector3i blockPos) {
return BlockState.of(getBlockId(blockPos));
}
protected Block getBlock(Vector3i blockPos) {
return getBlockState(blockPos).block();
}
protected float getWorldFluidHeight(Fluid fluidType, int blockId) {
return (float) switch (fluidType) { return (float) switch (fluidType) {
case WATER -> BlockStateValues.getWaterHeight(blockId); case WATER -> BlockStateValues.getWaterHeight(blockId);
case LAVA -> BlockStateValues.getLavaHeight(blockId); case LAVA -> BlockStateValues.getLavaHeight(blockId);
@ -302,12 +312,12 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
}; };
} }
private float getLogicalFluidHeight(Fluid fluidType, int blockId) { protected float getLogicalFluidHeight(Fluid fluidType, int blockId) {
return Math.min(getWorldFluidHeight(fluidType, blockId), MAX_LOGICAL_FLUID_HEIGHT); return Math.min(getWorldFluidHeight(fluidType, blockId), MAX_LOGICAL_FLUID_HEIGHT);
} }
private boolean isFlowBlocked(Fluid fluid, int adjacentBlockId) { protected boolean isFlowBlocked(Fluid fluid, int adjacentBlockId) {
if (adjacentBlockId == BlockStateValues.JAVA_ICE_ID) { if (BlockState.of(adjacentBlockId).block() == Blocks.ICE) {
return false; return false;
} }
@ -363,7 +373,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
protected void landMovement() { protected void landMovement() {
float gravity = getGravity(); float gravity = getGravity();
float slipperiness = BlockStateValues.getSlipperiness(getBlockAt(getVelocityAffectingPos())); float slipperiness = BlockStateValues.getSlipperiness(getBlockId(getVelocityAffectingPos()));
float drag = vehicle.isOnGround() ? 0.91f * slipperiness : 0.91f; float drag = vehicle.isOnGround() ? 0.91f * slipperiness : 0.91f;
float speed = vehicle.getVehicleSpeed() * (vehicle.isOnGround() ? BASE_SLIPPERINESS_CUBED / (slipperiness * slipperiness * slipperiness) : 0.1f); float speed = vehicle.getVehicleSpeed() * (vehicle.isOnGround() ? BASE_SLIPPERINESS_CUBED / (slipperiness * slipperiness * slipperiness) : 0.1f);
@ -441,8 +451,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
// Iterate backwards // Iterate backwards
for (int i = blocks.length - 1; i >= 0; i--) { for (int i = blocks.length - 1; i >= 0; i--) {
String cleanIdentifier = BlockRegistries.JAVA_BLOCKS.getOrDefault(blocks[i], BlockMapping.DEFAULT).getCleanJavaIdentifier(); Vector3f multiplier = MOVEMENT_MULTIPLIERS.get(BlockState.of(blocks[i]).block());
Vector3f multiplier = MOVEMENT_MULTIPLIERS.get(cleanIdentifier);
if (multiplier != null) { if (multiplier != null) {
return multiplier; return multiplier;
@ -463,14 +472,12 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
int[] blocks = vehicle.getSession().getGeyser().getWorldManager().getBlocksAt(vehicle.getSession(), iter); int[] blocks = vehicle.getSession().getGeyser().getWorldManager().getBlocksAt(vehicle.getSession(), iter);
for (iter.reset(); iter.hasNext(); iter.next()) { for (iter.reset(); iter.hasNext(); iter.next()) {
int blockId = blocks[iter.getIteration()]; BlockState blockState = BlockState.of(blocks[iter.getIteration()]);
if (BlockStateValues.JAVA_HONEY_BLOCK_ID == blockId) { if (blockState.block() == Blocks.HONEY_BLOCK) {
onHoneyBlockCollision(); onHoneyBlockCollision();
} else if (BlockStateValues.JAVA_BUBBLE_COLUMN_DRAG_ID == blockId) { } else if (blockState.block() == Blocks.BUBBLE_COLUMN) {
onBubbleColumnCollision(true); onBubbleColumnCollision(blockState.getValue(Properties.DRAG));
} else if (BlockStateValues.JAVA_BUBBLE_COLUMN_UPWARD_ID == blockId) {
onBubbleColumnCollision(false);
} }
} }
} }
@ -535,9 +542,9 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
boolean bounced = false; boolean bounced = false;
if (onGround) { if (onGround) {
Vector3i landingPos = newPos.sub(0, 0.2f, 0).toInt(); Vector3i landingPos = newPos.sub(0, 0.2f, 0).toInt();
int landingBlockId = getBlockAt(landingPos); int landingBlockId = getBlockId(landingPos);
if (landingBlockId == BlockStateValues.JAVA_SLIME_BLOCK_ID) { if (BlockState.of(landingBlockId).block() == Blocks.SLIME_BLOCK) {
motion = Vector3f.from(motion.getX(), -motion.getY(), motion.getZ()); motion = Vector3f.from(motion.getX(), -motion.getY(), motion.getZ());
bounced = true; bounced = true;
@ -582,7 +589,7 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
} }
Vector3i blockPos = boundingBox.getBottomCenter().toInt(); Vector3i blockPos = boundingBox.getBottomCenter().toInt();
int blockId = getBlockAt(blockPos); int blockId = getBlockId(blockPos);
if (BlockStateValues.isClimbable(blockId)) { if (BlockStateValues.isClimbable(blockId)) {
return true; return true;
@ -591,8 +598,8 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
// Check if the vehicle is in an open trapdoor with a ladder of the same direction under it // Check if the vehicle is in an open trapdoor with a ladder of the same direction under it
Direction openTrapdoorDirection = BlockStateValues.getOpenTrapdoorDirection(blockId); Direction openTrapdoorDirection = BlockStateValues.getOpenTrapdoorDirection(blockId);
if (openTrapdoorDirection != null) { if (openTrapdoorDirection != null) {
Direction ladderDirection = BlockStateValues.getLadderDirection(getBlockAt(blockPos.down())); BlockState ladder = getBlockState(blockPos.down());
return ladderDirection != null && ladderDirection == openTrapdoorDirection; return ladder.block() == Blocks.LADDER && ladder.getValue(Properties.HORIZONTAL_FACING) == openTrapdoorDirection;
} }
return false; return false;
@ -736,19 +743,17 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
} }
protected float getVelocityMultiplier() { protected float getVelocityMultiplier() {
int blockId = getBlockAt(boundingBox.getBottomCenter().toInt()); Block block = getBlock(boundingBox.getBottomCenter().toInt());
if (BlockStateValues.getWaterLevel(blockId) != -1 // getWaterLevel does not include waterlogged blocks if (block == Blocks.WATER || block == Blocks.BUBBLE_COLUMN) {
|| blockId == BlockStateValues.JAVA_BUBBLE_COLUMN_DRAG_ID
|| blockId == BlockStateValues.JAVA_BUBBLE_COLUMN_UPWARD_ID) {
return 1.0f; return 1.0f;
} }
if (blockId == BlockStateValues.JAVA_SOUL_SAND_ID || blockId == BlockStateValues.JAVA_HONEY_BLOCK_ID) { if (block == Blocks.SOUL_SAND || block == Blocks.HONEY_BLOCK) {
return 0.4f; return 0.4f;
} }
blockId = getBlockAt(getVelocityAffectingPos()); block = getBlock(getVelocityAffectingPos());
if (blockId == BlockStateValues.JAVA_SOUL_SAND_ID || blockId == BlockStateValues.JAVA_HONEY_BLOCK_ID) { if (block == Blocks.SOUL_SAND || block == Blocks.HONEY_BLOCK) {
return 0.4f; return 0.4f;
} }
@ -756,13 +761,13 @@ public class VehicleComponent<T extends LivingEntity & ClientVehicle> {
} }
protected float getJumpVelocityMultiplier() { protected float getJumpVelocityMultiplier() {
int blockId = getBlockAt(boundingBox.getBottomCenter().toInt()); Block block = getBlock(boundingBox.getBottomCenter().toInt());
if (blockId == BlockStateValues.JAVA_HONEY_BLOCK_ID) { if (block == Blocks.HONEY_BLOCK) {
return 0.5f; return 0.5f;
} }
blockId = getBlockAt(getVelocityAffectingPos()); block = getBlock(getVelocityAffectingPos());
if (blockId == BlockStateValues.JAVA_HONEY_BLOCK_ID) { if (block == Blocks.HONEY_BLOCK) {
return 0.5f; return 0.5f;
} }

Datei anzeigen

@ -71,7 +71,6 @@ public final class BlockStateValues {
private static final Int2IntMap WATER_LEVEL = new Int2IntOpenHashMap(); private static final Int2IntMap WATER_LEVEL = new Int2IntOpenHashMap();
private static final Int2IntMap LAVA_LEVEL = new Int2IntOpenHashMap(); private static final Int2IntMap LAVA_LEVEL = new Int2IntOpenHashMap();
private static final IntSet ALL_CLIMBABLE = new IntOpenHashSet(); private static final IntSet ALL_CLIMBABLE = new IntOpenHashSet();
private static final Int2ObjectMap<Direction> LADDER_DIRECTION = new Int2ObjectOpenHashMap<>();
private static final Int2ObjectMap<Direction> OPEN_TRAPDOOR_DIRECTION = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap<Direction> OPEN_TRAPDOOR_DIRECTION = new Int2ObjectOpenHashMap<>();
public static final int JAVA_AIR_ID = 0; public static final int JAVA_AIR_ID = 0;
@ -232,10 +231,6 @@ public final class BlockStateValues {
ALL_CLIMBABLE.add(javaBlockState); ALL_CLIMBABLE.add(javaBlockState);
} }
if (javaId.startsWith("minecraft:ladder")) {
LADDER_DIRECTION.put(javaBlockState, getBlockDirection(javaId));
}
if (javaId.contains("_trapdoor[") && javaId.contains("open=true")) { if (javaId.contains("_trapdoor[") && javaId.contains("open=true")) {
OPEN_TRAPDOOR_DIRECTION.put(javaBlockState, getBlockDirection(javaId)); OPEN_TRAPDOOR_DIRECTION.put(javaBlockState, getBlockDirection(javaId));
} }
@ -614,17 +609,6 @@ public final class BlockStateValues {
}; };
} }
/**
* Get the direction of a ladder.
* Used when determining if an entity is climbing
*
* @param state BlockState of the block
* @return The ladder's direction, or null if not a ladder
*/
public static @Nullable Direction getLadderDirection(int state) {
return LADDER_DIRECTION.get(state);
}
/** /**
* Get the direction of an open trapdoor. * Get the direction of an open trapdoor.
* Used when determining if an entity is climbing * Used when determining if an entity is climbing

Datei anzeigen

@ -52,7 +52,7 @@ import java.text.DecimalFormatSymbols;
import java.util.Locale; import java.util.Locale;
public class CollisionManager { public class CollisionManager {
public static final BlockCollision SOLID_COLLISION = new SolidCollision(""); public static final BlockCollision SOLID_COLLISION = new SolidCollision(null);
public static final BlockCollision FLUID_COLLISION = new OtherCollision(new BoundingBox[]{new BoundingBox(0.5, 0.25, 0.5, 1, 0.5, 1)}); public static final BlockCollision FLUID_COLLISION = new OtherCollision(new BoundingBox[]{new BoundingBox(0.5, 0.25, 0.5, 1, 0.5, 1)});
private final GeyserSession session; private final GeyserSession session;