send recipe JSON to all clients connecting to a server (does nothing in singleplayer, can be disabled via config)

This commit is contained in:
George Paton 2025-05-23 13:16:08 +10:00
parent e5cfc9cf2b
commit 933ecded5d
5 changed files with 201 additions and 63 deletions

View File

@ -38,6 +38,7 @@ public class GeneralConfig {
public static boolean enableGuideBook = true; public static boolean enableGuideBook = true;
public static boolean enableSoundExtension = true; public static boolean enableSoundExtension = true;
public static boolean enableMekanismChanges = true; public static boolean enableMekanismChanges = true;
public static boolean enableServerRecipeSync = true;
public static int normalSoundChannels = 200; public static int normalSoundChannels = 200;
public static boolean enableExpensiveMode = false; public static boolean enableExpensiveMode = false;
@ -81,6 +82,8 @@ public class GeneralConfig {
packetThreadingMaxCount = config.get(CATEGORY_GENERAL, "0.03_packetThreadingMaxCount", 1, "Maximum number of threads to create for packet threading. Must be greater than or equal to 0.02_packetThreadingCoreCount.").getInt(1); packetThreadingMaxCount = config.get(CATEGORY_GENERAL, "0.03_packetThreadingMaxCount", 1, "Maximum number of threads to create for packet threading. Must be greater than or equal to 0.02_packetThreadingCoreCount.").getInt(1);
packetThreadingErrorBypass = config.get(CATEGORY_GENERAL, "0.04_packetThreadingErrorBypass", false, "Forces the bypassing of most packet threading errors, only enable this if directed to or if you know what you're doing.").getBoolean(false); packetThreadingErrorBypass = config.get(CATEGORY_GENERAL, "0.04_packetThreadingErrorBypass", false, "Forces the bypassing of most packet threading errors, only enable this if directed to or if you know what you're doing.").getBoolean(false);
enableServerRecipeSync = config.get(CATEGORY_GENERAL, "0.05_enableServerRecipeSync", true, "Syncs any recipes customised via JSON to clients connecting to the server.").getBoolean(true);
enableDebugMode = config.get(CATEGORY_GENERAL, "1.00_enableDebugMode", false, "Enable debugging mode").getBoolean(false); enableDebugMode = config.get(CATEGORY_GENERAL, "1.00_enableDebugMode", false, "Enable debugging mode").getBoolean(false);
enableMycelium = config.get(CATEGORY_GENERAL, "1.01_enableMyceliumSpread", false, "Allows glowing mycelium to spread").getBoolean(false); enableMycelium = config.get(CATEGORY_GENERAL, "1.01_enableMyceliumSpread", false, "Allows glowing mycelium to spread").getBoolean(false);
enablePlutoniumOre = config.get(CATEGORY_GENERAL, "1.02_enablePlutoniumNetherOre", false, "Enables plutonium ore generation in the nether").getBoolean(false); enablePlutoniumOre = config.get(CATEGORY_GENERAL, "1.02_enablePlutoniumNetherOre", false, "Enables plutonium ore generation in the nether").getBoolean(false);

View File

@ -1,15 +1,12 @@
package com.hbm.inventory.recipes.loader; package com.hbm.inventory.recipes.loader;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
@ -37,8 +34,10 @@ import net.minecraft.item.ItemStack;
public abstract class SerializableRecipe { public abstract class SerializableRecipe {
public static final Gson gson = new Gson(); public static final Gson gson = new Gson();
public static List<SerializableRecipe> recipeHandlers = new ArrayList(); public static List<SerializableRecipe> recipeHandlers = new ArrayList<>();
public static List<IRecipeRegisterListener> additionalListeners = new ArrayList(); public static List<IRecipeRegisterListener> additionalListeners = new ArrayList<>();
public static Map<String, InputStream> recipeSyncHandlers = new HashMap<>();
public boolean modified = false; public boolean modified = false;
@ -108,7 +107,19 @@ public abstract class SerializableRecipe {
recipe.deleteRecipes(); recipe.deleteRecipes();
File recFile = new File(recDir.getAbsolutePath() + File.separatorChar + recipe.getFileName()); File recFile = new File(recDir.getAbsolutePath() + File.separatorChar + recipe.getFileName());
if(recFile.exists() && recFile.isFile()) { if(recipeSyncHandlers.containsKey(recipe.getFileName())) {
MainRegistry.logger.info("Reading synced recipe file " + recipe.getFileName());
InputStream stream = recipeSyncHandlers.get(recipe.getFileName());
try {
stream.reset();
Reader reader = new InputStreamReader(stream);
recipe.readRecipeStream(reader);
recipe.modified = true;
} catch(IOException ex) {
MainRegistry.logger.error("Failed to reset synced recipe stream", ex);
}
} else if(recFile.exists() && recFile.isFile()) {
MainRegistry.logger.info("Reading recipe file " + recFile.getName()); MainRegistry.logger.info("Reading recipe file " + recFile.getName());
recipe.readRecipeFile(recFile); recipe.readRecipeFile(recFile);
recipe.modified = true; recipe.modified = true;
@ -132,6 +143,17 @@ public abstract class SerializableRecipe {
MainRegistry.logger.info("Finished recipe init!"); MainRegistry.logger.info("Finished recipe init!");
} }
public static void receiveRecipes(String filename, byte[] data) {
recipeSyncHandlers.put(filename, new ByteArrayInputStream(data));
}
public static void clearReceivedRecipes() {
boolean hasCleared = !recipeSyncHandlers.isEmpty();
recipeSyncHandlers.clear();
if(hasCleared) initialize();
}
/* /*
* ABSTRACT * ABSTRACT
*/ */
@ -203,16 +225,19 @@ public abstract class SerializableRecipe {
} }
public void readRecipeFile(File file) { public void readRecipeFile(File file) {
try { try {
JsonObject json = gson.fromJson(new FileReader(file), JsonObject.class); readRecipeStream(new FileReader(file));
JsonArray recipes = json.get("recipes").getAsJsonArray();
for(JsonElement recipe : recipes) {
this.readRecipe(recipe);
}
} catch(FileNotFoundException ex) { } } catch(FileNotFoundException ex) { }
} }
public void readRecipeStream(Reader reader) {
JsonObject json = gson.fromJson(reader, JsonObject.class);
JsonArray recipes = json.get("recipes").getAsJsonArray();
for(JsonElement recipe : recipes) {
this.readRecipe(recipe);
}
}
/* /*
* JSON IO UTIL * JSON IO UTIL
*/ */

View File

@ -11,7 +11,6 @@ import com.hbm.config.RadiationConfig;
import com.hbm.config.ServerConfig; import com.hbm.config.ServerConfig;
import com.hbm.entity.mob.*; import com.hbm.entity.mob.*;
import com.hbm.entity.mob.ai.EntityAIFireGun; import com.hbm.entity.mob.ai.EntityAIFireGun;
import com.hbm.entity.mob.EntityCreeperTainted;
import com.hbm.entity.projectile.EntityBulletBaseMK4; import com.hbm.entity.projectile.EntityBulletBaseMK4;
import com.hbm.entity.projectile.EntityBurningFOEQ; import com.hbm.entity.projectile.EntityBurningFOEQ;
import com.hbm.entity.train.EntityRailCarBase; import com.hbm.entity.train.EntityRailCarBase;
@ -24,6 +23,7 @@ import com.hbm.handler.EntityEffectHandler;
import com.hbm.hazard.HazardSystem; import com.hbm.hazard.HazardSystem;
import com.hbm.interfaces.IBomb; import com.hbm.interfaces.IBomb;
import com.hbm.interfaces.Spaghetti; import com.hbm.interfaces.Spaghetti;
import com.hbm.inventory.recipes.loader.SerializableRecipe;
import com.hbm.handler.HTTPHandler; import com.hbm.handler.HTTPHandler;
import com.hbm.handler.HbmKeybinds.EnumKeybind; import com.hbm.handler.HbmKeybinds.EnumKeybind;
import com.hbm.handler.neutron.NeutronNodeWorld; import com.hbm.handler.neutron.NeutronNodeWorld;
@ -43,6 +43,7 @@ import com.hbm.lib.RefStrings;
import com.hbm.packet.PacketDispatcher; import com.hbm.packet.PacketDispatcher;
import com.hbm.packet.toclient.PermaSyncPacket; import com.hbm.packet.toclient.PermaSyncPacket;
import com.hbm.packet.toclient.PlayerInformPacket; import com.hbm.packet.toclient.PlayerInformPacket;
import com.hbm.packet.toclient.SerializableRecipePacket;
import com.hbm.particle.helper.BlackPowderCreator; import com.hbm.particle.helper.BlackPowderCreator;
import com.hbm.potion.HbmPotion; import com.hbm.potion.HbmPotion;
import com.hbm.tileentity.machine.TileEntityMachineRadarNT; import com.hbm.tileentity.machine.TileEntityMachineRadarNT;
@ -53,6 +54,8 @@ import com.hbm.uninos.UniNodespace;
import com.hbm.util.*; import com.hbm.util.*;
import com.hbm.util.ArmorRegistry.HazardClass; import com.hbm.util.ArmorRegistry.HazardClass;
import com.hbm.world.generator.TimedGenerator; import com.hbm.world.generator.TimedGenerator;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent; import cpw.mods.fml.common.gameevent.PlayerEvent;
@ -60,7 +63,9 @@ import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent; import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent.ClientDisconnectionFromServerEvent;
import cpw.mods.fml.relauncher.ReflectionHelper; import cpw.mods.fml.relauncher.ReflectionHelper;
import cpw.mods.fml.relauncher.Side;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator; import io.netty.buffer.PooledByteBufAllocator;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -113,6 +118,7 @@ import net.minecraftforge.event.world.WorldEvent;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.*;
@ -158,9 +164,35 @@ public class ModEventHandler {
props.hasReceivedBook = true; props.hasReceivedBook = true;
} }
} }
if(GeneralConfig.enableServerRecipeSync && FMLCommonHandler.instance().getSide() == Side.SERVER && event.player instanceof EntityPlayerMP) {
File recDir = new File(MainRegistry.configDir.getAbsolutePath() + File.separatorChar + "hbmRecipes");
MainRegistry.logger.info("Sending recipes to client!");
boolean hasSent = false;
for(SerializableRecipe recipe : SerializableRecipe.recipeHandlers) {
File recFile = new File(recDir.getAbsolutePath() + File.separatorChar + recipe.getFileName());
if(recFile.exists() && recFile.isFile()) {
MainRegistry.logger.info("Sending recipe file: " + recFile.getName());
PacketDispatcher.wrapper.sendTo(new SerializableRecipePacket(recFile), (EntityPlayerMP) event.player);
hasSent = true;
}
}
if(hasSent) {
PacketDispatcher.wrapper.sendTo(new SerializableRecipePacket(true), (EntityPlayerMP) event.player);
}
}
} }
} }
@SubscribeEvent
public void onPlayerLeftClient(ClientDisconnectionFromServerEvent event) {
SerializableRecipe.clearReceivedRecipes();
}
@SubscribeEvent @SubscribeEvent
public void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) { public void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) {

View File

@ -69,6 +69,8 @@ public class PacketDispatcher {
wrapper.registerMessage(BiomeSyncPacket.Handler.class, BiomeSyncPacket.class, i++, Side.CLIENT); wrapper.registerMessage(BiomeSyncPacket.Handler.class, BiomeSyncPacket.class, i++, Side.CLIENT);
//The not-so-convenient but not laggy one //The not-so-convenient but not laggy one
wrapper.registerMessage(BufPacket.Handler.class, BufPacket.class, i++, Side.CLIENT); wrapper.registerMessage(BufPacket.Handler.class, BufPacket.class, i++, Side.CLIENT);
//Syncs server recipe configs to the client
wrapper.registerMessage(SerializableRecipePacket.Handler.class, SerializableRecipePacket.class, i++, Side.CLIENT);
} }
} }

