Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Dec 29, 2024
1 parent ceeffe8 commit 40f6d3b
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 112 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Unless otherwise specified, any version comparison below is the comparison of se
- (API) Added an extra argument to `Dimension#breakBlock` method to control if the block breaking particle should be
played.

### Changed

- Introduced tag name constants where a large number of nbt saves and reads are involved. This improved the
maintainability of the project.

### Fixed

- (API) Corrected the return type of `Dimension#breakBlock(Vector3ic, ItemStack, Entity)` from `void` to `boolean`. Some
Expand Down
21 changes: 14 additions & 7 deletions api/src/main/java/org/allaymc/api/container/BaseContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@
*/
@Slf4j
public class BaseContainer implements Container {

protected static final String TAG_SLOT = "Slot";

protected final FullContainerType<? extends Container> containerType;
protected final BiMap<Byte, ContainerViewer> viewers = HashBiMap.create(new Byte2ObjectOpenHashMap<>());
protected final BiMap<Byte, ContainerViewer> viewers;
protected final ItemStack[] content;
protected final Set<Consumer<ContainerViewer>> onOpenListeners = new HashSet<>();
protected final Set<Consumer<ContainerViewer>> onCloseListeners = new HashSet<>();
protected final Int2ObjectMap<Set<Consumer<ItemStack>>> onSlotChangeListeners = new Int2ObjectOpenHashMap<>();
protected final Set<Consumer<ContainerViewer>> onOpenListeners;
protected final Set<Consumer<ContainerViewer>> onCloseListeners;
protected final Int2ObjectMap<Set<Consumer<ItemStack>>> onSlotChangeListeners;

public BaseContainer(FullContainerType<? extends Container> containerType) {
this.containerType = containerType;
this.viewers = HashBiMap.create(new Byte2ObjectOpenHashMap<>());
this.content = new ItemStack[containerType.size()];
this.onOpenListeners = new HashSet<>();
this.onCloseListeners = new HashSet<>();
this.onSlotChangeListeners = new Int2ObjectOpenHashMap<>();
Arrays.fill(this.content, ItemAirStack.AIR_STACK);
}

Expand Down Expand Up @@ -179,7 +186,7 @@ public List<NbtMap> saveNBT() {
// TODO: WasPickedUp?
var nbt = itemStack.saveNBT()
.toBuilder()
.putByte("Slot", (byte) slot)
.putByte(TAG_SLOT, (byte) slot)
.build();
list.add(nbt);
}
Expand All @@ -189,11 +196,11 @@ public List<NbtMap> saveNBT() {
@Override
public void loadNBT(List<NbtMap> nbtList) {
for (var nbt : nbtList) {
if (!nbt.containsKey("Slot")) {
if (!nbt.containsKey(TAG_SLOT)) {
log.warn("Item NBT does not contain a slot key! Skipping item...");
continue;
}
int slot = nbt.getByte("Slot");
int slot = nbt.getByte(TAG_SLOT);
ItemStack itemStack = fromNBT(nbt);
content[slot] = itemStack;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@
* @author daoge_cmd
*/
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 @@ -52,13 +60,13 @@ public void tick(long currentTick) {}
@Override
public NbtMap saveNBT() {
var builder = NbtMap.builder()
.putString("id", blockEntityType.getName())
.putInt("x", position.x())
.putInt("y", position.y())
.putInt("z", position.z())
.putBoolean("isMovable", true);
.putString(TAG_ID, blockEntityType.getName())
.putInt(TAG_X, position.x())
.putInt(TAG_Y, position.y())
.putInt(TAG_Z, position.z())
.putBoolean(TAG_IS_MOVABLE, true);
if (customName != null) {
builder.putString("CustomName", customName);
builder.putString(TAG_CUSTOM_NAME, customName);
}
var event = new CBlockEntitySaveNBTEvent(builder);
manager.callEvent(event);
Expand All @@ -67,12 +75,12 @@ public NbtMap saveNBT() {

@Override
public void loadNBT(NbtMap nbt) {
nbt.listenForString("CustomName", customName -> this.customName = customName);
nbt.listenForString(TAG_CUSTOM_NAME, customName -> this.customName = customName);

var pos = new Position3i(position);
pos.x = nbt.getInt("x", position.x());
pos.y = nbt.getInt("y", position.y());
pos.z = nbt.getInt("z", position.z());
pos.x = nbt.getInt(TAG_X, position.x());
pos.y = nbt.getInt(TAG_Y, position.y());
pos.z = nbt.getInt(TAG_Z, position.z());
position = pos;

var event = new CBlockEntityLoadNBTEvent(nbt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
public class BlockEntityEnchantTableBaseComponentImpl extends BlockEntityBaseComponentImpl {

protected static final String TAG_ROTT = "rott";

/**
* The clockwise rotation of the book in radians. Top of the book points West when 0
*
Expand All @@ -21,7 +23,7 @@ public BlockEntityEnchantTableBaseComponentImpl(BlockEntityInitInfo initInfo) {

@Override
public NbtMap saveNBT() {
return super.saveNBT().toBuilder().putFloat("rott", bookRot).build();
return super.saveNBT().toBuilder().putFloat(TAG_ROTT, bookRot).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* @author IWareQ
*/
public class BlockEntityJukeboxBaseComponentImpl extends BlockEntityBaseComponentImpl implements BlockEntityJukeboxBaseComponent {

protected static final String TAG_RECORD_ITEM = "RecordItem";

@Getter
@Setter
private ItemStack musicDiscItem;
Expand Down Expand Up @@ -60,7 +63,7 @@ public NbtMap saveNBT() {
var savedNbt = super.saveNBT();
if (musicDiscItem != null) {
savedNbt = savedNbt.toBuilder()
.putCompound("RecordItem", this.musicDiscItem.saveNBT())
.putCompound(TAG_RECORD_ITEM, this.musicDiscItem.saveNBT())
.build();
}
return savedNbt;
Expand All @@ -69,6 +72,6 @@ public NbtMap saveNBT() {
@Override
public void loadNBT(NbtMap nbt) {
super.loadNBT(nbt);
nbt.listenForCompound("RecordItem", value -> this.musicDiscItem = ItemHelper.fromNBT(value));
nbt.listenForCompound(TAG_RECORD_ITEM, value -> this.musicDiscItem = ItemHelper.fromNBT(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
@Getter
public class BlockEntitySignBaseComponentImpl extends BlockEntityBaseComponentImpl implements BlockEntitySignBaseComponent {

protected static final String TAG_IS_WAXED = "IsWaxed";
protected static final String TAG_FRONT_TEXT = "FrontText";
protected static final String TAG_BACK_TEXT = "BackText";
protected static final String TAG_LOCKED_FOR_EDITING_BY = "LockedForEditingBy";
protected static final String TAG_TEXT = "Text";
protected static final String TAG_IGNORE_LIGHTING = "IgnoreLighting";
protected static final String TAG_SIGN_TEXT_COLOR = "SignTextColor";
protected static final String TAG_HIDE_GLOW_OUTLINE = "HideGlowOutline";
protected static final String TAG_PERSIST_FORMATTING = "PersistFormatting";
protected static final String TAG_TEXT_OWNER = "TextOwner";

protected SignText frontText = new SignTextImpl();
protected SignText backText = new SignTextImpl();
protected boolean waxed = false;
Expand All @@ -37,20 +48,20 @@ public BlockEntitySignBaseComponentImpl(BlockEntityInitInfo initInfo) {
public void loadNBT(NbtMap nbt) {
super.loadNBT(nbt);

nbt.listenForBoolean("IsWaxed", value -> waxed = value);
nbt.listenForCompound("FrontText", value -> frontText = readSignTextFromNBT(value));
nbt.listenForCompound("BackText", value -> backText = readSignTextFromNBT(value));
nbt.listenForBoolean(TAG_IS_WAXED, value -> waxed = value);
nbt.listenForCompound(TAG_FRONT_TEXT, value -> frontText = readSignTextFromNBT(value));
nbt.listenForCompound(TAG_BACK_TEXT, value -> backText = readSignTextFromNBT(value));
}

@Override
public NbtMap saveNBT() {
return super.saveNBT()
.toBuilder()
.putBoolean("IsWaxed", waxed)
.putCompound("FrontText", frontText.saveNBT())
.putCompound("BackText", backText.saveNBT())
.putBoolean(TAG_IS_WAXED, waxed)
.putCompound(TAG_FRONT_TEXT, frontText.saveNBT())
.putCompound(TAG_BACK_TEXT, backText.saveNBT())
// Unused
.putLong("LockedForEditingBy", -1L)
.putLong(TAG_LOCKED_FOR_EDITING_BY, -1L)
.build();
}

Expand All @@ -67,11 +78,11 @@ public void openSignEditorFor(EntityPlayer player, boolean frontSide) {
public void applyClientChange(EntityPlayer player, NbtMap nbt) {
String[] newText;
boolean isFrontSide = true;
if (!frontText.flattenText().equals(nbt.getCompound("FrontText").getString("Text"))) {
newText = AllayStringUtils.fastSplit(nbt.getCompound("FrontText").getString("Text"), "\n").toArray(String[]::new);
} else if (!backText.flattenText().equals(nbt.getCompound("BackText").getString("Text"))) {
if (!frontText.flattenText().equals(nbt.getCompound(TAG_FRONT_TEXT).getString(TAG_TEXT))) {
newText = AllayStringUtils.fastSplit(nbt.getCompound(TAG_FRONT_TEXT).getString(TAG_TEXT), "\n").toArray(String[]::new);
} else if (!backText.flattenText().equals(nbt.getCompound(TAG_BACK_TEXT).getString(TAG_TEXT))) {
isFrontSide = false;
newText = AllayStringUtils.fastSplit(nbt.getCompound("BackText").getString("Text"), "\n").toArray(String[]::new);
newText = AllayStringUtils.fastSplit(nbt.getCompound(TAG_BACK_TEXT).getString(TAG_TEXT), "\n").toArray(String[]::new);
} else {
// No changes
return;
Expand Down Expand Up @@ -182,10 +193,10 @@ protected boolean isFrontSideInteracted(BlockFace interactedFace) {
protected SignText readSignTextFromNBT(NbtMap nbt) {
var signText = new SignTextImpl();

nbt.listenForString("Text", value -> {
nbt.listenForString(TAG_TEXT, value -> {
signText.text = AllayStringUtils.fastSplit(value, "\n", 4).toArray(String[]::new);
});
nbt.listenForBoolean("IgnoreLighting", value -> signText.glowing = value);
nbt.listenForBoolean(TAG_IGNORE_LIGHTING, value -> signText.glowing = value);

return signText;
}
Expand Down Expand Up @@ -236,16 +247,16 @@ public String flattenText() {
@Override
public NbtMap saveNBT() {
return NbtMap.builder()
.putString("Text", flattenText())
.putBoolean("IgnoreLighting", glowing)
.putString(TAG_TEXT, flattenText())
.putBoolean(TAG_IGNORE_LIGHTING, glowing)
// Not implemented
.putInt("SignTextColor", -16777216)
.putInt(TAG_SIGN_TEXT_COLOR, -16777216)
// true if the outer glow of a sign with glowing text does not show.
.putBoolean("HideGlowOutline", false)
.putBoolean(TAG_HIDE_GLOW_OUTLINE, false)
// Unknown. Maybe save formatting character like §?
.putBoolean("PersistFormatting", true)
.putBoolean(TAG_PERSIST_FORMATTING, true)
// Unknown. The player who placed the sign?
.putString("TextOwner", "")
.putString(TAG_TEXT_OWNER, "")
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import org.cloudburstmc.protocol.bedrock.packet.BlockEventPacket;

/**
* @author IWareQ
* @author IWareQ | daoge_cmd
*/
public class BlockEntityShulkerBoxBaseComponentImpl extends BlockEntityBaseComponentImpl {

protected static final String TAG_FACING = "facing";

@Dependency
private BlockEntityContainerHolderComponent containerHolderComponent;

Expand Down Expand Up @@ -67,13 +70,13 @@ public void onInitFinish(BlockEntityInitInfo initInfo) {
@Override
public void loadNBT(NbtMap nbt) {
super.loadNBT(nbt);
nbt.listenForByte("facing", facing -> this.facing = BlockFace.fromId(facing));
nbt.listenForByte(TAG_FACING, facing -> this.facing = BlockFace.fromId(facing));
}

@Override
public NbtMap saveNBT() {
return super.saveNBT().toBuilder()
.putByte("facing", (byte) facing.ordinal())
.putByte(TAG_FACING, (byte) facing.ordinal())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class EntityAttributeComponentImpl implements EntityAttributeComponent {
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:entity_attribute_component");

protected static final String TAG_ATTRIBUTES = "Attributes";

protected final Map<AttributeType, Attribute> attributes = new EnumMap<>(AttributeType.class);

@ComponentObject
Expand Down Expand Up @@ -64,7 +66,7 @@ public EntityAttributeComponentImpl(AttributeType[] attributeTypes, Attribute...
@EventHandler
protected void onLoadNBT(CEntityLoadNBTEvent event) {
var nbt = event.getNbt();
nbt.listenForList("Attributes", NbtType.COMPOUND, attributesNbt -> {
nbt.listenForList(TAG_ATTRIBUTES, NbtType.COMPOUND, attributesNbt -> {
attributesNbt.forEach(attributeNbt -> {
var attribute = Attribute.fromNBT(attributeNbt);
attributes.put(AttributeType.byKey(attribute.getKey()), attribute);
Expand All @@ -76,7 +78,7 @@ protected void onLoadNBT(CEntityLoadNBTEvent event) {
@EventHandler
protected void onSaveNBT(CEntitySaveNBTEvent event) {
event.getNbt().putList(
"Attributes",
TAG_ATTRIBUTES,
NbtType.COMPOUND,
saveAttributes()
);
Expand Down
Loading

1 comment on commit 40f6d3b

@smartcmd
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I forget to add message for this commit, please take a look at the changelog (

Please sign in to comment.