Skip to content

Commit

Permalink
Merge pull request #875 from Team-RTG/1.7.10/dim0-events
Browse files Browse the repository at this point in the history
Always-on, dimension-aware event handlers.
  • Loading branch information
whichonespink44 authored Jul 29, 2016
2 parents 7c65bd3 + f353909 commit 9103d96
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 72 deletions.
5 changes: 5 additions & 0 deletions etc/config/RTG/rtg.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ saplings {


"scattered features" {
# Must be set to TRUE for the other scattered feature settings to have any effect.
# If FALSE, RTG won't interfere with scattered feature generation at all.
# [default: true]
B:"Enable Scattered Feature Modifications"=true

# [default: true]
B:"Generate Scattered Features"=true

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/rtg/RTG.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public ConfigManager configManager(int dimension) {
public void preInit(FMLPreInitializationEvent event)
{
instance = this;


worldtype = new WorldTypeRTG("RTG");

MapGenStructureIO.registerStructure(MapGenScatteredFeatureRTG.Start.class, "rtg_MapGenScatteredFeatureRTG");
MapGenStructureIO.registerStructure(MapGenVillageRTG.Start.class, "rtg_MapGenVillageRTG");

Expand All @@ -85,8 +87,6 @@ public void preInit(FMLPreInitializationEvent event)

configPath = event.getModConfigurationDirectory() + "/RTG/";
ConfigManager.init(configPath);

worldtype = new WorldTypeRTG("RTG");
}

/*
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/rtg/config/rtg/ConfigRTG.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import net.minecraftforge.common.config.Configuration;

import cpw.mods.fml.common.Loader;

import org.apache.commons.lang3.ArrayUtils;

import rtg.util.Logger;
import rtg.util.ModPresenceTester;
import cpw.mods.fml.common.Loader;

public class ConfigRTG
{
Expand Down Expand Up @@ -113,7 +114,8 @@ public class ConfigRTG
public static int rtgTreeChance = 2;

/* ==================== Scattered Features ==================== */


public static boolean enableScatteredFeatureModifications = true;
public static boolean generateScatteredFeatures = true;
public static int minDistanceScatteredFeatures = 12; // Vanilla = 8
public static int maxDistanceScatteredFeatures = 48; // Vanilla = 32
Expand Down Expand Up @@ -417,7 +419,17 @@ public static void init(File configFile)
);

/* ==================== Scattered Features ==================== */


enableScatteredFeatureModifications = config.getBoolean(
"Enable Scattered Feature Modifications",
"Scattered Features",
enableScatteredFeatureModifications,
"Must be set to TRUE for the other scattered feature settings to have any effect."
+ Configuration.NEW_LINE +
"If FALSE, RTG won't interfere with scattered feature generation at all."
+ Configuration.NEW_LINE
);

generateScatteredFeatures = config.getBoolean("Generate Scattered Features", "Scattered Features", generateScatteredFeatures, "");

minDistanceScatteredFeatures = config.getInt("Minimum distance between scattered features", "Scattered Features", minDistanceScatteredFeatures, 1, Integer.MAX_VALUE, "Scattered features = desert temples, jungle temples, and witch huts; 8 = Vanilla" + Configuration.NEW_LINE);
Expand Down
117 changes: 51 additions & 66 deletions src/main/java/rtg/event/EventManagerRTG.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.terraingen.InitMapGenEvent;
import net.minecraftforge.event.terraingen.OreGenEvent;
import net.minecraftforge.event.terraingen.SaplingGrowTreeEvent;
import net.minecraftforge.event.terraingen.WorldTypeEvent;
import net.minecraftforge.event.terraingen.*;
import net.minecraftforge.event.world.ChunkEvent;
import net.minecraftforge.event.world.WorldEvent;

import cpw.mods.fml.common.eventhandler.Event.Result;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import rtg.RTG;
import rtg.config.rtg.ConfigRTG;
import rtg.util.Acceptor;
import rtg.util.Logger;
Expand All @@ -46,9 +43,11 @@ public class EventManagerRTG
private final InitBiomeGensRTG INIT_BIOME_GENS_EVENT_HANDLER = new InitBiomeGensRTG();
private final InitMapGenRTG INIT_MAP_GEN_EVENT_HANDLER = new InitMapGenRTG();
private final SaplingGrowTreeRTG SAPLING_GROW_TREE_EVENT_HANDLER = new SaplingGrowTreeRTG();
private final DecorateBiomeEventRTG DECORATE_BIOME_EVENT_HANDLER = new DecorateBiomeEventRTG();

private WeakHashMap<Integer, Acceptor<ChunkEvent.Load>> chunkLoadEvents = new WeakHashMap<>();
private long worldSeed;
private boolean isWorldTypeRTG = true;

public EventManagerRTG() {

Expand All @@ -62,6 +61,12 @@ public class LoadChunkRTG

@SubscribeEvent
public void loadChunkRTG(ChunkEvent.Load event) {

// Are we in an RTG world?
if (!(event.world.getWorldInfo().getTerrainType() instanceof WorldTypeRTG)) {
return;
}

Acceptor<ChunkEvent.Load> acceptor = chunkLoadEvents.get(event.world.provider.dimensionId);
if (acceptor != null) {
acceptor.accept(event);
Expand All @@ -78,6 +83,11 @@ public class GenerateMinableRTG
@SubscribeEvent
public void generateMinableRTG(OreGenEvent.GenerateMinable event) {

// Are we in an RTG world?
if (!(event.world.getWorldInfo().getTerrainType() instanceof WorldTypeRTG)) {
return;
}

switch (event.type) {

case COAL:
Expand Down Expand Up @@ -119,15 +129,8 @@ public class InitBiomeGensRTG
@SubscribeEvent
public void initBiomeGensRTG(WorldTypeEvent.InitBiomeGens event) {

// Are we in an RTG world?
if (!(event.worldType instanceof WorldTypeRTG)) {

/*
* None of RTG's other event handlers need to be unregistered this early since they'll all
* get unregistered before a non-RTG world loads when WorldEvent.Load is fired, but it's
* better to be safe than sorry, so let's unregister them here to be safe.
*/
unRegisterEventHandlers();

return;
}

Expand All @@ -153,12 +156,32 @@ public class InitMapGenRTG
@SubscribeEvent(priority = EventPriority.LOW)
public void initMapGenRTG(InitMapGenEvent event) {

// If the Overworld isn't an RTG dimension, then we definitely don't want to get involved here.
// We need to do a try/catch because sometimes this event gets fired before the Overworld has loaded.
try {
if (!(MinecraftServer.getServer().worldServerForDimension(0).getWorldInfo().getTerrainType() instanceof WorldTypeRTG)) {
return;
}
}
catch (Exception e) {

Logger.debug("Overworld not loaded... checking global variable.");

// Let's do one last sanity check to make sure it's safe to proceed.
if (!isWorldTypeRTG) {
return;
}
}

Logger.debug("event type = %s", event.type.toString());
Logger.debug("event originalGen = %s", event.originalGen.toString());

switch (event.type) {

case SCATTERED_FEATURE:
event.newGen = new MapGenScatteredFeatureRTG();
if (ConfigRTG.enableScatteredFeatureModifications) {
event.newGen = new MapGenScatteredFeatureRTG();
}
break;

case VILLAGE:
Expand Down Expand Up @@ -318,40 +341,27 @@ public void onWorldLoad(WorldEvent.Load event) {
worldSeed = event.world.getSeed();
Logger.info("World Seed: " + worldSeed);
}

/*
* When loading a non-RTG world, we need to make sure that RTG's event handlers are unregistered.
*
* If we're loading an RTG world, RTG's event handlers should already be registered at this point,
* but it doesn't hurt to call registerEventHandlers() here because Forge only registers event
* handlers that aren't already registered, so better to be safe than sorry.
*/
if (!(event.world.getWorldInfo().getTerrainType() instanceof WorldTypeRTG)) {

RTG.eventMgr.unRegisterEventHandlers();
}
else {

RTG.eventMgr.registerEventHandlers();
}
}

@SubscribeEvent
public void onWorldUnload(WorldEvent.Unload event) {

// Reset the world seed so that it logs on the next server start if the seed is the same as the last load.
worldSeed = 0;
}
}

/*
* When loading an RTG world, WorldTypeEvent.InitBiomeGens needs to be registered before
* FMLServerAboutToStartEvent is fired, so the only way to ensure that it gets registered
* between unloading a non-RTG world and loading an RTG world is to register RTG's event handlers
* when a non-RTG world unloads.
*/
if (!(event.world.getWorldInfo().getTerrainType() instanceof WorldTypeRTG)) {
public class DecorateBiomeEventRTG
{
DecorateBiomeEventRTG() {
logEventMessage("Initialising DecorateBiomeEventRTG...");
}

RTG.eventMgr.registerEventHandlers();
}
@SubscribeEvent
public void preBiomeDecorate(DecorateBiomeEvent.Pre event)
{
//Are we in an RTG world?
isWorldTypeRTG = (event.world.getWorldInfo().getTerrainType() instanceof WorldTypeRTG);
}
}

Expand All @@ -372,41 +382,16 @@ public void registerEventHandlers() {
MinecraftForge.TERRAIN_GEN_BUS.register(INIT_BIOME_GENS_EVENT_HANDLER);
MinecraftForge.TERRAIN_GEN_BUS.register(INIT_MAP_GEN_EVENT_HANDLER);
MinecraftForge.TERRAIN_GEN_BUS.register(SAPLING_GROW_TREE_EVENT_HANDLER);
MinecraftForge.TERRAIN_GEN_BUS.register(DECORATE_BIOME_EVENT_HANDLER);

logEventMessage("RTG's event handlers have been registered successfully.");
}

/*
* This method unregisters most of RTG's event handlers.
*
* We don't need to check if the event handlers are registered before unregistering them
* because Forge already performs those checks. If this method gets executed when none
* of RTG's event handlers are registered, nothing bad will happen.
*/
public void unRegisterEventHandlers() {

logEventMessage("Unregistering RTG's event handlers...");

/*
* The world event handler must always be registered.
*
* MinecraftForge.EVENT_BUS.unregister(WORLD_EVENT_HANDLER);
*/

MinecraftForge.EVENT_BUS.unregister(LOAD_CHUNK_EVENT_HANDLER);
MinecraftForge.ORE_GEN_BUS.unregister(GENERATE_MINABLE_EVENT_HANDLER);
MinecraftForge.TERRAIN_GEN_BUS.unregister(INIT_BIOME_GENS_EVENT_HANDLER);
MinecraftForge.TERRAIN_GEN_BUS.unregister(INIT_MAP_GEN_EVENT_HANDLER);
MinecraftForge.TERRAIN_GEN_BUS.unregister(SAPLING_GROW_TREE_EVENT_HANDLER);

logEventMessage("RTG's event handlers have been unregistered successfully.");
}

public void setDimensionChunkLoadEvent(int dimension, Acceptor<ChunkEvent.Load> action) {
chunkLoadEvents.put(dimension, action);
}

private static void logEventMessage(String message) {
Logger.info("RTG Event System: " + message);
Logger.debug("RTG Event System: " + message);
}
}

0 comments on commit 9103d96

Please sign in to comment.