diff --git a/common/src/main/java/me/shedaniel/architectury/extensions/ItemExtension.java b/common/src/main/java/me/shedaniel/architectury/extensions/ItemExtension.java new file mode 100644 index 00000000..4aa8ee7f --- /dev/null +++ b/common/src/main/java/me/shedaniel/architectury/extensions/ItemExtension.java @@ -0,0 +1,50 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package me.shedaniel.architectury.extensions; + +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.Nullable; + +/** + * Extensions to {@link net.minecraft.world.item.Item}, implement this on to your item. + */ +public interface ItemExtension { + /** + * Invoked every tick when this item is equipped. + * + * @param stack the item stack + * @param player the player wearing the armor + */ + default void tickArmor(ItemStack stack, Player player) { + } + + /** + * Returns the {@link EquipmentSlot} for {@link ItemStack}. + * + * @param stack the item stack + * @return the {@link EquipmentSlot}, return {@code null} to default to vanilla's {@link net.minecraft.world.entity.Mob#getEquipmentSlotForItem(ItemStack)} + */ + @Nullable + default EquipmentSlot getCustomEquipmentSlot(ItemStack stack) { + return null; + } +} diff --git a/common/src/main/java/me/shedaniel/architectury/registry/entity/EntityRenderers.java b/common/src/main/java/me/shedaniel/architectury/registry/entity/EntityRenderers.java index 336d3dc4..97cf471f 100644 --- a/common/src/main/java/me/shedaniel/architectury/registry/entity/EntityRenderers.java +++ b/common/src/main/java/me/shedaniel/architectury/registry/entity/EntityRenderers.java @@ -1,3 +1,22 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + package me.shedaniel.architectury.registry.entity; import me.shedaniel.architectury.annotations.ExpectPlatform; diff --git a/fabric/src/main/java/me/shedaniel/architectury/hooks/fabric/EntityHooksImpl.java b/fabric/src/main/java/me/shedaniel/architectury/hooks/fabric/EntityHooksImpl.java index 39739848..0cd8781b 100644 --- a/fabric/src/main/java/me/shedaniel/architectury/hooks/fabric/EntityHooksImpl.java +++ b/fabric/src/main/java/me/shedaniel/architectury/hooks/fabric/EntityHooksImpl.java @@ -30,12 +30,12 @@ public class EntityHooksImpl { @Nullable public static Entity fromCollision(CollisionContext ctx) { - return ((CollisionContextExtension) ctx).getEntity(); + return ((CollisionContextExtension) ctx).arch$getEntity(); } public interface CollisionContextExtension { @Nullable - default Entity getEntity() { + default Entity arch$getEntity() { return null; } } diff --git a/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinCollisionContext.java b/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinCollisionContext.java index cb79fb48..b0ac92ed 100644 --- a/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinCollisionContext.java +++ b/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinCollisionContext.java @@ -1,3 +1,22 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + package me.shedaniel.architectury.mixin.fabric; import me.shedaniel.architectury.hooks.fabric.EntityHooksImpl; diff --git a/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinEntityCollisionContext.java b/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinEntityCollisionContext.java index ed51c31c..ca38d210 100644 --- a/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinEntityCollisionContext.java +++ b/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinEntityCollisionContext.java @@ -1,3 +1,22 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + package me.shedaniel.architectury.mixin.fabric; import me.shedaniel.architectury.hooks.fabric.EntityHooksImpl; @@ -25,7 +44,7 @@ public abstract class MixinEntityCollisionContext implements CollisionContext, E @Nullable @Override - public Entity getEntity() { + public Entity arch$getEntity() { return entity; } diff --git a/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinInventory.java b/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinInventory.java new file mode 100644 index 00000000..098fd955 --- /dev/null +++ b/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinInventory.java @@ -0,0 +1,52 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package me.shedaniel.architectury.mixin.fabric; + +import me.shedaniel.architectury.extensions.ItemExtension; +import net.minecraft.core.NonNullList; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import org.spongepowered.asm.mixin.Final; +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; + +@Mixin(Inventory.class) +public class MixinInventory { + @Shadow + @Final + public NonNullList armor; + + @Shadow + @Final + public Player player; + + @Inject(method = "tick", at = @At("RETURN")) + private void updateItems(CallbackInfo ci) { + for (ItemStack stack : armor) { + if (stack.getItem() instanceof ItemExtension) { + ((ItemExtension) stack.getItem()).tickArmor(stack, player); + } + } + } +} diff --git a/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinMob.java b/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinMob.java new file mode 100644 index 00000000..a785d67f --- /dev/null +++ b/fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/MixinMob.java @@ -0,0 +1,44 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package me.shedaniel.architectury.mixin.fabric; + +import me.shedaniel.architectury.extensions.ItemExtension; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(Mob.class) +public class MixinMob { + @Inject(method = "getEquipmentSlotForItem", at = @At("HEAD"), cancellable = true) + private static void getEquipmentSlotForItem(ItemStack stack, CallbackInfoReturnable cir) { + Item item = stack.getItem(); + if (item instanceof ItemExtension) { + EquipmentSlot slot = ((ItemExtension) item).getCustomEquipmentSlot(stack); + if (slot != null) { + cir.setReturnValue(slot); + } + } + } +} diff --git a/fabric/src/main/java/me/shedaniel/architectury/registry/entity/fabric/EntityRenderersImpl.java b/fabric/src/main/java/me/shedaniel/architectury/registry/entity/fabric/EntityRenderersImpl.java index 96830f6c..20eec624 100644 --- a/fabric/src/main/java/me/shedaniel/architectury/registry/entity/fabric/EntityRenderersImpl.java +++ b/fabric/src/main/java/me/shedaniel/architectury/registry/entity/fabric/EntityRenderersImpl.java @@ -1,3 +1,22 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + package me.shedaniel.architectury.registry.entity.fabric; import net.fabricmc.fabric.api.client.rendereregistry.v1.EntityRendererRegistry; diff --git a/fabric/src/main/resources/architectury.mixins.json b/fabric/src/main/resources/architectury.mixins.json index f7607c32..1807f492 100644 --- a/fabric/src/main/resources/architectury.mixins.json +++ b/fabric/src/main/resources/architectury.mixins.json @@ -19,30 +19,13 @@ "client.MixinTextureAtlas" ], "mixins": [ - "ExplosionPreInvoker", - "LivingDeathInvoker", - "MixinBlockEntityExtension", - "MixinBlockItem", - "MixinCollisionContext", - "MixinCommands", - "MixinDedicatedServer", - "MixinEntityCollisionContext", - "MixinExplosion", - "MixinFurnaceResultSlot", - "MixinItemEntity", - "MixinLivingEntity", - "MixinPlayer", - "MixinPlayerAdvancements", - "MixinPlayerList", - "MixinResultSlot", - "MixinServerGamePacketListenerImpl", - "MixinServerLevel", - "MixinServerPlayer", - "MixinServerPlayerGameMode", - "PlayerAttackInvoker" + "ExplosionPreInvoker", "LivingDeathInvoker", "MixinBlockEntityExtension", "MixinBlockItem", "MixinCollisionContext", "MixinCommands", + "MixinDedicatedServer", "MixinEntityCollisionContext", "MixinExplosion", "MixinFurnaceResultSlot", "MixinInventory", "MixinItemEntity", + "MixinLivingEntity", "MixinMob", "MixinPlayer", "MixinPlayerAdvancements", "MixinPlayerList", "MixinResultSlot", "MixinServerGamePacketListenerImpl", + "MixinServerLevel", "MixinServerPlayer", "MixinServerPlayerGameMode", "PlayerAttackInvoker" ], "injectors": { "maxShiftBy": 5, "defaultRequire": 1 - } + } } diff --git a/forge/src/main/java/me/shedaniel/architectury/mixin/forge/MixinItemExtension.java b/forge/src/main/java/me/shedaniel/architectury/mixin/forge/MixinItemExtension.java new file mode 100644 index 00000000..8e07115e --- /dev/null +++ b/forge/src/main/java/me/shedaniel/architectury/mixin/forge/MixinItemExtension.java @@ -0,0 +1,43 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package me.shedaniel.architectury.mixin.forge; + +import me.shedaniel.architectury.extensions.ItemExtension; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraftforge.common.extensions.IForgeItem; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(ItemExtension.class) +public interface MixinItemExtension extends IForgeItem { + @Override + default void onArmorTick(ItemStack stack, Level world, Player player) { + ((ItemExtension) this).tickArmor(stack, player); + } + + @Nullable + @Override + default EquipmentSlot getEquipmentSlot(ItemStack stack) { + return ((ItemExtension) this).getCustomEquipmentSlot(stack); + } +} diff --git a/forge/src/main/java/me/shedaniel/architectury/registry/entity/forge/EntityRenderersImpl.java b/forge/src/main/java/me/shedaniel/architectury/registry/entity/forge/EntityRenderersImpl.java index 8be995d3..a2401130 100644 --- a/forge/src/main/java/me/shedaniel/architectury/registry/entity/forge/EntityRenderersImpl.java +++ b/forge/src/main/java/me/shedaniel/architectury/registry/entity/forge/EntityRenderersImpl.java @@ -1,3 +1,22 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + package me.shedaniel.architectury.registry.entity.forge; import net.minecraft.client.renderer.entity.EntityRenderDispatcher; diff --git a/forge/src/main/resources/architectury.mixins.json b/forge/src/main/resources/architectury.mixins.json index 383b59e3..277733fc 100644 --- a/forge/src/main/resources/architectury.mixins.json +++ b/forge/src/main/resources/architectury.mixins.json @@ -7,8 +7,9 @@ "client": [ ], "mixins": [ - "BiomeGenerationSettingsBuilderAccessor", "MixinRegistryEntry", "MixinBlockEntity", "MixinBlockEntityExtension", - "MobSpawnSettingsBuilderAccessor", "GameRulesAccessor", "GameRulesAccessor$BooleanValue", "GameRulesAccessor$BooleanValueSimple", "GameRulesAccessor$IntegerValue", "GameRulesAccessor$IntegerValueSimple" + "BiomeGenerationSettingsBuilderAccessor", "GameRulesAccessor", "GameRulesAccessor$BooleanValue", "GameRulesAccessor$BooleanValueSimple", + "GameRulesAccessor$IntegerValue", "GameRulesAccessor$IntegerValueSimple", "MixinBlockEntity", "MixinBlockEntityExtension", "MixinItemExtension", + "MixinRegistryEntry", "MobSpawnSettingsBuilderAccessor" ], "injectors": { "defaultRequire": 1 diff --git a/testmod-common/src/main/java/me/shedaniel/architectury/test/registry/TestRegistries.java b/testmod-common/src/main/java/me/shedaniel/architectury/test/registry/TestRegistries.java index a0d789c6..f4234efb 100644 --- a/testmod-common/src/main/java/me/shedaniel/architectury/test/registry/TestRegistries.java +++ b/testmod-common/src/main/java/me/shedaniel/architectury/test/registry/TestRegistries.java @@ -24,6 +24,7 @@ import me.shedaniel.architectury.registry.BlockProperties; import me.shedaniel.architectury.registry.DeferredRegister; import me.shedaniel.architectury.registry.RegistrySupplier; import me.shedaniel.architectury.test.TestMod; +import me.shedaniel.architectury.test.registry.objects.EquippableTickingItem; import me.shedaniel.architectury.test.tab.TestCreativeTabs; import net.minecraft.core.BlockPos; import net.minecraft.core.Registry; @@ -44,6 +45,8 @@ public class TestRegistries { public static final RegistrySupplier TEST_ITEM = ITEMS.register("test_item", () -> new Item(new Item.Properties().tab(TestCreativeTabs.TEST_TAB))); + public static final RegistrySupplier TEST_EQUIPPABLE = ITEMS.register("test_eqippable", () -> + new EquippableTickingItem(new Item.Properties().tab(TestCreativeTabs.TEST_TAB))); public static final RegistrySupplier TEST_BLOCK = BLOCKS.register("test_block", () -> new Block(BlockProperties.copy(Blocks.STONE))); diff --git a/testmod-common/src/main/java/me/shedaniel/architectury/test/registry/objects/EquippableTickingItem.java b/testmod-common/src/main/java/me/shedaniel/architectury/test/registry/objects/EquippableTickingItem.java new file mode 100644 index 00000000..c1fe3b37 --- /dev/null +++ b/testmod-common/src/main/java/me/shedaniel/architectury/test/registry/objects/EquippableTickingItem.java @@ -0,0 +1,46 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package me.shedaniel.architectury.test.registry.objects; + +import me.shedaniel.architectury.extensions.ItemExtension; +import me.shedaniel.architectury.test.TestMod; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.Nullable; + +public class EquippableTickingItem extends Item implements ItemExtension { + public EquippableTickingItem(Properties properties) { + super(properties); + } + + @Override + public void tickArmor(ItemStack stack, Player player) { + TestMod.SINK.accept("Ticking " + new TranslatableComponent(stack.getDescriptionId()).getString()); + } + + @Nullable + @Override + public EquipmentSlot getCustomEquipmentSlot(ItemStack stack) { + return EquipmentSlot.HEAD; + } +}