Jump to content

GUI not loading - Updated from 1.6 to 1.7


ashjack

Recommended Posts

So I have been updating the Sim-U-Kraft mod from 1.6.4 to 1.7.10, however, the GUI buttons that should load upon starting a new world don't load. I don't see anything wrong with the code, however, I am quite sure there is something that I'm missing. Here is my code:

 

package info.satscape.simukraft.client.Gui;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import org.lwjgl.input.Mouse;
import info.satscape.simukraft.common.ModSimukraft;

public class GuiRunMod extends GuiScreen
{
    public boolean running = true;
    private int mouseCount = 0;

    public boolean doesGuiPauseGame()
    {
        return true;
    }

    public GuiRunMod()
    {
    }

    public void initGui()
    {
        buttonList.add(new GuiButton(0, (width / 2 - 75) , 40, "Do NOT run Sim-U-Kraft"));
        buttonList.add(new GuiButton(1, (width / 2 - 75) , 90, "Normal Mode"));
        buttonList.add(new GuiButton(2, (width / 2 - 75) , 140, "Creative Mode"));
        buttonList.add(new GuiButton(3, (width / 2 - 75) , 190, "Hardcore Mode"));
    }

    public void drawScreen(int i, int j, float f)
    {
        try
        {
            if (mouseCount < 10)
            {
                mouseCount++;
                Mouse.setGrabbed(false);
            }

            drawDefaultBackground();
            drawCenteredString(fontRendererObj, "Please choose the game mode for Sim-U-Kraft", width / 2, 20, 0xffffff);
            drawCenteredString(fontRendererObj, "This mode switches off Sim-U-kraft for this world", width / 2, 60, 0xffff00);
            drawCenteredString(fontRendererObj, "Ideal for beginners and experts. Not too challenging.", width / 2, 110, 0xffff00);
            drawCenteredString(fontRendererObj, "No money needed, everything free, no blocks required, be creative!", width / 2, 160, 0xffff00);
            drawCenteredString(fontRendererObj, "Builders require ALL blocks, harder gameplay", width / 2, 210, 0xffff00);
            
            
            
        }
        catch (Exception e)
        {
        }

        super.drawScreen(i, j, f);
    }

    protected void actionPerformed(GuiButton guibutton)
    {
        if (guibutton.id == 0)   //do not run
        {
           // ModSimukraft.states.runMod = 0;
            ModSimukraft.states.gameModeNumber = 10;
        }
        else if (guibutton.id == 1)      // normal
        {
           // ModSimukraft.states.runMod = 1;
            ModSimukraft.states.gameModeNumber = 0;
            ModSimukraft.proxy.getClientWorld().playSound(mc.thePlayer.posX, mc.thePlayer.posY, mc.thePlayer.posZ, "satscapesimukraft:welcome", 1.0f, 1.0f, false);
        }
        else if (guibutton.id == 2)    //creative
        {
           // ModSimukraft.states.runMod = 1;
            ModSimukraft.states.gameModeNumber = 1;
            ModSimukraft.proxy.getClientWorld().playSound(mc.thePlayer.posX, mc.thePlayer.posY, mc.thePlayer.posZ, "satscapesimukraft:welcome", 1.0f, 1.0f, false);
        }
        else if (guibutton.id == 3)    //hardcore
        {
          //  ModSimukraft.states.runMod = 1;
            ModSimukraft.states.gameModeNumber = 2;
            ModSimukraft.proxy.getClientWorld().playSound(mc.thePlayer.posX, mc.thePlayer.posY, mc.thePlayer.posZ, "satscapesimukraft:welcome", 1.0f, 1.0f, false);
        }

        ModSimukraft.states.saveStates();
        this.running = false;
        mc.currentScreen = null;
        mc.setIngameFocus();
    }
}

Link to comment
Share on other sites

Sorry, I didn't mean to come across as rude, it's just demoralizing seeing my thread fall to the bottom of the page unanswered. The GUI should open as soon as a world is created.

 

