Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public static List<Generator> itemRegistries(final Context context) {
$ -> true,
RegistryScope.SERVER
),
new RegistryEntriesGenerator<>(
"data.type",
"ItemActionTypes",
"ITEM_ACTION_TYPE",
context.relativeClass("data.type", "ItemActionType"),
Registries.CONSUME_EFFECT_TYPE
),
new EnumEntriesValidator<>(
"entity.display",
"ItemDisplayTypes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
package org.spongepowered.common.data.provider.item.stack;

import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.util.StringUtil;
import net.minecraft.util.Unit;
Expand All @@ -43,22 +42,19 @@
import net.minecraft.world.item.component.Consumable;
import net.minecraft.world.item.component.CustomModelData;
import net.minecraft.world.item.component.DamageResistant;
import net.minecraft.world.item.component.DeathProtection;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.item.component.ItemLore;
import net.minecraft.world.item.component.Unbreakable;
import net.minecraft.world.item.component.UseCooldown;
import net.minecraft.world.item.component.UseRemainder;
import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
import net.minecraft.world.item.consume_effects.ClearAllStatusEffectsConsumeEffect;
import net.minecraft.world.item.consume_effects.ConsumeEffect;
import net.minecraft.world.item.consume_effects.PlaySoundConsumeEffect;
import net.minecraft.world.item.consume_effects.RemoveStatusEffectsConsumeEffect;
import net.minecraft.world.item.consume_effects.TeleportRandomlyConsumeEffect;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.Platform;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.data.Keys;
import org.spongepowered.api.data.type.ItemAction;
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.item.ItemRarity;
import org.spongepowered.api.item.ItemType;
Expand All @@ -78,7 +74,7 @@
public final class ItemStackData {

public static final FoodProperties DEFAULT_FOOD_PROPERTIES = new FoodProperties(0, 0, false);
public static final Consumable DEFAULT_CONSUMABLE_PROPERTIES = new Consumable(1.6F, ItemUseAnimation.EAT, null, true, List.of());
public static final Consumable DEFAULT_CONSUMABLE_PROPERTIES = new Consumable(1.6F, ItemUseAnimation.EAT, SoundEvents.GENERIC_EAT, true, List.of());

private ItemStackData() {
}
Expand All @@ -100,14 +96,6 @@ public static void register(final DataProviderRegistrator registrator) {
// TODO DataComponents.RECIPES - for Items.KNOWLEDGE_BOOK
// TODO DataComponents.OMINOUS_BOTTLE_AMPLIFIER 1.21

// TODO rework applicable potion effects to consume effects
final var applicablePotionEffects = Keys.APPLICABLE_POTION_EFFECTS;
ConsumeEffect newPotionEffects = new ApplyStatusEffectsConsumeEffect(List.of());
ConsumeEffect teleportRand = new TeleportRandomlyConsumeEffect(5);
ConsumeEffect removeStatusEffects = new RemoveStatusEffectsConsumeEffect(HolderSet.empty());
ConsumeEffect clearStatusEffects = new ClearAllStatusEffectsConsumeEffect();
ConsumeEffect playSoundEffect = new PlaySoundConsumeEffect(Holder.direct(null));

registrator
.asMutable(ItemStack.class)
.create(Keys.BURN_TIME)
Expand Down Expand Up @@ -252,6 +240,20 @@ public static void register(final DataProviderRegistrator registrator) {
})
.set((h, v) -> h.update(DataComponents.CONSUMABLE, DEFAULT_CONSUMABLE_PROPERTIES,
c -> new Consumable(v.ticks() / 20f, c.animation(), c.sound(), c.hasConsumeParticles(), c.onConsumeEffects())))
.create(Keys.CONSUME_ACTIONS)
.get(h -> {
final var consumable = h.get(DataComponents.CONSUMABLE);
return consumable == null ? null : (List<ItemAction>) (Object) consumable.onConsumeEffects();
})
.set((h, v) -> h.update(DataComponents.CONSUMABLE, DEFAULT_CONSUMABLE_PROPERTIES,
c -> new Consumable(c.consumeSeconds(), c.animation(), c.sound(), c.hasConsumeParticles(), (List<ConsumeEffect>) (Object) v)))
.create(Keys.DEATH_PROTECTION_ACTIONS)
.get(h -> {
final var deathProtection = h.get(DataComponents.DEATH_PROTECTION);
return deathProtection == null ? null : (List<ItemAction>) (Object) deathProtection.deathEffects();
})
.set((h, v) -> h.set(DataComponents.DEATH_PROTECTION, new DeathProtection((List<ConsumeEffect>) (Object) v)))
.delete(h -> h.remove(DataComponents.DEATH_PROTECTION))
.create(Keys.FOOD_CONVERTS_TO)
.get(h -> {
final var remainder = h.get(DataComponents.USE_REMAINDER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.item;

import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.tags.TagKey;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
import net.minecraft.world.item.consume_effects.ClearAllStatusEffectsConsumeEffect;
import net.minecraft.world.item.consume_effects.PlaySoundConsumeEffect;
import net.minecraft.world.item.consume_effects.RemoveStatusEffectsConsumeEffect;
import net.minecraft.world.item.consume_effects.TeleportRandomlyConsumeEffect;
import org.spongepowered.api.data.type.ItemAction;
import org.spongepowered.api.effect.potion.PotionEffect;
import org.spongepowered.api.effect.potion.PotionEffectType;
import org.spongepowered.api.effect.sound.SoundType;
import org.spongepowered.api.tag.Tag;

import java.util.List;
import java.util.Objects;
import java.util.Set;

public class SpongeItemActionFactory implements ItemAction.Factory {

@Override
public ItemAction.ApplyEffects applyEffects(final double chance, final List<PotionEffect> effects) {
Objects.requireNonNull(effects, "effects");
if (chance < 0 || chance > 1) {
throw new IllegalArgumentException("chance must be in range [0; 1]: " + chance);
}
return (ItemAction.ApplyEffects) (Object) new ApplyStatusEffectsConsumeEffect((List) List.copyOf(effects), (float) chance);
}

@Override
public ItemAction.RemoveEffects removeEffects(final Set<PotionEffectType> effectTypes) {
Objects.requireNonNull(effectTypes, "effectTypes");
final var holderSet = HolderSet.direct(
effectType -> BuiltInRegistries.MOB_EFFECT.wrapAsHolder((MobEffect) effectType),
effectTypes);
return (ItemAction.RemoveEffects) (Object) new RemoveStatusEffectsConsumeEffect(holderSet);
}

@Override
public ItemAction.RemoveEffects removeEffects(final Tag<PotionEffectType> effectTypeTag) {
Objects.requireNonNull(effectTypeTag, "effectTypeTag");
final var tag = (TagKey<MobEffect>) (Object) effectTypeTag;
final var holderSet = BuiltInRegistries.MOB_EFFECT.get(tag).map(hs -> (HolderSet<MobEffect>) hs).orElse(HolderSet.empty());
return (ItemAction.RemoveEffects) (Object) new RemoveStatusEffectsConsumeEffect(holderSet);
}

@Override
public ItemAction.ClearEffects clearEffects() {
return (ItemAction.ClearEffects) (Object) ClearAllStatusEffectsConsumeEffect.INSTANCE;
}

@Override
public ItemAction.PlaySound playSound(final SoundType soundType) {
Objects.requireNonNull(soundType, "soundType");
return (ItemAction.PlaySound) (Object) new PlaySoundConsumeEffect(BuiltInRegistries.SOUND_EVENT.wrapAsHolder((SoundEvent) (Object) soundType));
}

@Override
public ItemAction.TeleportRandomly teleportRandomly(final double distance) {
if (distance <= 0) {
throw new IllegalArgumentException("distance must be positive: " + distance);
}
return (ItemAction.TeleportRandomly) (Object) new TeleportRandomlyConsumeEffect((float) distance * 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ public static net.minecraft.world.item.ItemStack fromLikeToNative(@Nullable Item
return itemStack == null ? ItemStackUtil.emptyNative() : itemStack.isEmpty() ? ItemStackUtil.emptyNative() : ItemStackUtil.toNative(itemStack.asMutable());
}

public static net.minecraft.world.item.ItemStack fromLikeToNativeCopy(@Nullable ItemStackLike itemStack) {
return itemStack == null ? ItemStackUtil.emptyNative() : itemStack.isEmpty() ? ItemStackUtil.emptyNative() : ItemStackUtil.toNative(itemStack.asMutableCopy());
}

public static ItemStack empty() {
return ItemStackUtil.fromNative(net.minecraft.world.item.ItemStack.EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.spongepowered.api.command.registrar.tree.CommandTreeNode;
import org.spongepowered.api.command.selector.Selector;
import org.spongepowered.api.data.DataManipulator;
import org.spongepowered.api.data.type.ItemAction;
import org.spongepowered.api.data.type.ToolRule;
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.effect.ForwardingViewer;
Expand Down Expand Up @@ -126,6 +127,7 @@
import org.spongepowered.common.event.SpongeEventListenerRegistration;
import org.spongepowered.common.event.cause.entity.damage.SpongeDamageStepType;
import org.spongepowered.common.event.tracking.BlockChangeFlagManager;
import org.spongepowered.common.item.SpongeItemActionFactory;
import org.spongepowered.common.item.SpongeItemStack;
import org.spongepowered.common.item.SpongeItemStackSnapshot;
import org.spongepowered.common.item.SpongeToolRuleFactory;
Expand Down Expand Up @@ -285,6 +287,7 @@ public void registerDefaultFactories() {
.registerFactory(NaturalSpawner.Factory.class, new SpongeNaturalSpawnerFactory())
.registerFactory(ScoreFormat.Factory.class, new SpongeScoreFormatFactory())
.registerFactory(ToolRule.Factory.class, new SpongeToolRuleFactory())
.registerFactory(ItemAction.Factory.class, new SpongeItemActionFactory())
.registerFactory(PortalLogic.Factory.class, new SpongePortalLogicFactory())
.registerFactory(RecipeInput.Factory.class, new SpongeRecipeInputFactory())
.registerFactory(ArmorTrim.Factory.class, new SpongeArmorTrimFactory())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.mixin.api.minecraft.world.item.consume_effects;

import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
import org.spongepowered.api.data.type.ItemAction;
import org.spongepowered.api.effect.potion.PotionEffect;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import java.util.List;

@Mixin(ApplyStatusEffectsConsumeEffect.class)
@Implements(@Interface(iface = ItemAction.ApplyEffects.class, prefix = "consumeEffect$"))
public abstract class ApplyStatusEffectsConsumeEffectMixin_API implements ItemAction.ApplyEffects {

@Shadow @Final private float probability;
@Shadow @Final private List<MobEffectInstance> effects;

@Override
public double chance() {
return this.probability;
}

@Intrinsic
public List<PotionEffect> consumeEffect$effects() {
return (List) this.effects;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.mixin.api.minecraft.world.item.consume_effects;

import net.minecraft.world.item.consume_effects.ClearAllStatusEffectsConsumeEffect;
import org.spongepowered.api.data.type.ItemAction;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(ClearAllStatusEffectsConsumeEffect.class)
public abstract class ClearAllStatusEffectsConsumeEffectMixin_API implements ItemAction.ClearEffects {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.mixin.api.minecraft.world.item.consume_effects;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.consume_effects.ConsumeEffect;
import net.minecraft.world.level.Level;
import org.spongepowered.api.data.type.ItemAction;
import org.spongepowered.api.data.type.ItemActionType;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.item.util.ItemStackUtil;

import java.util.Objects;

@Mixin(ConsumeEffect.class)
public interface ConsumeEffectMixin_API extends ItemAction {

@Shadow ConsumeEffect.Type<? extends ConsumeEffect> shadow$getType();
@Shadow boolean shadow$apply(Level level, ItemStack itemStack, LivingEntity livingEntity);

@Override
default ItemActionType type() {
return (ItemActionType) (Object) this.shadow$getType();
}

@Override
default boolean apply(final Living entity, final ItemStackLike stack) {
Objects.requireNonNull(entity, "entity");
return this.shadow$apply(
(Level) entity.world(),
ItemStackUtil.fromLikeToNativeCopy(stack),
(LivingEntity) entity);
}
}
Loading
Loading