View File

@ -0,0 +1,76 @@
package com.hbm.packet.toclient;
import java.io.*;
import java.nio.file.Files;
import com.hbm.inventory.recipes.loader.SerializableRecipe;
import com.hbm.util.BufferUtil;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
public class SerializableRecipePacket implements IMessage {
private String filename;
private byte[] fileBytes;
private boolean reinit;
public SerializableRecipePacket() {}
public SerializableRecipePacket(File recipeFile) {
try {
filename = recipeFile.getName();
fileBytes = Files.readAllBytes(recipeFile.toPath());
} catch(IOException ex) {}
}
public SerializableRecipePacket(boolean reinit) {
this.reinit = reinit;
}
@Override
public void fromBytes(ByteBuf buf) {
reinit = buf.readBoolean();
if(reinit) return;
filename = BufferUtil.readString(buf);
fileBytes = new byte[buf.readInt()];
buf.readBytes(fileBytes);
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeBoolean(reinit);
if(reinit) return;
BufferUtil.writeString(buf, filename);
buf.writeInt(fileBytes.length);
buf.writeBytes(fileBytes);
}
public static class Handler implements IMessageHandler<SerializableRecipePacket, IMessage> {
@Override
@SideOnly(Side.CLIENT)
public IMessage onMessage(SerializableRecipePacket m, MessageContext ctx) {
try {
// Only reinitialize after receiving all recipes
if(m.reinit) {
SerializableRecipe.initialize();
return null;
}
SerializableRecipe.receiveRecipes(m.filename, m.fileBytes);
} catch (Exception x) { }
return null;
}
}
}