Skip to content

Commit

Permalink
feat: pdc for entity & entity block
Browse files Browse the repository at this point in the history
  • Loading branch information
IWareQ committed Feb 8, 2025
1 parent 35f6f77 commit 1af7e38
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.allaymc.api.blockentity.type.BlockEntityType;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.math.position.Position3ic;
import org.allaymc.api.pdc.PersistentDataHolder;
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.World;
import org.cloudburstmc.math.vector.Vector3i;
Expand All @@ -18,7 +19,15 @@
/**
* @author daoge_cmd
*/
public interface BlockEntityBaseComponent extends BlockEntityComponent {
public interface BlockEntityBaseComponent extends BlockEntityComponent, PersistentDataHolder {
String TAG_ID = "id";
String TAG_X = "x";
String TAG_Y = "y";
String TAG_Z = "z";
String TAG_IS_MOVABLE = "isMovable";
String TAG_CUSTOM_NAME = "CustomName";
String TAG_CUSTOM_NBT = "CustomNBT";


/**
* Gets the type of block entity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.allaymc.api.math.location.Location3ic;
import org.allaymc.api.math.position.Position3i;
import org.allaymc.api.math.position.Position3ic;
import org.allaymc.api.pdc.PersistentDataHolder;
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.World;
import org.allaymc.api.world.chunk.Chunk;
Expand All @@ -47,7 +48,16 @@
/**
* @author daoge_cmd
*/
public interface EntityBaseComponent extends EntityComponent, CommandSender, HasAABB, HasLongId {
public interface EntityBaseComponent extends EntityComponent, CommandSender, HasAABB, HasLongId, PersistentDataHolder {
String TAG_IDENTIFIER = "identifier";
String TAG_ON_GROUND = "OnGround";
String TAG_POS = "Pos";
String TAG_MOTION = "Motion";
String TAG_ROTATION = "Rotation";
String TAG_TAGS = "Tags";
String TAG_ACTIVE_EFFECTS = "ActiveEffects";
String TAG_UNIQUE_ID = "UniqueID";
String TAG_CUSTOM_NBT = "CustomNBT";

double SPRINTING_MOVEMENT_FACTOR = 1.3;
double WALKING_MOVEMENT_FACTOR = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
* @author daoge_cmd
*/
public interface ItemBaseComponent extends ItemComponent, PersistentDataHolder {
// The following tag is in extra tag.
String TAG_DAMAGE = "Damage";
String TAG_DISPLAY = "display";
String TAG_NAME = "Name";
String TAG_LORE = "Lore";
String TAG_ENCHANTMENT = "ench";
String TAG_BLOCK_ENTITY = "BlockEntityTag";
String TAG_LOCK_MODE = "minecraft:item_lock";
String TAG_CUSTOM_NBT = "CustomNBT";

int EMPTY_STACK_NETWORK_ID = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.allaymc.api.component.interfaces.ComponentManager;
import org.allaymc.api.math.position.Position3i;
import org.allaymc.api.math.position.Position3ic;
import org.allaymc.api.registry.Registries;
import org.allaymc.api.utils.Identifier;
import org.allaymc.server.block.component.event.CBlockOnInteractEvent;
import org.allaymc.server.block.component.event.CBlockOnNeighborUpdateEvent;
Expand All @@ -17,6 +18,7 @@
import org.allaymc.server.blockentity.component.event.CBlockEntitySaveNBTEvent;
import org.allaymc.server.component.annotation.Manager;
import org.allaymc.server.component.annotation.OnInitFinish;
import org.allaymc.server.pdc.AllayPersistentDataContainer;
import org.cloudburstmc.nbt.NbtMap;

/**
Expand All @@ -27,13 +29,6 @@ public class BlockEntityBaseComponentImpl implements BlockEntityBaseComponent {
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:block_entity_base_component");

protected static final String TAG_ID = "id";
protected static final String TAG_X = "x";
protected static final String TAG_Y = "y";
protected static final String TAG_Z = "z";
protected static final String TAG_IS_MOVABLE = "isMovable";
protected static final String TAG_CUSTOM_NAME = "CustomName";

@Manager
protected ComponentManager manager;

Expand All @@ -44,6 +39,9 @@ public class BlockEntityBaseComponentImpl implements BlockEntityBaseComponent {
@Getter
@Setter
protected String customName;
@Getter
@Setter
protected AllayPersistentDataContainer persistentDataContainer = new AllayPersistentDataContainer(Registries.PDC_REGISTRY);

public BlockEntityBaseComponentImpl(BlockEntityInitInfo initInfo) {
this.blockEntityType = initInfo.getBlockEntityType();
Expand All @@ -68,6 +66,9 @@ public NbtMap saveNBT() {
if (customName != null) {
builder.putString(TAG_CUSTOM_NAME, customName);
}
if (!persistentDataContainer.isEmpty()) {
builder.put(TAG_CUSTOM_NBT, persistentDataContainer.toCompoundTag());
}
var event = new CBlockEntitySaveNBTEvent(builder);
manager.callEvent(event);
return builder.build();
Expand All @@ -83,6 +84,11 @@ public void loadNBT(NbtMap nbt) {
pos.z = nbt.getInt(TAG_Z, position.z());
position = pos;

nbt.listenForCompound(TAG_CUSTOM_NBT, customNbt -> {
this.persistentDataContainer.clear();
this.persistentDataContainer.putAll(customNbt);
});

var event = new CBlockEntityLoadNBTEvent(nbt);
manager.callEvent(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.allaymc.api.math.position.Position3i;
import org.allaymc.api.permission.DefaultPermissions;
import org.allaymc.api.permission.tree.PermissionTree;
import org.allaymc.api.registry.Registries;
import org.allaymc.api.server.Server;
import org.allaymc.api.utils.AllayNbtUtils;
import org.allaymc.api.utils.Identifier;
Expand All @@ -36,6 +37,7 @@
import org.allaymc.server.component.annotation.Manager;
import org.allaymc.server.component.annotation.OnInitFinish;
import org.allaymc.server.entity.component.event.*;
import org.allaymc.server.pdc.AllayPersistentDataContainer;
import org.allaymc.server.world.chunk.AllayUnsafeChunk;
import org.allaymc.server.world.service.AllayEntityPhysicsService;
import org.cloudburstmc.math.vector.Vector2f;
Expand Down Expand Up @@ -79,16 +81,6 @@ public class EntityBaseComponentImpl implements EntityBaseComponent {
// NOTICE: the runtime id is counted from 1 not 0
protected static final AtomicLong RUNTIME_ID_COUNTER = new AtomicLong(1);

protected static final String TAG_IDENTIFIER = "identifier";
protected static final String TAG_ON_GROUND = "OnGround";
// This tag is also used in EntityPlayerNetworkComponentImpl, so make it public for reuse
public static final String TAG_POS = "Pos";
protected static final String TAG_MOTION = "Motion";
protected static final String TAG_ROTATION = "Rotation";
protected static final String TAG_TAGS = "Tags";
protected static final String TAG_ACTIVE_EFFECTS = "ActiveEffects";
protected static final String TAG_UNIQUE_ID = "UniqueID";

private static final CommandOriginData ENTITY_COMMAND_ORIGIN_DATA = new CommandOriginData(CommandOriginType.ENTITY, UUID.randomUUID(), "", 0);

@Getter
Expand Down Expand Up @@ -126,6 +118,9 @@ public class EntityBaseComponentImpl implements EntityBaseComponent {
@Setter
protected String displayName;
protected Set<String> tags = new HashSet<>();
@Getter
@Setter
protected AllayPersistentDataContainer persistentDataContainer = new AllayPersistentDataContainer(Registries.PDC_REGISTRY);

public EntityBaseComponentImpl(EntityInitInfo info) {
this.location = new Location3d(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, info.dimension());
Expand Down Expand Up @@ -453,7 +448,7 @@ protected void beforeTeleport(Location3dc target) {
}

protected void teleportInDimension(Location3dc target) {
// This method should always return true because we have loaded the chunk
// This method should always return true because we've loaded the chunk
setLocationAndCheckChunk(target, false);
broadcastMoveToViewers(target, true);
}
Expand Down Expand Up @@ -685,6 +680,9 @@ public NbtMap saveNBT() {
if (!effects.isEmpty()) {
builder.putList(TAG_ACTIVE_EFFECTS, NbtType.COMPOUND, effects.values().stream().map(EffectInstance::saveNBT).toList());
}
if (!persistentDataContainer.isEmpty()) {
builder.put(TAG_CUSTOM_NBT, persistentDataContainer.toCompoundTag());
}
saveUniqueId(builder);
var event = new CEntitySaveNBTEvent(builder);
manager.callEvent(event);
Expand Down Expand Up @@ -721,6 +719,10 @@ public void loadNBT(NbtMap nbt) {
addEffect(effectInstance);
}
});
nbt.listenForCompound(TAG_CUSTOM_NBT, customNbt -> {
this.persistentDataContainer.clear();
this.persistentDataContainer.putAll(customNbt);
});

loadUniqueId(nbt);
var event = new CEntityLoadNBTEvent(nbt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,6 @@ public class ItemBaseComponentImpl implements ItemBaseComponent {
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:item_base_component");

// The following tag is in extra tag.
protected static final String TAG_DAMAGE = "Damage";
protected static final String TAG_DISPLAY = "display";
protected static final String TAG_NAME = "Name";
protected static final String TAG_LORE = "Lore";
protected static final String TAG_ENCHANTMENT = "ench";
protected static final String TAG_BLOCK_ENTITY = "BlockEntityTag";
protected static final String TAG_LOCK_MODE = "minecraft:item_lock";
protected static final String TAG_CUSTOM_NBT = "CustomNBT";

private static final AtomicInteger STACK_NETWORK_ID_COUNTER = new AtomicInteger(1);

@Dependency
Expand Down Expand Up @@ -99,6 +89,7 @@ public class ItemBaseComponentImpl implements ItemBaseComponent {
@Setter
protected ItemLockMode lockMode = ItemLockMode.NONE;
@Getter
@Setter
protected AllayPersistentDataContainer persistentDataContainer = new AllayPersistentDataContainer(Registries.PDC_REGISTRY);

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class AllayChunk implements Chunk {
AllayChunk(AllayUnsafeChunk unsafeChunk) {
this.unsafeChunk = unsafeChunk;

// Init locks
var dimensionInfo = unsafeChunk.getDimensionInfo();
this.blockLocks = new ChunkSectionLocks(dimensionInfo);
this.biomeLocks = new ChunkSectionLocks(dimensionInfo);
Expand Down

0 comments on commit 1af7e38

Please sign in to comment.