Fixed not being able to right-click to equip amulets

This commit is contained in:
Sollace 2021-02-27 18:42:29 +02:00
parent 63e61826e8
commit 0221451543
4 changed files with 65 additions and 107 deletions

View file

@ -16,14 +16,7 @@ import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.item.ArmorMaterials;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Wearable;
import net.minecraft.recipe.Ingredient;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.StringVisitable;
@ -33,7 +26,7 @@ import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.world.World;
public class AmuletItem extends Item implements Wearable {
public class AmuletItem extends WearableItem {
private final int maxEnergy;
private final float drain;
@ -45,7 +38,7 @@ public class AmuletItem extends Item implements Wearable {
}
public AmuletItem(FabricItemSettings settings, int maxEnergy, int drainRate, ImmutableMultimap.Builder<EntityAttribute, EntityAttributeModifier> modifiers) {
super(settings.equipmentSlot(s -> EquipmentSlot.CHEST));
super(settings);
this.maxEnergy = maxEnergy;
drain = ((float)drainRate / (float)maxEnergy) / 10;
@ -54,9 +47,12 @@ public class AmuletItem extends Item implements Wearable {
@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
/*if (world.isClient) {
return;
}
if (isChargable() && entity instanceof LivingEntity && ((LivingEntity) entity).getEquippedStack(EquipmentSlot.CHEST) == stack) {
consumeEnergy(stack, drain);
}
}*/
}
@Override
@ -77,6 +73,11 @@ public class AmuletItem extends Item implements Wearable {
}
}
@Override
public EquipmentSlot getPreferredSlot(ItemStack stack) {
return EquipmentSlot.CHEST;
}
@Override
public boolean hasGlint(ItemStack stack) {
return !isChargable() || stack.hasEnchantments() || getEnergy(stack) > 0;
@ -122,71 +123,4 @@ public class AmuletItem extends Item implements Wearable {
stack.getOrCreateTag().putFloat("energy", energy);
}
}
public static class Settings extends FabricItemSettings implements ArmorMaterial {
private final String name;
private int protection;
private float toughness;
private float resistance;
public Settings(String name) {
this.name = name;
}
public Settings protection(int protection) {
this.protection = protection;
return this;
}
public Settings toughness(int toughness) {
this.toughness = toughness;
return this;
}
public Settings resistance(int resistance) {
this.resistance = resistance;
return this;
}
@Override
public int getDurability(EquipmentSlot slot) {
return ArmorMaterials.LEATHER.getDurability(slot);
}
@Override
public int getProtectionAmount(EquipmentSlot slot) {
return protection;
}
@Override
public int getEnchantability() {
return 0;
}
@Override
public SoundEvent getEquipSound() {
return SoundEvents.ITEM_ARMOR_EQUIP_DIAMOND;
}
@Override
public Ingredient getRepairIngredient() {
return Ingredient.EMPTY;
}
@Override
public String getName() {
return name;
}
@Override
public float getToughness() {
return toughness;
}
@Override
public float getKnockbackResistance() {
return resistance;
}
}
}

View file

@ -8,18 +8,14 @@ import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.ability.magic.Caster;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.DispenserBlock;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.DyeableItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Wearable;
import net.minecraft.sound.SoundEvents;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
@ -29,11 +25,10 @@ import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
public class FriendshipBraceletItem extends Item implements DyeableItem, Wearable, GlowableItem {
public class FriendshipBraceletItem extends WearableItem implements DyeableItem, GlowableItem {
public FriendshipBraceletItem(Settings settings) {
public FriendshipBraceletItem(FabricItemSettings settings) {
super(settings);
DispenserBlock.registerBehavior(this, ArmorItem.DISPENSER_BEHAVIOR);
}
@Override
@ -63,22 +58,7 @@ public class FriendshipBraceletItem extends Item implements DyeableItem, Wearabl
return TypedActionResult.consume(stack);
}
EquipmentSlot slot = MobEntity.getPreferredEquipmentSlot(stack);
ItemStack currentArmor = player.getEquippedStack(slot);
if (currentArmor.isEmpty()) {
ItemStack result = stack.copy();
result.setCount(1);
if (!player.abilities.creativeMode) {
stack.decrement(1);
}
player.equipStack(slot, result);
return TypedActionResult.success(stack, world.isClient());
}
return TypedActionResult.fail(stack);
return super.use(world, player, hand);
}
@Override
@ -92,6 +72,11 @@ public class FriendshipBraceletItem extends Item implements DyeableItem, Wearabl
}
}
@Override
public EquipmentSlot getPreferredSlot(ItemStack stack) {
return isSigned(stack) ? EquipmentSlot.CHEST : super.getPreferredSlot(stack);
}
private boolean checkSignature(ItemStack stack, PlayerEntity player) {
return player.getName().asString().contentEquals(getSignature(stack));
}
@ -119,8 +104,4 @@ public class FriendshipBraceletItem extends Item implements DyeableItem, Wearabl
return false;
}
public static EquipmentSlot getPreferredEquipmentSlot(ItemStack stack) {
return isSigned(stack) ? EquipmentSlot.CHEST : EquipmentSlot.OFFHAND;
}
}

View file

@ -45,7 +45,6 @@ public interface UItems {
new FabricItemSettings()
.rarity(Rarity.UNCOMMON)
.group(ItemGroup.TOOLS)
.equipmentSlot(FriendshipBraceletItem::getPreferredEquipmentSlot)
));
Item EMPTY_JAR = register("empty_jar", new JarItem(new Item.Settings().group(ItemGroup.DECORATIONS).maxCount(16).fireproof(), false, false, false));
@ -66,7 +65,7 @@ public interface UItems {
Item GOLDEN_FEATHER = register("golden_feather", new Item(new Item.Settings().rarity(Rarity.UNCOMMON).group(ItemGroup.MATERIALS)));
Item GOLDEN_WING = register("golden_wing", new Item(new Item.Settings().rarity(Rarity.UNCOMMON).group(ItemGroup.MATERIALS)));
AmuletItem PEGASUS_AMULET = register("pegasus_amulet", new AmuletItem(new AmuletItem.Settings("pegasus_amulet")
AmuletItem PEGASUS_AMULET = register("pegasus_amulet", new AmuletItem(new FabricItemSettings()
.maxDamage(890)
.rarity(Rarity.UNCOMMON)
.group(ItemGroup.DECORATIONS), 900, 10));

View file

@ -0,0 +1,44 @@
package com.minelittlepony.unicopia.item;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.block.DispenserBlock;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Wearable;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
public abstract class WearableItem extends Item implements Wearable {
public WearableItem(FabricItemSettings settings) {
super(settings.equipmentSlot(s -> ((WearableItem)s.getItem()).getPreferredSlot(s)));
DispenserBlock.registerBehavior(this, ArmorItem.DISPENSER_BEHAVIOR);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
ItemStack stack = player.getStackInHand(hand);
EquipmentSlot slot = MobEntity.getPreferredEquipmentSlot(stack);
ItemStack currentArmor = player.getEquippedStack(slot);
if (currentArmor.isEmpty()) {
ItemStack result = stack.copy();
result.setCount(1);
player.equipStack(slot, result);
stack.decrement(1);
return TypedActionResult.success(stack, world.isClient());
}
return TypedActionResult.fail(stack);
}
public EquipmentSlot getPreferredSlot(ItemStack stack) {
return EquipmentSlot.OFFHAND;
}
}