2021-02-03 21:25:42 +01:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
|
|
|
import java.util.List;
|
2022-01-01 21:08:50 +01:00
|
|
|
import java.util.UUID;
|
2022-09-16 12:46:48 +02:00
|
|
|
import java.util.stream.Stream;
|
2021-02-03 21:25:42 +01:00
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-02-03 21:25:42 +01:00
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.EquinePredicates;
|
2022-01-04 23:43:07 +01:00
|
|
|
import com.minelittlepony.unicopia.USounds;
|
2021-02-03 21:25:42 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.Caster;
|
2022-09-16 12:46:48 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
|
|
|
|
2021-02-03 21:25:42 +01:00
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
|
import net.fabricmc.api.Environment;
|
2021-02-27 17:42:29 +01:00
|
|
|
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
|
2021-02-03 21:25:42 +01:00
|
|
|
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.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.DyeableItem;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.stat.Stats;
|
|
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.minecraft.util.Formatting;
|
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.TypedActionResult;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2021-02-27 17:42:29 +01:00
|
|
|
public class FriendshipBraceletItem extends WearableItem implements DyeableItem, GlowableItem {
|
2021-02-03 21:25:42 +01:00
|
|
|
|
2021-02-27 17:42:29 +01:00
|
|
|
public FriendshipBraceletItem(FabricItemSettings settings) {
|
2021-02-03 21:25:42 +01:00
|
|
|
super(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
|
|
|
ItemStack stack = player.getStackInHand(hand);
|
|
|
|
|
|
|
|
if (!isSigned(stack) && EquinePredicates.PLAYER_UNICORN.test(player)) {
|
|
|
|
player.setCurrentHand(hand);
|
|
|
|
|
|
|
|
ItemStack result = stack.copy();
|
|
|
|
result.setCount(1);
|
2022-06-25 00:19:55 +02:00
|
|
|
result.getOrCreateNbt().putString("issuer", player.getName().getString());
|
2022-01-01 21:08:50 +01:00
|
|
|
result.getOrCreateNbt().putUuid("issuer_id", player.getUuid());
|
2021-02-03 21:25:42 +01:00
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
if (!player.getAbilities().creativeMode) {
|
2021-02-03 21:25:42 +01:00
|
|
|
stack.decrement(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
player.incrementStat(Stats.USED.getOrCreateStat(this));
|
2022-01-04 23:43:07 +01:00
|
|
|
player.playSound(USounds.ITEM_BRACELET_SIGN, 1, 1);
|
2021-02-03 21:25:42 +01:00
|
|
|
|
|
|
|
if (stack.isEmpty()) {
|
|
|
|
return TypedActionResult.consume(result);
|
|
|
|
}
|
|
|
|
if (!player.giveItemStack(result)) {
|
|
|
|
player.dropStack(result);
|
|
|
|
}
|
|
|
|
return TypedActionResult.consume(stack);
|
|
|
|
}
|
|
|
|
|
2021-02-27 17:42:29 +01:00
|
|
|
return super.use(world, player, hand);
|
2021-02-03 21:25:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
|
|
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> list, TooltipContext tooltipContext) {
|
|
|
|
if (isSigned(stack)) {
|
2022-06-25 00:19:55 +02:00
|
|
|
list.add(Text.translatable("item.unicopia.friendship_bracelet.issuer", getSignatorName(stack)));
|
2021-02-03 21:25:42 +01:00
|
|
|
}
|
|
|
|
if (isGlowing(stack)) {
|
2022-06-25 00:19:55 +02:00
|
|
|
list.add(Text.translatable("item.unicopia.friendship_bracelet.glowing").formatted(Formatting.ITALIC, Formatting.GRAY));
|
2021-02-03 21:25:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 17:42:29 +01:00
|
|
|
@Override
|
|
|
|
public EquipmentSlot getPreferredSlot(ItemStack stack) {
|
|
|
|
return isSigned(stack) ? EquipmentSlot.CHEST : super.getPreferredSlot(stack);
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:25:42 +01:00
|
|
|
private boolean checkSignature(ItemStack stack, PlayerEntity player) {
|
2022-01-01 21:08:50 +01:00
|
|
|
return checkSignature(stack, player.getUuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkSignature(ItemStack stack, UUID player) {
|
|
|
|
return player.equals(getSignatorId(stack));
|
2021-02-03 21:25:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
2022-01-01 21:08:50 +01:00
|
|
|
public static String getSignatorName(ItemStack stack) {
|
2021-12-22 10:15:09 +01:00
|
|
|
return isSigned(stack) ? stack.getNbt().getString("issuer") : null;
|
2021-02-03 21:25:42 +01:00
|
|
|
}
|
|
|
|
|
2022-01-01 21:08:50 +01:00
|
|
|
@Nullable
|
|
|
|
public static UUID getSignatorId(ItemStack stack) {
|
|
|
|
return isSigned(stack) ? stack.getNbt().getUuid("issuer_id") : null;
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:25:42 +01:00
|
|
|
public static boolean isSigned(ItemStack stack) {
|
2022-01-01 21:08:50 +01:00
|
|
|
return stack.hasNbt() && stack.getNbt().contains("issuer_id");
|
2021-02-03 21:25:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isSignedBy(ItemStack stack, PlayerEntity player) {
|
|
|
|
return stack.getItem() instanceof FriendshipBraceletItem
|
|
|
|
&& ((FriendshipBraceletItem)stack.getItem()).checkSignature(stack, player);
|
|
|
|
}
|
|
|
|
|
2022-01-01 21:08:50 +01:00
|
|
|
public static boolean isSignedBy(ItemStack stack, UUID player) {
|
|
|
|
return stack.getItem() instanceof FriendshipBraceletItem
|
|
|
|
&& ((FriendshipBraceletItem)stack.getItem()).checkSignature(stack, player);
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:25:42 +01:00
|
|
|
public static boolean isComrade(Caster<?> caster, Entity entity) {
|
2022-01-01 21:08:50 +01:00
|
|
|
if (entity instanceof LivingEntity) {
|
|
|
|
return caster.getMasterId().filter(id -> {
|
|
|
|
return isSignedBy(((LivingEntity)entity).getOffHandStack(), id)
|
|
|
|
|| isSignedBy(((LivingEntity)entity).getEquippedStack(EquipmentSlot.CHEST), id);
|
|
|
|
}).isPresent();
|
2021-02-03 21:25:42 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-16 12:46:48 +02:00
|
|
|
|
|
|
|
public static Stream<Pony> getPartyMembers(Caster<?> caster, double radius) {
|
|
|
|
return Pony.stream(caster.findAllEntitiesInRange(radius, entity -> isComrade(caster, entity)));
|
|
|
|
}
|
2021-02-03 21:25:42 +01:00
|
|
|
}
|