Skip to content

Commit

Permalink
Merge branch 'master' into KCauldron
Browse files Browse the repository at this point in the history
Conflicts:
	build.gradle
	src/main/java/com/wildex999/tickdynamic/TickDynamicMod.java
	src/main/java/com/wildex999/tickdynamic/WorldEventHandler.java
	src/main/java/com/wildex999/tickdynamic/listinject/ListManagerEntities.java
  • Loading branch information
wildex999 committed Feb 6, 2016
2 parents 26304cc + 72e238b commit fe0102f
Show file tree
Hide file tree
Showing 15 changed files with 404 additions and 111 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ forgeBase/
.classpath
.settings/
.project
run/
TickDynamic_Client.launch
TickDynamic_Server.launch
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# TickDynamic
Public Issues repository for the Minecraft mod Tick Dynamic.

The project will be Open-Sourced later at some point =)
Development Releases:
There are currently development releases of version 0.2.0 on github.
- Forge 1.8.9
- Forge 1.7.10
- KCauldron

Webpage: http://mods.stjerncraft.com/tickdynamic
Check the "Releases" tab on the top. I would appreciate any feedback on the dev versions, so I can make the final releases as bug-free as possible. =)

Webpage & final release downloads: http://mods.stjerncraft.com/tickdynamic
151 changes: 111 additions & 40 deletions src/main/java/com/wildex999/tickdynamic/TickDynamicConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;

import com.wildex999.tickdynamic.listinject.EntityGroup;
import com.wildex999.tickdynamic.listinject.EntityType;
import com.wildex999.tickdynamic.listinject.ListManager;
import com.wildex999.tickdynamic.timemanager.ITimed;
import com.wildex999.tickdynamic.timemanager.TimeManager;
import com.wildex999.tickdynamic.timemanager.TimedEntities;
import com.wildex999.tickdynamic.timemanager.TimedGroup;

