Removed old way of updating recipes based on config reload. Reason: doesn't work on server.
This commit is contained in:
@ -1,70 +0,0 @@
|
|||||||
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;
|
|
||||||
import net.minecraft.item.crafting.RecipeManager;
|
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
|
||||||
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 UpdateRecipesEventHandler {
|
|
||||||
public static class ModBus {
|
|
||||||
@SubscribeEvent
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
Network.channel.send(PacketDistributor.ALL.noArg(), new UpdateRecipesPacket());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class EventBus {
|
|
||||||
@SubscribeEvent
|
|
||||||
@OnlyIn(Dist.CLIENT)
|
|
||||||
public void onPlayerJoinEvent(EntityJoinWorldEvent event) {
|
|
||||||
if (!(event.getEntity() instanceof PlayerEntity)) { return; }
|
|
||||||
updateRecipeManager(event.getEntity().level.getRecipeManager());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void updateRecipeManager(RecipeManager recipeManager) {
|
|
||||||
ArrayList<IRecipe<?>> newRecipes = new ArrayList<>();
|
|
||||||
for (IRecipe<?> recipe : recipeManager.getRecipes()) {
|
|
||||||
if (recipe.getResultItem().getItem() instanceof StrawTotemItem) {
|
|
||||||
StrawTotemItem.setRecipe(recipe);
|
|
||||||
if (!Config.Server.getStrawTotemConfig().getIsEnabled()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newRecipes.add(recipe);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!newRecipes.contains(StrawTotemItem.getRecipe()) && Config.Server.getStrawTotemConfig().getIsEnabled()) {
|
|
||||||
newRecipes.add(StrawTotemItem.getRecipe());
|
|
||||||
}
|
|
||||||
|
|
||||||
recipeManager.replaceRecipes(newRecipes);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
package dev.micle.totemofreviving.network;
|
|
||||||
|
|
||||||
import dev.micle.totemofreviving.event.UpdateRecipesEventHandler;
|
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
import net.minecraft.network.PacketBuffer;
|
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
|
||||||
import net.minecraftforge.fml.DistExecutor;
|
|
||||||
import net.minecraftforge.fml.network.NetworkEvent;
|
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
public class UpdateRecipesPacket {
|
|
||||||
public UpdateRecipesPacket() {}
|
|
||||||
|
|
||||||
public static void encode(final UpdateRecipesPacket packet, final PacketBuffer buffer) {
|
|
||||||
Network.writeVersionInfo(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static UpdateRecipesPacket decode(final PacketBuffer buffer) {
|
|
||||||
Network.checkVersion(buffer);
|
|
||||||
return new UpdateRecipesPacket();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void handle(final UpdateRecipesPacket packet, final Supplier<NetworkEvent.Context> contextSupplier) {
|
|
||||||
final NetworkEvent.Context context = contextSupplier.get();
|
|
||||||
context.enqueueWork(() -> {
|
|
||||||
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientPacketHandler.handle(packet, contextSupplier));
|
|
||||||
});
|
|
||||||
context.setPacketHandled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ClientPacketHandler {
|
|
||||||
private static void handle(UpdateRecipesPacket packet, Supplier<NetworkEvent.Context> contextSupplier) {
|
|
||||||
UpdateRecipesEventHandler.updateRecipeManager(Minecraft.getInstance().getConnection().getRecipeManager());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user