Ah, I've just realised, the GUI is opened with this code:

 

    public static void resetAndLoadNewWorld() {
    	Side side = cpw.mods.fml.common.FMLCommonHandler.instance().getEffectiveSide();
    	log.info("RESETTING and loading world on "+side.toString()+" SIDE"); 
    	ModSimukraft.theBuildings.clear();
        ModSimukraft.theCourierPoints.clear();
        ModSimukraft.theCourierTasks.clear();
        ModSimukraft.theMiningBoxes.clear();
        ModSimukraft.theFarmingBoxes.clear();
        ModSimukraft.theFolks.clear();
        ModSimukraft.theRelationships.clear();
        
        File f=new File(ModSimukraft.getSavesDataFolder() + "settings.sk2");
        if (!f.exists()) {
        	f=new File(ModSimukraft.getSavesDataFolder() + "settings.suk");
        }
        if (f.exists())
        {
            ModSimukraft.states.loadStates();

            try
            {
                ModSimukraft.setGameModeFromNumber(ModSimukraft.states.gameModeNumber);
            }
            catch (Exception e)
            {
                ModSimukraft.setGameModeFromNumber(0);
            }
        }
        else
        {
            ModSimukraft.states = new GameStates();
            ModSimukraft.states.saveStates();
        }

        if (ModSimukraft.states == null)
        {
            new File(ModSimukraft.getSavesDataFolder() + "settings.sk2").delete();
            ModSimukraft.states = new GameStates();
            ModSimukraft.states.saveStates();
            ModSimukraft.sendChat("Your Sim-U-Kraft settings file was corrupted, I had to make a new one");
        }

        if (ModSimukraft.states.gameModeNumber == -1)
        {
            if (runModui == null)
            {
                GuiRunMod runModui = new GuiRunMod();
                Minecraft.getMinecraft().displayGuiScreen(runModui);
            }

            return;
        }

        if (ModSimukraft.states.gameModeNumber >= 0)
        {
            ModSimukraft.proxy.ranStartup = true;
        }

        ModSimukraft.sendChat("Welcome to Sim-U-Kraft " + ModSimukraft.version);
        ModSimukraft.theFolks.clear();
        Building.initialiseAllBuildings(); //load ALL buildings off disk and init them
        Building.loadAllBuildings();	   //load buildings in this world
        CourierTask.loadCourierTasksAndPoints();
        MiningBox.loadMiningBoxes();
        FarmingBox.loadFarmingBoxes();
        //PathBox.loadPathBoxes();
        FolkData.loadAndSpawnFolks();
        Relationship.loadRelationships();
        ModSimukraft.updateCheck();
        ModSimukraft.isDay = ModSimukraft.isDayTime();
        Building.checkTennants();
        ModSimukraft.proxy.ranStartup = true;
    }

 

However, this function is only executed when a packet is successfully sent and received, telling the game to run it. I have absolutely no idea if my packet code works. So I guess I will post it all here:

 

PacketHandler.java

package info.satscape.simukraft.common;

import info.satscape.simukraft.common.CommonProxy.V3;
import info.satscape.simukraft.packets.SimukraftPacket;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.NetworkRegistry;
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.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;

public class PacketHandler implements IMessageHandler<SimukraftPacket, IMessage>
{
    public PacketHandler()
    {
    	
    }
    
    
    
    
    @Override
    public IMessage onMessage(SimukraftPacket message, MessageContext ctx) 
    {
    	try
        {
    		String par1 = "";
    		String cmd = message.cmd;
    		String val = "";
            World world = null;
            Side side = cpw.mods.fml.common.FMLCommonHandler.instance().getEffectiveSide();
            String sside = "none";
            if (side == Side.SERVER)
            {
                sside = "Server";
                
                // CLINET GUI > SERVER, a building is being built, so load it server side
                if(cmd.contentEquals("loadbuilding")) {
                	Building.loadAllBuildings();
                
			// Client side Windmill is requesting Colour meta value
                }/*else if (cmd.contentEquals("requestMeta")) {
                	//world=MinecraftServer.getServer().worldServerForDimension(Integer.parseInt(val));
                	
                	V3 v3=new V3(par1);
                    TileEntityWindmill te=(TileEntityWindmill) world.getTileEntity(v3.x.intValue(),v3.y.intValue(),v3.z.intValue());
                    if (te !=null) {
                    	//te.sendMetaToClient();
                    } else {
                    	ModSimukraft.log.warning("TEwindmill was null at "+v3.toString());
                    }
                }*/
                
            }
            else if (side == Side.CLIENT)
            {
                sside = "Client";
                world = Minecraft.getMinecraft().theWorld;
                
                /// SERVER > CLIENT windmill announcing it's meta value for the colour of the sails.
                /*if (cmd.contentEquals("announceMeta")) {
                    V3 v3=new V3(par1);
                    TileEntityWindmill te=(TileEntityWindmill) world.getTileEntity(v3.x.intValue(),v3.y.intValue(),v3.z.intValue());
                    if (te !=null) {
                    	te.meta=Integer.parseInt(val);
                    } else {
                    	ModSimukraft.log.warning("***TEwindmill was null at "+v3.toString());
                    }
                */
                /// SERVER > CLIENT folks position update (every 10 or so seconds for each folk)
                } else if (cmd.contentEquals("updateFolkPosition")) {
                	FolkData folk=FolkData.getFolkByName(par1);
                	V3 newpos=new V3(val);
                	if (folk !=null && newpos !=null) {
                		folk.serverToClientLocationUpdate(newpos);
                	}
                
                /// SERVER > CLIENT - player has changed worlds, so reset and load in new SUK data	
                } else if (cmd.contentEquals("gamereset")) {
                	ModSimukraft.resetAndLoadNewWorld();  // still bugs with this
                }
                
            

            ModSimukraft.log.info("PacketHandler: "+sside + "-side PACKET RECIEVED: " +  cmd);
            
            
            
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return message;
        }

        
    	
        return null;
    }

