mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-19 15:09:15 +00:00
更新自定义实体构造方法
This commit is contained in:
@@ -13,7 +13,7 @@ public abstract class CustomEntity implements Cullable {
|
||||
protected WorldPosition position;
|
||||
protected boolean valid = true;
|
||||
|
||||
protected CustomEntity(CustomEntityType<?> type, WorldPosition position, UUID uuid) {
|
||||
protected CustomEntity(CustomEntityType<?> type, UUID uuid, WorldPosition position) {
|
||||
this.position = position;
|
||||
this.type = type;
|
||||
this.uuid = uuid;
|
||||
|
||||
@@ -15,12 +15,12 @@ public class InactiveCustomEntity extends CustomEntity {
|
||||
private final CompoundTag data;
|
||||
|
||||
public InactiveCustomEntity(UUID uuid, WorldPosition position) {
|
||||
super(CustomEntityTypes.INACTIVE, position, uuid);
|
||||
super(CustomEntityTypes.INACTIVE, uuid, position);
|
||||
this.data = INVALID_TAG;
|
||||
}
|
||||
|
||||
public InactiveCustomEntity(UUID uuid, WorldPosition position, CompoundTag data) {
|
||||
super(CustomEntityTypes.INACTIVE, position, uuid);
|
||||
super(CustomEntityTypes.INACTIVE, uuid, position);
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,18 +2,51 @@ package net.momirealms.craftengine.core.entity.furniture;
|
||||
|
||||
import net.momirealms.craftengine.core.entity.CustomEntity;
|
||||
import net.momirealms.craftengine.core.entity.CustomEntityType;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
import net.momirealms.sparrow.nbt.CompoundTag;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class Furniture extends CustomEntity {
|
||||
protected final FurnitureConfig config;
|
||||
protected final FurnitureDataAccessor dataAccessor;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public Furniture(CustomEntityType<?> type, WorldPosition position, FurnitureConfig config, CompoundTag data) {
|
||||
super(type, position);
|
||||
this.dataAccessor = new FurnitureDataAccessor(data);
|
||||
this.config = config;
|
||||
public abstract class Furniture extends CustomEntity {
|
||||
protected FurnitureConfig config;
|
||||
protected FurnitureDataAccessor dataAccessor;
|
||||
|
||||
protected Key furnitureId;
|
||||
protected CompoundTag inactiveFurnitureData;
|
||||
|
||||
protected Furniture(CustomEntityType<?> type, UUID uuid, WorldPosition position) {
|
||||
super(type, uuid, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveCustomData(CompoundTag tag) {
|
||||
tag.putString("id", this.config.id().asMinimalString());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadCustomData(CompoundTag tag) {
|
||||
this.furnitureId = readFurnitureId(tag);
|
||||
this.dataAccessor = new FurnitureDataAccessor(tag.getCompound("data"));
|
||||
Optional<FurnitureConfig> furnitureConfig = CraftEngine.instance().furnitureManager().furnitureById(this.furnitureId);
|
||||
if (furnitureConfig.isPresent()) {
|
||||
this.config = furnitureConfig.get();
|
||||
} else {
|
||||
this.inactiveFurnitureData = tag;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag saveAsTag() {
|
||||
// 如果家具数据只是不活跃,则返回不活跃家具数据
|
||||
if (this.inactiveFurnitureData != null) {
|
||||
return this.inactiveFurnitureData;
|
||||
}
|
||||
return super.saveAsTag();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -25,4 +58,8 @@ public abstract class Furniture extends CustomEntity {
|
||||
public FurnitureDataAccessor dataAccessor() {
|
||||
return this.dataAccessor;
|
||||
}
|
||||
|
||||
protected final Key readFurnitureId(CompoundTag tag) {
|
||||
return Key.of(tag.getString("id"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class FurnitureDataAccessor {
|
||||
private final CompoundTag data;
|
||||
|
||||
public FurnitureDataAccessor(CompoundTag data) {
|
||||
this.data = data;
|
||||
this.data = data == null ? new CompoundTag() : data;
|
||||
}
|
||||
|
||||
public static FurnitureDataAccessor of(CompoundTag data) {
|
||||
|
||||
Reference in New Issue
Block a user