Skip to content

Commit 94badf2

Browse files
committed
Add Entity Attribute api
1 parent 892e439 commit 94badf2

File tree

14 files changed

+947
-3
lines changed

14 files changed

+947
-3
lines changed

src/main/java/org/spongepowered/api/CatalogTypes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
import org.spongepowered.api.entity.EntityType;
4545
import org.spongepowered.api.entity.ai.goal.GoalExecutorType;
4646
import org.spongepowered.api.entity.ai.goal.GoalType;
47+
import org.spongepowered.api.entity.attribute.AttributeOperation;
48+
import org.spongepowered.api.entity.attribute.ModifierTemplate;
49+
import org.spongepowered.api.entity.attribute.type.AttributeType;
4750
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
4851
import org.spongepowered.api.event.cause.EventContextKey;
4952
import org.spongepowered.api.event.cause.entity.damage.DamageModifierType;
@@ -111,6 +114,10 @@ public final class CatalogTypes {
111114

112115
public static final Class<ArtType> ART_TYPE = ArtType.class;
113116

117+
public static final Class<AttributeOperation> ATTRIBUTE_OPERATION = AttributeOperation.class;
118+
119+
public static final Class<AttributeType> ATTRIBUTE_TYPE = AttributeType.class;
120+
114121
public static final Class<BannerPatternShape> BANNER_PATTERN_SHAPE = BannerPatternShape.class;
115122

116123
public static final Class<BanType> BAN_TYPE = BanType.class;
@@ -203,6 +210,8 @@ public final class CatalogTypes {
203210

204211
public static final Class<LlamaType> LLAMA_TYPE = LlamaType.class;
205212

213+
public static final Class<ModifierTemplate> MODIFIER_TEMPLATE = ModifierTemplate.class;
214+
206215
public static final Class<MooshroomType> MOOSHROOM_TYPE = MooshroomType.class;
207216

208217
public static final Class<MusicDisc> MUSIC_DISC = MusicDisc.class;
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.entity.attribute;
26+
27+
import org.spongepowered.api.entity.attribute.type.AttributeType;
28+
import org.spongepowered.api.item.inventory.ItemStack;
29+
30+
import java.util.Collection;
31+
import java.util.Optional;
32+
import java.util.UUID;
33+
import java.util.function.Supplier;
34+
35+
/**
36+
* Represents an instance of an {@link AttributeType} that contains a value.
37+
*
38+
* <p>Attributes are held in an {@link AttributeHolder}</p>
39+
*
40+
* <p>Attributes can have {@link AttributeModifier}s applied to an entity
41+
* through an equipped {@link ItemStack}</p>
42+
*/
43+
public interface Attribute {
44+
45+
/**
46+
* Gets the type of this attribute.
47+
*
48+
* @return The attribute type.
49+
*/
50+
AttributeType getType();
51+
52+
/**
53+
* Gets the base value of this attribute.
54+
*
55+
* <p>This is the value of this attribute before any {@link AttributeModifier}s are applied.</p>
56+
*
57+
* @return The base value.
58+
*/
59+
double getBaseValue();
60+
61+
/**
62+
* Sets the base value of this attribute.
63+
*
64+
* <p>This is the value <i>before</i> modifiers are applied.</p>
65+
*
66+
* @param baseValue The base value
67+
*/
68+
void setBaseValue(double baseValue);
69+
70+
/**
71+
* Gets the value of this attribute.
72+
*
73+
* <p>This is the value with modifiers applied.</p>
74+
*
75+
* @return The value
76+
*/
77+
double getValue();
78+
79+
/**
80+
* Gets a collection of all applied modifiers.
81+
*
82+
* @return A collection of applied modifiers
83+
*/
84+
Collection<AttributeModifier> getModifiers();
85+
86+
/**
87+
* Gets a collection of applied modifiers with the provided operation.
88+
*
89+
* @return A collection of modifiers
90+
*/
91+
default Collection<AttributeModifier> getModifiers(Supplier<? extends AttributeOperation> operation) {
92+
return this.getModifiers(operation.get());
93+
}
94+
95+
/**
96+
* Gets a collection of applied modifiers with the provided operation.
97+
*
98+
* @return A collection of modifiers
99+
*/
100+
Collection<AttributeModifier> getModifiers(AttributeOperation operation);
101+
102+
/**
103+
* Checks if this attribute has the provided modifier.
104+
*
105+
* @param modifier The modifier
106+
* @return {@code true} if this attribute has the modifier, {@code false}
107+
* otherwise
108+
*/
109+
boolean hasModifier(AttributeModifier modifier);
110+
111+
/**
112+
* Gets an attribute modifier by its unique id.
113+
*
114+
* @param uniqueId The unique id
115+
* @return The attribute modifier, if present, {@link Optional#empty()}
116+
* otherwise
117+
*/
118+
Optional<AttributeModifier> getModifier(UUID uniqueId);
119+
120+
/**
121+
* Adds a modifier to this attribute.
122+
*
123+
* @param modifier The modifier
124+
*/
125+
void addModifier(AttributeModifier modifier);
126+
127+
/**
128+
* Removes a modifier from this attribute.
129+
*
130+
* @param modifier The modifier
131+
*/
132+
void removeModifier(AttributeModifier modifier);
133+
134+
/**
135+
* Removes a modifier from this attribute using it's unique id.
136+
*
137+
* @param uniqueId The unique id of the modifier.
138+
*/
139+
void removeModifier(UUID uniqueId);
140+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.entity.attribute;
26+
27+
import org.spongepowered.api.entity.Entity;
28+
import org.spongepowered.api.entity.attribute.type.AttributeType;
29+
30+
import java.util.Optional;
31+
import java.util.function.Supplier;
32+
33+
/**
34+
* Represents an {@link Entity} which can hold {@link Attribute}s.
35+
*/
36+
public interface AttributeHolder {
37+
38+
/**
39+
* Gets an {@link Attribute} from this entity
40+
* @param type The attribute type.
41+
* @return An attribute, if present.
42+
*/
43+
default Optional<Attribute> getAttribute(final Supplier<? extends AttributeType> type) {
44+
return this.getAttribute(type.get());
45+
}
46+
47+
/**
48+
* Gets an {@link Attribute} from this entity
49+
* @param type The attribute type.
50+
* @return An attribute, if present.
51+
*/
52+
Optional<Attribute> getAttribute(final AttributeType type);
53+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.entity.attribute;
26+
27+
import org.spongepowered.api.Sponge;
28+
import org.spongepowered.api.item.inventory.ItemStack;
29+
import org.spongepowered.api.util.Identifiable;
30+
import org.spongepowered.api.util.ResettableBuilder;
31+
32+
import java.util.UUID;
33+
import java.util.function.Supplier;
34+
35+
/**
36+
* Represents a modifier to a value in a {@link Attribute} which is transformed
37+
* by an {@link AttributeOperation}.
38+
*
39+
* <p>Modifiers are usually found on {@link ItemStack}s.</p>
40+
*/
41+
public interface AttributeModifier extends Identifiable {
42+
43+
/**
44+
* Creates a new {@link Builder} to create an {@link AttributeModifier}.
45+
*
46+
* @return The new builder
47+
*/
48+
static Builder builder() {
49+
return Sponge.getRegistry().getBuilderRegistry().provideBuilder(Builder.class);
50+
}
51+
52+
/**
53+
* Gets the attribute name.
54+
*
55+
* @return The name
56+
*/
57+
String getName();
58+
59+
/**
60+
* Gets this modifier's operation.
61+
*
62+
* @return The operation
63+
*/
64+
AttributeOperation getOperation();
65+
66+
/**
67+
* Gets the amount this attribute will be modified by.
68+
*
69+
* @return The amount
70+
*/
71+
double getAmount();
72+
73+
/**
74+
* Represents a builder class to create {@link AttributeModifier}s.
75+
*
76+
* @see AttributeModifier
77+
*/
78+
interface Builder extends ResettableBuilder<AttributeModifier, Builder> {
79+
80+
/**
81+
* Sets the id of this attribute modifier.
82+
*
83+
* @param id The id
84+
* @return This builder
85+
*/
86+
Builder id(UUID id);
87+
88+
/**
89+
* Sets this attribute modifier to have a random id.
90+
*
91+
* @return This builder
92+
*/
93+
default Builder randomId() {
94+
return this.id(UUID.randomUUID());
95+
}
96+
97+
/**
98+
* Sets the name of this attribute modifier.
99+
*
100+
* <p>The name of an attribute modifier corresponds to the translation
101+
* displayed when listing all the modifiers on an item.</p>
102+
*
103+
* <p>The format of the translations is
104+
* <code>attribute.name.yournamehere</code>.</p>
105+
*
106+
* @param name The name
107+
* @return This builder
108+
*/
109+
Builder name(String name);
110+
111+
/**
112+
* Sets the operation of this attribute modifier.
113+
*
114+
* @param operation The operation
115+
* @return This builder
116+
*/
117+
default Builder operation(Supplier<? extends AttributeOperation> operation) {
118+
return this.operation(operation.get());
119+
}
120+
121+
/**
122+
* Sets the operation of this attribute modifier.
123+
*
124+
* @param operation The operation
125+
* @return This builder
126+
*/
127+
Builder operation(final AttributeOperation operation);
128+
129+
/**
130+
* Sets the {@link Builder#name(String) name} and
131+
* {@link Builder#id(UUID) id} of this attribute modifier to the
132+
* vanilla defaults.
133+
*
134+
* @param template The template to use.
135+
* @return This builder.
136+
*/
137+
default Builder fromTemplate(Supplier<? extends ModifierTemplate> template) {
138+
return this.fromTemplate(template.get());
139+
}
140+
141+
/**
142+
* Sets the {@link Builder#name(String) name} and
143+
* {@link Builder#id(UUID) id} of this attribute modifier to the
144+
* vanilla defaults.
145+
*
146+
* @param template The template to use.
147+
* @return This builder.
148+
*/
149+
default Builder fromTemplate(ModifierTemplate template) {
150+
return this.name(template.getName()).id(template.getUniqueId());
151+
}
152+
153+
/**
154+
* Sets the amount of the attribute modifier.
155+
*
156+
* @param amount The amount
157+
* @return This builder
158+
*/
159+
Builder amount(double amount);
160+
161+
/**
162+
* Build the attribute modifier from the values in this builder.
163+
*
164+
* @return The attribute modifier.
165+
*/
166+
AttributeModifier build();
167+
}
168+
}

0 commit comments

Comments
 (0)