Skip to content

Commit

Permalink
Update to 1.21 (emilyploszaj#319)
Browse files Browse the repository at this point in the history
* Start working on 1.21 update

* Maybe use this for now

* Add support for trinket enchantments

* Add method for adding trinkets slots to definitions for datagen usage

* Use getId instead of creating it by hand

* Update to rc

* Style fix
  • Loading branch information
Patbox authored Jun 15, 2024
1 parent 8613541 commit 48183ab
Show file tree
Hide file tree
Showing 30 changed files with 332 additions and 178 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependencies {

// Dev Runtime
testmodImplementation sourceSets.main.output
modLocalRuntime "dev.emi:emi-fabric:${emi_version}"
//modLocalRuntime "dev.emi:emi-fabric:${emi_version}"
//modRuntimeOnly "me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}"
//modLocalRuntime "com.terraformersmc:modmenu:${project.mod_menu_version}"
}
Expand Down
12 changes: 6 additions & 6 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-rc1
yarn_mappings=1.21-rc1+build.1
loader_version=0.15.11

mod_version = 3.9.0
mod_version = 3.10.0
maven_group = dev.emi
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 Down
2 changes: 1 addition & 1 deletion src/main/java/dev/emi/trinkets/TrinketScreenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import net.minecraft.util.Identifier;

public class TrinketScreenManager {
private static final Identifier MORE_SLOTS = new Identifier("trinkets", "textures/gui/more_slots.png");
private static final Identifier MORE_SLOTS = Identifier.of("trinkets", "textures/gui/more_slots.png");
public static TrinketScreen currentScreen;
public static Rect2i currentBounds = new Rect2i(0, 0, 0, 0);
public static Rect2i typeBounds = new Rect2i(0, 0, 0, 0);
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);
}
2 changes: 1 addition & 1 deletion src/main/java/dev/emi/trinkets/TrinketsMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void onInitialize() {
}
return TypedActionResult.pass(stack);
});
Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(MOD_ID, "attribute_modifiers"), TrinketsAttributeModifiersComponent.TYPE);
Registry.register(Registries.DATA_COMPONENT_TYPE, Identifier.of(MOD_ID, "attribute_modifiers"), TrinketsAttributeModifiersComponent.TYPE);
PayloadTypeRegistry.playS2C().register(TrinketsNetwork.BREAK, BreakPayload.CODEC);
PayloadTypeRegistry.playS2C().register(TrinketsNetwork.SYNC_INVENTORY, SyncInventoryPayload.CODEC);
PayloadTypeRegistry.playS2C().register(TrinketsNetwork.SYNC_SLOTS, SyncSlotsPayload.CODEC);
Expand Down
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 @@ -8,9 +8,9 @@

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,6 +18,7 @@
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import org.ladysnake.cca.api.v3.component.sync.AutoSyncedComponent;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttribute;
Expand Down Expand Up @@ -161,7 +162,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 @@ -234,9 +235,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
12 changes: 8 additions & 4 deletions src/main/java/dev/emi/trinkets/api/SlotType.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,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"));
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", 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", 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 All @@ -155,4 +155,8 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(group, name);
}

public String getId() {
return this.group + "/" + this.name;
}
}
17 changes: 10 additions & 7 deletions src/main/java/dev/emi/trinkets/api/Trinket.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dev.emi.trinkets.api;

import java.util.ArrayList;
import java.util.UUID;

import com.google.common.collect.Maps;
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 @@ -17,8 +19,9 @@
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 @@ -73,7 +76,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 @@ -107,14 +110,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 @@ -124,7 +127,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
32 changes: 22 additions & 10 deletions src/main/java/dev/emi/trinkets/api/TrinketsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.util.Function3;
import dev.emi.trinkets.TrinketSlotTarget;
import dev.emi.trinkets.TrinketsMain;
import dev.emi.trinkets.data.EntitySlotLoader;
import dev.emi.trinkets.payload.BreakPayload;
import net.minecraft.util.math.random.Random;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.server.world.ServerWorld;
import org.ladysnake.cca.api.v3.component.ComponentKey;
import org.ladysnake.cca.api.v3.component.ComponentRegistryV3;
import net.fabricmc.api.EnvType;
Expand All @@ -28,11 +30,13 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;

