Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity Lifespan API #3713

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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 @@ -18,12 +18,15 @@

import net.minecraft.component.type.AttributeModifiersComponent;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
import net.minecraft.world.World;

import net.fabricmc.fabric.impl.item.FabricItemInternals;
import net.fabricmc.fabric.mixin.item.ItemEntityAccessor;

/**
* General-purpose Fabric-provided extensions for {@link Item} subclasses.
Expand Down Expand Up @@ -119,15 +122,29 @@ default ItemStack getRecipeRemainder(ItemStack stack) {
* <p>Note that this method is only called <em>after</em> the {@link EnchantmentEvents#ALLOW_ENCHANTING} event, and
* only if none of the listeners to that event override the result.</p>
*
* @param stack the current stack
* @param stack the current stack
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert those changes

* @param enchantment the enchantment to check
* @param context the context in which the enchantment is being checked
* @param context the context in which the enchantment is being checked
* @return whether the enchantment is allowed to apply to the stack
*/
default boolean canBeEnchantedWith(ItemStack stack, Enchantment enchantment, EnchantingContext context) {
return enchantment.isAcceptableItem(stack);
}

/**
* Retrieves the {@link ItemEntityAccessor#getDespawnAge() default spawn age} of 6000 ticks or 5 minutes of this item when it is dropped on the ground
* as an {@link ItemEntity}.
* <p>
* This is in ticks.
*
* @param stack the current {@link ItemStack}
* @param world the world the entity is in
* @return the normal lifespan in ticks
*/
default int getEntityLifespan(ItemStack stack, ItemEntity entity, World world) {
return ItemEntityAccessor.getDespawnAge();
}

/**
* Fabric-provided extensions for {@link Item.Settings}.
* This interface is automatically implemented on all item settings via Mixin and interface injection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package net.fabricmc.fabric.api.item.v1;

import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

import net.fabricmc.fabric.api.util.TriState;

Expand Down Expand Up @@ -47,7 +49,7 @@ default ItemStack getRecipeRemainder() {
* {@link Enchantment#isAcceptableItem(ItemStack)}</p>
*
* @param enchantment the enchantment to check
* @param context the context in which the enchantment is being checked
* @param context the context in which the enchantment is being checked
* @return whether the enchantment is allowed to apply to the stack
* @see FabricItem#canBeEnchantedWith(ItemStack, Enchantment, EnchantingContext)
*/
Expand All @@ -59,4 +61,15 @@ default boolean canBeEnchantedWith(Enchantment enchantment, EnchantingContext co
);
return result.orElseGet(() -> ((ItemStack) this).getItem().canBeEnchantedWith((ItemStack) this, enchantment, context));
}

/**
* Retrieves the normal 'lifespan' of this item when it is dropped on the ground
* as an {@link net.minecraft.entity.ItemEntity}. This is in ticks. The standard result is 6000 ticks, or 5 minutes.
*
* @param world the world the entity is in
* @return the normal lifespan in ticks
*/
default int getEntityLifespan(ItemEntity entity, World world) {
return ((ItemStack) this).getItem().getEntityLifespan(((ItemStack) this), entity, world);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.item;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.entity.ItemEntity;

@Mixin(ItemEntity.class)
public interface ItemEntityAccessor {
@Accessor("DESPAWN_AGE")
static int getDespawnAge() {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.item;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

@Mixin(ItemEntity.class)
public abstract class ItemEntityMixin extends Entity {

@Shadow
private int itemAge;

private ItemEntityMixin(EntityType<?> type, World world) {
super(type, world);
}

@Inject(at = @At("TAIL"), method = "setDespawnImmediately")
private void modifyDespawnImmediately(CallbackInfo ci) {
this.itemAge = getStack().getEntityLifespan((ItemEntity) ((Entity) this), this.getWorld());
}

@Shadow
abstract ItemStack getStack();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"EnchantCommandMixin",
"EnchantmentHelperMixin",
"EnchantRandomlyLootFunctionMixin",
"ItemEntityAccessor",
"ItemEntityMixin",
"ItemAccessor",
"ItemMixin",
"ItemSettingsMixin",
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ fabric-transfer-api-v1-version=5.1.6
fabric-transitive-access-wideners-v1-version=6.0.10
fabric-convention-tags-v1-version=2.0.4
fabric-convention-tags-v2-version=2.1.0
fabric-client-tags-api-v1-version=1.1.12
fabric-client-tags-api-v1-version=1.1.12