Add server to client config sync.

This commit is contained in:
raoulvdberge
2018-06-27 11:06:26 +02:00
parent 89b7d2bfc8
commit 8b893aecb7
9 changed files with 75 additions and 21 deletions

View File

@@ -43,7 +43,7 @@ public final class RS {
@EventHandler @EventHandler
public void preInit(FMLPreInitializationEvent e) { public void preInit(FMLPreInitializationEvent e) {
config = new RSConfig(e.getSuggestedConfigurationFile()); config = new RSConfig(null, e.getSuggestedConfigurationFile());
PROXY.preInit(e); PROXY.preInit(e);
} }

View File

@@ -7,12 +7,14 @@ import net.minecraftforge.fml.client.config.IConfigElement;
import net.minecraftforge.fml.client.event.ConfigChangedEvent; import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import javax.annotation.Nullable;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public final class RSConfig { public class RSConfig {
private Configuration config; private Configuration config;
private RSConfig originalClientVersion;
//region Energy //region Energy
public int controllerBaseUsage; public int controllerBaseUsage;
@@ -119,18 +121,28 @@ public final class RSConfig {
private static final String UPGRADES = "upgrades"; private static final String UPGRADES = "upgrades";
//endregion //endregion
public RSConfig(File configFile) { public RSConfig(@Nullable RSConfig originalClientVersion, File configFile) {
config = new Configuration(configFile); this(originalClientVersion, new Configuration(configFile));
}
public RSConfig(@Nullable RSConfig originalClientVersion, Configuration config) {
this.originalClientVersion = originalClientVersion;
this.config = config;
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(this);
loadConfig(); this.loadConfig();
} }
public Configuration getConfig() { public Configuration getConfig() {
return config; return config;
} }
@Nullable
public RSConfig getOriginalClientVersion() {
return originalClientVersion;
}
@SubscribeEvent @SubscribeEvent
public void onConfigChangedEvent(ConfigChangedEvent.OnConfigChangedEvent event) { public void onConfigChangedEvent(ConfigChangedEvent.OnConfigChangedEvent event) {
if (event.getModID().equalsIgnoreCase(RS.ID)) { if (event.getModID().equalsIgnoreCase(RS.ID)) {

View File

@@ -109,7 +109,7 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
if (!controller.getPos().equals(otherControllerPos)) { if (!controller.getPos().equals(otherControllerPos)) {
IBlockState state = world.getBlockState(otherControllerPos); IBlockState state = world.getBlockState(otherControllerPos);
ItemStack stackToSpawn = ItemBlockController.createStackWithNBT(new ItemStack(RSBlocks.CONTROLLER, 1, state.getBlock().getMetaFromState(state))); ItemStack stackToSpawn = ItemBlockController.createStack(new ItemStack(RSBlocks.CONTROLLER, 1, state.getBlock().getMetaFromState(state)));
world.setBlockToAir(otherControllerPos); world.setBlockToAir(otherControllerPos);

View File

@@ -34,7 +34,7 @@ public class BlockController extends BlockBase {
@Override @Override
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> items) { public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> items) {
for (int i = 0; i <= 1; i++) { for (int i = 0; i <= 1; i++) {
items.add(ItemBlockController.createStackWithNBT(new ItemStack(this, 1, i))); items.add(ItemBlockController.createStack(new ItemStack(this, 1, i)));
} }
} }
@@ -102,7 +102,6 @@ public class BlockController extends BlockBase {
stack.setTagCompound(new NBTTagCompound()); stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setInteger(TileController.NBT_ENERGY, ((TileController) world.getTileEntity(pos)).getEnergy().getStored()); stack.getTagCompound().setInteger(TileController.NBT_ENERGY, ((TileController) world.getTileEntity(pos)).getEnergy().getStored());
stack.getTagCompound().setInteger(TileController.NBT_ENERGY_CAPACITY, ((TileController) world.getTileEntity(pos)).getEnergy().getCapacity());
drops.add(stack); drops.add(stack);
} }

View File

@@ -24,7 +24,7 @@ public class ItemBlockController extends ItemBlockBase {
super.addInformation(stack, world, tooltip, flag); super.addInformation(stack, world, tooltip, flag);
if (stack.getMetadata() != ControllerType.CREATIVE.getId()) { if (stack.getMetadata() != ControllerType.CREATIVE.getId()) {
tooltip.add(I18n.format("misc.refinedstorage:energy_stored", getEnergyStored(stack), getEnergyCapacity(stack))); tooltip.add(I18n.format("misc.refinedstorage:energy_stored", getEnergyStored(stack), RS.INSTANCE.config.controllerCapacity));
} }
} }
@@ -32,18 +32,14 @@ public class ItemBlockController extends ItemBlockBase {
return (stack.hasTagCompound() && stack.getTagCompound().hasKey(TileController.NBT_ENERGY)) ? stack.getTagCompound().getInteger(TileController.NBT_ENERGY) : 0; return (stack.hasTagCompound() && stack.getTagCompound().hasKey(TileController.NBT_ENERGY)) ? stack.getTagCompound().getInteger(TileController.NBT_ENERGY) : 0;
} }
public static int getEnergyCapacity(ItemStack stack) {
return (stack.hasTagCompound() && stack.getTagCompound().hasKey(TileController.NBT_ENERGY_CAPACITY)) ? stack.getTagCompound().getInteger(TileController.NBT_ENERGY_CAPACITY) : RS.INSTANCE.config.controllerCapacity;
}
@Override @Override
public void onCreated(ItemStack stack, World world, EntityPlayer player) { public void onCreated(ItemStack stack, World world, EntityPlayer player) {
super.onCreated(stack, world, player); super.onCreated(stack, world, player);
createStackWithNBT(stack); createStack(stack);
} }
public static ItemStack createStackWithNBT(ItemStack stack) { public static ItemStack createStack(ItemStack stack) {
NBTTagCompound tag = stack.getTagCompound(); NBTTagCompound tag = stack.getTagCompound();
if (tag == null) { if (tag == null) {

View File

@@ -0,0 +1,29 @@
package com.raoulvdberge.refinedstorage.network;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.RSConfig;
import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
public class MessageConfigSync implements IMessage, IMessageHandler<MessageConfigSync, IMessage> {
@Override
public void fromBytes(ByteBuf buf) {
RSConfig serverVersion = new RSConfig(RS.INSTANCE.config, RS.INSTANCE.config.getConfig());
serverVersion.controllerCapacity = buf.readInt();
RS.INSTANCE.config = serverVersion;
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(RS.INSTANCE.config.controllerCapacity);
}
@Override
public IMessage onMessage(MessageConfigSync message, MessageContext ctx) {
return null;
}
}

View File

@@ -273,7 +273,7 @@ public class ProxyClient extends ProxyCommon {
ModelLoader.setCustomStateMapper(RSBlocks.CRAFTER_MANAGER, new StateMapperCTM("refinedstorage:crafter_manager")); ModelLoader.setCustomStateMapper(RSBlocks.CRAFTER_MANAGER, new StateMapperCTM("refinedstorage:crafter_manager"));
ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(RSBlocks.CONTROLLER), stack -> { ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(RSBlocks.CONTROLLER), stack -> {
ControllerEnergyType energyType = stack.getItemDamage() == ControllerType.CREATIVE.getId() ? ControllerEnergyType.ON : TileController.getEnergyType(ItemBlockController.getEnergyStored(stack), ItemBlockController.getEnergyCapacity(stack)); ControllerEnergyType energyType = stack.getItemDamage() == ControllerType.CREATIVE.getId() ? ControllerEnergyType.ON : TileController.getEnergyType(ItemBlockController.getEnergyStored(stack), RS.INSTANCE.config.controllerCapacity);
return new ModelResourceLocation("refinedstorage:controller" + (Loader.isModLoaded("ctm") ? "_glow" : ""), "energy_type=" + energyType); return new ModelResourceLocation("refinedstorage:controller" + (Loader.isModLoaded("ctm") ? "_glow" : ""), "energy_type=" + energyType);
}); });

View File

@@ -57,6 +57,7 @@ import net.minecraft.block.Block;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentData; import net.minecraft.enchantment.EnchantmentData;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
@@ -72,6 +73,7 @@ import net.minecraftforge.common.crafting.IIngredientFactory;
import net.minecraftforge.common.crafting.JsonContext; import net.minecraftforge.common.crafting.JsonContext;
import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
@@ -187,6 +189,7 @@ public class ProxyCommon {
RS.INSTANCE.network.registerMessage(MessageWirelessCraftingMonitorSettings.class, MessageWirelessCraftingMonitorSettings.class, id++, Side.SERVER); RS.INSTANCE.network.registerMessage(MessageWirelessCraftingMonitorSettings.class, MessageWirelessCraftingMonitorSettings.class, id++, Side.SERVER);
RS.INSTANCE.network.registerMessage(MessageStorageDiskSizeRequest.class, MessageStorageDiskSizeRequest.class, id++, Side.SERVER); RS.INSTANCE.network.registerMessage(MessageStorageDiskSizeRequest.class, MessageStorageDiskSizeRequest.class, id++, Side.SERVER);
RS.INSTANCE.network.registerMessage(MessageStorageDiskSizeResponse.class, MessageStorageDiskSizeResponse.class, id++, Side.CLIENT); RS.INSTANCE.network.registerMessage(MessageStorageDiskSizeResponse.class, MessageStorageDiskSizeResponse.class, id++, Side.CLIENT);
RS.INSTANCE.network.registerMessage(MessageConfigSync.class, MessageConfigSync.class, id++, Side.CLIENT);
NetworkRegistry.INSTANCE.registerGuiHandler(RS.INSTANCE, new GuiHandler()); NetworkRegistry.INSTANCE.registerGuiHandler(RS.INSTANCE, new GuiHandler());
@@ -335,6 +338,20 @@ public class ProxyCommon {
} }
} }
@SubscribeEvent
public void onPlayerLoginEvent(net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent e) {
if (!e.player.world.isRemote) {
RS.INSTANCE.network.sendTo(new MessageConfigSync(), (EntityPlayerMP) e.player);
}
}
@SubscribeEvent
public void onPlayerLogoutEvent(WorldEvent.Unload e) {
if (e.getWorld().isRemote && RS.INSTANCE.config.getOriginalClientVersion() != null) {
RS.INSTANCE.config = RS.INSTANCE.config.getOriginalClientVersion();
}
}
@SubscribeEvent @SubscribeEvent
public void fixItemMappings(RegistryEvent.MissingMappings<Item> e) { public void fixItemMappings(RegistryEvent.MissingMappings<Item> e) {
OneSixMigrationHelper.removalHook(); OneSixMigrationHelper.removalHook();

View File

@@ -110,7 +110,6 @@ public class TileController extends TileBase implements ITickable, INetwork, IRe
private static final int THROTTLE_ACTIVE_TO_INACTIVE = 4; private static final int THROTTLE_ACTIVE_TO_INACTIVE = 4;
public static final String NBT_ENERGY = "Energy"; public static final String NBT_ENERGY = "Energy";
public static final String NBT_ENERGY_CAPACITY = "EnergyCapacity";
public static final String NBT_ENERGY_TYPE = "EnergyType"; public static final String NBT_ENERGY_TYPE = "EnergyType";
private static final String NBT_ITEM_STORAGE_TRACKER = "ItemStorageTracker"; private static final String NBT_ITEM_STORAGE_TRACKER = "ItemStorageTracker";
@@ -218,7 +217,7 @@ public class TileController extends TileBase implements ITickable, INetwork, IRe
this.energy.setStored(0); this.energy.setStored(0);
} }
} else if (getType() == ControllerType.CREATIVE) { } else if (getType() == ControllerType.CREATIVE) {
this.energy.setStored(this.energy.getCapacity()); this.energy.setStored(this.energy.getCapacity());
} }
boolean canRun = canRun(); boolean canRun = canRun();
@@ -602,7 +601,9 @@ public class TileController extends TileBase implements ITickable, INetwork, IRe
@Override @Override
public int getEnergyUsage() { public int getEnergyUsage() {
int usage = RS.INSTANCE.config.controllerBaseUsage; int usage = RS.INSTANCE.config.controllerBaseUsage;
usage += nodeGraph.all().stream().mapToInt(x-> x.getEnergyUsage()).sum();
usage += nodeGraph.all().stream().mapToInt(INetworkNode::getEnergyUsage).sum();
return usage; return usage;
} }