Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	gradle.properties
#	src/main/java/dev/emi/trinkets/SurvivalTrinketSlot.java
#	src/main/java/dev/emi/trinkets/TrinketScreenManager.java
#	src/main/java/dev/emi/trinkets/TrinketsClient.java
#	src/main/java/dev/emi/trinkets/TrinketsMain.java
#	src/main/java/dev/emi/trinkets/api/LivingEntityTrinketComponent.java
#	src/main/java/dev/emi/trinkets/api/SlotType.java
#	src/main/java/dev/emi/trinkets/api/Trinket.java
#	src/main/java/dev/emi/trinkets/api/TrinketsApi.java
#	src/main/java/dev/emi/trinkets/data/EntitySlotLoader.java
#	src/main/java/dev/emi/trinkets/data/SlotLoader.java
#	src/main/java/dev/emi/trinkets/mixin/ClickableWidgetMixin.java
#	src/main/java/dev/emi/trinkets/mixin/ClientPlayNetworkHandlerMixin.java
#	src/main/java/dev/emi/trinkets/mixin/CreativeInventoryScreenMixin.java
#	src/main/java/dev/emi/trinkets/mixin/HandledScreenMixin.java
#	src/main/java/dev/emi/trinkets/mixin/InventoryScreenMixin.java
#	src/main/java/dev/emi/trinkets/mixin/LivingEntityMixin.java
#	src/main/java/dev/emi/trinkets/mixin/PlayerManagerMixin.java
#	src/main/java/dev/emi/trinkets/mixin/PlayerScreenHandlerMixin.java
#	src/main/resources/trinkets.mixins.json
#	src/testmod/java/dev/emi/trinkets/TestTrinket.java
  • Loading branch information
Patbox committed Jun 15, 2024
2 parents b9edf9c + 48183ab commit 6542eb2
Show file tree
Hide file tree
Showing 26 changed files with 349 additions and 178 deletions.
20 changes: 10 additions & 10 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx2G

minecraft_version=1.20.6
yarn_mappings=1.20.6+build.1
loader_version=0.15.10
minecraft_version=1.21
yarn_mappings=1.21+build.1
loader_version=0.15.11

mod_version = 3.9.0
mod_version = 3.10.0
maven_group = eu.pb4.polyport
archives_base_name = trinkets

fabric_version=0.97.6+1.20.6
cca_version=6.0.0-beta.3
fabric_version=0.100.0+1.21
cca_version=6.1.0
mod_menu_version=10.0.0-beta.1
emi_version=1.1.6+1.20.6
rei_version=15.0.728
Expand All @@ -19,7 +19,7 @@ cloth_config_version=14.0.126
port_version=1


polymer_version=0.8.1+1.20.6
sgui_version=1.5.1+1.20.5
translation_version=2.3.0+1.20.5-rc2
pda_version=0.5.0+1.20.5
polymer_version=0.9.1+1.21
sgui_version=1.6.0+1.21
translation_version=2.3.1+1.21-pre2
pda_version=0.6.0+1.21
13 changes: 13 additions & 0 deletions src/main/java/dev/emi/trinkets/SurvivalTrinketSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;

