Skip to content

Commit

Permalink
Implement Attributes API
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB committed Jul 15, 2020
1 parent 38eca6a commit f1323d7
Show file tree
Hide file tree
Showing 20 changed files with 792 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.accessor.entity.monster;

import net.minecraft.entity.ai.attributes.IAttribute;
import net.minecraft.entity.monster.ZombieEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ZombieEntity.class)
public interface ZombieEntityAccessor {

@Accessor("SPAWN_REINFORCEMENTS_CHANCE")
static IAttribute accessor$getSpawnReinforcementsChance() {
throw new IllegalStateException("Untransformed Accessor!");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.accessor.entity.passive.horse;

import net.minecraft.entity.ai.attributes.IAttribute;
import net.minecraft.entity.passive.horse.AbstractHorseEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(AbstractHorseEntity.class)
public interface AbstractHorseEntityAccessor {

@Accessor("JUMP_STRENGTH")
static IAttribute accessor$getJumpStrength() {
throw new IllegalStateException("Untransformed Accessor!");
}

}
2 changes: 2 additions & 0 deletions src/accessors/resources/mixins.common.accessors.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"entity.monster.SpellcastingIllagerEntityAccessor",
"entity.monster.VexEntityAccessor",
"entity.monster.VindicatorEntityAccessor",
"entity.monster.ZombieEntityAccessor",
"entity.monster.ZombiePigmanEntityAccessor",
"entity.passive.AbstractChestedHorseEntityAccessor",
"entity.passive.AnimalEntityAccessor",
Expand All @@ -64,6 +65,7 @@
"entity.passive.TurtleEntityAccessor",
"entity.passive.WolfEntityAccessor",
"entity.passive.fish.PufferfishEntityAccessor",
"entity.passive.horse.AbstractHorseEntityAccessor",
"entity.passive.horse.LlamaEntityAccessor",
"entity.passive.horse.TraderLlamaEntityAccessor",
"entity.player.PlayerAbilitiesAccessor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
import org.spongepowered.common.SpongeCatalogType;
import org.spongepowered.common.util.MissingImplementationException;

import java.util.Arrays;

public class SpongeEquipmentType extends SpongeCatalogType implements EquipmentType {

Expand All @@ -44,6 +47,6 @@ public EquipmentSlotType[] getSlots() {

@Override
public boolean includes(EquipmentType other) {
return false;
throw new MissingImplementationException("SpongeEquipmentType", "includes");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.entity.attribute;

import com.google.common.base.Preconditions;
import org.spongepowered.api.entity.attribute.AttributeModifier;
import org.spongepowered.api.entity.attribute.AttributeOperation;

import java.util.UUID;

public final class SpongeAttributeModifierBuilder implements AttributeModifier.Builder {
// Use a random id
private UUID id = UUID.randomUUID();
private String name;
private AttributeOperation operation;
private double amount;

public SpongeAttributeModifierBuilder() {
}

@Override
public AttributeModifier.Builder id(final UUID id) {
this.id = Preconditions.checkNotNull(id, "Modifier id cannot be null");
return this;
}

@Override
public AttributeModifier.Builder name(final String name) {
this.name = Preconditions.checkNotNull(name, "Name cannot be null");
return this;
}

@Override
public AttributeModifier.Builder operation(final AttributeOperation operation) {
this.operation = Preconditions.checkNotNull(operation, "Operation cannot be null");
return this;
}

@Override
public AttributeModifier.Builder amount(final double amount) {
this.amount = amount;
return this;
}

@Override
public AttributeModifier build() {
Preconditions.checkNotNull(this.name, "Name must be set");
Preconditions.checkNotNull(this.operation, "Operation must be set");
return (AttributeModifier) new net.minecraft.entity.ai.attributes.AttributeModifier(this.id, this.name, this.amount, (net.minecraft.entity.ai.attributes.AttributeModifier.Operation) (Object) this.operation);
}

@Override
public AttributeModifier.Builder reset() {
// Randomize id when reset
this.id = UUID.randomUUID();
this.name = null;
this.amount = 0.0D;
this.operation = null;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.base.Preconditions;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
Expand All @@ -48,9 +51,12 @@
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
import org.spongepowered.api.item.inventory.equipment.EquipmentTypes;
import org.spongepowered.api.item.inventory.equipment.WornEquipmentType;
import org.spongepowered.common.SpongeImplHooks;
import org.spongepowered.common.block.SpongeBlockSnapshot;
import org.spongepowered.common.data.persistence.NbtTranslator;
import org.spongepowered.common.data.type.SpongeEquipmentType;
import org.spongepowered.common.util.Constants;
import org.spongepowered.common.util.MissingImplementationException;
import org.spongepowered.common.util.PrettyPrinter;
Expand Down Expand Up @@ -124,8 +130,46 @@ public ItemStack.Builder fromItemStack(final ItemStack itemStack) {
}

@Override
public ItemStack.Builder attributeModifier(AttributeType attributeType, AttributeModifier modifier, EquipmentType equipmentType) {
throw new MissingImplementationException("SpongeItemStackBuilder", "attributeModifier");
public ItemStack.Builder attributeModifier(final AttributeType attributeType, final AttributeModifier modifier, final EquipmentType equipmentType) {
Preconditions.checkNotNull(attributeType, "AttributeType cannot be null");
Preconditions.checkNotNull(modifier, "AttributeModifier cannot be null");
Preconditions.checkNotNull(equipmentType, "EquipmentType cannot be null");

// Create the compound if needed
if (this.compound == null) {
this.compound = new CompoundNBT();
}

final CompoundNBT compound = this.compound;

if (!compound.contains(Constants.ItemStack.ATTRIBUTE_MODIFIERS, Constants.NBT.TAG_LIST)) {
compound.put(Constants.ItemStack.ATTRIBUTE_MODIFIERS, new ListNBT());
}

final ListNBT attributeModifiers = compound.getList(Constants.ItemStack.ATTRIBUTE_MODIFIERS, Constants.NBT.TAG_COMPOUND);

// The modifier will apply in any slot, equipable or not. Pass null for the slot
if (equipmentType.equals(EquipmentTypes.ANY.get()) || equipmentType.equals(EquipmentTypes.EQUIPPED.get())) {
this.writeAttributeModifier(attributeModifiers, (net.minecraft.entity.ai.attributes.AttributeModifier) modifier, null);
} else {
// Write modifier to every applicable slot.
for (EquipmentSlotType slot : ((SpongeEquipmentType) equipmentType).getSlots()) {
this.writeAttributeModifier(attributeModifiers, (net.minecraft.entity.ai.attributes.AttributeModifier) modifier, slot);
}
}

return this;
}

private void writeAttributeModifier(final ListNBT attributeModifiers, final net.minecraft.entity.ai.attributes.AttributeModifier attributeModifier, final EquipmentSlotType slot) {
final CompoundNBT modifierNbt = SharedMonsterAttributes.writeAttributeModifier(attributeModifier);
modifierNbt.putString(Constants.ItemStack.ATTRIBUTE_NAME, attributeModifier.getName());

if (slot != null) {
modifierNbt.putString(Constants.ItemStack.ATTRIBUTE_SLOT, slot.getName());
}

attributeModifiers.add(modifierNbt);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.parameter.Parameter;
import org.spongepowered.api.command.parameter.managed.standard.VariableValueParameters;
import org.spongepowered.api.entity.attribute.AttributeModifier;
import org.spongepowered.api.registry.BuilderRegistry;
import org.spongepowered.api.registry.DuplicateRegistrationException;
import org.spongepowered.api.registry.UnknownTypeException;
Expand All @@ -43,6 +44,7 @@
import org.spongepowered.common.command.parameter.multi.SpongeSequenceParameterBuilder;
import org.spongepowered.common.command.parameter.subcommand.SpongeSubcommandParameterBuilder;
import org.spongepowered.common.command.result.SpongeCommandResultBuilder;
import org.spongepowered.common.entity.attribute.SpongeAttributeModifierBuilder;

import java.util.Map;
import java.util.function.Supplier;
Expand Down Expand Up @@ -148,6 +150,7 @@ public void registerDefaultBuilders() {
// .register(FilteredTrigger.Builder.class, SpongeFilteredTriggerBuilder::new)
// .register(Trigger.Builder.class, SpongeTriggerBuilder::new)
// .register(ResourceKey.Builder.class, SpongeCatalogKeyBuilder::new)
.register(AttributeModifier.Builder.class, SpongeAttributeModifierBuilder::new)
.register(Command.Builder.class, SpongeParameterizedCommandBuilder::new)
.register(Parameter.FirstOfBuilder.class, SpongeFirstOfParameterBuilder::new)
.register(Parameter.SequenceBuilder.class, SpongeSequenceParameterBuilder::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.inject.Singleton;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.monster.PhantomEntity;
import net.minecraft.entity.monster.SpellcastingIllagerEntity;
import net.minecraft.entity.passive.FoxEntity;
Expand Down Expand Up @@ -101,13 +102,16 @@
import org.spongepowered.api.effect.sound.music.MusicDisc;
import org.spongepowered.api.entity.ai.goal.GoalExecutorType;
import org.spongepowered.api.entity.ai.goal.GoalType;
import org.spongepowered.api.entity.attribute.AttributeOperation;
import org.spongepowered.api.entity.attribute.type.AttributeType;
import org.spongepowered.api.entity.living.monster.boss.dragon.phase.DragonPhaseType;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.event.cause.EventContextKey;
import org.spongepowered.api.event.cause.entity.damage.DamageType;
import org.spongepowered.api.event.cause.entity.dismount.DismountType;
import org.spongepowered.api.event.cause.entity.spawn.SpawnType;
import org.spongepowered.api.item.FireworkShape;
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
import org.spongepowered.api.item.inventory.query.QueryType;
import org.spongepowered.api.registry.CatalogRegistry;
import org.spongepowered.api.registry.DuplicateRegistrationException;
Expand Down Expand Up @@ -148,6 +152,7 @@
import org.spongepowered.common.registry.builtin.sponge.RabbitTypeStreamGenerator;
import org.spongepowered.common.registry.builtin.sponge.SpawnTypeStreamGenerator;
import org.spongepowered.common.registry.builtin.sponge.WoodTypeStreamGenerator;
import org.spongepowered.common.registry.builtin.vanilla.AttributeTypeStreamGenerator;
import org.spongepowered.common.registry.builtin.vanilla.BiomeSupplier;
import org.spongepowered.common.registry.builtin.vanilla.BlockSupplier;
import org.spongepowered.common.registry.builtin.vanilla.ContainerTypeSupplier;
Expand All @@ -157,6 +162,7 @@
import org.spongepowered.common.registry.builtin.vanilla.EffectSupplier;
import org.spongepowered.common.registry.builtin.vanilla.EnchantmentSupplier;
import org.spongepowered.common.registry.builtin.vanilla.EntityTypeSupplier;
import org.spongepowered.common.registry.builtin.vanilla.EquipmentTypeStreamGenerator;
import org.spongepowered.common.registry.builtin.vanilla.FireworkShapeStreamGenerator;
import org.spongepowered.common.registry.builtin.vanilla.FluidSupplier;
import org.spongepowered.common.registry.builtin.vanilla.ItemSupplier;
Expand Down Expand Up @@ -412,6 +418,8 @@ public void registerDefaultRegistries() {
.registerRegistry(AdvancementTree.class, ResourceKey.minecraft("advancement_tree"))
// .generateRegistry(AdvancementType.class, ResourceKey.minecraft("advancement_type"), Arrays.stream(FrameType.values()), true)
.generateRegistry(ArmorMaterial.class, ResourceKey.minecraft("armor_material"), Arrays.stream(net.minecraft.item.ArmorMaterial.values()), true)
.generateRegistry(AttributeOperation.class, ResourceKey.minecraft("attribute_operation"), Arrays.stream(AttributeModifier.Operation.values()), true)
.generateRegistry(AttributeType.class, ResourceKey.minecraft("attribute_type"), AttributeTypeStreamGenerator.stream(), true)
.generateRegistry(BanType.class, ResourceKey.minecraft("ban_type"), BanTypeStreamGenerator.stream(), true)
.generateRegistry(BannerPatternShape.class, ResourceKey.minecraft("banner_pattern_shape"), Arrays.stream(BannerPattern.values()), true)
// .generateRegistry(BossBarOverlay.class, ResourceKey.minecraft("boss_bar_overlay"), Arrays.stream(BossInfo.Overlay.values()), true)
Expand All @@ -431,6 +439,7 @@ public void registerDefaultRegistries() {
.generateRegistry(DyeColor.class, ResourceKey.minecraft("dye_color"), Arrays.stream(net.minecraft.item.DyeColor.values()), true)
.generateRegistry(CatalogedValueParameter.class, ResourceKey.sponge("value_parameter"), CatalogedValueParameterStreamGenerator.stream(), true)
.generateRegistry(CommandRegistrar.class, ResourceKey.sponge("command_registrar"), CommandRegistrarStreamGenerator.stream(), true)
.generateRegistry(EquipmentType.class, ResourceKey.minecraft("equipment_type"), EquipmentTypeStreamGenerator.stream(), true)
.generateRegistry(EventContextKey.class, ResourceKey.sponge("event_context_key"), EventContextKeyStreamGenerator.stream(), true)
.generateRegistry(FoxType.class, ResourceKey.minecraft("fox_type"), Arrays.stream(FoxEntity.Type.values()), true)
.generateRegistry(GameMode.class, ResourceKey.minecraft("game_mode"), Arrays.stream(GameType.values()), true)
Expand Down

0 comments on commit f1323d7

Please sign in to comment.