Skip to content

Commit

Permalink
feat: Update Forge to 1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Nov 25, 2024
1 parent ebe24a2 commit f9f8c3a
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public boolean isFakePlayer(Player player) {

@Override
public ItemStack getCraftingRemainingItem(ItemStack itemStack) {
return itemStack.getCraftingRemainingItem();
return itemStack.getCraftingRemainder();
}

@Override
Expand All @@ -114,8 +114,8 @@ public boolean canItemsStack(ItemStack first, ItemStack second) {
}

@Override
public int getBurnTime(ItemStack itemStack) {
return ForgeHooks.getBurnTime(itemStack, RecipeType.SMELTING);
public int getBurnTime(Level level, ItemStack itemStack) {
return level.fuelValues().burnDuration(itemStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.blay09.mods.balm.api.BalmHooks;
import net.blay09.mods.balm.api.BalmRegistries;
import net.blay09.mods.balm.api.BalmRuntime;
import net.blay09.mods.balm.api.EmptyLoadContext;
import net.blay09.mods.balm.api.block.BalmBlockEntities;
import net.blay09.mods.balm.api.block.BalmBlocks;
import net.blay09.mods.balm.api.command.BalmCommands;
Expand Down Expand Up @@ -39,22 +38,22 @@
import net.blay09.mods.balm.forge.sound.ForgeBalmSounds;
import net.blay09.mods.balm.forge.stats.ForgeBalmStats;
import net.blay09.mods.balm.forge.world.ForgeBalmWorldGen;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.*;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLEnvironment;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public class ForgeBalmRuntime implements BalmRuntime<EmptyLoadContext> {
public class ForgeBalmRuntime implements BalmRuntime<ForgeLoadContext> {
private final BalmWorldGen worldGen = new ForgeBalmWorldGen();
private final BalmBlocks blocks = new ForgeBalmBlocks();
private final BalmBlockEntities blockEntities = new ForgeBalmBlockEntities();
Expand Down Expand Up @@ -176,17 +175,16 @@ public String getModName(String modId) {
}

@Override
public void initialize(String modId, EmptyLoadContext context, Runnable initializer) {
public void initialize(String modId, ForgeLoadContext context, Runnable initializer) {
((ForgeBalmItems) items).register();
((ForgeBalmEntities) entities).register();
((ForgeBalmWorldGen) worldGen).register();
((ForgeBalmStats) stats).register();

initializer.run();

IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.addListener((FMLLoadCompleteEvent event) -> initializeAddons());
DeferredRegisters.register(modId, modEventBus);
context.modBus().addListener((FMLLoadCompleteEvent event) -> initializeAddons());
DeferredRegisters.register(modId, context.modBus());
}

@Override
Expand Down Expand Up @@ -224,8 +222,8 @@ private void initializeAddons() {
}

@Override
public void addServerReloadListener(ResourceLocation identifier, PreparableReloadListener reloadListener) {
MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> event.addListener(reloadListener));
public void addServerReloadListener(ResourceLocation identifier, Function<HolderLookup.Provider, PreparableReloadListener> reloadListener) {
MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> event.addListener(reloadListener.apply(event.getRegistryAccess())));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.blay09.mods.balm.forge;

import net.blay09.mods.balm.api.BalmRuntimeLoadContext;
import net.minecraftforge.eventbus.api.IEventBus;

public record ForgeLoadContext(IEventBus modBus) implements BalmRuntimeLoadContext {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,33 @@
import net.blay09.mods.balm.forge.DeferredRegisters;
import net.blay09.mods.balm.api.DeferredObject;
import net.blay09.mods.balm.api.block.BalmBlocks;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;
import java.util.function.BiFunction;
import java.util.function.Function;

public class ForgeBalmBlocks implements BalmBlocks {
@Override
public BlockBehaviour.Properties blockProperties() {
return BlockBehaviour.Properties.of();
}

@Override
public DeferredObject<Block> registerBlock(Supplier<Block> supplier, ResourceLocation identifier) {
DeferredRegister<Block> register = DeferredRegisters.get(ForgeRegistries.BLOCKS, identifier.getNamespace());
RegistryObject<Block> registryObject = register.register(identifier.getPath(), supplier);
public DeferredObject<Block> registerBlock(Function<ResourceLocation, Block> supplier, ResourceLocation identifier) {
final var register = DeferredRegisters.get(Registries.BLOCK, identifier.getNamespace());
final var registryObject = register.register(identifier.getPath(), () -> supplier.apply(identifier));
return new DeferredObject<>(identifier, registryObject, registryObject::isPresent);
}

@Override
public DeferredObject<Item> registerBlockItem(Supplier<BlockItem> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
return Balm.getItems().registerItem(supplier::get, identifier, creativeTab);
public DeferredObject<Item> registerBlockItem(Function<ResourceLocation, BlockItem> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
return Balm.getItems().registerItem(supplier::apply, identifier, creativeTab);
}

@Override
public void register(Supplier<Block> blockSupplier, Supplier<BlockItem> blockItemSupplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
registerBlock(blockSupplier, identifier);
registerBlockItem(blockItemSupplier, identifier, creativeTab);
public void register(Function<ResourceLocation, Block> blockSupplier, BiFunction<Block, ResourceLocation, BlockItem> blockItemSupplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
final var deferredBlock = registerBlock(blockSupplier, identifier);
registerBlockItem((id) -> blockItemSupplier.apply(deferredBlock.get(), id), identifier, creativeTab);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraftforge.registries.RegistryObject;

import java.util.Arrays;
import java.util.Set;
import java.util.function.Supplier;

public class ForgeBalmBlockEntities implements BalmBlockEntities {
Expand All @@ -21,7 +22,7 @@ public <T extends BlockEntity> DeferredObject<BlockEntityType<T>> registerBlockE
DeferredRegister<BlockEntityType<?>> register = DeferredRegisters.get(ForgeRegistries.BLOCK_ENTITY_TYPES, identifier.getNamespace());
RegistryObject<BlockEntityType<T>> registryObject = register.register(identifier.getPath(), () -> {
Block[] resolvedBlocks = blocks.get();
return BlockEntityType.Builder.of(factory::create, resolvedBlocks).build(null);
return new BlockEntityType<>(factory::create, Set.of(resolvedBlocks));
});
return new DeferredObject<>(identifier, registryObject, registryObject::isPresent);
}
Expand All @@ -31,7 +32,7 @@ public <T extends BlockEntity> DeferredObject<BlockEntityType<T>> registerBlockE
DeferredRegister<BlockEntityType<?>> register = DeferredRegisters.get(ForgeRegistries.BLOCK_ENTITY_TYPES, identifier.getNamespace());
RegistryObject<BlockEntityType<T>> registryObject = register.register(identifier.getPath(), () -> {
Block[] resolvedBlocks = Arrays.stream(blocks).map(DeferredObject::get).toArray(Block[]::new);
return BlockEntityType.Builder.of(factory::create, resolvedBlocks).build(null);
return new BlockEntityType<>(factory::create, Set.of(resolvedBlocks));
});
return new DeferredObject<>(identifier, registryObject, registryObject::isPresent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.blay09.mods.balm.forge.client;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.EmptyLoadContext;
import net.blay09.mods.balm.api.client.BalmClientRuntime;
import net.blay09.mods.balm.api.client.keymappings.BalmKeyMappings;
import net.blay09.mods.balm.api.client.rendering.BalmModels;
import net.blay09.mods.balm.api.client.rendering.BalmRenderers;
import net.blay09.mods.balm.api.client.rendering.BalmTextures;
import net.blay09.mods.balm.api.client.screen.BalmScreens;
import net.blay09.mods.balm.forge.ForgeLoadContext;
import net.blay09.mods.balm.forge.event.ForgeBalmEvents;
import net.blay09.mods.balm.forge.client.keymappings.ForgeBalmKeyMappings;
import net.blay09.mods.balm.forge.client.rendering.ForgeBalmModels;
Expand All @@ -16,7 +16,7 @@
import net.blay09.mods.balm.forge.client.screen.ForgeBalmScreens;
import net.blay09.mods.balm.forge.event.ForgeBalmClientEvents;

public class ForgeBalmClientRuntime implements BalmClientRuntime<EmptyLoadContext> {
public class ForgeBalmClientRuntime implements BalmClientRuntime<ForgeLoadContext> {

private final BalmRenderers renderers = new ForgeBalmRenderers();
private final BalmTextures textures = new ForgeBalmTextures();
Expand Down Expand Up @@ -54,10 +54,10 @@ public BalmKeyMappings getKeyMappings() {
}

@Override
public void initialize(String modId, EmptyLoadContext context, Runnable initializer) {
public void initialize(String modId, ForgeLoadContext context, Runnable initializer) {
((ForgeBalmRenderers) renderers).register();
((ForgeBalmScreens) screens).register();
((ForgeBalmModels) models).register();
((ForgeBalmModels) models).register(modId, context.modBus());
((ForgeBalmKeyMappings) keyMappings).register();

initializer.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.event.ModelEvent;
import net.minecraftforge.client.model.SimpleModelState;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void resolveAndSet(ModelBakery modelBakery, Map<ModelResourceLocation, Ba
set(resolve(modelBakery, modelRegistry, textureGetter));
} catch (Exception exception) {
LOGGER.warn("Unable to bake model: '{}':", getIdentifier(), exception);
set(modelBakery.getBakedTopLevelModels().get(ModelBakery.MISSING_MODEL_LOCATION));
set(modelBakery.getBakedTopLevelModels().get(MissingBlockModel.VARIANT));
}
}

Expand All @@ -69,10 +70,10 @@ public void setTextureGetter(ModelBakery.TextureGetter textureGetter) {
this.textureGetter = textureGetter;
}

@SubscribeEvent
public void onRegisterAdditionalModels(ModelEvent.RegisterAdditional event) {
additionalModels.forEach(it -> event.register(it.getModelResourceLocation()));
}
// TODO @SubscribeEvent
// TODO public void onRegisterAdditionalModels(ModelEvent.RegisterAdditional event) {
// TODO additionalModels.forEach(it -> event.register(it.getModelResourceLocation()));
// TODO }

@SubscribeEvent
public void onModelBakingCompleted(ModelEvent.ModifyBakingResult event) {
Expand All @@ -96,6 +97,7 @@ public void onModelBakingCompleted(ModelEvent.BakingCompleted event) {
}
}


private final Map<String, Registrations> registrations = new ConcurrentHashMap<>();
private ModelBakery modelBakery;

Expand Down Expand Up @@ -158,7 +160,7 @@ public DeferredObject<BakedModel> loadDynamicModel(ModelResourceLocation identif
public BakedModel resolve(ModelBakery bakery, Map<ModelResourceLocation, BakedModel> modelRegistry, ModelBakery.TextureGetter textureGetter) {
final var unbakedModels = new HashMap<ModelResourceLocation, UnbakedModel>();
for (final var modelId : models) {
unbakedModels.put(modelId, ((ModelBakeryAccessor) bakery).callGetModel(modelId.id()));
unbakedModels.put(modelId, getUnbakedModelOrMissing(modelId.id()));
}
return new ForgeCachedDynamicModel(bakery,
unbakedModels,
Expand Down Expand Up @@ -187,20 +189,24 @@ public ModelState getModelState(Transformation transformation) {

@Override
public UnbakedModel getUnbakedModelOrMissing(ResourceLocation location) {
return ((ModelBakeryAccessor) modelBakery).callGetModel(location);
return ((ModelBakeryAccessor) modelBakery).getUnbakedModels().getOrDefault(location, ((ModelBakeryAccessor) modelBakery).getMissingModel());
}

@Override
public UnbakedModel getUnbakedMissingModel() {
return ((ModelBakeryAccessor) modelBakery).callGetModel(ModelBakery.MISSING_MODEL_LOCATION);
return ((ModelBakeryAccessor) modelBakery).getMissingModel();
}

public void register() {
FMLJavaModLoadingContext.get().getModEventBus().register(getActiveRegistrations());
public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return registrations.computeIfAbsent(ModLoadingContext.get().getActiveNamespace(), it -> new Registrations());
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}

@Override
Expand All @@ -214,4 +220,4 @@ public ModelBaker createBaker(ModelResourceLocation location, ModelBakery.Textur
throw new RuntimeException("Balm failed to create model baker", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.blay09.mods.balm.api.DeferredObject;
import net.blay09.mods.balm.api.entity.BalmEntities;
import net.blay09.mods.balm.forge.DeferredRegisters;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
Expand Down Expand Up @@ -40,7 +42,7 @@ public void registerAttributes(EntityAttributeCreationEvent event) {
@Override
public <T extends Entity> DeferredObject<EntityType<T>> registerEntity(ResourceLocation identifier, EntityType.Builder<T> typeBuilder) {
DeferredRegister<EntityType<?>> register = DeferredRegisters.get(ForgeRegistries.ENTITY_TYPES, identifier.getNamespace());
RegistryObject<EntityType<T>> registryObject = register.register(identifier.getPath(), () -> typeBuilder.build(identifier.toString()));
RegistryObject<EntityType<T>> registryObject = register.register(identifier.getPath(), () -> typeBuilder.build(ResourceKey.create(Registries.ENTITY_TYPE, identifier)));
return new DeferredObject<>(identifier, registryObject, registryObject::isPresent);
}

Expand All @@ -49,7 +51,7 @@ public <T extends LivingEntity> DeferredObject<EntityType<T>> registerEntity(Res
final DeferredRegister<EntityType<?>> register = DeferredRegisters.get(ForgeRegistries.ENTITY_TYPES, identifier.getNamespace());
final Registrations registrations = getActiveRegistrations();
final RegistryObject<EntityType<T>> registryObject = register.register(identifier.getPath(), () -> {
EntityType<T> entityType = typeBuilder.build(identifier.toString());
EntityType<T> entityType = typeBuilder.build(ResourceKey.create(Registries.ENTITY_TYPE, identifier));
registrations.attributeSuppliers.put(entityType, attributeBuilder.get().build());
return entityType;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,6 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(RecipesUpdatedEvent.class, priority -> {
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.client.event.RecipesUpdatedEvent orig) -> {
RegistryAccess registryAccess = Minecraft.getInstance().level.registryAccess(); // same way that Minecraft does it in the packet handler
final RecipesUpdatedEvent event = new RecipesUpdatedEvent(orig.getRecipeManager(), registryAccess);
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(ItemTooltipEvent.class, priority -> {
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.event.entity.player.ItemTooltipEvent orig) -> {
final ItemTooltipEvent event = new ItemTooltipEvent(orig.getItemStack(), orig.getEntity(), orig.getToolTip(), orig.getFlags());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;

public class ForgeBalmItems implements BalmItems {
Expand All @@ -48,14 +49,9 @@ public void buildCreativeTabContents(ResourceLocation tabIdentifier, CreativeMod
private final Map<String, Registrations> registrations = new ConcurrentHashMap<>();

@Override
public Item.Properties itemProperties() {
return new Item.Properties();
}

@Override
public DeferredObject<Item> registerItem(Supplier<Item> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
DeferredRegister<Item> register = DeferredRegisters.get(ForgeRegistries.ITEMS, identifier.getNamespace());
RegistryObject<Item> registryObject = register.register(identifier.getPath(), supplier);
public DeferredObject<Item> registerItem(Function<ResourceLocation, Item> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
final var register = DeferredRegisters.get(Registries.ITEM, identifier.getNamespace());
final var registryObject = register.register(identifier.getPath(), () -> supplier.apply(identifier));
if (creativeTab != null) {
getActiveRegistrations().creativeTabContents.put(creativeTab, () -> new ItemLike[]{registryObject.get()});
}
Expand All @@ -64,8 +60,8 @@ public DeferredObject<Item> registerItem(Supplier<Item> supplier, ResourceLocati

@Override
public DeferredObject<CreativeModeTab> registerCreativeModeTab(Supplier<ItemStack> iconSupplier, ResourceLocation identifier) {
DeferredRegister<CreativeModeTab> register = DeferredRegisters.get(Registries.CREATIVE_MODE_TAB, identifier.getNamespace());
RegistryObject<CreativeModeTab> registryObject = register.register(identifier.getPath(), () -> {
final var register = DeferredRegisters.get(Registries.CREATIVE_MODE_TAB, identifier.getNamespace());
final var registryObject = register.register(identifier.getPath(), () -> {
Component displayName = Component.translatable("itemGroup." + identifier.toString().replace(':', '.'));
final var registrations = getActiveRegistrations();
return CreativeModeTab.builder()
Expand Down
Loading

0 comments on commit f9f8c3a

Please sign in to comment.