Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dungeon Tweaks Compat again #228

Merged
merged 7 commits into from Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -87,6 +87,9 @@ public class AS_BattleTowersCore
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
DungeonTweaksCompat.legacyCheck();
DungeonTweaksCompat.registerDungeons();

configuration = new Configuration(event.getSuggestedConfigurationFile(), false);
loadForgeConfig();

Expand Down
Expand Up @@ -154,7 +154,7 @@ else if (countFoliage == result)
}

@SuppressWarnings("deprecation") // is needed because getDefaultState on stairs does not work
public void generate(World world, int ix, int jy, int kz, int towerchoice, boolean underground)
public void generate(World world, Random random, int ix, int jy, int kz, int towerchoice, boolean underground)
{
TowerTypes towerChosen = TowerTypes.values()[towerchoice];

Expand Down Expand Up @@ -381,51 +381,27 @@ public void generate(World world, int ix, int jy, int kz, int towerchoice, boole
TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner) world.getTileEntity(new BlockPos(ix + 2, builderHeight + 6, kz + 2));
if (tileentitymobspawner != null)
{
if (!Loader.isModLoaded("dungeontweaks"))
if (!DungeonTweaksCompat.isLoaded)
{
tileentitymobspawner.getSpawnerBaseLogic().setEntityId(getMobType(world.rand));
}
else
{
try
{
@SuppressWarnings("unchecked")
Constructor<? extends Event> constructor = (Constructor<? extends Event>) Class.forName("com.EvilNotch.dungeontweeks.main.Events.EventDungeon$Post")
.getConstructor(TileEntity.class, BlockPos.class, Random.class, ResourceLocation.class, World.class);
Event event = constructor.newInstance(tileentitymobspawner, tileentitymobspawner.getPos(), world.rand, new ResourceLocation("battletowers:" + towerChosen.getName()),
world);
MinecraftForge.EVENT_BUS.post(event);
}
catch (Throwable t)
{
t.printStackTrace();
}
DungeonTweaksCompat.fireDungeonSpawn(tileentitymobspawner, world, random, towerChosen);
}
}

world.setBlockState(new BlockPos(ix - 3, builderHeight + 6, kz + 2), Blocks.MOB_SPAWNER.getDefaultState());
tileentitymobspawner = (TileEntityMobSpawner) world.getTileEntity(new BlockPos(ix - 3, builderHeight + 6, kz + 2));
if (tileentitymobspawner != null)
{
if (!Loader.isModLoaded("dungeontweaks"))
if (!DungeonTweaksCompat.isLoaded)
{
tileentitymobspawner.getSpawnerBaseLogic().setEntityId(getMobType(world.rand));
}
else
{
try
{
@SuppressWarnings("unchecked")
Constructor<? extends Event> constructor = (Constructor<? extends Event>) Class.forName("com.EvilNotch.dungeontweeks.main.Events.EventDungeon$Post")
.getConstructor(TileEntity.class, BlockPos.class, Random.class, ResourceLocation.class, World.class);
Event event = constructor.newInstance(tileentitymobspawner, tileentitymobspawner.getPos(), world.rand, new ResourceLocation("battletowers:" + towerChosen.getName()),
world);
MinecraftForge.EVENT_BUS.post(event);
}
catch (Throwable t)
{
t.printStackTrace();
}
DungeonTweaksCompat.fireDungeonSpawn(tileentitymobspawner, world, random, towerChosen);
}
}
}
Expand Down Expand Up @@ -672,6 +648,11 @@ public String getName()
{
return this.typeName;
}

public ResourceLocation getId()
{
return new ResourceLocation("battletowers:" + this.typeName);
}
}

}
@@ -0,0 +1,122 @@
package atomicstryker.battletowers.common;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Random;

import atomicstryker.battletowers.common.AS_WorldGenTower.TowerTypes;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityMobSpawner;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.eventhandler.Event;

