diff --git a/src/main/java/makeo/gadomancy/common/CommonProxy.java b/src/main/java/makeo/gadomancy/common/CommonProxy.java index b80fdf8..dfd1f4f 100644 --- a/src/main/java/makeo/gadomancy/common/CommonProxy.java +++ b/src/main/java/makeo/gadomancy/common/CommonProxy.java @@ -21,8 +21,8 @@ import makeo.gadomancy.common.registration.RegisteredItems; import makeo.gadomancy.common.registration.RegisteredPotions; import makeo.gadomancy.common.registration.RegisteredRecipes; import makeo.gadomancy.common.registration.RegisteredResearches; -import makeo.gadomancy.common.utils.WorldProviderTCEldrich; import makeo.gadomancy.common.utils.Injector; +import makeo.gadomancy.common.utils.world.WorldProviderTCEldrich; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; @@ -79,8 +79,8 @@ public class CommonProxy implements IGuiHandler { RegisteredPotions.init(); - //DimensionManager.registerProviderType(ModConfig.dimOuterId, WorldProviderTCEldrich.class, false); - //DimensionManager.registerDimension(ModConfig.dimOuterId, ModConfig.dimOuterId); + DimensionManager.registerProviderType(ModConfig.dimOuterId, WorldProviderTCEldrich.class, false); + DimensionManager.registerDimension(ModConfig.dimOuterId, ModConfig.dimOuterId); } public void postInitalize() { diff --git a/src/main/java/makeo/gadomancy/common/data/ModConfig.java b/src/main/java/makeo/gadomancy/common/data/ModConfig.java index 9158e4d..ee8d632 100644 --- a/src/main/java/makeo/gadomancy/common/data/ModConfig.java +++ b/src/main/java/makeo/gadomancy/common/data/ModConfig.java @@ -18,6 +18,7 @@ public class ModConfig { public static int golemDatawatcherId = 29; public static int dimOuterId = -73; + public static int maxMazeCount = -1; private ModConfig() {} @@ -34,5 +35,6 @@ public class ModConfig { private static void loadFromConfig() { golemDatawatcherId = config.getInt("datawatcherId", "golem", 29, 0, 31, "Do not edit unless you know what are you doing!"); dimOuterId = config.getInt("dimOuterId", "dimension", -73, Integer.MIN_VALUE, Integer.MAX_VALUE, "Dimension Id for the eldrich mazes accessed via Node Manipulator"); + maxMazeCount = config.getInt("maxMazeCount", "dimension", -1, -1, Integer.MAX_VALUE, "Defines how many Eldritch mazes may exist at the same time using the Gadomancy Eldritch ritual. (-1 = infinite) Note that 1 maze = 1 player; Once the player finishes the maze, the maze closes itself and teleports the player out."); } } diff --git a/src/main/java/makeo/gadomancy/common/events/EventHandlerNetwork.java b/src/main/java/makeo/gadomancy/common/events/EventHandlerNetwork.java index 706cfa2..a9330ce 100644 --- a/src/main/java/makeo/gadomancy/common/events/EventHandlerNetwork.java +++ b/src/main/java/makeo/gadomancy/common/events/EventHandlerNetwork.java @@ -7,6 +7,7 @@ import makeo.gadomancy.common.network.PacketHandler; import makeo.gadomancy.common.network.packets.PacketFamiliar; import makeo.gadomancy.common.network.packets.PacketUpdateGolemTypeOrder; import makeo.gadomancy.common.utils.GolemEnumHelper; +import makeo.gadomancy.common.utils.world.TCMazeHandler; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; @@ -33,5 +34,7 @@ public class EventHandlerNetwork { public void on(PlayerEvent.PlayerLoggedOutEvent e) { EntityPlayer player = e.player; Gadomancy.proxy.familiarHandler.notifyUnequip(player.worldObj, player); + + TCMazeHandler.closeSession(e.player); } } diff --git a/src/main/java/makeo/gadomancy/common/events/EventHandlerWorld.java b/src/main/java/makeo/gadomancy/common/events/EventHandlerWorld.java index 5044610..f75b53d 100644 --- a/src/main/java/makeo/gadomancy/common/events/EventHandlerWorld.java +++ b/src/main/java/makeo/gadomancy/common/events/EventHandlerWorld.java @@ -2,6 +2,7 @@ package makeo.gadomancy.common.events; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; import makeo.gadomancy.common.Gadomancy; import makeo.gadomancy.common.blocks.tiles.TileNodeManipulator; import makeo.gadomancy.common.blocks.tiles.TileStickyJar; @@ -9,6 +10,7 @@ import makeo.gadomancy.common.registration.RegisteredBlocks; import makeo.gadomancy.common.registration.RegisteredItems; import makeo.gadomancy.common.utils.GolemEnumHelper; import makeo.gadomancy.common.utils.JarMultiblockHandler; +import makeo.gadomancy.common.utils.world.TCMazeHandler; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.server.MinecraftServer; @@ -42,6 +44,8 @@ public class EventHandlerWorld { GolemEnumHelper.validateSavedMapping(); GolemEnumHelper.reorderEnum(); + + TCMazeHandler.init(); } } @@ -49,9 +53,17 @@ public class EventHandlerWorld { public void on(WorldEvent.Unload e) { if (!e.world.isRemote && e.world.equals(MinecraftServer.getServer().getEntityWorld())) { Gadomancy.unloadModData(); + + TCMazeHandler.closeAllSessionsAndCleanup(); } } + @SubscribeEvent + public void on(TickEvent.WorldTickEvent event) { + if(!event.world.isRemote && event.world.provider.dimensionId == 0) { + TCMazeHandler.tick(); + } + } private Map interacts = null; diff --git a/src/main/java/makeo/gadomancy/common/utils/world/TCMazeHandler.java b/src/main/java/makeo/gadomancy/common/utils/world/TCMazeHandler.java new file mode 100644 index 0000000..d18b25a --- /dev/null +++ b/src/main/java/makeo/gadomancy/common/utils/world/TCMazeHandler.java @@ -0,0 +1,165 @@ +package makeo.gadomancy.common.utils.world; + +import makeo.gadomancy.common.data.ModConfig; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.ChunkCoordinates; +import net.minecraft.util.Vec3; +import net.minecraft.world.World; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.DimensionManager; +import net.minecraftforge.common.util.BlockSnapshot; +import thaumcraft.common.config.ConfigBlocks; +import thaumcraft.common.lib.world.dim.Cell; +import thaumcraft.common.lib.world.dim.CellLoc; +import thaumcraft.common.lib.world.dim.MazeHandler; +import thaumcraft.common.lib.world.dim.MazeThread; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; + +/** + * This class is part of the Gadomancy Mod + * Gadomancy is Open Source and distributed under the + * GNU LESSER GENERAL PUBLIC LICENSE + * for more read the LICENSE file + * + * Created by HellFirePvP @ 05.11.2015 10:40 + */ +public class TCMazeHandler { + + //How maze gen works overall (for us): + //Defining what chunks are currently occupied with the 'labyrinthCopy' map. + //If a chunk is not listed in there, it's considered empty. + //level saving itself is disabled. Players without active session will get teleported out. + + //Our map to work with. + public static ConcurrentHashMap labyrinthCopy = new ConcurrentHashMap(); + public static final int TELEPORT_LAYER_Y = 53; + + private static Map runningSessions = new HashMap(); + + public static void closeAllSessionsAndCleanup() { + for (EntityPlayer pl : runningSessions.keySet()) { + runningSessions.get(pl).closeSession(); + } + init(); + } + + public static void tick() { + WorldServer w = DimensionManager.getWorld(ModConfig.dimOuterId); + if(w != null) { + WorldServer out = MinecraftServer.getServer().worldServerForDimension(0); + for(Object playerObj : w.playerEntities) { + EntityPlayer player = (EntityPlayer) playerObj; + if(!hasOpenSession(player)) { + WorldUtil.tryTeleportBack((EntityPlayerMP) player, 0); + ChunkCoordinates cc = out.getSpawnPoint(); + player.setPosition(cc.posX + 0.5, cc.posY + 0.5, cc.posZ + 0.5); + } + } + for(EntityPlayer player : runningSessions.keySet()) { + if(player.worldObj.provider.dimensionId != ModConfig.dimOuterId) { //If the player left our dim, but he should still be in the session, ... + closeSession(player); + } + } + } + } + + /* + Coordinates wanted here are the absolute Portal coordinates. + */ + public static boolean createSessionAndTeleport(EntityPlayer player, int pX, int pY, int pZ) { + if(hasOpenSession(player) || !hasFreeSessionSpace()) return false; + Map chunksAffected = setupChunks(player.worldObj, pX, pZ); + Vec3 currentPos = Vec3.createVectorHelper(player.posX, player.posY, player.posZ); + int originDim = player.worldObj.provider.dimensionId; + TCMazeSession session = new TCMazeSession(player, chunksAffected, originDim, currentPos); + session.startSession(); + runningSessions.put(player, session); + return true; + } + + private static Map setupChunks(World world, int pX, int pZ) { + ConcurrentHashMap oldDat = MazeHandler.labyrinth; + ConcurrentHashMap bufferOld = labyrinthCopy; + MazeHandler.labyrinth = labyrinthCopy; + int chX = pX >> 4; + int chZ = pZ >> 4; + int w = randWH(world.rand); + int h = randWH(world.rand); + while (MazeHandler.mazesInRange(chX, chZ, w, h)) { + chX++; + } + MazeThread mt = new MazeThread(chX, chZ, w, h, world.rand.nextLong()); + mt.run(); + Map locs = calculateDifferences(bufferOld); + for(CellLoc l : locs.keySet()) { + Cell c = new Cell(locs.get(l)); + boolean oldFlag = false; + if(c.feature == 1) { + oldFlag = world.captureBlockSnapshots; + world.captureBlockSnapshots = true; + } + MazeHandler.generateEldritch(world, world.rand, l.x, l.z); + if(c.feature == 1) { + world.captureBlockSnapshots = oldFlag; + ArrayList snapshots = world.capturedBlockSnapshots; + for(BlockSnapshot sn : snapshots) { + if(sn.getCurrentBlock() == ConfigBlocks.blockEldritch && (sn.world.getBlockMetadata(sn.x, sn.y, sn.z) == 1 || sn.world.getBlockMetadata(sn.x, sn.y, sn.z) == 2)) { + sn.world.setBlockToAir(sn.x, sn.y, sn.z); + } + } + } + } + labyrinthCopy = MazeHandler.labyrinth; + MazeHandler.labyrinth = oldDat; + return locs; + } + + private static Map calculateDifferences(ConcurrentHashMap bufferOld) { + ConcurrentHashMap newDat = MazeHandler.labyrinth; //Only the new data has data, the old one doesn't have. + Map list = new HashMap(); + for(CellLoc loc : newDat.keySet()) { + if(!bufferOld.containsKey(loc)) { + list.put(loc, newDat.get(loc)); + } + } + return list; + } + + private static boolean hasFreeSessionSpace() { + return ModConfig.maxMazeCount == -1 || runningSessions.size() < ModConfig.maxMazeCount; + } + + private static int randWH(Random random) { + return 15 + random.nextInt(8) * 2; + } + + public static void init() { + runningSessions = new HashMap(); + } + + public static boolean hasOpenSession(EntityPlayer player) { + return !player.worldObj.isRemote && runningSessions.get(player) != null; + } + + public static void closeSession(EntityPlayer player) { + if(player.worldObj.isRemote) return; + + if(runningSessions.containsKey(player)) { + runningSessions.get(player).closeSession(); + runningSessions.remove(player); + } + } + + public static void free(Map locations) { + for(CellLoc loc : locations.keySet()) { + labyrinthCopy.remove(loc); + } + } +} diff --git a/src/main/java/makeo/gadomancy/common/utils/world/TCMazeSession.java b/src/main/java/makeo/gadomancy/common/utils/world/TCMazeSession.java new file mode 100644 index 0000000..18cde5e --- /dev/null +++ b/src/main/java/makeo/gadomancy/common/utils/world/TCMazeSession.java @@ -0,0 +1,69 @@ +package makeo.gadomancy.common.utils.world; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.Vec3; +import org.apache.logging.log4j.LogManager; +import thaumcraft.common.lib.world.dim.Cell; +import thaumcraft.common.lib.world.dim.CellLoc; + +import java.util.Map; + +/** + * This class is part of the Gadomancy Mod + * Gadomancy is Open Source and distributed under the + * GNU LESSER GENERAL PUBLIC LICENSE + * for more read the LICENSE file + * + * Created by HellFirePvP @ 05.11.2015 14:12 + */ +class TCMazeSession { + + public final EntityPlayerMP player; + public final Map chunksAffected; + public final CellLoc portalCell; + + public final int originDimId; + public final Vec3 originLocation; + + TCMazeSession(EntityPlayer owner, Map locations, int dim, Vec3 origin) { + this.player = (EntityPlayerMP) owner; + this.chunksAffected = locations; + this.originDimId = dim; + this.originLocation = origin; + this.portalCell = findPortal(); + } + + private CellLoc findPortal() { + for(CellLoc loc : chunksAffected.keySet()) { + Short s = chunksAffected.get(loc); + Cell c = new Cell(s); + if(c.feature == 1) { + return loc; + } + } + return null; + } + + final void closeSession() { + TCMazeHandler.free(chunksAffected); + WorldUtil.tryTeleportBack(player, originDimId); + player.setPosition(originLocation.xCoord, originLocation.yCoord, originLocation.zCoord); + } + + final void startSession() { + if(portalCell == null) { + LogManager.getLogger().error("Thaumcraft didn't generate a portal! Stopping instance!", new IllegalStateException()); + closeSession(); + player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Thaumcraft didn't generate a portal in the Eldritch dimension. Sorry, we can't teleport you..")); + } else { + WorldUtil.teleportToFakeOuter(player); + int x = portalCell.x * 16 + 7; + int z = portalCell.z * 16 + 7; + player.setPosition(x, TCMazeHandler.TELEPORT_LAYER_Y, z); + } + } + +} diff --git a/src/main/java/makeo/gadomancy/common/utils/world/TeleporterNothing.java b/src/main/java/makeo/gadomancy/common/utils/world/TeleporterNothing.java new file mode 100644 index 0000000..b8eac5e --- /dev/null +++ b/src/main/java/makeo/gadomancy/common/utils/world/TeleporterNothing.java @@ -0,0 +1,36 @@ +package makeo.gadomancy.common.utils.world; + +import net.minecraft.entity.Entity; +import net.minecraft.world.Teleporter; +import net.minecraft.world.WorldServer; + +/** + * This class is part of the Gadomancy Mod + * Gadomancy is Open Source and distributed under the + * GNU LESSER GENERAL PUBLIC LICENSE + * for more read the LICENSE file + * + * Created by HellFirePvP @ 05.11.2015 13:57 + */ +public class TeleporterNothing extends Teleporter { + + public TeleporterNothing(WorldServer worldServer) { + super(worldServer); + } + + @Override + public void removeStalePortalLocations(long p_85189_1_) {} + + @Override + public void placeInPortal(Entity p_77185_1_, double p_77185_2_, double p_77185_4_, double p_77185_6_, float p_77185_8_) {} + + @Override + public boolean placeInExistingPortal(Entity p_77184_1_, double p_77184_2_, double p_77184_4_, double p_77184_6_, float p_77184_8_) { + return true; + } + + @Override + public boolean makePortal(Entity entity) { + return true; + } +} diff --git a/src/main/java/makeo/gadomancy/common/utils/WorldProviderTCEldrich.java b/src/main/java/makeo/gadomancy/common/utils/world/WorldProviderTCEldrich.java similarity index 98% rename from src/main/java/makeo/gadomancy/common/utils/WorldProviderTCEldrich.java rename to src/main/java/makeo/gadomancy/common/utils/world/WorldProviderTCEldrich.java index 89e6283..6c221bd 100644 --- a/src/main/java/makeo/gadomancy/common/utils/WorldProviderTCEldrich.java +++ b/src/main/java/makeo/gadomancy/common/utils/world/WorldProviderTCEldrich.java @@ -1,4 +1,4 @@ -package makeo.gadomancy.common.utils; +package makeo.gadomancy.common.utils.world; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; diff --git a/src/main/java/makeo/gadomancy/common/utils/world/WorldUtil.java b/src/main/java/makeo/gadomancy/common/utils/world/WorldUtil.java new file mode 100644 index 0000000..bcd7def --- /dev/null +++ b/src/main/java/makeo/gadomancy/common/utils/world/WorldUtil.java @@ -0,0 +1,32 @@ +package makeo.gadomancy.common.utils.world; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import makeo.gadomancy.common.data.ModConfig; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.server.MinecraftServer; + +/** + * This class is part of the Gadomancy Mod + * Gadomancy is Open Source and distributed under the + * GNU LESSER GENERAL PUBLIC LICENSE + * for more read the LICENSE file + * + * Created by HellFirePvP @ 05.11.2015 14:01 + */ +public class WorldUtil { + + @SideOnly(Side.SERVER) + public static void teleportToFakeOuter(EntityPlayerMP player) { + MinecraftServer mServer = FMLCommonHandler.instance().getMinecraftServerInstance(); + mServer.getConfigurationManager().transferPlayerToDimension(player, ModConfig.dimOuterId, new TeleporterNothing(mServer.worldServerForDimension(ModConfig.dimOuterId))); + } + + @SideOnly(Side.SERVER) + public static void tryTeleportBack(EntityPlayerMP player, int dim) { + MinecraftServer mServer = FMLCommonHandler.instance().getMinecraftServerInstance(); + mServer.getConfigurationManager().transferPlayerToDimension(player, dim, new TeleporterNothing(mServer.worldServerForDimension(dim))); + } + +}