Skip to content

Commit

Permalink
feat: support waterlogged
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 19, 2024
1 parent 5fa7007 commit d490324
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ private JeBlockState(String data) {
completeMissingProperties();
}

public String getPropertyValue(String key) {
return properties.get(key);
}

private void completeMissingProperties() {
var defaultProperties = Mapping.getJeBlockDefaultProperties(identifier);
if(properties.size() == defaultProperties.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,32 @@
import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.properties.Property;

import java.util.Objects;


/**
* Terra Project 2024/6/16
*
* @author daoge_cmd
*/
public record AllayBlockState(BlockState allayBlockState, JeBlockState jeBlockState) implements com.dfsek.terra.api.block.state.BlockState {
public final class AllayBlockState implements com.dfsek.terra.api.block.state.BlockState {

public static final AllayBlockState AIR = new AllayBlockState(BlockTypes.AIR_TYPE.getDefaultState(),
JeBlockState.fromString("minecraft:air"));
private final BlockState allayBlockState;
private final JeBlockState jeBlockState;
private final boolean containsWater;

public static final AllayBlockState AIR = new AllayBlockState(BlockTypes.AIR_TYPE.getDefaultState(), JeBlockState.fromString("minecraft:air"));
public AllayBlockState(BlockState allayBlockState, JeBlockState jeBlockState) {
this.allayBlockState = allayBlockState;
this.jeBlockState = jeBlockState;
this.containsWater = "true".equals(jeBlockState.getPropertyValue("waterlogged"));
}

@Override
public boolean matches(com.dfsek.terra.api.block.state.BlockState other) {
return ((AllayBlockState) other).allayBlockState == this.allayBlockState;
public boolean matches(com.dfsek.terra.api.block.state.BlockState o) {
var other = ((AllayBlockState) o);
return other.allayBlockState == this.allayBlockState && other.containsWater == this.containsWater;
}

@Override
Expand Down Expand Up @@ -59,4 +72,10 @@ public boolean isAir() {
public BlockState getHandle() {
return allayBlockState;
}

public BlockState allayBlockState() { return allayBlockState; }

public boolean containsWater() { return containsWater; }

public JeBlockState jeBlockState() { return jeBlockState; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.allaymc.terra.allay.delegate;

import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.data.VanillaBlockPropertyTypes;
import org.allaymc.api.world.chunk.Chunk;
import org.allaymc.terra.allay.Mapping;
import org.jetbrains.annotations.NotNull;
Expand All @@ -14,9 +16,19 @@
* @author daoge_cmd
*/
public record AllayChunk(ServerWorld world, Chunk allayChunk) implements com.dfsek.terra.api.world.chunk.Chunk {

private static final org.allaymc.api.block.type.BlockState WATER = BlockTypes.WATER_TYPE.ofState(VanillaBlockPropertyTypes.LIQUID_DEPTH.createValue(15));

@Override
public void setBlock(int x, int y, int z, BlockState data, boolean physics) {
allayChunk.setBlockState(x, y, z, ((AllayBlockState)data).allayBlockState());
var allayBlockState = (AllayBlockState)data;
var containsWater = allayBlockState.containsWater();
if (!containsWater) {
var oldBlock = allayChunk.getBlockState(x, y, z);
containsWater = oldBlock == BlockTypes.WATER_TYPE;
}
allayChunk.setBlockState(x, y, z, allayBlockState.allayBlockState());
if (containsWater) allayChunk.setBlockState(x, y, z, WATER, 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.allaymc.terra.allay.delegate;

import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.data.VanillaBlockPropertyTypes;
import org.allaymc.api.world.chunk.UnsafeChunk;
import org.allaymc.terra.allay.Mapping;
import org.jetbrains.annotations.NotNull;
Expand All @@ -14,14 +16,24 @@
* @author daoge_cmd
*/
public record AllayProtoChunk(UnsafeChunk allayChunk) implements ProtoChunk {

private static final org.allaymc.api.block.type.BlockState WATER = BlockTypes.WATER_TYPE.ofState(VanillaBlockPropertyTypes.LIQUID_DEPTH.createValue(15));

@Override
public int getMaxHeight() {
return allayChunk.getDimensionInfo().maxHeight();
}

@Override
public void setBlock(int x, int y, int z, @NotNull BlockState blockState) {
allayChunk.setBlockState(x, y, z, ((AllayBlockState)blockState).allayBlockState());
var allayBlockState = (AllayBlockState)blockState;
var containsWater = allayBlockState.containsWater();
if (!containsWater) {
var oldBlock = allayChunk.getBlockState(x, y, z);
containsWater = oldBlock == BlockTypes.WATER_TYPE;
}
allayChunk.setBlockState(x, y, z, allayBlockState.allayBlockState());
if (containsWater) allayChunk.setBlockState(x, y, z, WATER, 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.dfsek.terra.api.util.vector.Vector3;

import org.allaymc.api.world.chunk.ChunkAccessible;
import org.allaymc.api.world.chunk.UnsafeChunk;
import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.data.VanillaBlockPropertyTypes;
import org.allaymc.api.world.generator.context.OtherChunkAccessibleContext;
import org.allaymc.terra.allay.Mapping;

Expand All @@ -24,6 +24,8 @@
*/
public record AllayProtoWorld(AllayServerWorld allayServerWorld, OtherChunkAccessibleContext context) implements ProtoWorld {

private static final org.allaymc.api.block.type.BlockState WATER = BlockTypes.WATER_TYPE.ofState(VanillaBlockPropertyTypes.LIQUID_DEPTH.createValue(15));

@Override
public int centerChunkX() {
return context.getCurrentChunk().getX();
Expand All @@ -41,7 +43,14 @@ public ServerWorld getWorld() {

@Override
public void setBlockState(int x, int y, int z, BlockState data, boolean physics) {
context.setBlockState(x, y, z, ((AllayBlockState)data).allayBlockState());
var allayBlockState = (AllayBlockState)data;
var containsWater = allayBlockState.containsWater();
if (!containsWater) {
var oldBlock = context.getBlockState(x, y, z).getBlockType();
containsWater = oldBlock == BlockTypes.WATER_TYPE;
}
context.setBlockState(x, y, z, allayBlockState.allayBlockState());
if (containsWater) context.setBlockState(x, y, z, WATER, 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.dfsek.terra.api.util.vector.Vector3;

import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.data.VanillaBlockPropertyTypes;
import org.allaymc.api.world.Dimension;
import org.allaymc.terra.allay.Mapping;
import org.allaymc.terra.allay.generator.AllayGeneratorWrapper;
Expand All @@ -23,15 +25,24 @@
* @author daoge_cmd
*/
public record AllayServerWorld(AllayGeneratorWrapper allayGeneratorWrapper, Dimension allayDimension) implements ServerWorld {

private static final org.allaymc.api.block.type.BlockState WATER = BlockTypes.WATER_TYPE.ofState(VanillaBlockPropertyTypes.LIQUID_DEPTH.createValue(15));

@Override
public Chunk getChunkAt(int x, int z) {
return new AllayChunk(this, allayDimension.getChunkService().getChunk(x ,z));
}

@Override
public void setBlockState(int x, int y, int z, BlockState data, boolean physics) {
var allayBlockState = ((AllayBlockState)data).allayBlockState();
allayDimension.setBlockState(x, y, z, allayBlockState);
var allayBlockState = (AllayBlockState)data;
var containsWater = allayBlockState.containsWater();
if (!containsWater) {
var oldBlock = allayDimension.getBlockState(x, y, z).getBlockType();
containsWater = oldBlock == BlockTypes.WATER_TYPE;
}
allayDimension.setBlockState(x, y, z, allayBlockState.allayBlockState());
if (containsWater) allayDimension.setBlockState(x, y, z, WATER, 1);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion platforms/allay/src/main/resources/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"authors": [
"daoge_cmd"
],
"version": "1.0.0",
"version": "1.0.1",
"order": "START_UP"
}

0 comments on commit d490324

Please sign in to comment.