-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: default entity attribute attachment
- Loading branch information
1 parent
414e888
commit b5296c6
Showing
15 changed files
with
275 additions
and
32 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
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
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 @@ | ||
moduleDependencies(project, "specter-core", "specter-registry") |
11 changes: 11 additions & 0 deletions
11
specter-entity/src/client/resources/specter-entity.client.mixins.json
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,11 @@ | ||
{ | ||
"required": true, | ||
"minVersion": "0.8", | ||
"package": "dev.spiritstudios.specter.mixin.entity.client", | ||
"compatibilityLevel": "JAVA_21", | ||
"injectors": { | ||
"defaultRequire": 1 | ||
}, | ||
"client": [ | ||
] | ||
} |
29 changes: 29 additions & 0 deletions
29
specter-entity/src/main/java/dev/spiritstudios/specter/api/entity/EntityAttachments.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,29 @@ | ||
package dev.spiritstudios.specter.api.entity; | ||
|
||
import dev.spiritstudios.specter.api.registry.attachment.Attachment; | ||
import dev.spiritstudios.specter.impl.entity.DataDefaultAttributeBuilder; | ||
import net.minecraft.entity.EntityType; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.util.Identifier; | ||
|
||
import static dev.spiritstudios.specter.api.core.SpecterGlobals.MODID; | ||
|
||
public final class EntityAttachments { | ||
public static final Attachment<EntityType<?>, DataDefaultAttributeBuilder> DEFAULT_ATTRIBUTES = Attachment.builder( | ||
Registries.ENTITY_TYPE, | ||
Identifier.of(MODID, "default_attributes"), | ||
DataDefaultAttributeBuilder.CODEC, | ||
DataDefaultAttributeBuilder.PACKET_CODEC | ||
).build(); | ||
|
||
/** | ||
* Hacky workaround to force class loading. | ||
*/ | ||
@SuppressWarnings("EmptyMethod") | ||
public static void init() { | ||
// NO-OP | ||
} | ||
|
||
private EntityAttachments() { | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...tity/src/main/java/dev/spiritstudios/specter/impl/entity/DataDefaultAttributeBuilder.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,45 @@ | ||
package dev.spiritstudios.specter.impl.entity; | ||
|
||
import com.mojang.serialization.Codec; | ||
import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap; | ||
import net.minecraft.entity.attribute.DefaultAttributeContainer; | ||
import net.minecraft.entity.attribute.EntityAttribute; | ||
import net.minecraft.network.RegistryByteBuf; | ||
import net.minecraft.network.codec.PacketCodec; | ||
import net.minecraft.network.codec.PacketCodecs; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.registry.RegistryKeys; | ||
import net.minecraft.registry.entry.RegistryEntry; | ||
|
||
import java.util.Map; | ||
|
||
// Yes, this is a builder builder. I love codecs. | ||
public record DataDefaultAttributeBuilder(Map<RegistryEntry<EntityAttribute>, Double> attributes) { | ||
public DefaultAttributeContainer build() { | ||
DefaultAttributeContainer.Builder builder = DefaultAttributeContainer.builder(); | ||
attributes.forEach(builder::add); | ||
return builder.build(); | ||
} | ||
|
||
public static final Codec<DataDefaultAttributeBuilder> CODEC = Codec.unboundedMap( | ||
Registries.ATTRIBUTE.getEntryCodec(), | ||
Codec.DOUBLE | ||
).xmap(DataDefaultAttributeBuilder::new, DataDefaultAttributeBuilder::attributes); | ||
|
||
public static final PacketCodec<RegistryByteBuf, DataDefaultAttributeBuilder> PACKET_CODEC = PacketCodec.tuple( | ||
PacketCodecs.map( | ||
Object2DoubleOpenHashMap::new, | ||
PacketCodecs.registryEntry(RegistryKeys.ATTRIBUTE), | ||
PacketCodecs.DOUBLE | ||
), | ||
DataDefaultAttributeBuilder::attributes, | ||
DataDefaultAttributeBuilder::new | ||
); | ||
|
||
public static DataDefaultAttributeBuilder with(DataDefaultAttributeBuilder original, DefaultAttributeContainer attributes) { | ||
Map<RegistryEntry<EntityAttribute>, Double> newAttributes = new Object2DoubleOpenHashMap<>(); | ||
attributes.instances.forEach((attribute, value) -> newAttributes.put(attribute, value.getBaseValue())); | ||
newAttributes.putAll(original.attributes); | ||
return new DataDefaultAttributeBuilder(newAttributes); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
specter-entity/src/main/java/dev/spiritstudios/specter/impl/entity/SpecterEntity.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,11 @@ | ||
package dev.spiritstudios.specter.impl.entity; | ||
|
||
import dev.spiritstudios.specter.api.entity.EntityAttachments; | ||
import net.fabricmc.api.ModInitializer; | ||
|
||
public class SpecterEntity implements ModInitializer { | ||
@Override | ||
public void onInitialize() { | ||
EntityAttachments.init(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...y/src/main/java/dev/spiritstudios/specter/mixin/entity/DefaultAttributeRegistryMixin.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,40 @@ | ||
package dev.spiritstudios.specter.mixin.entity; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import dev.spiritstudios.specter.api.entity.EntityAttachments; | ||
import dev.spiritstudios.specter.impl.entity.DataDefaultAttributeBuilder; | ||
import net.minecraft.entity.EntityType; | ||
import net.minecraft.entity.attribute.DefaultAttributeContainer; | ||
import net.minecraft.entity.attribute.DefaultAttributeRegistry; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@Mixin(DefaultAttributeRegistry.class) | ||
public class DefaultAttributeRegistryMixin { | ||
@SuppressWarnings("unchecked") | ||
@WrapOperation(method = "get", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;")) | ||
private static <K, V> V get(Map<K, V> instance, Object o, Operation<V> original) { | ||
if (!(o instanceof EntityType<?> entityType)) return original.call(instance, o); | ||
|
||
Optional<DataDefaultAttributeBuilder> attributeBuilder = EntityAttachments.DEFAULT_ATTRIBUTES.get(entityType); | ||
if (attributeBuilder.isEmpty()) return original.call(instance, o); | ||
|
||
DefaultAttributeContainer originalAttributes = (DefaultAttributeContainer) original.call(instance, o); | ||
if (originalAttributes == null) return (V) attributeBuilder.get().build(); | ||
|
||
return (V) DataDefaultAttributeBuilder.with(attributeBuilder.get(), originalAttributes).build(); | ||
} | ||
|
||
@WrapOperation(method = "hasDefinitionFor", at = @At(value = "INVOKE", target = "Ljava/util/Map;containsKey(Ljava/lang/Object;)Z")) | ||
private static <K, V> boolean containsKey(Map<K, V> instance, Object o, Operation<Boolean> original) { | ||
if (!(o instanceof EntityType<?> entityType)) return original.call(instance, o); | ||
|
||
boolean hasDefinition = Objects.nonNull(EntityAttachments.DEFAULT_ATTRIBUTES.get(entityType)); | ||
return hasDefinition || original.call(instance, o); | ||
} | ||
} |
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,40 @@ | ||
{ | ||
"schemaVersion": 1, | ||
"id": "specter-entity", | ||
"version": "${version}", | ||
"name": "Specter Entity", | ||
"description": "Library for Spirit Studios mods (Entity module)", | ||
"authors": [], | ||
"contact": { | ||
"repo": "https://github.com/SpiritGameStudios/Specter", | ||
"issues": "https://github.com/SpiritGameStudios/Specter/issues" | ||
}, | ||
"license": "MPL-2.0", | ||
"environment": "*", | ||
"entrypoints": { | ||
"main": [ | ||
"dev.spiritstudios.specter.impl.entity.SpecterEntity" | ||
] | ||
}, | ||
"mixins": [ | ||
"specter-entity.mixins.json", | ||
{ | ||
"config": "specter-entity.client.mixins.json", | ||
"environment": "client" | ||
} | ||
], | ||
"depends": { | ||
"fabricloader": ">=${loader_version}", | ||
"minecraft": "~${minecraft_version}", | ||
"fabric-api": "*", | ||
"java": ">=21" | ||
}, | ||
"custom": { | ||
"modmenu": { | ||
"parent": "specter", | ||
"badges": [ | ||
"library" | ||
] | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
specter-entity/src/main/resources/specter-entity.mixins.json
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,12 @@ | ||
{ | ||
"required": true, | ||
"minVersion": "0.8", | ||
"package": "dev.spiritstudios.specter.mixin.entity", | ||
"compatibilityLevel": "JAVA_21", | ||
"mixins": [ | ||
"DefaultAttributeRegistryMixin" | ||
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
.../testmod/resources/data/specter/attachments/minecraft/entity_type/default_attributes.json
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,8 @@ | ||
{ | ||
"replace": false, | ||
"values": { | ||
"minecraft:warden": { | ||
"minecraft:generic.max_health": 1.0 | ||
} | ||
} | ||
} |
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,22 @@ | ||
{ | ||
"schemaVersion": 1, | ||
"id": "specter-entity-testmod", | ||
"version": "${version}", | ||
"name": "Specter", | ||
"description": "Library for Spirit Studios mods (Entity module tests)", | ||
"authors": [], | ||
"contact": { | ||
"repo": "https://github.com/SpiritGameStudios/Specter", | ||
"issues": "https://github.com/SpiritGameStudios/Specter/issues" | ||
}, | ||
"license": "MPL-2.0", | ||
"environment": "*", | ||
"entrypoints": { | ||
}, | ||
"depends": { | ||
"fabricloader": ">=${loader_version}", | ||
"minecraft": "~${minecraft_version}", | ||
"fabric-api": "*", | ||
"java": ">=21" | ||
} | ||
} |
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
Oops, something went wrong.