Fixed being able to equip amulets the the chest slot when trinkets is installed. #300

This commit is contained in:
Sollace 2024-03-20 00:21:50 +00:00
parent cef8ef15ce
commit f7534f9f84
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
6 changed files with 39 additions and 11 deletions

View file

@ -30,7 +30,7 @@ public interface TrinketsDelegate {
TrinketsDelegate EMPTY = new TrinketsDelegate() {}; TrinketsDelegate EMPTY = new TrinketsDelegate() {};
static TrinketsDelegate getInstance(@Nullable LivingEntity entity) { static TrinketsDelegate getInstance(@Nullable LivingEntity entity) {
if (!(entity instanceof PlayerEntity && hasTrinkets())) { if (!hasTrinkets() || (entity != null && !(entity instanceof PlayerEntity))) {
return EMPTY; return EMPTY;
} }
return TrinketsDelegateImpl.INSTANCE; return TrinketsDelegateImpl.INSTANCE;

View file

@ -3,6 +3,8 @@ package com.minelittlepony.unicopia.compat.trinkets;
import java.util.UUID; import java.util.UUID;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.minelittlepony.unicopia.entity.ItemTracker;
import com.minelittlepony.unicopia.entity.Living;
import com.minelittlepony.unicopia.item.FriendshipBraceletItem; import com.minelittlepony.unicopia.item.FriendshipBraceletItem;
import com.minelittlepony.unicopia.item.WearableItem; import com.minelittlepony.unicopia.item.WearableItem;
@ -30,8 +32,19 @@ public class UnicopiaTrinket implements Trinket {
return; return;
} }
if (!(stack.getItem() instanceof ItemTracker.Trackable) && stack.getItem() instanceof Equipment q) {
entity.playSound(q.getEquipSound(), 1, 1);
}
}
@Override
public void onUnequip(ItemStack stack, SlotReference slot, LivingEntity entity) {
if (stack.getItem() instanceof ItemTracker.Trackable t) {
Living<?> l = Living.living(entity);
t.onUnequipped(l, l.getArmour().forceRemove(t));
}
if (stack.getItem() instanceof Equipment q) { if (stack.getItem() instanceof Equipment q) {
entity.playSound( q.getEquipSound(), 1, 1); entity.playSound(q.getEquipSound(), 1, 1);
} }
} }

View file

@ -4,6 +4,8 @@ import java.util.*;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.compat.trinkets.TrinketsDelegate; import com.minelittlepony.unicopia.compat.trinkets.TrinketsDelegate;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.util.*; import com.minelittlepony.unicopia.util.*;
@ -33,6 +35,7 @@ public class ItemTracker implements NbtSerialisable, Copyable<ItemTracker>, Tick
} }
private final Map<Trackable, Long> items = new HashMap<>(); private final Map<Trackable, Long> items = new HashMap<>();
private final Map<Trackable, Boolean> forced = new HashMap<>();
public static Predicate<LivingEntity> wearing(Trackable charm, Predicate<Long> range) { public static Predicate<LivingEntity> wearing(Trackable charm, Predicate<Long> range) {
return e -> Living.getOrEmpty(e) return e -> Living.getOrEmpty(e)
@ -67,19 +70,23 @@ public class ItemTracker implements NbtSerialisable, Copyable<ItemTracker>, Tick
@Override @Override
public void tick() { public void tick() {
update(living, living.getArmourStacks()); update(living.getArmourStacks());
} }
private void update(Living<?> living, Stream<ItemStack> stacks) { private void update(Stream<ItemStack> stacks) {
final Set<Trackable> found = new HashSet<>(); final Set<Trackable> found = new HashSet<>();
final Set<ItemStack> foundStacks = new HashSet<>(); final Set<ItemStack> foundStacks = new HashSet<>();
stacks.forEach(stack -> { stacks.forEach(stack -> {
if (stack.getItem() instanceof Trackable trackable) { if (stack.getItem() instanceof Trackable trackable) {
items.compute(trackable, (item, prev) -> prev == null ? 1 : prev + 1); if (items.compute(trackable, (item, prev) -> prev == null ? 1 : prev + 1) == 1) {
trackable.onEquipped(this.living);
}
found.add(trackable); found.add(trackable);
foundStacks.add(stack); foundStacks.add(stack);
} }
}); });
items.entrySet().removeIf(e -> { items.entrySet().removeIf(e -> {
if (!found.contains(e.getKey())) { if (!found.contains(e.getKey())) {
e.getKey().onUnequipped(living, e.getValue()); e.getKey().onUnequipped(living, e.getValue());
@ -90,13 +97,16 @@ public class ItemTracker implements NbtSerialisable, Copyable<ItemTracker>, Tick
if (!(living instanceof Pony)) { if (!(living instanceof Pony)) {
foundStacks.forEach(stack -> { foundStacks.forEach(stack -> {
if (getTicks((Trackable)stack.getItem()) == 1) {
stack.inventoryTick(living.asWorld(), living.asEntity(), 0, false); stack.inventoryTick(living.asWorld(), living.asEntity(), 0, false);
}
}); });
} }
} }
public long forceRemove(Trackable charm) {
@Nullable Long time = items.remove(charm);
return time == null ? 0 : time;
}
public long getTicks(Trackable charm) { public long getTicks(Trackable charm) {
return items.getOrDefault(charm.asItem(), 0L); return items.getOrDefault(charm.asItem(), 0L);
} }

View file

@ -134,7 +134,7 @@ public class AlicornAmuletItem extends AmuletItem implements ItemTracker.Trackab
@Override @Override
public void onEquipped(Living<?> wearer) { public void onEquipped(Living<?> wearer) {
wearer.playSound(USounds.ITEM_ALICORN_AMULET_CURSE, 3, 1); wearer.playSound(USounds.ITEM_ALICORN_AMULET_CURSE, 0.5F, 1);
} }
@Override @Override

View file

@ -68,7 +68,7 @@ public class AmuletItem extends WearableItem implements ChargeableItem {
@Override @Override
public EquipmentSlot getSlotType(ItemStack stack) { public EquipmentSlot getSlotType(ItemStack stack) {
return EquipmentSlot.CHEST; return TrinketsDelegate.hasTrinkets() ? EquipmentSlot.OFFHAND : EquipmentSlot.CHEST;
} }
@Override @Override

View file

@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.util;
import com.minelittlepony.unicopia.EntityConvertable; import com.minelittlepony.unicopia.EntityConvertable;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvent;
import net.minecraft.util.math.random.Random; import net.minecraft.util.math.random.Random;
@ -26,8 +27,12 @@ public interface SoundEmitter<E extends Entity> extends EntityConvertable<E> {
} }
static void playSoundAt(Entity entity, SoundEvent sound, SoundCategory category, float volume, float pitch) { static void playSoundAt(Entity entity, SoundEvent sound, SoundCategory category, float volume, float pitch) {
if (entity.getWorld().isClient && entity instanceof PlayerEntity p) {
entity.getWorld().playSound(p, entity.getX(), entity.getY(), entity.getZ(), sound, category, volume, pitch);
} else {
entity.getWorld().playSound(null, entity.getX(), entity.getY(), entity.getZ(), sound, category, volume, pitch); entity.getWorld().playSound(null, entity.getX(), entity.getY(), entity.getZ(), sound, category, volume, pitch);
} }
}
static float getRandomPitch(Random rng) { static float getRandomPitch(Random rng) {
return (float)rng.nextTriangular(0.5F, 0.2F); return (float)rng.nextTriangular(0.5F, 0.2F);