Skip to content

Commit

Permalink
refactor: identifier component annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
IWareQ committed Nov 21, 2024
1 parent 3e87eb7 commit f821420
Show file tree
Hide file tree
Showing 27 changed files with 134 additions and 108 deletions.
11 changes: 11 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions api/src/main/java/org/allaymc/api/utils/Identifier.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.allaymc.api.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static org.allaymc.api.utils.AllayStringUtils.fastTwoPartSplit;
import static org.allaymc.api.utils.IdentifierUtils.isNamespaceValid;
import static org.allaymc.api.utils.IdentifierUtils.isPathValid;
Expand All @@ -22,10 +27,12 @@ public Identifier(String id) {
public Identifier(String namespace, String path) {
this.namespace = namespace.isEmpty() ? DEFAULT_NAMESPACE : namespace;
this.path = path;
if (!isNamespaceValid(this.namespace))
if (!isNamespaceValid(this.namespace)) {
throw new InvalidIdentifierException("Non [A-Za-z0-9_.-] character in namespace of location: " + this.namespace + NAMESPACE_SEPARATOR + this.path);
if (!isPathValid(this.path))
}
if (!isPathValid(this.path)) {
throw new InvalidIdentifierException("Non [A-Za-z0-9/._-] character in path of location: " + this.namespace + NAMESPACE_SEPARATOR + this.path);
}
}

public String toString() {
Expand All @@ -36,8 +43,8 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof Identifier lv) {
return this.namespace.equals(lv.namespace) && this.path.equals(lv.path);
if (o instanceof Identifier(String namespace1, String path1)) {
return this.namespace.equals(namespace1) && this.path.equals(path1);
}
return false;
}
Expand All @@ -46,8 +53,13 @@ public int hashCode() {
return 31 * this.namespace.hashCode() + this.path.hashCode();
}

@SuppressWarnings("MethodDoesntCallSuperMethod")
@Override
public Identifier clone() {
return new Identifier(namespace, path);
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Component {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.enchantment.type.EnchantmentTypes;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.utils.Utils;
import org.allaymc.api.world.Dimension;
import org.allaymc.server.block.component.event.*;
import org.allaymc.server.block.type.BlockLootTable;
import org.allaymc.server.component.annotation.Identifier;
import org.allaymc.server.component.annotation.Manager;
import org.allaymc.server.loottable.context.BreakBlockContext;
import org.cloudburstmc.protocol.bedrock.data.GameType;
Expand All @@ -32,8 +32,8 @@
*/
public class BlockBaseComponentImpl implements BlockBaseComponent {

@Identifier
public static final org.allaymc.api.utils.Identifier IDENTIFIER = new org.allaymc.api.utils.Identifier("minecraft:block_base_component");
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:block_base_component");

@Manager
protected ComponentManager manager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
import org.allaymc.api.blockentity.type.BlockEntityType;
import org.allaymc.api.eventbus.EventHandler;
import org.allaymc.api.math.position.Position3i;
import org.allaymc.api.utils.Identifier;
import org.allaymc.server.block.component.event.CBlockOnInteractEvent;
import org.allaymc.server.block.component.event.CBlockOnNeighborUpdateEvent;
import org.allaymc.server.block.component.event.CBlockOnPlaceEvent;
import org.allaymc.server.block.component.event.CBlockOnReplaceEvent;
import org.allaymc.server.blockentity.component.BlockEntityBaseComponentImpl;
import org.allaymc.server.component.annotation.Identifier;

/**
* @author daoge_cmd
*/
@Slf4j
@RequiredArgsConstructor
public class BlockEntityHolderComponentImpl<T extends BlockEntity> implements BlockEntityHolderComponent<T> {
@Identifier
public static final org.allaymc.api.utils.Identifier IDENTIFIER = new org.allaymc.api.utils.Identifier("minecraft:block_entity_holder_component");
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:block_entity_holder_component");

@Getter
protected final BlockEntityType<T> blockEntityType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.allaymc.api.block.type.BlockType;
import org.allaymc.api.math.voxelshape.VoxelShape;
import org.allaymc.api.registry.Registries;
import org.allaymc.server.component.annotation.Identifier;
import org.allaymc.api.utils.Identifier;
import org.allaymc.server.datastruct.collections.nb.Int2ObjectNonBlockingMap;
import org.apache.commons.lang3.function.TriFunction;

Expand All @@ -22,8 +22,8 @@
*/
public class BlockStateDataComponentImpl implements BlockStateDataComponent {

@Identifier
public static final org.allaymc.api.utils.Identifier IDENTIFIER = new org.allaymc.api.utils.Identifier("minecraft:block_state_data_component");
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:block_state_data_component");

protected static final BlockStateDataComponentImpl DEFAULT = BlockStateDataComponentImpl.ofGlobalStatic(BlockStateData.DEFAULT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
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.utils.Identifier;
import org.allaymc.server.block.component.event.CBlockOnInteractEvent;
import org.allaymc.server.block.component.event.CBlockOnNeighborUpdateEvent;
import org.allaymc.server.block.component.event.CBlockOnPlaceEvent;
import org.allaymc.server.block.component.event.CBlockOnReplaceEvent;
import org.allaymc.server.blockentity.component.event.CBlockEntityLoadNBTEvent;
import org.allaymc.server.blockentity.component.event.CBlockEntitySaveNBTEvent;
import org.allaymc.server.component.annotation.Identifier;
import org.allaymc.server.component.annotation.Manager;
import org.allaymc.server.component.annotation.OnInitFinish;
import org.cloudburstmc.nbt.NbtMap;
Expand All @@ -23,8 +23,8 @@
* @author daoge_cmd
*/
public class BlockEntityBaseComponentImpl implements BlockEntityBaseComponent {
@Identifier
public static final org.allaymc.api.utils.Identifier IDENTIFIER = new org.allaymc.api.utils.Identifier("minecraft:block_entity_base_component");
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:block_entity_base_component");

@Manager
protected ComponentManager manager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import org.allaymc.api.container.impl.BlockContainer;
import org.allaymc.api.eventbus.EventHandler;
import org.allaymc.api.item.interfaces.ItemAirStack;
import org.allaymc.api.utils.Identifier;
import org.allaymc.server.block.component.event.CBlockOnInteractEvent;
import org.allaymc.server.block.component.event.CBlockOnReplaceEvent;
import org.allaymc.server.blockentity.component.event.CBlockEntityLoadNBTEvent;
import org.allaymc.server.blockentity.component.event.CBlockEntitySaveNBTEvent;
import org.allaymc.server.component.annotation.Dependency;
import org.allaymc.server.component.annotation.Identifier;
import org.cloudburstmc.nbt.NbtType;
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
import org.joml.Vector3f;
Expand All @@ -27,8 +27,8 @@
* @author daoge_cmd
*/
public class BlockEntityContainerHolderComponentImpl implements BlockEntityContainerHolderComponent {
@Identifier
protected static final org.allaymc.api.utils.Identifier IDENTIFIER = new org.allaymc.api.utils.Identifier("minecraft:block_entity_inventory_holder_component");
@Identifier.Component
protected static final Identifier IDENTIFIER = new Identifier("minecraft:block_entity_inventory_holder_component");

@Dependency
protected BlockEntityBaseComponent baseComponent;
Expand Down Expand Up @@ -101,8 +101,8 @@ protected void onReplace(CBlockOnReplaceEvent event) {
dimension.dropItem(itemStack, new Vector3f(
pos.x() + rand.nextFloat(0.5f) + 0.25f,
pos.y() + rand.nextFloat(0.5f) + 0.25f,
pos.z() + rand.nextFloat(0.5f) + 0.25f)
);
pos.z() + rand.nextFloat(0.5f) + 0.25f
));
}

container.clearAllSlots();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.SneakyThrows;
import org.allaymc.api.component.interfaces.Component;
import org.allaymc.api.component.interfaces.ComponentInitInfo;
import org.allaymc.server.component.annotation.Identifier;
import org.allaymc.api.utils.Identifier;

import java.util.HashMap;
import java.util.List;
Expand All @@ -31,20 +31,22 @@ static <T extends Component> ComponentProvider<T> ofSingleton(T singleton) {
return of((info) -> singleton, singleton.getClass());
}

static <P extends Component> Map<org.allaymc.api.utils.Identifier, ComponentProvider<? extends P>> toMap(List<ComponentProvider<? extends P>> componentProviders) {
var map = new HashMap<org.allaymc.api.utils.Identifier, ComponentProvider<? extends P>>();
static <P extends Component> Map<Identifier, ComponentProvider<? extends P>> toMap(List<ComponentProvider<? extends P>> componentProviders) {
var map = new HashMap<Identifier, ComponentProvider<? extends P>>();
componentProviders.forEach(componentProvider -> {
var id = componentProvider.findComponentIdentifier();
if (map.containsKey(id))
if (map.containsKey(id)) {
throw new IllegalArgumentException("Duplicate component: " + id);
}

map.put(id, componentProvider);
});
return map;
}

@SneakyThrows
static org.allaymc.api.utils.Identifier findComponentIdentifier(Class<?> clazz) {
org.allaymc.api.utils.Identifier identifier = null;
static Identifier findComponentIdentifier(Class<?> clazz) {
Identifier identifier = null;
while (identifier == null) {
identifier = findComponentIdentifierInCertainClass(clazz);
if (identifier == null) clazz = clazz.getSuperclass();
Expand All @@ -54,11 +56,11 @@ static org.allaymc.api.utils.Identifier findComponentIdentifier(Class<?> clazz)
}

@SneakyThrows
static org.allaymc.api.utils.Identifier findComponentIdentifierInCertainClass(Class<?> clazz) {
static Identifier findComponentIdentifierInCertainClass(Class<?> clazz) {
for (var field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(Identifier.class) && org.allaymc.api.utils.Identifier.class == field.getType() && isStatic(field.getModifiers())) {
if (field.isAnnotationPresent(Identifier.Component.class) && Identifier.class == field.getType() && isStatic(field.getModifiers())) {
field.setAccessible(true);
return (org.allaymc.api.utils.Identifier) field.get(null);
return (Identifier) field.get(null);
}
}
return null;
Expand All @@ -69,7 +71,7 @@ static org.allaymc.api.utils.Identifier findComponentIdentifierInCertainClass(Cl
Class<?> getComponentClass();

@SneakyThrows
default org.allaymc.api.utils.Identifier findComponentIdentifier() {
default Identifier findComponentIdentifier() {
return findComponentIdentifier(getComponentClass());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.allaymc.api.entity.component.attribute.EntityAttributeComponent;
import org.allaymc.api.eventbus.EventHandler;
import org.allaymc.api.eventbus.event.entity.EntityHealthChangeEvent;
import org.allaymc.api.utils.Identifier;
import org.allaymc.server.component.annotation.ComponentedObject;
import org.allaymc.server.component.annotation.Identifier;
import org.allaymc.server.component.annotation.Manager;
import org.allaymc.server.entity.component.event.CEntityAttributeChangeEvent;
import org.allaymc.server.entity.component.event.CEntityLoadNBTEvent;
Expand All @@ -30,8 +30,8 @@
@Slf4j
public class EntityAttributeComponentImpl implements EntityAttributeComponent {

@Identifier
public static final org.allaymc.api.utils.Identifier IDENTIFIER = new org.allaymc.api.utils.Identifier("minecraft:entity_attribute_component");
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:entity_attribute_component");

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

Expand Down Expand Up @@ -101,7 +101,7 @@ public Attribute getAttribute(AttributeType attributeType) {
public void setAttributeValue(AttributeType attributeType, float value) {
var attribute = this.attributes.get(attributeType);
if (attribute == null) {
throw unsupportAttributeTypeException(attributeType);
throw unsupportedAttributeTypeException(attributeType);
}
attribute.setCurrentValue(value);
manager.callEvent(CEntityAttributeChangeEvent.INSTANCE);
Expand All @@ -111,7 +111,7 @@ public void setAttributeValue(AttributeType attributeType, float value) {
public void setAttribute(Attribute attribute) {
var attributeType = AttributeType.byKey(attribute.getKey());
if (!this.attributes.containsKey(attributeType)) {
throw unsupportAttributeTypeException(attributeType);
throw unsupportedAttributeTypeException(attributeType);
}
this.attributes.put(AttributeType.byKey(attribute.getKey()), attribute);
manager.callEvent(CEntityAttributeChangeEvent.INSTANCE);
Expand All @@ -121,7 +121,7 @@ public void setAttribute(Attribute attribute) {
public float getAttributeValue(AttributeType attributeType) {
var attribute = this.getAttribute(attributeType);
if (attribute == null) {
throw unsupportAttributeTypeException(attributeType);
throw unsupportedAttributeTypeException(attributeType);
}
return attribute.getCurrentValue();
}
Expand All @@ -131,7 +131,7 @@ public void setHealth(float value) {
if (!supportHealth()) {
// Check if health is supported by the entity here
// To make sure that if health is not supported, event won't be called
throw unsupportAttributeTypeException(AttributeType.HEALTH);
throw unsupportedAttributeTypeException(AttributeType.HEALTH);
}
if (value > 0 && value < 1) {
// Client will think he is dead if the health is less than 1
Expand All @@ -149,7 +149,7 @@ public void setHealth(float value) {
setAttributeValue(AttributeType.HEALTH, event.getNewHealth());
}

protected IllegalArgumentException unsupportAttributeTypeException(AttributeType attributeType) {
protected IllegalArgumentException unsupportedAttributeTypeException(AttributeType attributeType) {
return new IllegalArgumentException("Attribute type " + attributeType + " is not found in entity " + thisEntity.getEntityType().getIdentifier());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
import org.allaymc.api.permission.DefaultPermissions;
import org.allaymc.api.permission.tree.PermissionTree;
import org.allaymc.api.server.Server;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.chunk.Chunk;
import org.allaymc.server.component.annotation.*;
import org.allaymc.server.component.annotation.ComponentedObject;
import org.allaymc.server.component.annotation.Dependency;
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.world.chunk.AllayChunk;
import org.cloudburstmc.math.vector.Vector2f;
Expand Down Expand Up @@ -63,8 +67,8 @@
@Slf4j
public class EntityBaseComponentImpl implements EntityBaseComponent {

@Identifier
public static final org.allaymc.api.utils.Identifier IDENTIFIER = new org.allaymc.api.utils.Identifier("minecraft:entity_base_component");
@Identifier.Component
public static final Identifier IDENTIFIER = new Identifier("minecraft:entity_base_component");

public static final int DEFAULT_DEAD_TIMER = 20;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import org.allaymc.api.entity.component.EntityContainerHolderComponent;
import org.allaymc.api.eventbus.EventHandler;
import org.allaymc.api.item.interfaces.ItemAirStack;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.world.gamerule.GameRule;
import org.allaymc.server.component.annotation.ComponentedObject;
import org.allaymc.server.component.annotation.Identifier;
import org.allaymc.server.entity.component.event.CEntityDieEvent;
import org.jetbrains.annotations.UnmodifiableView;
import org.joml.Vector3f;
Expand All @@ -22,8 +22,8 @@
*/
public class EntityContainerHolderComponentImpl extends BaseContainerHolder implements EntityContainerHolderComponent {

@Identifier
protected static final org.allaymc.api.utils.Identifier IDENTIFIER = new org.allaymc.api.utils.Identifier("minecraft:entity_inventory_holder_component");
@Identifier.Component
protected static final Identifier IDENTIFIER = new Identifier("minecraft:entity_inventory_holder_component");

@ComponentedObject
protected static Entity thisEntity;
Expand Down
Loading

0 comments on commit f821420

Please sign in to comment.