import net.minecraft.world.World;

public class TrinketsApi {
public static final ComponentKey<TrinketComponent> TRINKET_COMPONENT = ComponentRegistryV3.INSTANCE
.getOrCreate(new Identifier(TrinketsMain.MOD_ID, "trinkets"), TrinketComponent.class);
.getOrCreate(Identifier.of(TrinketsMain.MOD_ID, "trinkets"), TrinketComponent.class);
private static final Map<Identifier, Function3<ItemStack, SlotReference, LivingEntity, TriState>> PREDICATES = new HashMap<>();

private static final Map<Item, Trinket> TRINKETS = new HashMap<>();
Expand Down Expand Up @@ -63,7 +67,7 @@ public static Optional<TrinketComponent> getTrinketComponent(LivingEntity living

/**
* Called to sync a trinket breaking event with clients. Should generally be
* called in the callback of {@link ItemStack#damage(int, Random, ServerPlayerEntity, Runnable)}
* called in the callback of {@link ItemStack#damage(int, ServerWorld, ServerPlayerEntity, Consumer)}
*/
public static void onTrinketBroken(ItemStack stack, SlotReference ref, LivingEntity entity) {
World world = entity.getWorld();
Expand Down Expand Up @@ -157,22 +161,30 @@ public static boolean evaluatePredicateSet(Set<Identifier> set, ItemStack stack,
return state.get();
}

public static Enchantment.Definition withTrinketSlots(Enchantment.Definition definition, Set<String> slots) {
var def = new Enchantment.Definition(definition.supportedItems(), definition.primaryItems(), definition.weight(), definition.maxLevel(),
definition.minCost(), definition.maxCost(), definition.anvilCost(), definition.slots());

((TrinketSlotTarget) (Object) def).trinkets$slots(slots);
return def;
}

static {
TrinketsApi.registerTrinketPredicate(new Identifier("trinkets", "all"), (stack, ref, entity) -> TriState.TRUE);
TrinketsApi.registerTrinketPredicate(new Identifier("trinkets", "none"), (stack, ref, entity) -> TriState.FALSE);
TagKey<Item> trinketsAll = TagKey.of(RegistryKeys.ITEM, new Identifier("trinkets", "all"));
TrinketsApi.registerTrinketPredicate(Identifier.of("trinkets", "all"), (stack, ref, entity) -> TriState.TRUE);
TrinketsApi.registerTrinketPredicate(Identifier.of("trinkets", "none"), (stack, ref, entity) -> TriState.FALSE);
TagKey<Item> trinketsAll = TagKey.of(RegistryKeys.ITEM, Identifier.of("trinkets", "all"));

TrinketsApi.registerTrinketPredicate(new Identifier("trinkets", "tag"), (stack, ref, entity) -> {
TrinketsApi.registerTrinketPredicate(Identifier.of("trinkets", "tag"), (stack, ref, entity) -> {
SlotType slot = ref.inventory().getSlotType();
TagKey<Item> tag = TagKey.of(RegistryKeys.ITEM, new Identifier("trinkets", slot.getGroup() + "/" + slot.getName()));
TagKey<Item> tag = TagKey.of(RegistryKeys.ITEM, Identifier.of("trinkets", slot.getId()));

if (stack.isIn(tag) || stack.isIn(trinketsAll)) {
return TriState.TRUE;
}
return TriState.DEFAULT;
});
TrinketsApi.registerTrinketPredicate(new Identifier("trinkets", "relevant"), (stack, ref, entity) -> {
var map = TrinketsApi.getTrinket(stack.getItem()).getModifiers(stack, ref, entity, SlotAttributes.getUuid(ref));
TrinketsApi.registerTrinketPredicate(Identifier.of("trinkets", "relevant"), (stack, ref, entity) -> {
var map = TrinketsApi.getTrinket(stack.getItem()).getModifiers(stack, ref, entity, SlotAttributes.getIdentifier(ref));
if (!map.isEmpty()) {
return TriState.TRUE;
}
Expand Down
Loading

0 comments on commit 48183ab

Please sign in to comment.