/**
* this is backwards and modern dungeon tweaks compatible
*
* @author jredfox
*
*/
public class DungeonTweaksCompat
{

public static boolean isLegacy = false;
public static boolean isLoaded = false;

/**
* make backwards compatability when isLegacy becomes true
*/
public static void legacyCheck()
{
isLoaded = Loader.isModLoaded("dungeontweaks");

if(!isLoaded)
{
return;
}

try
{
Class c = Class.forName("com.EvilNotch.dungeontweeks.main.Events.EventDungeon$Post");
isLegacy = true;
}
catch (Throwable t)
{

}
}

/**
* register all dungeon tweaks mobs to anydim towers
*/
public static void registerDungeons()
{
if (!isLoaded || isLegacy)
{
return;// I supported this mod in older versions
}

try
{
Method addDungeonMob = Class.forName("com.evilnotch.dungeontweeks.main.world.worldgen.mobs.DungeonMobs").getMethod("addDungeonMob", ResourceLocation.class, ResourceLocation.class,
int.class);
for (AS_WorldGenTower.TowerTypes tower : AS_WorldGenTower.TowerTypes.values())
{
boolean nether = tower == TowerTypes.Netherrack;
addDungeonMob.invoke(null, tower.getId(), new ResourceLocation("cave_spider"), 100);
addDungeonMob.invoke(null, tower.getId(), new ResourceLocation("spider"), 90);
addDungeonMob.invoke(null, tower.getId(), nether ? new ResourceLocation("wither_skeleton") : new ResourceLocation("skeleton"), 120);
addDungeonMob.invoke(null, tower.getId(), new ResourceLocation("zombie"), 120);

if (nether)
{
addDungeonMob.invoke(null, tower.getId(), new ResourceLocation("blaze"), 20);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* fire the event based upon tower definitions
*/
public static void fireDungeonSpawn(TileEntityMobSpawner spawner, World world, Random random, TowerTypes towerChosen)
{
ResourceLocation towerId = towerChosen.getId();
if (isLegacy)
{
try
{
@SuppressWarnings("unchecked")
Constructor<? extends Event> constructor = (Constructor<? extends Event>) Class.forName("com.EvilNotch.dungeontweeks.main.Events.EventDungeon$Post").getConstructor(TileEntity.class,
BlockPos.class, Random.class, ResourceLocation.class, World.class);
Event event = constructor.newInstance(spawner, spawner.getPos(), world.rand, towerId, world);
MinecraftForge.EVENT_BUS.post(event);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
else
{
try
{
Method fireDungeonTweaks = Class.forName("com.evilnotch.dungeontweeks.main.world.worldgen.mobs.DungeonMobs").getMethod("fireDungeonTweaks", ResourceLocation.class, TileEntity.class,
Random.class, World.class);
fireDungeonTweaks.invoke(null, towerId, spawner, random, world);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}

}
Expand Up @@ -148,7 +148,7 @@ private boolean attemptToSpawnTower(World world, TowerPosition pos, Random rando
if (choice >= 0)
{
pos.underground = world.rand.nextInt(100) + 1 < AS_BattleTowersCore.instance.chanceTowerIsUnderGround;
generator.generate(world, x, y, z, choice, pos.underground);
generator.generate(world, random, x, y, z, choice, pos.underground);
return true;
}

Expand All @@ -158,7 +158,7 @@ private boolean attemptToSpawnTower(World world, TowerPosition pos, Random rando
public static void generateTower(World world, int x, int y, int z, int type, boolean underground)
{
disableGenerationHook++;
instance.generator.generate(world, x, y, z, type, underground);
instance.generator.generate(world, world.rand, x, y, z, type, underground);
obtainTowerPosListAccess();
towerPositions.add(instance.new TowerPosition(x, y, z, type, underground));
releaseTowerPosListAccess();
Expand Down Expand Up @@ -460,7 +460,7 @@ public static TowerPosition deleteNearestTower(World world, int x, int z)

if (chosen != null)
{
instance.generator.generate(world, chosen.x, chosen.y, chosen.z, AS_WorldGenTower.TowerTypes.Null.ordinal(), chosen.underground);
instance.generator.generate(world, world.rand, chosen.x, chosen.y, chosen.z, AS_WorldGenTower.TowerTypes.Null.ordinal(), chosen.underground);
obtainTowerPosListAccess();
towerPositions.remove(chosen);
releaseTowerPosListAccess();
Expand Down Expand Up @@ -490,7 +490,7 @@ public static void deleteAllTowers(World world, boolean regenerate)
obtainTowerPosListAccess();
for (TowerPosition tp : towerPositions)
{
instance.generator.generate(world, tp.x, tp.y, tp.z, regenerate ? tp.type : AS_WorldGenTower.TowerTypes.Null.ordinal(), tp.underground);
instance.generator.generate(world, world.rand, tp.x, tp.y, tp.z, regenerate ? tp.type : AS_WorldGenTower.TowerTypes.Null.ordinal(), tp.underground);
}

if (!regenerate)
Expand Down