-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces a new attribute system, including `Attribute`, `AttributeType`, `AttributeProvider`, and their implementation in `PaperAttributeProvider`. Attributes can now be registered, retrieved, and serialized for characters, enhancing extensibility and functionality within the system.
- Loading branch information
Showing
8 changed files
with
251 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
api/src/main/java/net/thenextlvl/character/attribute/Attribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.thenextlvl.character.attribute; | ||
|
||
import core.nbt.serialization.TagSerializable; | ||
import org.jspecify.annotations.NonNull; | ||
|
||
public interface Attribute<T> extends TagSerializable { | ||
@NonNull | ||
AttributeType<T> getType(); | ||
|
||
T getValue(); | ||
|
||
boolean setValue(T value); | ||
} |
24 changes: 24 additions & 0 deletions
24
api/src/main/java/net/thenextlvl/character/attribute/AttributeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package net.thenextlvl.character.attribute; | ||
|
||
import net.thenextlvl.character.Character; | ||
import org.bukkit.entity.Entity; | ||
import org.jetbrains.annotations.Unmodifiable; | ||
import org.jspecify.annotations.NullMarked; | ||
|
||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
|
||
@NullMarked | ||
public interface AttributeProvider { | ||
<V extends Entity, T> void register( | ||
AttributeType<T> type, Class<V> target, | ||
BiConsumer<Character<V>, T> setter | ||
); | ||
|
||
@Unmodifiable | ||
Set<AttributeType<?>> getAttributeTypes(); | ||
|
||
boolean isRegistered(AttributeType<?> type); | ||
|
||
boolean unregister(AttributeType<?> type); | ||
} |
43 changes: 43 additions & 0 deletions
43
api/src/main/java/net/thenextlvl/character/attribute/AttributeType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package net.thenextlvl.character.attribute; | ||
|
||
import org.jspecify.annotations.NonNull; | ||
|
||
import java.util.Objects; | ||
|
||
public class AttributeType<T> { | ||
private final @NonNull Class<T> dataType; | ||
private final @NonNull String name; | ||
|
||
public AttributeType(@NonNull String name, @NonNull Class<T> dataType) { | ||
this.dataType = dataType; | ||
this.name = name; | ||
} | ||
|
||
public @NonNull Class<T> getDataType() { | ||
return dataType; | ||
} | ||
|
||
public @NonNull String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
AttributeType<?> that = (AttributeType<?>) o; | ||
return Objects.equals(dataType, that.dataType) && Objects.equals(name, that.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(dataType, name); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "AttributeType{" + | ||
"dataType=" + dataType + | ||
", name='" + name + '\'' + | ||
'}'; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
api/src/main/java/net/thenextlvl/character/attribute/AttributeTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.thenextlvl.character.attribute; | ||
|
||
import org.bukkit.entity.Pose; | ||
import org.jspecify.annotations.NullMarked; | ||
|
||
@NullMarked | ||
public class AttributeTypes { | ||
public static AttributeType<Pose> POSE = new AttributeType<>("pose", Pose.class); | ||
|
||
public static class Player { | ||
public static AttributeType<Boolean> SNEAKING = new AttributeType<>("pose", boolean.class); | ||
} | ||
|
||
public static class Allay { | ||
public static AttributeType<Boolean> DANCING = new AttributeType<>("dancing", boolean.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/net/thenextlvl/character/plugin/character/attribute/PaperAttribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package net.thenextlvl.character.plugin.character.attribute; | ||
|
||
import core.nbt.serialization.ParserException; | ||
import core.nbt.tag.Tag; | ||
import net.thenextlvl.character.Character; | ||
import net.thenextlvl.character.attribute.Attribute; | ||
import net.thenextlvl.character.attribute.AttributeType; | ||
import net.thenextlvl.character.plugin.CharacterPlugin; | ||
import org.bukkit.entity.Entity; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jspecify.annotations.NonNull; | ||
|
||
import java.util.Objects; | ||
import java.util.function.BiConsumer; | ||
|
||
public class PaperAttribute<V extends Entity, T> implements Attribute<T> { | ||
private final @NonNull AttributeType<T> type; | ||
private final @NonNull BiConsumer<Character<@NotNull V>, T> onChange; | ||
private final @NonNull Character<@NotNull V> character; | ||
private final @NonNull CharacterPlugin plugin; | ||
private T value; | ||
|
||
public PaperAttribute(@NonNull AttributeType<T> type, @NonNull BiConsumer<Character<@NotNull V>, T> onChange, | ||
@NonNull Character<@NotNull V> character, @NonNull CharacterPlugin plugin) { | ||
this.type = type; | ||
this.onChange = onChange; | ||
this.character = character; | ||
this.plugin = plugin; | ||
} | ||
|
||
|
||
@Override | ||
public @NotNull AttributeType<T> getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public T getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean setValue(T value) { | ||
if (Objects.equals(this.value, value)) return false; | ||
onChange.accept(character, this.value = value); | ||
return true; | ||
} | ||
|
||
@Override | ||
public @NotNull Tag serialize() throws ParserException { | ||
return plugin.nbt().toTag(value); | ||
} | ||
|
||
@Override | ||
public void deserialize(@NotNull Tag tag) throws ParserException { | ||
setValue(plugin.nbt().fromTag(tag, type.getDataType())); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...main/java/net/thenextlvl/character/plugin/character/attribute/PaperAttributeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package net.thenextlvl.character.plugin.character.attribute; | ||
|
||
import com.google.common.base.Preconditions; | ||
import net.thenextlvl.character.Character; | ||
import net.thenextlvl.character.attribute.Attribute; | ||
import net.thenextlvl.character.attribute.AttributeProvider; | ||
import net.thenextlvl.character.attribute.AttributeType; | ||
import net.thenextlvl.character.plugin.CharacterPlugin; | ||
import org.bukkit.entity.Entity; | ||
import org.jetbrains.annotations.Unmodifiable; | ||
import org.jspecify.annotations.NullMarked; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
|
||
@NullMarked | ||
public class PaperAttributeProvider implements AttributeProvider { | ||
private final Map<AttributeType<?>, Function<Character<?>, @Nullable Attribute<?>>> attributes = new HashMap<>(); | ||
private final CharacterPlugin plugin; | ||
|
||
public PaperAttributeProvider(CharacterPlugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> Optional<Attribute<T>> createAttribute(AttributeType<T> type, Character<?> character) { | ||
return Optional.ofNullable(attributes.get(type)).map(function -> | ||
(Attribute<T>) function.apply(character)); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <V extends Entity, T> void register( | ||
AttributeType<T> type, Class<V> target, | ||
BiConsumer<Character<V>, T> onChange | ||
) { | ||
Preconditions.checkArgument(!isRegistered(type), "Attribute type already registered: %s", type); | ||
attributes.put(type, character -> { | ||
var entityClass = character.getType().getEntityClass(); | ||
if (entityClass == null || !target.isAssignableFrom(entityClass)) return null; | ||
return new PaperAttribute<>(type, onChange, (Character<V>) character, plugin); | ||
}); | ||
} | ||
|
||
@Override | ||
public @Unmodifiable Set<AttributeType<?>> getAttributeTypes() { | ||
return Set.copyOf(attributes.keySet()); | ||
} | ||
|
||
@Override | ||
public boolean isRegistered(AttributeType<?> type) { | ||
return attributes.containsKey(type); | ||
} | ||
|
||
@Override | ||
public boolean unregister(AttributeType<?> type) { | ||
return attributes.remove(type) != null; | ||
} | ||
} |