public class TickDynamicConfig {

public static void loadConfig(TickDynamicMod mod, boolean includeExisting) {
mod.config.load();

public static void loadConfig(TickDynamicMod mod, boolean groups) {
//mod.config.load();
mod.config = new Configuration(mod.config.getConfigFile());

//--GENERAL CONFIG--
mod.config.getCategory("general");
mod.config.setCategoryComment("general", "WEBSITE: http://mods.stjerncraft.com/tickdynamic <- Head here for the documentation, if you have problems or if you have questions."
Expand All @@ -45,6 +50,8 @@ public static void loadConfig(TickDynamicMod mod, boolean includeExisting) {

mod.debug = mod.config.get("general", "debug", mod.debug, "Debug output. Warning: Might output a lot of data to console!").getBoolean();

mod.debugGroups = mod.config.get("general", "debugGroups", mod.debugGroups, "Debug Group mapping and assignment. Will spam during world load and config reload!!!").getBoolean();

mod.debugTimer = mod.config.get("general", "debugTimer", mod.debugTimer, "Debug output from time allocation and calculation. Warning: Setting this to true will cause a lot of console spam.\n"
+ "Only do it if developer or someone else asks for the output!").getBoolean();

Expand Down Expand Up @@ -72,24 +79,63 @@ public static void loadConfig(TickDynamicMod mod, boolean includeExisting) {
mod.defaultTileEntityMinimumObjects = mod.config.get(mod.configCategoryDefaultTileEntities, TimedGroup.configKeyMinimumObjects, mod.defaultTileEntityMinimumObjects,
"The minimum number of TileEntities to update per tick, independent of time given.").getInt();*/

//Load New, Reload and Remove old Global groups
loadGlobalGroups(mod);

//Default example for Entities and TileEntities in dim0(Overworld)
if(!mod.config.hasCategory("worlds.dim0.entity"))
mod.config.get("worlds.dim0.entity", ITimed.configKeySlicesMax, 1000);
if(!mod.config.hasCategory("worlds.dim0.tileentity"))
mod.config.get("worlds.dim0.tileentity", ITimed.configKeySlicesMax, 1000);

//Reload local groups
if(includeExisting) {

for(ITimed timed : mod.timedObjects.values())
timed.loadConfig(false);

if(mod.root != null)
mod.root.setTimeMax(mod.defaultTickTime * TimeManager.timeMilisecond);
}
//Load New, Reload and Remove old groups
if(groups)
{
loadGlobalGroups(mod);

//Default example for Entities and TileEntities in dim0(Overworld)
if(!mod.config.hasCategory("worlds.dim0.entity"))
{
mod.config.get("worlds.dim0.entity", ITimed.configKeySlicesMax, mod.defaultEntitySlicesMax);
mod.config.get("worlds.dim0.entity", EntityGroup.config_groupType, EntityType.Entity.toString());
}
if(!mod.config.hasCategory("worlds.dim0.tileentity"))
{
mod.config.get("worlds.dim0.tileentity", ITimed.configKeySlicesMax, mod.defaultEntitySlicesMax);
mod.config.get("worlds.dim0.tileentity", EntityGroup.config_groupType, EntityType.TileEntity.toString());
}

//Reload local groups
WorldServer[] worlds = DimensionManager.getWorlds();
for(WorldServer world : worlds) {
if(mod.debug)
System.out.println("Reloading " + world.provider.getDimensionName());

if(world.loadedEntityList instanceof ListManager) {
ListManager entityList = (ListManager)world.loadedEntityList;
if(mod.debug)
System.out.println("Reloading " + entityList.size() + " Entities...");
entityList.reloadGroups();
}
if(world.loadedTileEntityList instanceof ListManager) {
ListManager tileList = (ListManager)world.loadedTileEntityList;
if(mod.debug)
System.out.println("Reloading " + tileList.size() + " TileEntities...");
tileList.reloadGroups();
}
}

if(mod.debug)
System.out.println("Done reloading worlds");

//Reload Timed
for(ITimed timed : mod.timedObjects.values())
{
if(timed instanceof TimedEntities)
{
TimedEntities timedGroup = (TimedEntities)timed;
if(!timedGroup.getEntityGroup().valid) {
mod.timedObjects.remove(timedGroup);
continue;
}
}
timed.loadConfig(false);
}

if(mod.root != null)
mod.root.setTimeMax(mod.defaultTickTime * TimeManager.timeMilisecond);
}

//Save any new defaults
mod.config.save();
Expand All @@ -112,12 +158,22 @@ public static void loadGlobalGroups(TickDynamicMod mod) {
//Load/Create default entity and tileentity groups
loadDefaultGlobalGroups(mod);

ConfigCategory groupsCat = mod.config.getCategory("groups");
//Load Global groups
loadGroups(mod, "groups");
}

//Load all groups under the given category
public static void loadGroups(TickDynamicMod mod, String category) {
ConfigCategory groupsCat = mod.config.getCategory(category);
Set<ConfigCategory> groups = groupsCat.getChildren();

//Remove every group which is no longer in groups set
ArrayList<String> toRemove = new ArrayList<String>();
for(String groupPath : mod.entityGroups.keySet())
{
if(!groupPath.startsWith(category))
continue; //We only care about groups in the same category

int nameIndex = groupPath.lastIndexOf(".");
String groupName;
if(nameIndex == -1)
Expand All @@ -126,52 +182,71 @@ public static void loadGlobalGroups(TickDynamicMod mod) {
groupName = groupPath.substring(nameIndex+1);

boolean remove = true;
for(ConfigCategory group : groups)
{
if(group.getName().equals(groupName))
{
remove = false;
break;
if(mod.config.hasCategory(groupPath))
remove = false;

//Check if local copy of a global group
if(remove) {
EntityGroup entityGroup = mod.entityGroups.get(groupPath);
if(entityGroup != null && entityGroup.base != null) {
//Check if the global group still exists
if(mod.config.hasCategory("groups." + groupName))
remove = false;
}
}

//Mark for removal after loop
if(remove)
{

if(mod.debug)
System.out.println("Remove Global Group: " + groupPath);
mod.entityGroups.remove(groupPath);
System.out.println("Remove Group: " + groupPath);
toRemove.add(groupPath);
}
}

//Remove after due to ConcurrentException
for(String groupPath : toRemove) {
EntityGroup groupRemoved = mod.entityGroups.remove(groupPath);
if(groupRemoved != null)
groupRemoved.valid = false;
}

//Load new groups
ArrayList<EntityGroup> updateGroups = new ArrayList<EntityGroup>();
for(ConfigCategory group : groups)
{
//Check if group already exists
EntityGroup entityGroup = mod.getEntityGroup(group.getName());
String groupPath = category + "." + group.getName();
EntityGroup entityGroup = mod.getEntityGroup(groupPath);
if(entityGroup == null)
{
String groupPath = "groups." + group.getName();
if(mod.debug)
System.out.println("Loading group: " + groupPath);
entityGroup = new EntityGroup(mod, null, null, group.getName(), groupPath, EntityType.Entity, null);

TimedEntities timedEntities = (TimedEntities) mod.getTimedGroup(groupPath);
if(timedEntities == null) {
timedEntities = new TimedEntities(mod, null, group.getName(), groupPath, null);
timedEntities.init();
}

entityGroup = new EntityGroup(mod, null, timedEntities, group.getName(), groupPath, EntityType.Entity, null);
mod.entityGroups.put(groupPath, entityGroup);
if(mod.debug)
System.out.println("New Global Group: " + groupPath);
System.out.println("New Group: " + groupPath);
}
else
{
//Add to list of groups to update
updateGroups.add(entityGroup);
if(mod.debug)
System.out.println("Update Global Group: groups." + group.getName());
System.out.println("Update Group: " + groupPath);
}
}

//Update old
for(EntityGroup entityGroup : updateGroups)
entityGroup.readConfig(false);

}

public static void loadDefaultGlobalGroups(TickDynamicMod mod) {
Expand Down Expand Up @@ -218,8 +293,4 @@ public static void loadDefaultGlobalGroups(TickDynamicMod mod) {

}

//Update config mark old config options as Decrepated
public void notifyDecrepated() {
//TODO
}
}
64 changes: 47 additions & 17 deletions src/main/java/com/wildex999/tickdynamic/TickDynamicMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class TickDynamicMod extends DummyModContainer
public static final String MODID = "tickDynamic";
public static final String VERSION = "0.2.0-dev1";
public static boolean debug = false;
public static boolean debugGroups = false;
public static boolean debugTimer = false;
public static TickDynamicMod tickDynamic;

Expand Down Expand Up @@ -142,10 +143,10 @@ public void preInit(FMLPreInitializationEvent event) {
}

//Load the configuration file
//includeExisting: Whether to reload the config options for already loaded Managers and Groups.
public void loadConfig(boolean includeExisting) {
//groups: Whether to (re)load the groups
public void loadConfig(boolean groups) {
//TODO: Separate Initial load, reload and write
TickDynamicConfig.loadConfig(this, includeExisting);
TickDynamicConfig.loadConfig(this, groups);
}

public void writeConfig(boolean saveFile) {
Expand All @@ -163,7 +164,7 @@ public void init(FMLInitializationEvent event) {
timedObjects = new HashMap<String, ITimed>();
entityGroups = new HashMap<String, EntityGroup>();

loadConfig(false);
loadConfig(true);

root = new TimeManager(this, null, "root", null);
root.init();
Expand Down Expand Up @@ -301,7 +302,7 @@ public TimedGroup getTimedGroup(String name) {
return (TimedGroup)timedObjects.get(name);
}

//Get a named EntityGroup which does not belong to a world
//Get a named EntityGroup which possibly does not belong to a world
//Return: Null if doesn't exist in config
public EntityGroup getEntityGroup(String name) {
//All Global Groups are loaded during config load/reload
Expand All @@ -314,13 +315,21 @@ public TimeManager getTimeManager(String name) {
return (TimeManager)timedObjects.get(name);
}

//Get the TimeManager for a world.
//Will create if it doesn't exist.
public TimeManager getWorldTimeManager(World world) {
private String getEntityGroupName(World world, String name) {
String remote = "";
if(world.isRemote)
remote = "client_";
String managerName = new StringBuilder().append("worlds.").append(remote).append("dim").append(world.provider.dimensionId).toString();
StringBuilder strBuilder = new StringBuilder().append("worlds.").append(remote).append("dim").append(world.provider.dimensionId);
if(name != null && name.length() > 0)
strBuilder.append(".").append(name);

return strBuilder.toString();
}

//Get the TimeManager for a world.
//Will create if it doesn't exist.
public TimeManager getWorldTimeManager(World world) {
String managerName = getEntityGroupName(world, null);
TimeManager worldManager = getTimeManager(managerName);

if(worldManager == null)
Expand All @@ -342,10 +351,7 @@ public TimeManager getWorldTimeManager(World world) {
//canCreate: Whether to create if it does not exist
//hasConfig: Whether to create with config entry
public TimedEntities getWorldTimedGroup(World world, String name, boolean canCreate, boolean hasConfig) {
String remote = "";
if(world.isRemote)
remote = "client_";
String groupName = new StringBuilder().append("worlds.").append(remote).append("dim").append(world.provider.dimensionId).append(".").append(name).toString();
String groupName = getEntityGroupName(world, name);
TimedGroup group = getTimedGroup(groupName);

if((group == null || !(group instanceof TimedEntities)) && canCreate)
Expand All @@ -367,10 +373,7 @@ public TimedEntities getWorldTimedGroup(World world, String name, boolean canCre
//canCreate: Whether to create the group if it does not exist
//hasConfig: Whether to create with config entry
public EntityGroup getWorldEntityGroup(World world, String name, EntityType groupType, boolean canCreate, boolean hasConfig) {
String remote = "";
if(world.isRemote)
remote = "client_";
String groupName = new StringBuilder().append("worlds.").append(remote).append("dim").append(world.provider.dimensionId).append(".").append(name).toString();
String groupName = getEntityGroupName(world, name);
EntityGroup group = getEntityGroup(groupName);

if(group == null && canCreate) //Create group for world
Expand Down Expand Up @@ -409,6 +412,33 @@ public List<EntityGroup> getWorldEntityGroups(World world) {
return groups;
}

//Unload every Entity Group for the given world
public void clearWorldEntityGroups(World world) {
if(world == null)
return;

List<EntityGroup> groups = getWorldEntityGroups(world);
int groupCount = 0;
for(EntityGroup group : groups) {
if(group.getWorld() == null) {
if(debug)
System.out.println("Unable to unload group: " + group.getName() + ". World is null.");
continue;
}
String groupName = getEntityGroupName(group.getWorld(), group.getName());
if(!entityGroups.remove(groupName, group)) {
System.err.println("Failed to unload EntityGroup: " + groupName + " for world: " + world.provider.getDimensionName());
System.err.println("This might cause the world to remain in memory!");
}
else
groupCount++;
group.valid = false;
}

if(debug)
System.out.println("Unloaded " + groupCount + " EntityGroups while unloading world: " + world.provider.getDimensionName());
}

public String getWorldPrefix(World world) {
return "worlds.dim" + world.provider.dimensionId;
}
Expand Down
Loading

0 comments on commit fe0102f

Please sign in to comment.