From 322417c30b8230298c51b9b815abcb89d79aefaf Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 29 Feb 2024 18:30:09 +0000 Subject: [PATCH] Small fixes + changes --- .../provider/FabricAdvancementProvider.java | 10 ++-- .../v1/provider/FabricLanguageProvider.java | 41 +++++++------ .../v1/provider/FabricTagProvider.java | 2 +- .../datagen/DataGeneratorTestEntrypoint.java | 14 ++--- .../ModifyItemAttributeModifiersCallback.java | 60 ------------------- .../fabric/mixin/item/ItemStackMixin.java | 16 ----- ...ifyItemAttributeModifiersCallbackTest.java | 38 ------------ .../src/testmod/resources/fabric.mod.json | 1 - 8 files changed, 36 insertions(+), 146 deletions(-) delete mode 100644 fabric-item-api-v1/src/main/java/net/fabricmc/fabric/api/item/v1/ModifyItemAttributeModifiersCallback.java delete mode 100644 fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/ModifyItemAttributeModifiersCallbackTest.java diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricAdvancementProvider.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricAdvancementProvider.java index 0f75b766be..9717042446 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricAdvancementProvider.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricAdvancementProvider.java @@ -65,7 +65,7 @@ protected FabricAdvancementProvider(FabricDataOutput output, CompletableFutureUse {@link Advancement.Builder#build(Consumer, String)} to help build advancements. */ - public abstract void generateAdvancement(Consumer consumer); + public abstract void generateAdvancement(RegistryWrapper.WrapperLookup registryLookup, Consumer consumer); /** * Return a new exporter that applies the specified conditions to any advancement it receives. @@ -80,12 +80,12 @@ protected Consumer withConditions(Consumer e @Override public CompletableFuture run(DataWriter writer) { - final Set identifiers = Sets.newHashSet(); - final Set advancements = Sets.newHashSet(); + return this.registryLookup.thenCompose(lookup -> { + final Set identifiers = Sets.newHashSet(); + final Set advancements = Sets.newHashSet(); - generateAdvancement(advancements::add); + generateAdvancement(lookup, advancements::add); - return this.registryLookup.thenCompose(lookup -> { RegistryOps ops = lookup.getOps(JsonOps.INSTANCE); final List> futures = new ArrayList<>(); diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLanguageProvider.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLanguageProvider.java index 4f4a968000..1a82706d65 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLanguageProvider.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLanguageProvider.java @@ -41,6 +41,7 @@ import net.minecraft.item.ItemGroup; import net.minecraft.registry.Registries; import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryWrapper; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.stat.StatType; import net.minecraft.text.TextContent; @@ -52,21 +53,23 @@ /** * Extend this class and implement {@link FabricLanguageProvider#generateTranslations(TranslationBuilder)}. - * Make sure to use {@link FabricLanguageProvider#FabricLanguageProvider(FabricDataOutput, String)} FabricLanguageProvider} to declare what language code is being generated if it isn't {@code en_us}. + * Make sure to use {@link FabricLanguageProvider#FabricLanguageProvider(FabricDataOutput, String, CompletableFuture)} FabricLanguageProvider} to declare what language code is being generated if it isn't {@code en_us}. * *

Register an instance of the class with {@link FabricDataGenerator.Pack#addProvider} in a {@link net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint}. */ public abstract class FabricLanguageProvider implements DataProvider { protected final FabricDataOutput dataOutput; private final String languageCode; + private final CompletableFuture registryLookup; - protected FabricLanguageProvider(FabricDataOutput dataOutput) { - this(dataOutput, "en_us"); + protected FabricLanguageProvider(FabricDataOutput dataOutput, CompletableFuture registryLookup) { + this(dataOutput, "en_us", registryLookup); } - protected FabricLanguageProvider(FabricDataOutput dataOutput, String languageCode) { + protected FabricLanguageProvider(FabricDataOutput dataOutput, String languageCode, CompletableFuture registryLookup) { this.dataOutput = dataOutput; this.languageCode = languageCode; + this.registryLookup = registryLookup; } /** @@ -74,30 +77,32 @@ protected FabricLanguageProvider(FabricDataOutput dataOutput, String languageCod * *

Call {@link TranslationBuilder#add(String, String)} to add a translation. */ - public abstract void generateTranslations(TranslationBuilder translationBuilder); + public abstract void generateTranslations(RegistryWrapper.WrapperLookup registryLookup, TranslationBuilder translationBuilder); @Override public CompletableFuture run(DataWriter writer) { TreeMap translationEntries = new TreeMap<>(); - generateTranslations((String key, String value) -> { - Objects.requireNonNull(key); - Objects.requireNonNull(value); + return this.registryLookup.thenCompose(lookup -> { + generateTranslations(lookup, (String key, String value) -> { + Objects.requireNonNull(key); + Objects.requireNonNull(value); - if (translationEntries.containsKey(key)) { - throw new RuntimeException("Existing translation key found - " + key + " - Duplicate will be ignored."); - } + if (translationEntries.containsKey(key)) { + throw new RuntimeException("Existing translation key found - " + key + " - Duplicate will be ignored."); + } - translationEntries.put(key, value); - }); + translationEntries.put(key, value); + }); - JsonObject langEntryJson = new JsonObject(); + JsonObject langEntryJson = new JsonObject(); - for (Map.Entry entry : translationEntries.entrySet()) { - langEntryJson.addProperty(entry.getKey(), entry.getValue()); - } + for (Map.Entry entry : translationEntries.entrySet()) { + langEntryJson.addProperty(entry.getKey(), entry.getValue()); + } - return DataProvider.writeToPath(writer, langEntryJson, getLangFilePath(this.languageCode)); + return DataProvider.writeToPath(writer, langEntryJson, getLangFilePath(this.languageCode)); + }); } private Path getLangFilePath(String code) { diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricTagProvider.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricTagProvider.java index 59da115619..21e6461675 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricTagProvider.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricTagProvider.java @@ -84,7 +84,7 @@ public FabricTagProvider(FabricDataOutput output, RegistryKey registriesFuture) { + super(output, registriesFuture); } @Override - public void generateTranslations(TranslationBuilder translationBuilder) { + public void generateTranslations(RegistryWrapper.WrapperLookup registryLookup, TranslationBuilder translationBuilder) { translationBuilder.add(SIMPLE_BLOCK, "Simple Block"); translationBuilder.add(new Identifier(MOD_ID, "identifier_test"), "Identifier Test"); translationBuilder.add(EntityType.ALLAY, "Allay"); @@ -282,12 +282,12 @@ public void generateTranslations(TranslationBuilder translationBuilder) { } private static class JapaneseLangProvider extends FabricLanguageProvider { - private JapaneseLangProvider(FabricDataOutput output) { - super(output, "ja_jp"); + private JapaneseLangProvider(FabricDataOutput output, CompletableFuture registriesFuture) { + super(output, "ja_jp", registriesFuture); } @Override - public void generateTranslations(TranslationBuilder translationBuilder) { + public void generateTranslations(RegistryWrapper.WrapperLookup registryLookup, TranslationBuilder translationBuilder) { translationBuilder.add(SIMPLE_BLOCK, "シンプルブロック"); translationBuilder.add(SIMPLE_ITEM_GROUP, "データ生成項目"); translationBuilder.add("this.is.a.test", "こんにちは"); @@ -369,7 +369,7 @@ private TestAdvancementProvider(FabricDataOutput output, CompletableFuture consumer) { + public void generateAdvancement(RegistryWrapper.WrapperLookup registryLookup, Consumer consumer) { AdvancementEntry root = Advancement.Builder.create() .display( SIMPLE_BLOCK, diff --git a/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/api/item/v1/ModifyItemAttributeModifiersCallback.java b/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/api/item/v1/ModifyItemAttributeModifiersCallback.java deleted file mode 100644 index 2958c8bfdb..0000000000 --- a/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/api/item/v1/ModifyItemAttributeModifiersCallback.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.api.item.v1; - -import com.google.common.collect.Multimap; - -import net.minecraft.entity.EquipmentSlot; -import net.minecraft.entity.attribute.EntityAttribute; -import net.minecraft.entity.attribute.EntityAttributeModifier; -import net.minecraft.item.ItemStack; -import net.minecraft.registry.entry.RegistryEntry; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; - -/** - * Stack-aware attribute modifier callback for foreign items. - * Instead of using Mixin to change attribute modifiers in items not in your mod, - * you can use this event instead, either checking the Item itself or using a tag. - * This event provides you with a guaranteed mutable map you can put attribute modifiers in. - * Do not use for your own Item classes; see {@link FabricItem#getAttributeModifiers} instead. - * For example, the following code modifies a Diamond Helmet to give you five extra hearts when wearing. - * - *

- * {@code
- * ModifyItemAttributeModifiersCallback.EVENT.register((stack, slot, attributeModifiers) -> {
- * 	if (stack.isOf(Items.DIAMOND_HELMET) && slot.getEntitySlotId() == HEAD_SLOT_ID) {
- * 		attributeModifiers.put(EntityAttributes.GENERIC_MAX_HEALTH, MODIFIER);
- * 	}
- * });
- * }
- * 
- */ -@FunctionalInterface -public interface ModifyItemAttributeModifiersCallback { - void modifyAttributeModifiers(ItemStack stack, EquipmentSlot slot, Multimap, EntityAttributeModifier> attributeModifiers); - - Event EVENT = EventFactory.createArrayBacked( - ModifyItemAttributeModifiersCallback.class, - callbacks -> (stack, slot, attributeModifiers) -> { - for (ModifyItemAttributeModifiersCallback callback : callbacks) { - callback.modifyAttributeModifiers(stack, slot, attributeModifiers); - } - } - ); -} diff --git a/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/mixin/item/ItemStackMixin.java b/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/mixin/item/ItemStackMixin.java index aab98641f0..aba106c96d 100644 --- a/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/mixin/item/ItemStackMixin.java +++ b/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/mixin/item/ItemStackMixin.java @@ -61,22 +61,6 @@ private void hookDamage(ItemStack instance, int amount, Random random, ServerPla original.call(instance, amount, random, serverPlayerEntity, runnable); } - // TODO 1.20.5 -// @Redirect( -// method = "getAttributeModifiers", -// at = @At( -// value = "INVOKE", -// target = "Lnet/minecraft/item/Item;getAttributeModifiers(Lnet/minecraft/entity/EquipmentSlot;)Lcom/google/common/collect/Multimap;" -// ) -// ) -// public Multimap, EntityAttributeModifier> hookGetAttributeModifiers(Item item, EquipmentSlot slot) { -// ItemStack stack = (ItemStack) (Object) this; -// //we need to ensure it is modifiable for the callback, use linked map to preserve ordering -// Multimap, EntityAttributeModifier> attributeModifiers = LinkedHashMultimap.create(item.getAttributeModifiers(stack, slot)); -// ModifyItemAttributeModifiersCallback.EVENT.invoker().modifyAttributeModifiers(stack, slot, attributeModifiers); -// return attributeModifiers; -// } - @Redirect( method = "isSuitableFor", at = @At( diff --git a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/ModifyItemAttributeModifiersCallbackTest.java b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/ModifyItemAttributeModifiersCallbackTest.java deleted file mode 100644 index 72e54d9cd6..0000000000 --- a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/ModifyItemAttributeModifiersCallbackTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.test.item; - -import net.minecraft.entity.attribute.EntityAttributeModifier; -import net.minecraft.entity.attribute.EntityAttributes; -import net.minecraft.item.Items; - -import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.item.v1.ModifyItemAttributeModifiersCallback; - -public class ModifyItemAttributeModifiersCallbackTest implements ModInitializer { - public static final int HEAD_SLOT_ID = 3; - public static final EntityAttributeModifier MODIFIER = new EntityAttributeModifier("generic_max_health_modifier", 5.0, EntityAttributeModifier.Operation.ADDITION); - - @Override - public void onInitialize() { - ModifyItemAttributeModifiersCallback.EVENT.register((stack, slot, attributeModifiers) -> { - if (stack.isOf(Items.DIAMOND_HELMET) && slot.getEntitySlotId() == HEAD_SLOT_ID) { - attributeModifiers.put(EntityAttributes.GENERIC_MAX_HEALTH, MODIFIER); - } - }); - } -} diff --git a/fabric-item-api-v1/src/testmod/resources/fabric.mod.json b/fabric-item-api-v1/src/testmod/resources/fabric.mod.json index f7e93dc50f..b5fc1501f6 100644 --- a/fabric-item-api-v1/src/testmod/resources/fabric.mod.json +++ b/fabric-item-api-v1/src/testmod/resources/fabric.mod.json @@ -12,7 +12,6 @@ "main": [ "net.fabricmc.fabric.test.item.CustomDamageTest", "net.fabricmc.fabric.test.item.ItemUpdateAnimationTest", - "net.fabricmc.fabric.test.item.ModifyItemAttributeModifiersCallbackTest", "net.fabricmc.fabric.test.item.ArmorKnockbackResistanceTest", "net.fabricmc.fabric.test.item.FoodGameInitializer" ],