Skip to content

Commit

Permalink
Item api rewrites (#30)
Browse files Browse the repository at this point in the history
* made armor api auto implement the armor item interfaces

* changed playeritem api to use interface injection

* combined 3 interfaces in armor api into 1

* rewrote remaining item apis

* injectedd method into Enchantment

* bumped versions of changed modules
  • Loading branch information
Trinsdar committed Jan 14, 2024
1 parent e038423 commit bb4421c
Show file tree
Hide file tree
Showing 47 changed files with 238 additions and 210 deletions.
1 change: 1 addition & 0 deletions felt-armor-api/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodule_version=1.1.0
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public interface ArmorEquipItem {
public interface ArmorItem {
/**
* Called to tick armor in the armor slot. Override to do something
*/
default void onArmorTick(ItemStack stack, Level level, Player player){

}

/**
* Determines if the specific ItemStack can be placed in the specified armor
Expand All @@ -16,8 +24,19 @@ public interface ArmorEquipItem {
* @param entity The entity trying to equip the armor
* @return True if the given ItemStack can be inserted in the slot
*/
default boolean canEquip(ItemStack stack, EquipmentSlot armorType, Entity entity)
{
default boolean canEquip(ItemStack stack, EquipmentSlot armorType, Entity entity) {
return Mob.getEquipmentSlotForItem(stack) == armorType;
}

/**
* Called every tick from {@code Horse#playGallopSound(SoundEvent)} on the item in the
* armor slot.
*
* @param stack the armor itemstack
* @param level the level the horse is in
* @param horse the horse wearing this armor
*/
default void onHorseArmorTick(ItemStack stack, Level level, Mob horse){

}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.feltmc.feltapi.mixin.armor;

import net.feltmc.feltapi.api.armor.HorseArmorTickItem;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.animal.horse.AbstractHorse;
import net.minecraft.world.entity.animal.horse.Horse;
Expand All @@ -24,6 +23,6 @@ protected HorseEntityMixin(EntityType<? extends AbstractHorse> entityType, Level
@Inject(method = "playGallopSound", at = @At("TAIL"))
private void injectPlayWalkSound(SoundType group, CallbackInfo ci){
ItemStack stack = this.inventory.getItem(1);
if (isArmor(stack) && stack.getItem() instanceof HorseArmorTickItem extension) extension.onHorseArmorTick(stack, this.level, this);
if (isArmor(stack)) stack.getItem().onHorseArmorTick(stack, this.level, this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.feltmc.feltapi.mixin.armor;

import net.feltmc.feltapi.api.armor.ArmorItem;
import net.minecraft.world.item.Item;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(Item.class)
public class ItemMixin implements ArmorItem {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.feltmc.feltapi.mixin.armor;


import net.feltmc.feltapi.api.armor.ArmorTickItem;
import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
Expand All @@ -22,7 +21,7 @@ public class PlayerInventoryMixin {
@Inject(method = "tick", at = @At("TAIL"))
private void injectOnArmorTick(CallbackInfo c){
armor.forEach(e -> {
if (e.getItem() instanceof ArmorTickItem extension) extension.onArmorTick(e, player.level, player);
e.getItem().onArmorTick(e, player.level, player);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.feltmc.feltapi.mixin.armor;

import net.feltmc.feltapi.api.armor.ArmorEquipItem;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.item.ItemStack;
Expand All @@ -22,7 +21,6 @@ public class PlayerScreenHandlerMixin {

@Inject(method = "mayPlace", at = @At("HEAD"))
private void injectCanEquip(ItemStack stack, CallbackInfoReturnable<Boolean> info){
if (stack.getItem() instanceof ArmorEquipItem extension)
info.setReturnValue(extension.canEquip(stack, val$slot, ((PlayerScreenHandlerAccessor) field_7833).getOwner()));
info.setReturnValue(stack.getItem().canEquip(stack, val$slot, ((PlayerScreenHandlerAccessor) field_7833).getOwner()));
}
}
1 change: 1 addition & 0 deletions felt-armor-api/src/main/resources/armorapi.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"mixins": [
"HorseEntityMixin",
"ItemMixin",
"PlayerInventoryMixin",
"PlayerScreenHandlerAccessor",
"PlayerScreenHandlerMixin"
Expand Down
5 changes: 5 additions & 0 deletions felt-armor-api/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"library"
],
"parent": "felt-api"
},
"loom:injected_interfaces": {
"net/minecraft/class_1792": [
"net/feltmc/feltapi/api/armor/ArmorItem"
]
}
}
}
3 changes: 2 additions & 1 deletion felt-enchanting-api/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mixin_extras=true
mixin_extras=true
module_version=1.1.0

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;

public interface EnchantabilityItem {
public interface EnchantingItem {
/**
* ItemStack sensitive version of getEnchantability
*
Expand Down Expand Up @@ -32,4 +32,16 @@ default boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantme
{
return enchantment.category.canEnchant(stack.getItem());
}

/**
* Allow or forbid the specific book/item combination as an anvil enchant
*
* @param stack The item
* @param book The book
* @return if the enchantment is allowed
*/
default boolean isBookEnchantable(ItemStack stack, ItemStack book)
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.feltmc.feltapi.api.enchanting;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;

public interface EnchantmentExtension {
default boolean canApplyAtEnchantingTable(ItemStack stack) {
return stack.getItem().canApplyAtEnchantingTable(stack, (Enchantment) this);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.feltmc.feltapi.mixin.enchanting;

import net.feltmc.feltapi.api.enchanting.BookEnchantableItem;
import net.minecraft.world.inventory.AnvilMenu;
import net.minecraft.world.item.EnchantedBookItem;
import net.minecraft.world.item.ItemStack;
Expand All @@ -17,12 +16,10 @@
public class AnvilScreenHandlerMixin {
@Inject(method = "createResult", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/DataSlot;set(I)V", ordinal = 5), locals = LocalCapture.CAPTURE_FAILHARD)
private void injectBookCheck(CallbackInfo ci, ItemStack itemStack, int i, int j, int k, ItemStack itemStack2, ItemStack itemStack3, Map map){
if (itemStack2.getItem() instanceof BookEnchantableItem item){
if (itemStack3.is(Items.ENCHANTED_BOOK) &&
!EnchantedBookItem.getEnchantments(itemStack3).isEmpty() &&
!item.isBookEnchantable(itemStack2, itemStack3)) {
itemStack2 .setCount(0);
}
if (itemStack3.is(Items.ENCHANTED_BOOK) &&
!EnchantedBookItem.getEnchantments(itemStack3).isEmpty() &&
!itemStack2.getItem().isBookEnchantable(itemStack2, itemStack3)) {
itemStack2.setCount(0);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.feltmc.feltapi.api.enchanting.EnchantabilityItem;
import net.feltmc.feltapi.api.enchanting.EnchantingItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
Expand All @@ -16,11 +16,11 @@ public class EnchantmentHelperMixin {

@WrapOperation(method = "getEnchantmentCost", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/Item;getEnchantmentValue()I"))
private static int hookModifiedExperience(Item instance, Operation<Integer> operation, Random random, int slotIndex, int bookshelfCount, ItemStack stack){
return instance instanceof EnchantabilityItem extension ? extension.getEnchantability(stack) : operation.call(instance);
return stack.getItem().getEnchantability(stack);
}

@WrapOperation(method = "selectEnchantment", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/Item;getEnchantmentValue()I"))
private static int hookModifiedExperience2(Item instance, Operation<Integer> operation, Random random, ItemStack stack, int level, boolean treasureAllowed){
return instance instanceof EnchantabilityItem extension ? extension.getEnchantability(stack) : operation.call(instance);
return stack.getItem().getEnchantability(stack);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package net.feltmc.feltapi.mixin.enchanting;


import net.feltmc.feltapi.api.enchanting.EnchantabilityItem;
import net.feltmc.feltapi.api.enchanting.EnchantingItem;
import net.feltmc.feltapi.api.enchanting.EnchantmentExtension;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -10,11 +11,9 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Enchantment.class)
public class EnchantmentMixin {
public class EnchantmentMixin implements EnchantmentExtension {
@Inject(method = "canEnchant", at = @At("HEAD"), cancellable = true)
public void injectEnchantmentCheck(ItemStack stack, CallbackInfoReturnable<Boolean> callback){
if (stack.getItem() instanceof EnchantabilityItem extension){
callback.setReturnValue(extension.canApplyAtEnchantingTable(stack, (Enchantment) (Object)this));
}
callback.setReturnValue(this.canApplyAtEnchantingTable(stack));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.feltmc.feltapi.mixin.enchanting;

import net.minecraft.world.item.Item;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(Item.class)
public class ItemMixin {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"mixins": [
"AnvilScreenHandlerMixin",
"EnchantmentHelperMixin",
"EnchantmentMixin"
"EnchantmentMixin",
"ItemMixin"
]
}
10 changes: 10 additions & 0 deletions felt-enchanting-api/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,15 @@
},
"recommends": {
"feltfabric": "*"
},
"custom": {
"loom:injected_interfaces": {
"net/minecraft/class_1792": [
"net/feltmc/feltapi/api/enchanting/EnchantingItem"
],
"net/minecraft/class_1887": [
"net/feltmc/feltapi/api/enchanting/EnchantmentExtension"
]
}
}
}
3 changes: 2 additions & 1 deletion felt-entity-item-api/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mixin_extras=true
mixin_extras=true
module_version=1.1.0
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ItemEntityMixin(EntityType<?> type, Level world) {

@Inject(method = "<init>(Lnet/minecraft/world/level/Level;DDDLnet/minecraft/world/item/ItemStack;DDD)V", at = @At("TAIL"))
private void injectInit(Level world, double x, double y, double z, ItemStack stack, double velocityX, double velocityY, double velocityZ, CallbackInfo ci){
this.lifespan = !stack.isEmpty() && stack.getItem() instanceof EntityCustomItem feltItem ? feltItem.getEntityLifespan(stack, world) : 6000;
this.lifespan = stack.getItem().getEntityLifespan(stack, world);
}

@ModifyConstant(method = "tick", constant = @Constant(intValue = 6000))
Expand All @@ -43,16 +43,11 @@ private int redirectConstant(int constant){

@ModifyConstant(method = "makeFakeItem", constant = @Constant(intValue = 5999))
private int redirectSetDespawnImmediately(int constant){
if (getItem().getItem() instanceof EntityCustomItem customItem) return customItem.getEntityLifespan(getItem(), this.level) - 1;
return constant;
return getItem().getItem().getEntityLifespan(getItem(), this.level) - 1;
}

@WrapOperation(method = "hurt",at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;onDestroyed(Lnet/minecraft/world/entity/item/ItemEntity;)V"))
private void wrapOnItemEntityDestroyed(ItemStack instance, ItemEntity entity, Operation<Void> original, DamageSource source, float amount){
if (instance.getItem() instanceof EntityCustomItem customItem){
customItem.onItemEntityDestroyed(entity, source);
return;
}
original.call(instance, entity);
instance.getItem().onItemEntityDestroyed(entity, source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.feltmc.feltapi.mixin.entityitem;

import net.feltmc.feltapi.api.entityitem.EntityCustomItem;
import net.minecraft.world.item.Item;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(Item.class)
public class ItemMixin implements EntityCustomItem {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"package": "net.feltmc.feltapi.mixin.entityitem",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ItemEntityMixin"
"ItemEntityMixin",
"ItemMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
7 changes: 7 additions & 0 deletions felt-entity-item-api/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@
},
"recommends": {
"feltfabric": "*"
},
"custom": {
"loom:injected_interfaces": {
"net/minecraft/class_1792": [
"net/feltmc/feltapi/api/entityitem/EntityCustomItem"
]
}
}
}
3 changes: 2 additions & 1 deletion felt-mob-item-api/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mixin_extras=true
mixin_extras=true
module_version=1.1.0
Loading

0 comments on commit bb4421c

Please sign in to comment.