Post by thatdarnowl on Dec 24, 2023 11:01:38 GMT
Here;s some code that implements an idea i've been playing with of having some form of persistance to objects that are created in the world, and having a more 'java-ish' approach to writing mods.
Using a factory object to create and serve objects, is in general a better approach in java then a lot of throwaway objects, I've extended this so that you can have subclassed objects created and held as well.
I'm trying to help on the Mage mod with Yith, and do get some things done that I want I've created a subclass of CodexActor called Mage, which allows me to hold extra state information, and override calls (such as perceive aura and return what i want)
I've included an initial version of CodexFactory which will create actor objects,
The only problem with all this is that it requires a change in the way you create objects, instead of doing :
new CodexActor(guid) you need to do CodexFactory.getCodexActor(int guid)
I've got more work to do in terms of other object types - and also possibly flushing the cache on scene changes and the like.
Let me know if I've duplicated someone else's idea , or if theres a majot problem in the way this works,
m<
Code Follows (apologies for formatting):
Using a factory object to create and serve objects, is in general a better approach in java then a lot of throwaway objects, I've extended this so that you can have subclassed objects created and held as well.
I'm trying to help on the Mage mod with Yith, and do get some things done that I want I've created a subclass of CodexActor called Mage, which allows me to hold extra state information, and override calls (such as perceive aura and return what i want)
I've included an initial version of CodexFactory which will create actor objects,
The only problem with all this is that it requires a change in the way you create objects, instead of doing :
new CodexActor(guid) you need to do CodexFactory.getCodexActor(int guid)
I've got more work to do in terms of other object types - and also possibly flushing the cache on scene changes and the like.
Let me know if I've duplicated someone else's idea , or if theres a majot problem in the way this works,
m<
Code Follows (apologies for formatting):
/**
* CodexFactory
*
* Handles caching and creating of codex subtypes - allows more of a java
* style approach to the writing of scripts.
*
* @author MAN
*/
import java.util.Hashtable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import Codex;
import CodexThing;
import CodexActor;
public class CodexFactory
{
private static Hashtable codexActors;
private static Hashtable codexThings;
//private Hashmap codexPlayers;
static
{
codexActors = new Hashtable();
codexThings = new Hashtable();
// codexActors = new Hashmap();
}
public static CodexActor getCodexActor(int guid)
{
// return getCodexActor(guid,Codex.class);
return new CodexActor(guid);
}
public static CodexActor getCodexActor(int guid,Class desiredClass)
{
// check to see if in hashmap
Integer lookup = new Integer(guid);
CodexActor theActor = null;
if(codexActors.contains(lookup))
{
theActor = (CodexActor)codexActors.get(lookup);
}
else
{
// if not create and add it
Constructor[] ctorarray = desiredClass.getConstructors();
// need a constructor that takes a simple int for the guid.
int i=0;
for(;i<ctorarray.length;i++)
{
Class[] classarray = ctorarray[i].getParameterTypes();
// if the ctor has only a single int argument , then its the one we want...
if (classarray != null && classarray.length > 0 && classarray[i] == Integer.TYPE)
{
break;
}
}
if (i != ctorarray.length)
{
CodexConsole.PrintNLS(guid, 0,"found a ctor that matches");
// we found something that matches.
Object[] ctorargs = {lookup};
try
{
theActor = (CodexActor)ctorarray[i].newInstance(ctorargs);
}
catch(InstantiationException ie)
{
ie.printStackTrace();
}
catch(IllegalAccessException iae)
{
iae.printStackTrace();
}
catch(InvocationTargetException ite)
{
ite.printStackTrace();
}
}
else
{
CodexConsole.PrintNLS(guid, 0,"no matching ctor ");
}
if (theActor != null)
{
codexActors.put(lookup,theActor);
}
}
if(theActor == null)
{
theActor = new CodexActor(guid);
}
return theActor;
}
public static CodexThing getCodexThing(int guid)
{
// check to see if in hashmap
Integer lookup = new Integer(guid);
CodexThing theThing = null;
if(codexThings.contains(lookup))
{
theThing = (CodexThing)codexThings.get(lookup);
}
else
{
// if not create and add it
theThing = new CodexThing(guid);
codexActors.put(lookup,theThing);
}
return theThing;
}
}
Social Media