   /*@Override
    public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
    {
        if (packet.channel.equals("SUKMain"))
        {
            DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));

            try
            {
                String par1 = ByteBufUtils.readUTF();
                String cmd = ByteBufUtils.readUTF();
                String val = ByteBufUtils.readUTF();
                World world = null;
                Side side = cpw.mods.fml.common.FMLCommonHandler.instance().getEffectiveSide();
                String sside = "none";

                if (side == Side.SERVER)
                {
                    sside = "Server";
                    
                    // CLINET GUI > SERVER, a building is being built, so load it server side
                    if(cmd.contentEquals("loadbuilding")) {
                    	Building.loadAllBuildings();
                    
				// Client side Windmill is requesting Colour meta value
                    }else if (cmd.contentEquals("requestMeta")) {
                    	world=MinecraftServer.getServer().worldServerForDimension(Integer.parseInt(val));
                    	
                    	V3 v3=new V3(par1);
                    TileEntityWindmill te=(TileEntityWindmill) world.getTileEntity(v3.x.intValue(),v3.y.intValue(),v3.z.intValue());
                    if (te !=null) {
                    	te.sendMetaToClient();
                    } else {
                    	ModSimukraft.log.warning("TEwindmill was null at "+v3.toString());
                    }
                    }
                    
                }
                else if (side == Side.CLIENT)
                {
                    sside = "Client";
                    world = Minecraft.getMinecraft().theWorld;
                    
                    /// SERVER > CLIENT windmill announcing it's meta value for the colour of the sails.
                    if (cmd.contentEquals("announceMeta")) {
                    V3 v3=new V3(par1);
                    TileEntityWindmill te=(TileEntityWindmill) world.getTileEntity(v3.x.intValue(),v3.y.intValue(),v3.z.intValue());
                    if (te !=null) {
                    	te.meta=Integer.parseInt(val);
                    } else {
                    	ModSimukraft.log.warning("***TEwindmill was null at "+v3.toString());
                    }
                    
                /// SERVER > CLIENT folks position update (every 10 or so seconds for each folk)
                    } else if (cmd.contentEquals("updateFolkPosition")) {
                    	FolkData folk=FolkData.getFolkByName(par1);
                    	V3 newpos=new V3(val);
                    	if (folk !=null && newpos !=null) {
                    		folk.serverToClientLocationUpdate(newpos);
                    	}
                    
                    /// SERVER > CLIENT - player has changed worlds, so reset and load in new SUK data	
                    } else if (cmd.contentEquals("gamereset")) {
                    	ModSimukraft.resetAndLoadNewWorld();  // still bugs with this
                    }
                    
                }

                ModSimukraft.log.info("PacketHandler: "+sside + "-side PACKET RECIEVED: " + par1 + " - " + cmd + " = " + val);
                
                
                
                
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return;
            }
        }
    }

    public static Packet250CustomPayload makePacket(String par1, String cmd, String val)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(;
        DataOutputStream outputStream = new DataOutputStream(bos);

        try
        {
            outputStream.writeUTF(par1);
            outputStream.writeUTF(cmd);
            outputStream.writeUTF(val);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        Packet250CustomPayload packet = new Packet250CustomPayload();
        packet.channel = "SUKMain";
        packet.data = bos.toByteArray();
        packet.length = bos.size();
        ModSimukraft.log.info("PacketHandler: Packet sent: "+par1+" "+cmd+" "+val);
        return packet;
    }*/
}

 

