Skip to content

Commit

Permalink
Fix liquid traits
Browse files Browse the repository at this point in the history
  • Loading branch information
Creeperface01 committed Jun 7, 2021
1 parent c8107a6 commit ee187e9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ public float getMaxY(Block block) {
// }

public static float getFluidHeightPercent(BlockState state) {
return (state.ensureTrait(BlockTraits.LIQUID_DEPTH) + 1) / 9f;
return (state.ensureTrait(BlockTraits.FLUID_LEVEL) + 1) / 9f;
}

protected int getFlowDecay(BlockState state) {
if (!isSameLiquid(state.getType())) {
return -1;
}

return state.ensureTrait(BlockTraits.LIQUID_DEPTH);
return state.ensureTrait(BlockTraits.FLUID_LEVEL);
}

protected int getEffectiveFlowDecay(BlockState state) {
if (!isSameLiquid(state.getType())) {
return -1;
}

return state.ensureTrait(BlockTraits.LIQUID_DEPTH);
return state.ensureTrait(BlockTraits.FLUID_LEVEL);
}

public void clearCaches() {
Expand Down Expand Up @@ -197,7 +197,7 @@ public int onUpdate(final Block block, int type) {
BlockState bottomBlockState = block.down().getLiquid();
if (bottomBlockState.getBehavior().isSolid(bottomBlockState)) {
newDecay = 0;
} else if (isWater(bottomBlockState.getType()) && bottomBlockState.ensureTrait(BlockTraits.LIQUID_DEPTH) == 0 && !bottomBlockState.ensureTrait(BlockTraits.IS_FLOWING)) {
} else if (isWater(bottomBlockState.getType()) && bottomBlockState.ensureTrait(BlockTraits.FLUID_LEVEL) == 0 && !bottomBlockState.ensureTrait(BlockTraits.IS_FLOWING)) {
newDecay = 0;
}
}
Expand Down Expand Up @@ -402,7 +402,7 @@ protected boolean canFlowInto(Block block) {
}

if (state.inCategory(BlockCategory.LIQUID)) {
return state.ensureTrait(BlockTraits.LIQUID_DEPTH) != 0;
return state.ensureTrait(BlockTraits.FLUID_LEVEL) != 0;
}

return true;
Expand All @@ -414,7 +414,7 @@ private boolean canBlockBeFlooded(BlockState state) {

protected BlockState getState(int decay, boolean falling) {
return CloudBlockRegistry.get().getBlock(flowingId)
.withTrait(BlockTraits.LIQUID_DEPTH, decay & 0x7)
.withTrait(BlockTraits.FLUID_LEVEL, decay & 0x7)
.withTrait(BlockTraits.IS_FLOWING, falling);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public class FluidBlockSerializer implements BlockSerializer {
@Override
public void serialize(NbtMapBuilder builder, BlockType blockType, Map<BlockTrait<?>, Comparable<?>> traits) {
NbtMapBuilder statesBuilder = NbtMap.builder();
int fluidLevel = (Integer) traits.get(BlockTraits.LIQUID_DEPTH);
/* boolean flowing = (Boolean) traits.get(BlockTraits.IS_FLOWING);
int fluidLevel = (Integer) traits.get(BlockTraits.FLUID_LEVEL);
boolean flowing = (Boolean) traits.get(BlockTraits.IS_FLOWING);
if (flowing) {
fluidLevel |= 8;
}*/
}

statesBuilder.putInt(TAG_LIQUID_DEPTH, fluidLevel);
builder.putCompound(TAG_STATES, statesBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public boolean isValid(CloudPlayer source) {
if (!source.isCreative()) {
return false;
}

ItemStack item = CloudItemRegistry.get().getCreativeItems().get(creativeItemNetId - 1);
return !item.isNull() && source.getCraftingInventory().setItem(CloudCraftingGrid.CRAFTING_RESULT_SLOT,
item.withAmount(item.getBehavior().getMaxStackSize(item)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public <T> T getMetadata(Class<T> metadataClass, T defaultValue) {
if (value == null) {
var serializer = CloudItemRegistry.get().getSerializer(metadataClass);
if (serializer != null) {
value = (T) serializer.deserialize(this.id, getNbt(false), getDataTag());
value = (T) serializer.deserialize(this.id, getNbt(true), getDataTag());
}

if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,24 @@
public class ItemBucketBehavior extends CloudItemBehavior {

protected static String getName(int meta) {
switch (meta) {
case 1:
return "Milk";
case 2:
return "Bucket of Cod";
case 3:
return "Bucket of Salmon";
case 4:
return "Bucket of Tropical Fish";
case 5:
return "Bucket of Pufferfish";
case 8:
return "Water Bucket";
case 10:
return "Lava Bucket";
default:
return "Bucket";
}
return switch (meta) {
case 1 -> "Milk";
case 2 -> "Bucket of Cod";
case 3 -> "Bucket of Salmon";
case 4 -> "Bucket of Tropical Fish";
case 5 -> "Bucket of Pufferfish";
case 8 -> "Water Bucket";
case 10 -> "Lava Bucket";
default -> "Bucket";
};
}

public static BlockType getBlockIdFromDamage(Bucket data) {
switch (data) {
case WATER:
return FLOWING_WATER;
case LAVA:
return FLOWING_LAVA;
default:
return AIR;
}
return switch (data) {
case WATER -> FLOWING_WATER;
case LAVA -> FLOWING_LAVA;
default -> AIR;
};
}

public Bucket getDamageFromIdentifier(BlockType id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.nukkitx.nbt.NbtMapBuilder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.cloudburstmc.api.item.ItemIds;
import org.cloudburstmc.api.item.ItemStack;
import org.cloudburstmc.api.item.data.Bucket;
import org.cloudburstmc.api.util.Identifier;
Expand All @@ -16,24 +17,24 @@ public class BucketSerializer implements ItemDataSerializer<Bucket> {

public static final BucketSerializer INSTANCE = new BucketSerializer();

private static final BiMap<Short, Bucket> dataMap = HashBiMap.create(ImmutableMap.<Short, Bucket>builder()
.put((short) 0, Bucket.EMPTY)
.put((short) 1, Bucket.MILK)
.put((short) 2, Bucket.COD)
.put((short) 3, Bucket.SALMON)
.put((short) 4, Bucket.TROPICAL_FISH)
.put((short) 5, Bucket.PUFFERFISH)
.put((short) 8, Bucket.WATER)
.put((short) 10, Bucket.LAVA)
private static final BiMap<Identifier, Bucket> dataMap = HashBiMap.create(ImmutableMap.<Identifier, Bucket>builder()
.put(ItemIds.BUCKET, Bucket.EMPTY)
.put(ItemIds.MILK_BUCKET, Bucket.MILK)
.put(ItemIds.COD_BUCKET, Bucket.COD)
.put(ItemIds.SALMON_BUCKET, Bucket.SALMON)
.put(ItemIds.TROPICAL_FISH_BUCKET, Bucket.TROPICAL_FISH)
.put(ItemIds.PUFFERFISH_BUCKET, Bucket.PUFFERFISH)
.put(ItemIds.WATER_BUCKET, Bucket.WATER)
.put(ItemIds.LAVA_BUCKET, Bucket.LAVA)
.build());

@Override
public void serialize(ItemStack item, NbtMapBuilder rootTag, NbtMapBuilder dataTag, Bucket value) {
rootTag.putShort("Damage", dataMap.inverse().getOrDefault(value, (short) 0));
rootTag.putString("Name", dataMap.inverse().getOrDefault(value, ItemIds.BUCKET).toString());
}

@Override
public Bucket deserialize(Identifier id, NbtMap rootTag, NbtMap dataTag) {
return dataMap.getOrDefault(rootTag.getShort("Damage"), Bucket.EMPTY);
return dataMap.getOrDefault(Identifier.fromString(rootTag.getString("Name")), Bucket.EMPTY);
}
}

0 comments on commit ee187e9

Please sign in to comment.