Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.registry;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.minecraft.component.type.FireworkExplosionComponent;
import net.minecraft.item.ItemConvertible;
import net.minecraft.recipe.FireworkStarRecipe;

import net.fabricmc.fabric.impl.content.registry.FireworkStarExplosionTypeRegistryImpl;

/**
* A registry for the {@link FireworkExplosionComponent.Type} of an item for the {@link FireworkStarRecipe}.
*/
public final class FireworkStarExplosionTypeRegistry {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this class seems misleading, as it appears to be a registry for the crafting recipe's map rather than the actual Type enum. Consider changing this to FireworkStarExplosionTypeRecipeRegistry (although this also seems a bit verbose?).

private static final Logger LOGGER = LoggerFactory.getLogger(FireworkStarExplosionTypeRegistry.class);

private FireworkStarExplosionTypeRegistry() {
}

/**
* Registers an explosion type for the item.
* @param item the item to register
* @param explosionType the firework explosion type
*/
public static void register(ItemConvertible item, FireworkExplosionComponent.Type explosionType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the logic in here should be moved to the impl class, and this method should only be calling a register method in the impl class instead.

Objects.requireNonNull(item, "Item cannot be null!");
FireworkExplosionComponent.Type oldValue = FireworkStarExplosionTypeRegistryImpl.getFireworkStarExplosionTypeRegistry().put(item.asItem(), explosionType);

if (oldValue != null) {
LOGGER.info("Overriding previous firework star explosion type of {}, was: {}, now: {}", item.asItem().toString(), oldValue, explosionType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.impl.content.registry;

import java.util.Map;

import net.minecraft.component.type.FireworkExplosionComponent;
import net.minecraft.item.Item;

import net.fabricmc.fabric.impl.content.registry.util.ImmutableCollectionUtils;
import net.fabricmc.fabric.mixin.content.registry.FireworkStarRecipeAccessor;

public final class FireworkStarExplosionTypeRegistryImpl {
private FireworkStarExplosionTypeRegistryImpl() {
}

public static Map<Item, FireworkExplosionComponent.Type> getFireworkStarExplosionTypeRegistry() {
return ImmutableCollectionUtils.getAsMutableMap(FireworkStarRecipeAccessor::fabric_getTypeModifierMap, FireworkStarRecipeAccessor::fabric_setTypeModifierMap);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.mixin.content.registry;

import java.util.Map;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.component.type.FireworkExplosionComponent;
import net.minecraft.item.Item;
import net.minecraft.recipe.FireworkStarRecipe;

@Mixin(FireworkStarRecipe.class)
public interface FireworkStarRecipeAccessor {
@Mutable
@Accessor("TYPE_MODIFIER_MAP")
static void fabric_setTypeModifierMap(Map<Item, FireworkExplosionComponent.Type> typeModifierMap) {
throw new AssertionError("Untransformed @Accessor");
}

@Accessor("TYPE_MODIFIER_MAP")
static Map<Item, FireworkExplosionComponent.Type> fabric_getTypeModifierMap() {
throw new AssertionError("Untransformed @Accessor");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"BrewingRecipeRegistryBuilderMixin",
"FarmerWorkTaskAccessor",
"FireBlockMixin",
"FireworkStarRecipeAccessor",
"FuelRegistryMixin",
"GiveGiftsToHeroTaskAccessor",
"GiveGiftsToHeroTaskMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package net.fabricmc.fabric.test.content.registry;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import it.unimi.dsi.fastutil.ints.IntList;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.ComposterBlock;
Expand All @@ -29,13 +32,18 @@
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.block.enums.BlockHalf;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.FireworkExplosionComponent;
import net.minecraft.component.type.PotionContentsComponent;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.potion.Potions;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.input.CraftingRecipeInput;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.test.TestContext;
import net.minecraft.text.Text;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
Expand Down Expand Up @@ -243,4 +251,25 @@ public void testBrewingDirt(TestContext context) {
context.complete();
});
}

@GameTest
public void testFireworkStarExplosionTypeRegistry(TestContext context) {
CraftingRecipeInput inventory = CraftingRecipeInput.create(1, 3, List.of(
Items.GUNPOWDER.getDefaultStack(),
Items.RED_DYE.getDefaultStack(),
Items.NETHER_STAR.getDefaultStack()));
ServerWorld world = context.getWorld();
ItemStack result = world.getRecipeManager().getFirstMatch(RecipeType.CRAFTING, inventory, world)
.map(recipe -> recipe.value().craft(inventory, world.getRegistryManager()))
.orElse(ItemStack.EMPTY);
ItemStack expected = Items.FIREWORK_STAR.getDefaultStack();
expected.set(DataComponentTypes.FIREWORK_EXPLOSION, new FireworkExplosionComponent(
FireworkExplosionComponent.Type.STAR,
IntList.of(DyeColor.RED.getFireworkColor()),
IntList.of(),
false,
false));
context.assertTrue(ItemStack.areEqual(result, expected), Text.literal("crafting result should be red firework star with star explosion type"));
context.complete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.block.Blocks;
import net.minecraft.block.Oxidizable;
import net.minecraft.block.OxidizableBlock;
import net.minecraft.component.type.FireworkExplosionComponent;
import net.minecraft.entity.ai.pathing.PathNodeType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.HoeItem;
Expand Down Expand Up @@ -55,6 +56,7 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.registry.CompostingChanceRegistry;
import net.fabricmc.fabric.api.registry.FabricBrewingRecipeRegistryBuilder;
import net.fabricmc.fabric.api.registry.FireworkStarExplosionTypeRegistry;
import net.fabricmc.fabric.api.registry.FlammableBlockRegistry;
import net.fabricmc.fabric.api.registry.FlattenableBlockRegistry;
import net.fabricmc.fabric.api.registry.FuelRegistryEvents;
Expand Down Expand Up @@ -107,6 +109,7 @@ public void onInitialize() {
// - instant health potions can be brewed from awkward potions with any item in the 'minecraft:small_flowers' tag
// - if Redstone Experiments experiment is enabled, luck potions can be brewed from awkward potions with a bundle
// - dirty potions can be brewed by adding any item in the 'minecraft:dirt' tag to any standard potion
// - nether stars can be used to create a firework star with the star explosion type

CompostingChanceRegistry.INSTANCE.add(Items.OBSIDIAN, 0.5F);
FlammableBlockRegistry.getDefaultInstance().add(Blocks.DIAMOND_BLOCK, 4, 4);
Expand Down Expand Up @@ -195,6 +198,8 @@ public void onInitialize() {
builder.registerPotionRecipe(Potions.AWKWARD, Ingredient.ofItems(Items.BUNDLE), Potions.LUCK);
}
});

FireworkStarExplosionTypeRegistry.register(Items.NETHER_STAR, FireworkExplosionComponent.Type.STAR);
}

public static class TestEventBlock extends Block {
Expand Down
Loading