Skip to content

Commit

Permalink
Port fabric-content-registries-v0
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Jun 19, 2024
1 parent d0bb4ef commit d1308de
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 75 deletions.
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ allprojects {
)
}
}

// Run this task after updating minecraft to regenerate any required resources
tasks.register("generateResources") {

}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@

package net.fabricmc.fabric.api.registry;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Predicate;

import com.mojang.datafixers.util.Pair;
import net.fabricmc.fabric.mixin.content.registry.HoeItemAccessor;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.HoeItem;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.common.ToolActions;
import net.neoforged.neoforge.event.level.BlockEvent;

/**
* A registry for hoe tilling interactions. A vanilla example is turning dirt to dirt paths.
*/
@EventBusSubscriber
public final class TillableBlockRegistry {
private static final Map<Block, Pair<Predicate<UseOnContext>, Consumer<UseOnContext>>> TILLABLES = new IdentityHashMap<>();
private TillableBlockRegistry() {
}

Expand All @@ -53,7 +62,7 @@ private TillableBlockRegistry() {
*/
public static void register(Block input, Predicate<UseOnContext> usagePredicate, Consumer<UseOnContext> tillingAction) {
Objects.requireNonNull(input, "input block cannot be null");
HoeItemAccessor.getTillingActions().put(input, Pair.of(usagePredicate, tillingAction));
TILLABLES.put(input, Pair.of(usagePredicate, tillingAction));
}

/**
Expand Down Expand Up @@ -81,4 +90,20 @@ public static void register(Block input, Predicate<UseOnContext> usagePredicate,
Objects.requireNonNull(droppedItem, "dropped item cannot be null");
register(input, usagePredicate, HoeItem.changeIntoStateAndDropItem(tilled, droppedItem));
}

@SubscribeEvent
static void modify(BlockEvent.BlockToolModificationEvent event) {
if (event.getToolAction() == ToolActions.HOE_TILL && event.getHeldItemStack().canPerformAction(ToolActions.HOE_TILL)) {
var modified = TILLABLES.get(event.getState().getBlock());
if (modified != null && modified.getFirst().test(event.getContext())) {
if (!event.isSimulated() && !event.getLevel().isClientSide()) {
modified.getSecond().accept(event.getContext());
if (event.getContext().getPlayer() != null) {
event.getContext().getItemInHand().hurtAndBreak(1, event.getPlayer(), LivingEntity.getSlotForHand(event.getContext().getHand()));
}
}
event.setCanceled(true);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package net.fabricmc.fabric.api.registry;

import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -41,6 +42,7 @@
* villagers have with the world.
*/
public final class VillagerInteractionRegistries {
public static final Map<VillagerProfession, ResourceKey<LootTable>> MODIFIED_GIFTS = new IdentityHashMap<>();
private static final Logger LOGGER = LoggerFactory.getLogger(VillagerInteractionRegistries.class);

private VillagerInteractionRegistries() {
Expand Down Expand Up @@ -96,11 +98,13 @@ public static void registerGiftLootTable(VillagerProfession profession, Resource
public static void registerGiftLootTable(VillagerProfession profession, ResourceKey<LootTable> lootTable) {
Objects.requireNonNull(profession, "Profession cannot be null!");
Objects.requireNonNull(lootTable, "Loot table identifier cannot be null!");
ResourceKey<LootTable> oldValue = GiveGiftsToHeroTaskAccessor.fabric_getGifts().put(profession, lootTable);
ResourceKey<LootTable> oldValue = GiveGiftsToHeroTaskAccessor.fabric_getGifts().get(profession);

if (oldValue != null) {
LOGGER.info("Overriding previous gift loot table of {} profession, was: {}, now: {}", profession.name(), oldValue, lootTable);
}

MODIFIED_GIFTS.put(profession, lootTable);
}

private static Set<Item> getCollectableRegistry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.ComposterBlock;
import net.neoforged.neoforge.registries.datamaps.builtin.NeoForgeDataMaps;

import java.util.IdentityHashMap;
import java.util.Map;

public class CompostingChanceRegistryImpl implements CompostingChanceRegistry {
static final Map<Item, Float> CUSTOM = new IdentityHashMap<>();
@Override
public Float get(ItemLike item) {
return ComposterBlock.COMPOSTABLES.getOrDefault(item.asItem(), 0.0F);
var fromCustom = CUSTOM.get(item.asItem());
if (fromCustom == null) {
var dmap = item.asItem().builtInRegistryHolder().getData(NeoForgeDataMaps.COMPOSTABLES);
return dmap == null ? 0 : dmap.chance();
}
return fromCustom < 0 ? 0 : fromCustom;
}

@Override
public void add(ItemLike item, Float value) {
ComposterBlock.COMPOSTABLES.put(item.asItem(), value);
CUSTOM.put(item.asItem(), value);
}

@Override
Expand All @@ -40,7 +50,7 @@ public void add(TagKey<Item> tag, Float value) {

@Override
public void remove(ItemLike item) {
ComposterBlock.COMPOSTABLES.removeFloat(item.asItem());
CUSTOM.put(item.asItem(), -1f);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.fabricmc.fabric.impl.content.registry;

import net.fabricmc.fabric.api.registry.VillagerInteractionRegistries;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.neoforged.neoforge.registries.datamaps.DataMapType;
import net.neoforged.neoforge.registries.datamaps.builtin.Compostable;
import net.neoforged.neoforge.registries.datamaps.builtin.NeoForgeDataMaps;
import net.neoforged.neoforge.registries.datamaps.builtin.RaidHeroGift;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

public class DataMapModifications {
@SuppressWarnings("unchecked")
public static <A, T> void modify(Registry<T> registry, DataMapType<T, A> type, ResourceKey<T> key, CallbackInfoReturnable<A> cir) {
if (type == NeoForgeDataMaps.COMPOSTABLES) {
registry.getHolder(key).ifPresent(holder -> {
var fromCustom = CompostingChanceRegistryImpl.CUSTOM.get(holder.value());
if (fromCustom != null) {
if (fromCustom < 0) {
cir.setReturnValue(null);
} else {
cir.setReturnValue((A)new Compostable(fromCustom));
}
}
});
} else if (type == NeoForgeDataMaps.RAID_HERO_GIFTS) {
registry.getHolder(key).ifPresent(holder -> {
var fromCustom = VillagerInteractionRegistries.MODIFIED_GIFTS.get(holder.value());
if (fromCustom != null) {
cir.setReturnValue((A) new RaidHeroGift(fromCustom));
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@

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

import java.util.IdentityHashMap;
import java.util.Map;

import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.furnace.FurnaceFuelBurnTimeEvent;
import net.neoforged.neoforge.registries.datamaps.DataMapsUpdatedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.fabricmc.fabric.api.registry.FuelRegistry;
Expand All @@ -31,10 +38,12 @@
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;

// TODO: Clamp values to 32767 (+ add hook for mods which extend the limit to disable the check?)
@EventBusSubscriber
public final class FuelRegistryImpl implements FuelRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(FuelRegistryImpl.class);
private final Object2IntMap<ItemLike> itemCookTimes = new Object2IntLinkedOpenHashMap<>();
private final Object2IntMap<TagKey<Item>> tagCookTimes = new Object2IntLinkedOpenHashMap<>();
private final Map<Item, Integer> finalTimes = new IdentityHashMap<>();

public FuelRegistryImpl() {
}
Expand Down Expand Up @@ -93,38 +102,43 @@ public void clear(TagKey<Item> tag) {
resetCache();
}

public void apply(Map<Item, Integer> map) {
public void resetCache() {
resetCache(BuiltInRegistries.ITEM);
}

public void resetCache(Registry<Item> registry) {
finalTimes.clear();
// tags take precedence before blocks
for (TagKey<Item> tag : tagCookTimes.keySet()) {
int time = tagCookTimes.getInt(tag);

if (time <= 0) {
for (Holder<Item> key : BuiltInRegistries.ITEM.getTagOrEmpty(tag)) {
final Item item = key.value();
map.remove(item);
}
} else {
AbstractFurnaceBlockEntity.add(map, tag, time);
for (Holder<Item> key : registry.getTagOrEmpty(tag)) {
final Item item = key.value();
finalTimes.put(item, time);
}
}

for (ItemLike item : itemCookTimes.keySet()) {
int time = itemCookTimes.getInt(item);
finalTimes.put(item.asItem(), time);
}
}

if (time <= 0) {
map.remove(item.asItem());
} else {
AbstractFurnaceBlockEntity.add(map, item, time);
}
@SubscribeEvent
static void reload(DataMapsUpdatedEvent event) {
event.ifRegistry(Registries.ITEM, registry -> ((FuelRegistryImpl) FuelRegistry.INSTANCE).resetCache(registry));
}

@SubscribeEvent
static void getBurnTime(FurnaceFuelBurnTimeEvent event) {
var registry = (FuelRegistryImpl) FuelRegistry.INSTANCE;
var modified = registry.finalTimes.get(event.getItemStack().getItem());
if (modified != null) {
event.setBurnTime(modified < 0 ? 0 : modified);
}
}

private static String getTagName(TagKey<?> tag) {
return tag.location().toString();
}

public void resetCache() {
// Note: tag reload is already handled by vanilla, see DataPackContents#refresh
AbstractFurnaceBlockEntity.invalidateCache();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.fabricmc.fabric.mixin.content.registry;

import net.fabricmc.fabric.impl.content.registry.DataMapModifications;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.neoforged.neoforge.registries.BaseMappedRegistry;
import net.neoforged.neoforge.registries.datamaps.DataMapType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(BaseMappedRegistry.class)
public abstract class BaseRegistryMixin<T> {
@Inject(at = @At("HEAD"), method = "getData", cancellable = true)
private <A> void getDataMapConsideringFAPI(DataMapType<T, A> type, ResourceKey<T> key, CallbackInfoReturnable<A> cir) {
DataMapModifications.modify((Registry<T>) this, type, key, cir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import net.minecraft.world.level.block.state.BlockBehaviour;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -45,7 +46,7 @@ private int getIgniteOdds(BlockState block_1) {
}

@Inject(at = @At("RETURN"), method = "<init>")
private void afterConstruct(Block.Settings settings, CallbackInfo info) {
private void afterConstruct(BlockBehaviour.Properties settings, CallbackInfo info) {
fabric_registry = FlammableBlockRegistryImpl.getInstance((Block) (Object) this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"compatibilityLevel": "JAVA_17",
"mixins": [
"AxeItemAccessor",
"BaseRegistryMixin",
"BrewingRecipeRegistryBuilderMixin",
"PathContextMixin",
"FarmerWorkTaskAccessor",
"FireBlockMixin",
"GiveGiftsToHeroTaskAccessor",
"HoeItemAccessor",
"HoneycombItemMixin",
"LandPathNodeMakerMixin",
"AbstractFurnaceBlockEntityMixin",
"FireBlockMixin",
"OxidizableMixin",
"PathContextMixin",
"ShovelItemAccessor",
"VillagerEntityAccessor"
],
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ include("fabric-block-view-api-v2")
include("fabric-blockrenderlayer-v1")
//include 'fabric-client-tags-api-v1'
include("fabric-command-api-v2")
//include 'fabric-content-registries-v0'
include("fabric-content-registries-v0")
//include 'fabric-convention-tags-v1'
//include 'fabric-data-generation-api-v1'
//include 'fabric-entity-events-v1'
Expand Down

0 comments on commit d1308de

Please sign in to comment.