forked from FabricMC/fabric
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '1.18.2' of https://github.com/QuiltMC/quilted-fabric-api …
…into 1.18.2
- Loading branch information
Showing
31 changed files
with
460 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ensions-v1/src/main/java/net/fabricmc/fabric/mixin/dimension/LevelStorageBugfixMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2016, 2017, 2018, 2019 FabricMC | ||
* Copyright 2022 QuiltMC | ||
* | ||
* 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.mixin.dimension; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import com.mojang.datafixers.DataFixer; | ||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Dynamic; | ||
import com.mojang.serialization.Lifecycle; | ||
|
||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.nbt.NbtElement; | ||
import net.minecraft.world.gen.GeneratorOptions; | ||
import net.minecraft.world.level.storage.LevelStorage; | ||
|
||
/** | ||
* After removing a dimension mod or a dimension datapack, Minecraft may fail to enter | ||
* the world, because it fails to deserialize the chunk generator of the custom dimensions in file {@code level.dat} | ||
* This mixin will remove the custom dimensions from the nbt tag, so the deserializer and DFU cannot see custom | ||
* dimensions and won't cause errors. | ||
* The custom dimensions will be re-added later. | ||
* | ||
* <p>This Mixin changes a vanilla behavior that is deemed as a bug (MC-197860). In vanilla, the custom dimension | ||
* is not removed after uninstalling the dimension datapack. | ||
* This makes custom dimensions non-removable. Most players don't want this behavior. | ||
* With this Mixin, custom dimensions will be removed when its datapack is removed. | ||
*/ | ||
@Mixin(LevelStorage.class) | ||
public class LevelStorageBugfixMixin { | ||
@SuppressWarnings("unchecked") | ||
@Inject(method = "readGeneratorProperties", at = @At("HEAD")) | ||
private static <T> void onReadGeneratorProperties( | ||
Dynamic<T> nbt, DataFixer dataFixer, int version, | ||
CallbackInfoReturnable<Pair<GeneratorOptions, Lifecycle>> cir | ||
) { | ||
NbtElement nbtTag = ((Dynamic<NbtElement>) nbt).getValue(); | ||
|
||
NbtCompound worldGenSettings = ((NbtCompound) nbtTag).getCompound("WorldGenSettings"); | ||
|
||
removeNonVanillaDimensionsFromWorldGenSettingsTag(worldGenSettings); | ||
} | ||
|
||
/** | ||
* Removes all non-vanilla dimensions from the tag. The custom dimensions will be re-added later from the datapacks. | ||
*/ | ||
@Unique | ||
private static void removeNonVanillaDimensionsFromWorldGenSettingsTag(NbtCompound worldGenSettings) { | ||
String[] vanillaDimensionIds = | ||
new String[]{"minecraft:overworld", "minecraft:the_nether", "minecraft:the_end"}; | ||
|
||
NbtCompound dimensions = worldGenSettings.getCompound("dimensions"); | ||
|
||
if (dimensions.getSize() > vanillaDimensionIds.length) { | ||
NbtCompound newDimensions = new NbtCompound(); | ||
|
||
for (String dimId : vanillaDimensionIds) { | ||
if (dimensions.contains(dimId)) { | ||
newDimensions.put(dimId, dimensions.getCompound(dimId)); | ||
} | ||
} | ||
|
||
worldGenSettings.put("dimensions", newDimensions); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...1/src/main/java/net/fabricmc/fabric/api/item/v1/ModifyItemAttributeModifiersCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2016, 2017, 2018, 2019 FabricMC | ||
* Copyright 2022 QuiltMC | ||
* | ||
* 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.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. | ||
* | ||
* <pre> | ||
* {@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); | ||
* } | ||
* }); | ||
* } | ||
* </pre> | ||
*/ | ||
@FunctionalInterface | ||
public interface ModifyItemAttributeModifiersCallback { | ||
void modifyAttributeModifiers(ItemStack stack, EquipmentSlot slot, Multimap<EntityAttribute, EntityAttributeModifier> attributeModifiers); | ||
|
||
Event<ModifyItemAttributeModifiersCallback> EVENT = EventFactory.createArrayBacked( | ||
ModifyItemAttributeModifiersCallback.class, | ||
callbacks -> (stack, slot, attributeModifiers) -> { | ||
for (ModifyItemAttributeModifiersCallback callback : callbacks) { | ||
callback.modifyAttributeModifiers(stack, slot, attributeModifiers); | ||
} | ||
} | ||
); | ||
} |
41 changes: 41 additions & 0 deletions
41
.../testmod/java/net/fabricmc/fabric/test/item/ModifyItemAttributeModifiersCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2016, 2017, 2018, 2019 FabricMC | ||
* Copyright 2022 QuiltMC | ||
* | ||
* 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 org.quiltmc.loader.api.ModContainer; | ||
import org.quiltmc.qsl.base.api.entrypoint.ModInitializer; | ||
|
||
import net.minecraft.entity.attribute.EntityAttributeModifier; | ||
import net.minecraft.entity.attribute.EntityAttributes; | ||
import net.minecraft.item.Items; | ||
|
||
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(ModContainer mod) { | ||
ModifyItemAttributeModifiersCallback.EVENT.register((stack, slot, attributeModifiers) -> { | ||
if (stack.isOf(Items.DIAMOND_HELMET) && slot.getEntitySlotId() == HEAD_SLOT_ID) { | ||
attributeModifiers.put(EntityAttributes.GENERIC_MAX_HEALTH, MODIFIER); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.