Skip to content

Commit

Permalink
feat: structure api (#523)
Browse files Browse the repository at this point in the history
Co-authored-by: daoge_cmd <3523206925@qq.com>
  • Loading branch information
harry-xi and smartcmd authored Dec 24, 2024
1 parent b79889f commit f6c73f7
Show file tree
Hide file tree
Showing 10 changed files with 451 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and any changes to API will have a prefix `(API)`.

- (API) Added `TextFormat#MATERIAL_RESIN`.
- (API) Entity#teleport method now accepts an extra `Reason` argument.
- (API) Added structure API by @harry-xi.

### Changed

Expand All @@ -30,6 +31,7 @@ and any changes to API will have a prefix `(API)`.
- Entity#teleport method now will reset fall distance correctly.
- Fixed visual flashes when eating chorus fruits.
- Fixed incorrect comparison of `Position3x#dimension`.
- Fixed a number of falling block related bugs.

## [0.1.0](https://github.com/AllayMC/Allay/releases/tag/0.1.0) (API 0.1.0) - 2024-12-22

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,72 @@
package org.allaymc.api.block.type;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.block.property.type.BlockPropertyType;
import org.allaymc.api.network.ProtocolInfo;
import org.allaymc.api.registry.Registries;
import org.allaymc.api.utils.HashUtils;
import org.allaymc.api.utils.Identifier;
import org.allaymc.updater.block.BlockStateUpdaters;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
* BlockStateSafeGetter is used to get a block state safely, which means that
* the plugin developers can get an item type without worrying about being broken
* in the next minecraft version compared to using {@link BlockTypes} directly.
*
* @author daoge_cmd
* @author daoge_cmd | harry-xi
*/
@Slf4j
@UtilityClass
public final class BlockStateSafeGetter {

/**
* Get the block state from the block state NBT.
*
* @param nbt the block state NBT.
*
* @return the block state, or the default state of {@code BlockTypes.UNKNOWN}
* if the block state is not found, or the block state version is too new.
*/
public static BlockState fromNBT(NbtMap nbt) {
var version = nbt.getInt("version");
if (version > ProtocolInfo.BLOCK_STATE_VERSION_NUM) {
log.warn("Block state version is too new: {}", nbt);
return BlockTypes.UNKNOWN.getDefaultState();
}

if (version < ProtocolInfo.BLOCK_STATE_VERSION_NUM) {
nbt = BlockStateUpdaters.updateBlockState(nbt, BlockStateUpdaters.LATEST_VERSION);
}

// Make sure that tree map is used
// If the map inside states nbt is not tree map
// the block state hash will be wrong!
var states = new TreeMap<>(nbt.getCompound("states"));
// To calculate the hash of the block state
// "name" field must be in the first place
var tag = NbtMap.builder()
.putString("name", nbt.getString("name"))
.putCompound("states", NbtMap.fromMap(states))
.build();
int blockStateHash = HashUtils.fnv1a_32_nbt(tag);

// Get block state by hash
BlockState blockState = Registries.BLOCK_STATE_PALETTE.get(blockStateHash);
if (blockState != null) {
return blockState;
}

log.warn("Block state not found: {}", nbt);
return BlockTypes.UNKNOWN.getDefaultState();
}

/**
* Get a block type by its name.
*
Expand Down
35 changes: 32 additions & 3 deletions api/src/main/java/org/allaymc/api/utils/AllayNbtUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,26 @@ public static Vector3f readVector3f(NbtMap nbt, String rootName, String f1, Stri
* @param vector3f the vector3.
*/
public static void writeVector3f(NbtMapBuilder nbt, String rootName, String f1, String f2, String f3, Vector3fc vector3f) {
writeVector3f(nbt, rootName, f1, f2, f3, vector3f.x(), vector3f.y(), vector3f.z());
}

/**
* Write a vector3 to NBT.
*
* @param nbt the NBT builder.
* @param rootName the root name.
* @param f1 the x field.
* @param f2 the y field.
* @param f3 the z field.
* @param x the x value.
* @param y the y value.
* @param z the z value.
*/
public static void writeVector3f(NbtMapBuilder nbt, String rootName, String f1, String f2, String f3, float x, float y, float z) {
var pos = NbtMap.builder()
.putFloat(f1, vector3f.x())
.putFloat(f2, vector3f.y())
.putFloat(f3, vector3f.z())
.putFloat(f1, x)
.putFloat(f2, y)
.putFloat(f3, z)
.build();
nbt.putCompound(rootName, pos);
}
Expand Down Expand Up @@ -177,4 +193,17 @@ public static void writeVector2f(NbtMapBuilder nbt, String rootName, String f1,
.build();
nbt.putCompound(rootName, pos);
}

/**
* Remove the position tag from NBT.
*
* @param nbt the NBT map.
*
* @return the NBT map without the position tag.
*/
public static NbtMap removePosTag(NbtMap nbt) {
var builder = nbt.toBuilder();
builder.remove("Pos");
return builder.build();
}
}
Loading

0 comments on commit f6c73f7

Please sign in to comment.