Renamed RecipeUpdateEventHandler to UpdateRecipesEventHandler. Added method for mod config reload event on server-side to send packet to client.

This commit is contained in:
2022-01-16 13:34:18 +00:00
parent 438d543283
commit b7d29ecf27
2 changed files with 20 additions and 15 deletions

View File

@ -3,6 +3,8 @@ package dev.micle.totemofreviving.event;
import dev.micle.totemofreviving.TotemOfReviving;
import dev.micle.totemofreviving.config.Config;
import dev.micle.totemofreviving.item.StrawTotemItem;
import dev.micle.totemofreviving.network.Network;
import dev.micle.totemofreviving.network.UpdateRecipesPacket;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.crafting.IRecipe;
@ -12,26 +14,29 @@ import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.network.PacketDistributor;
import java.util.ArrayList;
public class RecipeUpdateEventHandler {
public class UpdateRecipesEventHandler {
public static class ModBus {
@SubscribeEvent
@OnlyIn(Dist.CLIENT)
public void onModConfigReloadEvent(ModConfig.Reloading event) {
public void onClientModConfigReloadEvent(ModConfig.Reloading event) {
if (!event.getConfig().getModId().equals(TotemOfReviving.MOD_ID) ||
Minecraft.getInstance().getSingleplayerServer() == null) {
return;
}
updateRecipeManager(Minecraft.getInstance().getSingleplayerServer().getRecipeManager());
}
@SubscribeEvent
@OnlyIn(Dist.DEDICATED_SERVER)
public void onServerModConfigReloadEvent(ModConfig.Reloading event) {
if (!event.getConfig().getModId().equals(TotemOfReviving.MOD_ID)) {
return;
}
if (Minecraft.getInstance().getSingleplayerServer() != null) {
updateRecipeManager(Minecraft.getInstance().getSingleplayerServer().getRecipeManager());
return;
}
if (Minecraft.getInstance().getConnection() != null) {
updateRecipeManager(Minecraft.getInstance().getConnection().getRecipeManager());
return;
}
System.out.println("FAILED TO GET RECIPE MANAGER!");
Network.channel.send(PacketDistributor.ALL.noArg(), new UpdateRecipesPacket());
}
}
@ -44,7 +49,7 @@ public class RecipeUpdateEventHandler {
}
}
private static void updateRecipeManager(RecipeManager recipeManager) {
public static void updateRecipeManager(RecipeManager recipeManager) {
ArrayList<IRecipe<?>> newRecipes = new ArrayList<>();
for (IRecipe<?> recipe : recipeManager.getRecipes()) {
if (recipe.getResultItem().getItem() instanceof StrawTotemItem) {

View File

@ -1,7 +1,7 @@
package dev.micle.totemofreviving.setup;
import dev.micle.totemofreviving.TotemOfReviving;
import dev.micle.totemofreviving.event.RecipeUpdateEventHandler;
import dev.micle.totemofreviving.event.UpdateRecipesEventHandler;
import net.minecraft.item.Item;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
@ -20,9 +20,9 @@ public class Registration {
ITEMS.register(modEventBus);
ModItems.register();
modEventBus.register(new RecipeUpdateEventHandler.ModBus());
modEventBus.register(new UpdateRecipesEventHandler.ModBus());
MinecraftForge.EVENT_BUS.register(new RecipeUpdateEventHandler.EventBus());
MinecraftForge.EVENT_BUS.register(new UpdateRecipesEventHandler.EventBus());
}
private static <T extends IForgeRegistryEntry<T>> DeferredRegister<T> create(IForgeRegistry<T> registry) {