Skip to content

Commit 8b4f93e

Browse files
authored
Add item attributes (#2602)
1 parent 3462203 commit 8b4f93e

File tree

7 files changed

+282
-0
lines changed

7 files changed

+282
-0
lines changed

src/main/java/org/spongepowered/api/data/Keys.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
import org.spongepowered.api.entity.Item;
130130
import org.spongepowered.api.entity.Ownable;
131131
import org.spongepowered.api.entity.ai.goal.GoalExecutorTypes;
132+
import org.spongepowered.api.entity.attribute.ItemAttribute;
132133
import org.spongepowered.api.entity.display.BillboardType;
133134
import org.spongepowered.api.entity.display.DisplayEntity;
134135
import org.spongepowered.api.entity.display.ItemDisplayType;
@@ -2181,6 +2182,11 @@ public final class Keys {
21812182
*/
21822183
public static final Key<Value<Boolean>> IS_WET = Keys.key(ResourceKey.sponge("is_wet"), Boolean.class);
21832184

2185+
/**
2186+
* The {@link ItemAttribute}s an {@link ItemStackLike} can apply.
2187+
*/
2188+
public static final Key<ListValue<ItemAttribute>> ITEM_ATTRIBUTES = Keys.listKey(ResourceKey.sponge("item_attributes"), ItemAttribute.class);
2189+
21842190
/**
21852191
* The {@link ItemDisplayType display type} of a {@link org.spongepowered.api.entity.display.ItemDisplay}.
21862192
*/

src/main/java/org/spongepowered/api/entity/attribute/AttributeModifier.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package org.spongepowered.api.entity.attribute;
2626

27+
import org.spongepowered.api.ResourceKey;
2728
import org.spongepowered.api.ResourceKeyed;
2829
import org.spongepowered.api.Sponge;
2930
import org.spongepowered.api.item.inventory.ItemStack;
@@ -38,6 +39,30 @@
3839
*/
3940
public interface AttributeModifier extends ResourceKeyed {
4041

42+
/**
43+
* Creates an attribute modifier with the given values.
44+
*
45+
* @param key The resource key
46+
* @param operation The attribute operation
47+
* @param amount The amount
48+
* @return The attribute modifier
49+
*/
50+
static AttributeModifier of(final ResourceKey key, final Supplier<? extends AttributeOperation> operation, final double amount) {
51+
return AttributeModifier.builder().key(key).operation(operation).amount(amount).build();
52+
}
53+
54+
/**
55+
* Creates an attribute modifier with the given values.
56+
*
57+
* @param key The resource key
58+
* @param operation The attribute operation
59+
* @param amount The amount
60+
* @return The attribute modifier
61+
*/
62+
static AttributeModifier of(final ResourceKey key, final AttributeOperation operation, final double amount) {
63+
return AttributeModifier.builder().key(key).operation(operation).amount(amount).build();
64+
}
65+
4166
/**
4267
* Creates a new {@link Builder} to create an {@link AttributeModifier}.
4368
*
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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.entity.attribute.type.AttributeType;
29+
import org.spongepowered.api.item.inventory.ItemStackLike;
30+
import org.spongepowered.api.item.inventory.equipment.EquipmentCondition;
31+
32+
import java.util.Objects;
33+
import java.util.function.Supplier;
34+
35+
/**
36+
* Represents an {@link AttributeModifier} for the specific {@link AttributeType}
37+
* an {@link ItemStackLike} can apply when the {@link EquipmentCondition} is met.
38+
*/
39+
public interface ItemAttribute {
40+
41+
/**
42+
* Creates an item attribute with the given values.
43+
*
44+
* @param type The attribute type
45+
* @param modifier The attribute modifier
46+
* @param condition The equipment condition
47+
* @return The item attribute
48+
*/
49+
static ItemAttribute of(final Supplier<? extends AttributeType> type, final AttributeModifier modifier, final Supplier<? extends EquipmentCondition> condition) {
50+
return ItemAttribute.of(Objects.requireNonNull(type, "type").get(), modifier, Objects.requireNonNull(condition, "condition").get());
51+
}
52+
53+
/**
54+
* Creates an item attribute with the given values.
55+
*
56+
* @param type The attribute type
57+
* @param modifier The attribute modifier
58+
* @param condition The equipment condition
59+
* @return The item attribute
60+
*/
61+
static ItemAttribute of(final Supplier<? extends AttributeType> type, final AttributeModifier modifier, final EquipmentCondition condition) {
62+
return ItemAttribute.of(Objects.requireNonNull(type, "type").get(), modifier, condition);
63+
}
64+
65+
/**
66+
* Creates an item attribute with the given values.
67+
*
68+
* @param type The attribute type
69+
* @param modifier The attribute modifier
70+
* @param condition The equipment condition
71+
* @return The item attribute
72+
*/
73+
static ItemAttribute of(final AttributeType type, final AttributeModifier modifier, final Supplier<? extends EquipmentCondition> condition) {
74+
return ItemAttribute.of(type, modifier, Objects.requireNonNull(condition, "condition").get());
75+
}
76+
77+
/**
78+
* Creates an item attribute with the given values.
79+
*
80+
* @param type The attribute type
81+
* @param modifier The attribute modifier
82+
* @param condition The equipment condition
83+
* @return The item attribute
84+
*/
85+
static ItemAttribute of(final AttributeType type, final AttributeModifier modifier, final EquipmentCondition condition) {
86+
return Sponge.game().factoryProvider().provide(Factory.class).of(type, modifier, condition);
87+
}
88+
89+
/**
90+
* Returns the attribute type.
91+
*
92+
* @return The attribute type
93+
*/
94+
AttributeType type();
95+
96+
/**
97+
* Returns the attribute modifier.
98+
*
99+
* @return The attribute modifier
100+
*/
101+
AttributeModifier modifier();
102+
103+
/**
104+
* Returns the equipment condition.
105+
*
106+
* @return The equipment condition
107+
*/
108+
EquipmentCondition condition();
109+
110+
interface Factory {
111+
112+
ItemAttribute of(AttributeType type, AttributeModifier modifier, EquipmentCondition condition);
113+
}
114+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.item.inventory.equipment;
26+
27+
import net.kyori.adventure.text.ComponentLike;
28+
import org.spongepowered.api.data.type.StringRepresentable;
29+
import org.spongepowered.api.registry.DefaultedRegistryValue;
30+
import org.spongepowered.api.util.annotation.CatalogedBy;
31+
32+
import java.util.Objects;
33+
import java.util.function.Supplier;
34+
35+
/**
36+
* Represents some condition for {@link EquipmentType}.
37+
*/
38+
@CatalogedBy(EquipmentConditions.class)
39+
public interface EquipmentCondition extends DefaultedRegistryValue<EquipmentCondition>, StringRepresentable, ComponentLike {
40+
41+
/**
42+
* Tests whether the equipment type is suitable for this condition.
43+
*
44+
* @param type The equipment type to test
45+
* @return True if the equipment type is suitable for this condition
46+
*/
47+
default boolean test(final Supplier<? extends EquipmentType> type) {
48+
return this.test(Objects.requireNonNull(type, "type").get());
49+
}
50+
51+
/**
52+
* Tests whether the equipment type is suitable for this condition.
53+
*
54+
* @param type The equipment type to test
55+
* @return True if the equipment type is suitable for this condition
56+
*/
57+
boolean test(EquipmentType type);
58+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.item.inventory.equipment;
26+
27+
import org.spongepowered.api.ResourceKey;
28+
import org.spongepowered.api.Sponge;
29+
import org.spongepowered.api.registry.DefaultedRegistryReference;
30+
import org.spongepowered.api.registry.Registry;
31+
import org.spongepowered.api.registry.RegistryKey;
32+
import org.spongepowered.api.registry.RegistryTypes;
33+
34+
/**
35+
* <!-- This file is automatically generated. Any manual changes will be overwritten. -->
36+
*/
37+
public final class EquipmentConditions {
38+
39+
public static final DefaultedRegistryReference<EquipmentCondition> ANY = EquipmentConditions.key(ResourceKey.sponge("any"));
40+
41+
public static final DefaultedRegistryReference<EquipmentCondition> ARMOR = EquipmentConditions.key(ResourceKey.sponge("armor"));
42+
43+
public static final DefaultedRegistryReference<EquipmentCondition> BODY = EquipmentConditions.key(ResourceKey.sponge("body"));
44+
45+
public static final DefaultedRegistryReference<EquipmentCondition> CHEST = EquipmentConditions.key(ResourceKey.sponge("chest"));
46+
47+
public static final DefaultedRegistryReference<EquipmentCondition> FEET = EquipmentConditions.key(ResourceKey.sponge("feet"));
48+
49+
public static final DefaultedRegistryReference<EquipmentCondition> HAND = EquipmentConditions.key(ResourceKey.sponge("hand"));
50+
51+
public static final DefaultedRegistryReference<EquipmentCondition> HEAD = EquipmentConditions.key(ResourceKey.sponge("head"));
52+
53+
public static final DefaultedRegistryReference<EquipmentCondition> LEGS = EquipmentConditions.key(ResourceKey.sponge("legs"));
54+
55+
public static final DefaultedRegistryReference<EquipmentCondition> MAINHAND = EquipmentConditions.key(ResourceKey.sponge("mainhand"));
56+
57+
public static final DefaultedRegistryReference<EquipmentCondition> OFFHAND = EquipmentConditions.key(ResourceKey.sponge("offhand"));
58+
59+
private EquipmentConditions() {
60+
}
61+
62+
public static Registry<EquipmentCondition> registry() {
63+
return Sponge.game().registry(RegistryTypes.EQUIPMENT_CONDITION);
64+
}
65+
66+
private static DefaultedRegistryReference<EquipmentCondition> key(final ResourceKey location) {
67+
return RegistryKey.of(RegistryTypes.EQUIPMENT_CONDITION, location).asDefaultedReference(Sponge::game);
68+
}
69+
}

src/main/java/org/spongepowered/api/item/inventory/equipment/EquipmentType.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ public interface EquipmentType extends DefaultedRegistryValue<EquipmentType>, St
4040
* @return The group
4141
*/
4242
EquipmentGroup group();
43+
44+
/**
45+
* Gets the most specific {@link EquipmentCondition} for this equipment type.
46+
*
47+
* @return The condition
48+
*/
49+
EquipmentCondition condition();
4350
}

src/main/java/org/spongepowered/api/registry/RegistryTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
import org.spongepowered.api.item.ItemType;
129129
import org.spongepowered.api.item.enchantment.EnchantmentType;
130130
import org.spongepowered.api.item.inventory.ContainerType;
131+
import org.spongepowered.api.item.inventory.equipment.EquipmentCondition;
131132
import org.spongepowered.api.item.inventory.equipment.EquipmentGroup;
132133
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
133134
import org.spongepowered.api.item.inventory.menu.ClickType;
@@ -372,6 +373,8 @@ public final class RegistryTypes {
372373

373374
public static final DefaultedRegistryType<DyeColor> DYE_COLOR = RegistryTypes.spongeKeyInGame("dye_color");
374375

376+
public static final DefaultedRegistryType<EquipmentCondition> EQUIPMENT_CONDITION = RegistryTypes.spongeKeyInGame("equipment_condition");
377+
375378
public static final DefaultedRegistryType<EquipmentGroup> EQUIPMENT_GROUP = RegistryTypes.spongeKeyInGame("equipment_group");
376379

377380
public static final DefaultedRegistryType<EquipmentType> EQUIPMENT_TYPE = RegistryTypes.spongeKeyInGame("equipment_type");

0 commit comments

Comments
 (0)