Skip to content

Commit

Permalink
Version 1.4 - Properly use sided proxies to fix the server crash + ad…
Browse files Browse the repository at this point in the history
…ded docstrings
  • Loading branch information
Brittank88 committed Mar 27, 2024
1 parent 60e3cdd commit 7782a3e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 46 deletions.
56 changes: 54 additions & 2 deletions src/main/java/com/brittank88/clipshot/ClientProxy.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
package com.brittank88.clipshot;

import java.awt.HeadlessException;
import java.awt.Toolkit;

import com.brittank88.clipshot.config.ConfigurationHandler;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

/**
* Override CommonProxy methods here, if you want a different behaviour on the client (e.g. registering renders).
* <p>
* Don't forget to call the super methods as well.
*
* @see CommonProxy
*/
@SuppressWarnings("unused")
public class ClientProxy extends CommonProxy {

// Override CommonProxy methods here, if you want a different behaviour on the client (e.g. registering renders).
// Don't forget to call the super methods as well.
/**
* {@inheritDoc}
*
* @param event The pre-initialisation event.
*/
@Override
public void preInit(FMLPreInitializationEvent event) {
super.preInit(event);

// Initialise the configuration file.
ConfigurationHandler.init(event.getSuggestedConfigurationFile());
FMLCommonHandler.instance()
.bus()
.register(new ConfigurationHandler());
}

/**
* {@inheritDoc}
*
* @param event The initialisation event.
*/
@Override
public void init(FMLInitializationEvent event) {
super.init(event);

ClipShot.LOG.info("Getting system clipboard via AWT...");

try {
Toolkit.getDefaultToolkit()
.getSystemClipboard();
} catch (HeadlessException e) {
throw ClipShot.LOG.throwing(
new RuntimeException(
"Failed to get system clipboard; java.awt.headless property was not set properly: ",
e));
}

ClipShot.LOG.info("ClipShot " + Tags.VERSION + " loaded!");
}
}
16 changes: 14 additions & 2 deletions src/main/java/com/brittank88/clipshot/ClipShot.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@ public class ClipShot {
@SuppressWarnings("unused")
public static CommonProxy proxy;

/**
* Run before anything else.
* <p>
* Read your config, create blocks, items, etc., and register them with the GameRegistry.
*
* @param event The pre-initialisation event.
*/
@Mod.EventHandler
@SuppressWarnings("unused")
// preInit "Run before anything else. Read your config, create blocks, items, etc., and register them with the
// GameRegistry." (Remove if not needed)
public void preInit(FMLPreInitializationEvent event) {
proxy.preInit(event);
}

/**
* Do your mod setup.
* <p>
* Build whatever data structures you care about. Register recipes.
*
* @param event The initialisation event.
*/
@Mod.EventHandler
@SuppressWarnings("unused")
// load "Do your mod setup. Build whatever data structures you care about. Register recipes." (Remove if not needed)
Expand Down
60 changes: 18 additions & 42 deletions src/main/java/com/brittank88/clipshot/CommonProxy.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
package com.brittank88.clipshot;

import java.awt.HeadlessException;
import java.awt.Toolkit;

import com.brittank88.clipshot.config.ConfigurationHandler;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

public class CommonProxy {

private boolean isClient = false;

// preInit "Run before anything else. Read your config, create blocks, items, etc., and register them with the
// GameRegistry." (Remove if not needed)
/**
* Run before anything else.
* <p>
* Read your config, create blocks, items, etc., and register them with the GameRegistry.
*
* @param event The pre-initialisation event.
*/
public void preInit(FMLPreInitializationEvent event) {

// Skip initialisation on dedicated servers.
isClient = event.getSide()
.isClient();
if (!isClient) {
// Warn about being useless on a dedicated server.
if (event.getSide()
.isServer())
ClipShot.LOG.warn("ClipShot will do nothing on a dedicated server as it is a client-only mod!");
return;
}

// Initialise the configuration file.
ConfigurationHandler.init(event.getSuggestedConfigurationFile());
FMLCommonHandler.instance()
.bus()
.register(new ConfigurationHandler());
}

// load "Do your mod setup. Build whatever data structures you care about. Register recipes." (Remove if not needed)
public void init(@SuppressWarnings("unused") FMLInitializationEvent event) {

// Skip initialisation on dedicated servers.
if (isClient) return;

ClipShot.LOG.info("Getting system clipboard via AWT...");

try {
Toolkit.getDefaultToolkit()
.getSystemClipboard();
} catch (HeadlessException e) {
throw ClipShot.LOG.throwing(
new RuntimeException(
"Failed to get system clipboard via AWT. The java.awt.headless property was not set properly: ",
e));
}

ClipShot.LOG.info("ClipShot " + Tags.VERSION + " loaded!");
}
/**
* Do your mod setup.
* <p>
* Build whatever data structures you care about. Register recipes.
*
* @param event The initialisation event.
*/
public void init(@SuppressWarnings("unused") FMLInitializationEvent event) {}
}

0 comments on commit 7782a3e

Please sign in to comment.