/**
* A gui slot for a trinket slot, used in the survival inventory, but suited for any case
Expand Down Expand Up @@ -48,6 +49,18 @@ public boolean canTakeItems(PlayerEntity player) {
@Override
public boolean isEnabled() {
if (alwaysVisible) {
if (x < 0) {
World world = trinketInventory.getComponent().getEntity().getWorld();
if (world.isClient) {
MinecraftClient client = MinecraftClient.getInstance();
Screen s = client.currentScreen;
if (s instanceof InventoryScreen screen) {
if (screen.getRecipeBookWidget().isOpen()) {
return false;
}
}
}
}
return true;
}
return isTrinketFocused();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/dev/emi/trinkets/TrinketSlotTarget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.emi.trinkets;

import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.Set;

public interface TrinketSlotTarget {
Set<String> trinkets$slots();
void trinkets$slots(Set<String> slots);
}
6 changes: 3 additions & 3 deletions src/main/java/dev/emi/trinkets/TrinketsNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
@Deprecated
public class TrinketsNetwork {

public static final CustomPayload.Id<SyncSlotsPayload> SYNC_SLOTS = new CustomPayload.Id<>(new Identifier(TrinketsMain.MOD_ID, "sync_slots"));
public static final CustomPayload.Id<SyncInventoryPayload> SYNC_INVENTORY = new CustomPayload.Id<>(new Identifier(TrinketsMain.MOD_ID, "sync_inventory"));
public static final CustomPayload.Id<BreakPayload> BREAK = new CustomPayload.Id<>(new Identifier(TrinketsMain.MOD_ID, "break"));
public static final CustomPayload.Id<SyncSlotsPayload> SYNC_SLOTS = new CustomPayload.Id<>(Identifier.of(TrinketsMain.MOD_ID, "sync_slots"));
public static final CustomPayload.Id<SyncInventoryPayload> SYNC_INVENTORY = new CustomPayload.Id<>(Identifier.of(TrinketsMain.MOD_ID, "sync_inventory"));
public static final CustomPayload.Id<BreakPayload> BREAK = new CustomPayload.Id<>(Identifier.of(TrinketsMain.MOD_ID, "break"));

private TrinketsNetwork() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import net.minecraft.registry.entry.RegistryEntry;
import org.ladysnake.cca.api.v3.component.Component;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.util.Identifier;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Pair;
Expand Down Expand Up @@ -159,7 +161,7 @@ public void removeModifiers(Multimap<String, EntityAttributeModifier> modifiers)
if (groupInv != null) {
TrinketInventory inv = groupInv.get(slot);
if (inv != null) {
inv.removeModifier(modifier.uuid());
inv.removeModifier(modifier.id());
}
}
}
Expand Down Expand Up @@ -197,7 +199,7 @@ public void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup lookup) {
if (groupSlots != null) {
for (String slotKey : groupTag.getKeys()) {
NbtCompound slotTag = groupTag.getCompound(slotKey);
NbtList list = slotTag.getList("Items", NbtType.COMPOUND);
NbtList list = slotTag.getList("Items", NbtElement.COMPOUND_TYPE);
TrinketInventory inv = groupSlots.get(slotKey);

if (inv != null) {
Expand All @@ -217,7 +219,7 @@ public void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup lookup) {
} else {
for (String slotKey : groupTag.getKeys()) {
NbtCompound slotTag = groupTag.getCompound(slotKey);
NbtList list = slotTag.getList("Items", NbtType.COMPOUND);
NbtList list = slotTag.getList("Items", NbtElement.COMPOUND_TYPE);
for (int i = 0; i < list.size(); i++) {
NbtCompound c = list.getCompound(i);
dropped.add(ItemStack.fromNbtOrEmpty(lookup, c));
Expand All @@ -232,9 +234,9 @@ public void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup lookup) {
Multimap<String, EntityAttributeModifier> slotMap = HashMultimap.create();
this.forEach((ref, stack) -> {
if (!stack.isEmpty()) {
UUID uuid = SlotAttributes.getUuid(ref);
Identifier identifier = SlotAttributes.getIdentifier(ref);
Trinket trinket = TrinketsApi.getTrinket(stack.getItem());
Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> map = trinket.getModifiers(stack, ref, entity, uuid);
Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> map = trinket.getModifiers(stack, ref, entity, identifier);
for (RegistryEntry<EntityAttribute> entityAttribute : map.keySet()) {
if (entityAttribute.hasKeyAndValue() && entityAttribute.value() instanceof SlotAttributes.SlotEntityAttribute slotEntityAttribute) {
slotMap.putAll(slotEntityAttribute.slot, map.get(entityAttribute));
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/dev/emi/trinkets/api/SlotAttributes.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package dev.emi.trinkets.api;

import java.util.Map;
import java.util.UUID;

import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;

public class SlotAttributes {
private static Map<String, UUID> CACHED_UUIDS= Maps.newHashMap();
private static Map<String, RegistryEntry<EntityAttribute>> CACHED_ATTRIBUTES = Maps.newHashMap();
private static final Map<String, Identifier> CACHED_IDS = Maps.newHashMap();
private static final Map<String, RegistryEntry<EntityAttribute>> CACHED_ATTRIBUTES = Maps.newHashMap();

/**
* Adds an Entity Attribute Nodifier for slot count to the provided multimap
*/
public static void addSlotModifier(Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> map, String slot, UUID uuid, double amount,
public static void addSlotModifier(Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> map, String slot, Identifier identifier, double amount,
EntityAttributeModifier.Operation operation) {
CACHED_ATTRIBUTES.putIfAbsent(slot, RegistryEntry.of(new SlotEntityAttribute(slot)));
map.put(CACHED_ATTRIBUTES.get(slot), new EntityAttributeModifier(uuid, slot, amount, operation));
map.put(CACHED_ATTRIBUTES.get(slot), new EntityAttributeModifier(identifier, amount, operation));
}

public static UUID getUuid(SlotReference ref) {
String key = ref.inventory().getSlotType().getGroup() + "/" + ref.inventory().getSlotType().getName() + "/" + ref.index();
CACHED_UUIDS.putIfAbsent(key, UUID.nameUUIDFromBytes(key.getBytes()));
return CACHED_UUIDS.get(key);
public static Identifier getIdentifier(SlotReference ref) {
String key = ref.inventory().getSlotType().getId() + "/" + ref.index();
CACHED_IDS.computeIfAbsent(key, Identifier::of);
return CACHED_IDS.get(key);
}

public static class SlotEntityAttribute extends EntityAttribute {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/dev/emi/trinkets/api/SlotType.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,24 @@ public static SlotType read(NbtCompound data) {
String name = slotData.getString("Name");
int order = slotData.getInt("Order");
int amount = slotData.getInt("Amount");
Identifier icon = new Identifier(slotData.getString("Icon"));
NbtList quickMoveList = slotData.getList("QuickMovePredicates", NbtType.STRING);
Identifier icon = Identifier.of(slotData.getString("Icon"));
NbtList quickMoveList = slotData.getList("QuickMovePredicates", NbtElement.STRING_TYPE);
Set<Identifier> quickMovePredicates = new HashSet<>();

for (NbtElement tag : quickMoveList) {
quickMovePredicates.add(new Identifier(tag.asString()));
quickMovePredicates.add(Identifier.of(tag.asString()));
}
NbtList validatorList = slotData.getList("ValidatorPredicates", NbtType.STRING);
NbtList validatorList = slotData.getList("ValidatorPredicates", NbtElement.STRING_TYPE);
Set<Identifier> validatorPredicates = new HashSet<>();

for (NbtElement tag : validatorList) {
validatorPredicates.add(new Identifier(tag.asString()));
validatorPredicates.add(Identifier.of(tag.asString()));
}
NbtList tooltipList = slotData.getList("TooltipPredicates", NbtType.STRING);
NbtList tooltipList = slotData.getList("TooltipPredicates", NbtElement.STRING_TYPE);
Set<Identifier> tooltipPredicates = new HashSet<>();

for (NbtElement tag : tooltipList) {
tooltipPredicates.add(new Identifier(tag.asString()));
tooltipPredicates.add(Identifier.of(tag.asString()));
}
String dropRuleName = slotData.getString("DropRule");
DropRule dropRule = DropRule.DEFAULT;
Expand Down Expand Up @@ -185,4 +185,8 @@ public int hashCode() {
public ItemStack getIconItem() {
return this.itemIcon.copy();
}

public String getId() {
return this.group + "/" + this.name;
}
}
16 changes: 11 additions & 5 deletions src/main/java/dev/emi/trinkets/api/Trinket.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

import dev.emi.trinkets.mixin.accessor.LivingEntityAccessor;
import java.util.function.Consumer;

import net.minecraft.component.EnchantmentEffectComponentTypes;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttribute;
Expand All @@ -16,8 +20,10 @@
import net.minecraft.item.ItemStack;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.math.random.Random;
import net.minecraft.util.Identifier;

public interface Trinket {

Expand Down Expand Up @@ -72,7 +78,7 @@ default boolean canEquip(ItemStack stack, SlotReference slot, LivingEntity entit
* @return Whether the stack can be unequipped
*/
default boolean canUnequip(ItemStack stack, SlotReference slot, LivingEntity entity) {
return !EnchantmentHelper.hasBindingCurse(stack) || (entity instanceof PlayerEntity player && player.isCreative());
return !EnchantmentHelper.hasAnyEnchantmentsWith(stack, EnchantmentEffectComponentTypes.PREVENT_ARMOR_CHANGE) || (entity instanceof PlayerEntity player && player.isCreative());
}

/**
Expand Down Expand Up @@ -106,14 +112,14 @@ default RegistryEntry<SoundEvent> getEquipSound(ItemStack stack, SlotReference s
* If modifiers do not change based on stack, slot, or entity, caching based on passed UUID
* should be considered
*
* @param uuid The UUID to use for creating attributes
* @param slotIdentifier The Identifier to use for creating attributes
*/
default Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> getModifiers(ItemStack stack, SlotReference slot, LivingEntity entity, UUID uuid) {
default Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> getModifiers(ItemStack stack, SlotReference slot, LivingEntity entity, Identifier slotIdentifier) {
Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> map = Multimaps.newMultimap(Maps.newLinkedHashMap(), ArrayList::new);

if (stack.contains(TrinketsAttributeModifiersComponent.TYPE)) {
for (var entry : stack.getOrDefault(TrinketsAttributeModifiersComponent.TYPE, TrinketsAttributeModifiersComponent.DEFAULT).modifiers()) {
if (entry.slot().isEmpty() || entry.slot().get().equals(slot.inventory().getSlotType().getGroup() + "/" + slot.inventory().getSlotType().getName())) {
if (entry.slot().isEmpty() || entry.slot().get().equals(slot.inventory().getSlotType().getId())) {
map.put(entry.attribute(), entry.modifier());
}
}
Expand All @@ -123,7 +129,7 @@ default Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> getMod

/**
* Called by Trinkets when a trinket is broken on the client if {@link TrinketsApi#onTrinketBroken}
* is called by the callback in {@link ItemStack#damage(int, Random, ServerPlayerEntity, Runnable)} server side
* is called by the callback in {@link ItemStack#damage(int, ServerWorld, ServerPlayerEntity, Consumer)} server side
* <p>
* The default implementation works the same as breaking vanilla equipment, a sound is played and
* particles are spawned based on the item
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/dev/emi/trinkets/api/TrinketInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;

import java.util.*;
Expand All @@ -20,7 +21,7 @@ public class TrinketInventory implements Inventory {
private final SlotType slotType;
private final int baseSize;
private final TrinketComponent component;
private final Map<UUID, EntityAttributeModifier> modifiers = new HashMap<>();
private final Map<Identifier, EntityAttributeModifier> modifiers = new HashMap<>();
private final Set<EntityAttributeModifier> persistentModifiers = new HashSet<>();
private final Set<EntityAttributeModifier> cachedModifiers = new HashSet<>();
private final Multimap<EntityAttributeModifier.Operation, EntityAttributeModifier> modifiersByOperation = HashMultimap.create();
Expand Down Expand Up @@ -105,7 +106,7 @@ public boolean canPlayerUse(PlayerEntity player) {
return true;
}

public Map<UUID, EntityAttributeModifier> getModifiers() {
public Map<Identifier, EntityAttributeModifier> getModifiers() {
return this.modifiers;
}

Expand All @@ -114,7 +115,7 @@ public Collection<EntityAttributeModifier> getModifiersByOperation(EntityAttribu
}

public void addModifier(EntityAttributeModifier modifier) {
this.modifiers.put(modifier.uuid(), modifier);
this.modifiers.put(modifier.id(), modifier);
this.getModifiersByOperation(modifier.operation()).add(modifier);
this.markUpdate();
}
Expand All @@ -124,8 +125,8 @@ public void addPersistentModifier(EntityAttributeModifier modifier) {
this.persistentModifiers.add(modifier);
}

public void removeModifier(UUID uuid) {
EntityAttributeModifier modifier = this.modifiers.remove(uuid);
public void removeModifier(Identifier identifier) {
EntityAttributeModifier modifier = this.modifiers.remove(identifier);
if (modifier != null) {
this.persistentModifiers.remove(modifier);
this.getModifiersByOperation(modifier.operation()).remove(modifier);
Expand All @@ -134,7 +135,7 @@ public void removeModifier(UUID uuid) {
}

public void clearModifiers() {
Iterator<UUID> iter = this.getModifiers().keySet().iterator();
Iterator<Identifier> iter = this.getModifiers().keySet().iterator();

while(iter.hasNext()) {
this.removeModifier(iter.next());
Expand All @@ -147,7 +148,7 @@ public void removeCachedModifier(EntityAttributeModifier attributeModifier) {

public void clearCachedModifiers() {
for (EntityAttributeModifier cachedModifier : this.cachedModifiers) {
this.removeModifier(cachedModifier.uuid());
this.removeModifier(cachedModifier.id());
}
this.cachedModifiers.clear();
}
Expand Down Expand Up @@ -271,7 +272,7 @@ public NbtCompound getSyncTag() {
NbtCompound NbtCompound = new NbtCompound();
if (!this.modifiers.isEmpty()) {
NbtList NbtList = new NbtList();
for (Map.Entry<UUID, EntityAttributeModifier> modifier : this.modifiers.entrySet()) {
for (Map.Entry<Identifier, EntityAttributeModifier> modifier : this.modifiers.entrySet()) {
NbtList.add(modifier.getValue().toNbt());
}

Expand Down
Loading

0 comments on commit 6542eb2

Please sign in to comment.