CommonTickHandler.java

package info.satscape.simukraft.common;

import info.satscape.simukraft.client.Gui.GuiRunMod;
import info.satscape.simukraft.common.CommonProxy.V3;
import info.satscape.simukraft.packets.SimukraftPacket;
import io.netty.buffer.ByteBuf;

import java.io.File;
import java.util.ArrayList;
import java.util.EnumSet;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.NetworkRegistry;
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.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.server.FMLServerHandler;

public class CommonTickHandler
{
    private World serverWorld = null;
    Long lastSecondTickAt = 0l;
    Long lastMinuteTickAt = 0l;

    GuiRunMod runModui = null;
    String currentWorld = "";
    Minecraft mc = Minecraft.getMinecraft();
    long lastReset = 0;
    
    //public static final PacketPipeline packetPipeline = new PacketPipeline();

    
    public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("SUKMain");
    
    public void onTickInGame()
    {
        if (mc.currentScreen != null)
        {
            if (mc.currentScreen.toString().toLowerCase().contains("guimainmenu"))
            {
            	ModSimukraft.log.info("CommTH: in Gui Main menu");
            }
        }

        if (ModSimukraft.states.gameModeNumber == 10)
        {
            ModSimukraft.proxy.ranStartup = true;
            return;
        }

        Long now = System.currentTimeMillis();

        if (serverWorld != null)
        {
            //fire onUpdate() for each folkData
            FolkData.triggerAllUpdates();
            //handle day-night-day transitions
            ModSimukraft.dayTransitionHandler();

            //if a farm needs upgarding, incrementally upgrade it
            if (ModSimukraft.farmToUpgrade != null)
            {
                ModSimukraft.upgradeFarm();
            }

            if (ModSimukraft.demolishBlocks.size() > 0) //and demolishing buildings
            {
                ModSimukraft.demolishBlocks();
            }
        }

        // ***** ONCE A SECOND
        if (now - lastSecondTickAt > 1000)
        {
            if (serverWorld == null)
            {
                try
                {
                    //this is a crazy way to delay running stuff until things have spawned! :-)
				//Also, this won't work in MC1.7+ as there's no ModLoader class in Forge any more...good luck with that!
                    for (World w : MinecraftServer.getServer().worldServers)
                    {
                        if (w.loadedEntityList.size() > 0)
                        {
                            serverWorld = w;
                            currentWorld = ModSimukraft.getSavesDataFolder();
                            ModSimukraft.log.info("CommTH: Startup - set serverWorld/currentWorld");
                            ModSimukraft.resetAndLoadNewWorld();
                            INSTANCE.registerMessage(PacketHandler.class, SimukraftPacket.class, 0, Side.SERVER);
    	                    INSTANCE.sendToServer(new SimukraftPacket());
                            
                           
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    serverWorld = null;   //will exception until first player has spawned!
                }
            }
            else      //used to detect world change - Still a bug with this, not unloading world when player switches via main menu
            {
                if (!currentWorld.contentEquals(ModSimukraft.getSavesDataFolder()))
                {
                    if (now - lastReset >30000) {
                    	ModSimukraft.log.info("currentWorld="+currentWorld+"     getSaves="+ModSimukraft.getSavesDataFolder());
                	currentWorld = ModSimukraft.getSavesDataFolder();
                    ModSimukraft.proxy.ranStartup = false;
                    ModSimukraft.resetAndLoadNewWorld();
                    //PacketPipeline.sendPacketToAllPlayers(packetPipeline.encode("", "gamereset", "")  /*PacketHandler.makePacket("", "gamereset", "")*/);
                    INSTANCE.registerMessage(PacketHandler.class, SimukraftPacket.class, 0, Side.SERVER);
                    INSTANCE.sendToServer(new SimukraftPacket());
                    }
                }

                //STOP THE RAIN MOD - Implemented this when I had a world where it rained ALL THE TIME!
                if (serverWorld.isRaining() && serverWorld.getWorldInfo().getRainTime() > 1 &&  ModSimukraft.configStopRain == true)
                {
                    serverWorld.getWorldInfo().setRainTime(2);  //setting to 1 or 0 doesn't work every time.
                }
            }


            // ONCE A SECOND EVERY SECOND
            lastSecondTickAt = now;
            
            
        }

        
        // ***** ONCE A MINUTE
        if (serverWorld != null && System.currentTimeMillis() - lastMinuteTickAt > 60000)
        {
            if (lastMinuteTickAt > 0)
            {
                Long start = System.currentTimeMillis();
                FolkData.generateNewFolk(serverWorld);
                ModSimukraft.states.saveStates();
                Building.checkTennants();
                Building.saveAllBuildings();
                CourierTask.saveCourierTasksAndPoints();
                MiningBox.saveMiningBoxes();
                FarmingBox.saveFarmingBoxes();
                Relationship.saveRelationships();
                //PathBox.savePathBoxes();
                ModSimukraft.log.info("CTH: Saved game data in " + (System.currentTimeMillis() - start) + " ms");
            }

            lastMinuteTickAt = now;
        }
    }

    public void resetSimUKraft()
    {
        //this resets everything first, if the player has switched worlds, gets hit several times due to weird MC GUI switching,
        // so lastReset stops it from running more than once every 30 seconds.
        if (System.currentTimeMillis() - lastReset > 30000)
        {
            
            lastReset = System.currentTimeMillis();
            Side side = cpw.mods.fml.common.FMLCommonHandler.instance().getEffectiveSide();
            
            ModSimukraft.log.info(side.toString()+"-side CommTH: resetSimUKraft()");
        }
    }

    /** runs when a world has loaded, so we can set everything up */
    private void startingWorld()
    {
        if (!ModSimukraft.proxy.ranStartup)
        {
           // TODO: no longer used
           

        }
    }

    ////////////////////////////////////////////////
    

    public void tickStart(EnumSet<TickEvent.Type> type, Object... tickData)
    {
    }

    public void tickEnd(EnumSet<TickEvent.Type> type, Object... tickData)
    {
        if (type.equals(EnumSet.of(TickEvent.Type.SERVER)))
        {
            onTickInGame();
        }

        if (type.equals(EnumSet.of(TickEvent.Type.WORLD)))
        {
            System.out.println("WorldLoad event tick");
        }
    }

    public EnumSet<TickEvent.Type> ticks()
    {
        return EnumSet.of(TickEvent.Type.SERVER, TickEvent.Type.WORLD); //add more types?
    }

    public String getLabel()
    {
        return "CommonTickHandler";
    }

    public CommonTickHandler()
    {
    	
    }



/*public static class Handler implements IMessageHandler<CommonTickHandler, IMessage> {
        /**
         * This gets called when the packet is read and received.
         */
        /*@Override
        public IMessage onMessage(CommonTickHandler message, MessageContext ctx) {
            return null;
        }
    }*/


}


 

SimukraftPacket.java

package info.satscape.simukraft.packets;

import io.netty.buffer.ByteBuf;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;

public class SimukraftPacket implements IMessage{

//enum Type{GameReset, };
public static String par1;
public static String cmd = "";
public static String folkName = "";

@Override
public void fromBytes(ByteBuf buf) 
{
	cmd = ByteBufUtils.readUTF8String(buf);
}

@Override
public void toBytes(ByteBuf buf) 
{

	ByteBufUtils.writeUTF8String(buf, cmd);
	//ByteBufUtils.writeUTF8String(buf, folkName);
}


    
}


Link to comment
Share on other sites

Thank you! I updated the ticking functions(in the CommonTickHandler and ClientTickHandler), and part of the GUI shows up (HUD from the main mod file) however, the GUI buttons still do not show themselves. There's no error log for me to refer to, so I cannot work out why this is happening.

Link to comment
Share on other sites

Ok, so I added more system output points in the ticking functions, and for some reason the server isn't ticking. I don't see anything wrong with this code - hell, I got it off of a 1.7 modding tutorial, there shouldn't be anything wrong with it...

 

@EventHandler
    public void init(FMLInitializationEvent event)
    {
       FMLCommonHandler.instance().bus().register(this);
    }

    @SubscribeEvent
    public void tick(WorldTickEvent event)
    {
        System.out.println("WorldLoad event tick");
    }

    @SubscribeEvent
    public void tick(ServerTickEvent event)
    {
        System.out.println("Server event tick");
        onTickInGame();
    }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.