Search in sources :

Example 1 with PostEntryTask

use of com.mraof.minestuck.util.PostEntryTask in project Minestuck by mraof.

the class MinestuckSaveHandler method onWorldSave.

@SubscribeEvent
public void onWorldSave(WorldEvent.Save event) {
    if (// Only save one time each world-save instead of one per dimension each world-save.
    event.getWorld().provider.getDimension() != 0)
        return;
    File dataFile = event.getWorld().getSaveHandler().getMapFileFromName("MinestuckData");
    if (dataFile != null) {
        NBTTagCompound nbt = new NBTTagCompound();
        // Keep this before skaianet
        ServerEditHandler.saveData(nbt);
        MinestuckDimensionHandler.saveData(nbt);
        TileEntityTransportalizer.saveTransportalizers(nbt);
        SkaianetHandler.saveData(nbt);
        MinestuckPlayerData.writeToNBT(nbt);
        NBTTagList list = new NBTTagList();
        for (PostEntryTask task : ServerEventHandler.tickTasks) list.appendTag(task.toNBTTagCompound());
        if (list.tagCount() > 0)
            nbt.setTag("tickTasks", list);
        try {
            CompressedStreamTools.writeCompressed(nbt, new FileOutputStream(dataFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) FileOutputStream(java.io.FileOutputStream) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) PostEntryTask(com.mraof.minestuck.util.PostEntryTask) File(java.io.File) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with PostEntryTask

use of com.mraof.minestuck.util.PostEntryTask in project Minestuck by mraof.

the class MinestuckSaveHandler method onWorldLoad.

@SubscribeEvent
public void onWorldLoad(WorldEvent.Load event) {
    if (event.getWorld().provider.getDimension() != 0 || event.getWorld().isRemote)
        return;
    ISaveHandler saveHandler = event.getWorld().getSaveHandler();
    File dataFile = saveHandler.getMapFileFromName("MinestuckData");
    if (dataFile != null && dataFile.exists()) {
        NBTTagCompound nbt = null;
        try {
            nbt = CompressedStreamTools.readCompressed(new FileInputStream(dataFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nbt != null) {
            ServerEditHandler.loadData(nbt);
            MinestuckDimensionHandler.loadData(nbt);
            SkaianetHandler.loadData(nbt.getCompoundTag("skaianet"));
            MinestuckPlayerData.readFromNBT(nbt);
            TileEntityTransportalizer.loadTransportalizers(nbt.getCompoundTag("transportalizers"));
            ServerEventHandler.tickTasks.clear();
            if (nbt.hasKey("tickTasks", 9)) {
                NBTTagList list = nbt.getTagList("tickTasks", 10);
                for (int i = 0; i < list.tagCount(); i++) ServerEventHandler.tickTasks.add(new PostEntryTask(list.getCompoundTagAt(i)));
            }
            return;
        }
    }
    ServerEventHandler.tickTasks.clear();
    SkaianetHandler.loadData(null);
    MinestuckPlayerData.readFromNBT(null);
    ServerEditHandler.loadData(null);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) ISaveHandler(net.minecraft.world.storage.ISaveHandler) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) PostEntryTask(com.mraof.minestuck.util.PostEntryTask) File(java.io.File) FileInputStream(java.io.FileInputStream) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 3 with PostEntryTask

use of com.mraof.minestuck.util.PostEntryTask in project Minestuck by mraof.

the class ItemCruxiteArtifact method makeDestination.

@Override
public void makeDestination(Entity entity, WorldServer worldserver0, WorldServer worldserver1) {
    if (entity instanceof EntityPlayerMP) {
        // ((EntityPlayerMP) entity).addStat(MinestuckAchievementHandler.enterMedium);
        Debug.infof("Starting entry for player %s", entity.getName());
        int x = (int) entity.posX;
        if (entity.posX < 0)
            x--;
        int y = (int) entity.posY;
        int z = (int) entity.posZ;
        if (entity.posZ < 0)
            z--;
        boolean creative = ((EntityPlayerMP) entity).interactionManager.isCreative();
        int topY = MinestuckConfig.adaptEntryBlockHeight ? getTopHeight(worldserver0, x, y, z) : y + artifactRange;
        int yDiff = 127 - topY;
        // Set again, but with a more precise now that the y-coordinate is properly decided.
        MinestuckDimensionHandler.setSpawn(worldserver1.provider.getDimension(), new BlockPos(x, y + yDiff, z));
        Debug.debug("Loading spawn chunks...");
        for (// Prevent anything to generate on the piece that we move
        int chunkX = ((x - artifactRange) >> 4) - 1; // Prevent anything to generate on the piece that we move
        chunkX <= ((x + artifactRange) >> 4) + 2; // Prevent anything to generate on the piece that we move
        chunkX++) for (// from the overworld.
        int chunkZ = ((z - artifactRange) >> 4) - 1; // from the overworld.
        chunkZ <= ((z + artifactRange) >> 4) + 2; // from the overworld.
        chunkZ++) worldserver1.getChunkProvider().provideChunk(chunkX, chunkZ);
        Debug.debug("Placing blocks...");
        long time = System.currentTimeMillis();
        int bl = 0;
        for (int blockX = x - artifactRange; blockX <= x + artifactRange; blockX++) {
            int zWidth = (int) Math.sqrt((artifactRange + 0.5) * (artifactRange + 0.5) - (blockX - x) * (blockX - x));
            for (int blockZ = z - zWidth; blockZ <= z + zWidth; blockZ++) {
                Chunk chunk = worldserver1.getChunkFromChunkCoords(blockX >> 4, blockZ >> 4);
                Chunk chunk2 = worldserver0.getChunkFromChunkCoords(blockX >> 4, blockZ >> 4);
                int height = (int) Math.sqrt(artifactRange * artifactRange - (((blockX - x) * (blockX - x) + (blockZ - z) * (blockZ - z)) / 2));
                for (int blockY = Math.max(0, y - height); blockY <= Math.min(topY, y + height); blockY++) {
                    BlockPos pos = new BlockPos(blockX, blockY, blockZ);
                    BlockPos pos1 = pos.up(yDiff);
                    IBlockState block = worldserver0.getBlockState(pos);
                    TileEntity te = worldserver0.getTileEntity(pos);
                    long t = System.currentTimeMillis();
                    if (block.getBlock() != Blocks.BEDROCK && block.getBlock() != Blocks.PORTAL) {
                        copyBlockDirect(chunk, chunk2, blockX & 15, blockY + yDiff, blockY, blockZ & 15);
                    } else {
                        worldserver1.setBlockState(new BlockPos(blockX, blockY + yDiff, blockZ), Blocks.AIR.getDefaultState(), 3);
                    }
                    bl += System.currentTimeMillis() - t;
                    if ((te) != null) {
                        TileEntity te1 = null;
                        try {
                            te1 = te.getClass().newInstance();
                        } catch (Exception e) {
                            e.printStackTrace();
                            continue;
                        }
                        NBTTagCompound nbt = new NBTTagCompound();
                        te.writeToNBT(nbt);
                        nbt.setInteger("y", pos1.getY());
                        te1.readFromNBT(nbt);
                        worldserver1.removeTileEntity(pos1);
                        worldserver1.setTileEntity(pos1, te1);
                        if (te instanceof TileEntityComputer)
                            SkaianetHandler.movingComputer((TileEntityComputer) te, (TileEntityComputer) te1);
                    }
                }
                for (int blockY = Math.min(topY, y + height) + yDiff + 1; blockY < 256; blockY++) worldserver1.setBlockState(new BlockPos(blockX, blockY, blockZ), Blocks.AIR.getDefaultState(), 0);
            }
        }
        int total = (int) (System.currentTimeMillis() - time);
        Debug.debugf("Total: %d, block: %d", total, bl);
        Debug.debug("Teleporting entities...");
        AxisAlignedBB entityTeleportBB = entity.getEntityBoundingBox().grow((double) artifactRange, artifactRange, (double) artifactRange);
        List<Entity> list = worldserver0.getEntitiesWithinAABBExcludingEntity(entity, entityTeleportBB);
        Iterator<Entity> iterator = list.iterator();
        entity.setPositionAndUpdate(entity.posX, entity.posY + yDiff, entity.posZ);
        while (iterator.hasNext()) {
            Entity e = iterator.next();
            if (MinestuckConfig.entryCrater || e instanceof EntityPlayer || !creative && e instanceof EntityItem) {
                if (e instanceof EntityPlayer && ServerEditHandler.getData((EntityPlayer) e) != null)
                    ServerEditHandler.reset(ServerEditHandler.getData((EntityPlayer) e));
                else {
                    Teleport.teleportEntity(e, worldserver1.provider.getDimension(), null, e.posX, e.posY + yDiff, e.posZ);
                }
            } else // Copy instead of teleport
            {
                Entity newEntity = EntityList.newEntity(entity.getClass(), worldserver1);
                if (newEntity != null) {
                    NBTTagCompound nbttagcompound = new NBTTagCompound();
                    entity.writeToNBT(nbttagcompound);
                    nbttagcompound.removeTag("Dimension");
                    newEntity.readFromNBT(nbttagcompound);
                    newEntity.dimension = worldserver1.provider.getDimension();
                    newEntity.setPosition(newEntity.posX, newEntity.posY + yDiff, newEntity.posZ);
                    worldserver1.spawnEntity(newEntity);
                }
            }
        }
        Debug.debug("Removing old blocks...");
        for (int blockX = x - artifactRange; blockX <= x + artifactRange; blockX++) {
            int zWidth = (int) Math.sqrt(artifactRange * artifactRange - (blockX - x) * (blockX - x));
            boolean isEdgeX = Math.abs(blockX - x) == artifactRange;
            for (int blockZ = z - zWidth; blockZ <= z + zWidth; blockZ++) {
                double radius = Math.sqrt(((blockX - x) * (blockX - x) + (blockZ - z) * (blockZ - z)) / 2);
                int height = (int) (Math.sqrt(artifactRange * artifactRange - radius * radius));
                int minY = y - height;
                minY = minY < 0 ? 0 : minY;
                int maxY = MinestuckConfig.entryCrater ? Math.min(topY, y + height) + 1 : 256;
                boolean isEdgeZ = Math.abs(blockZ - z) == zWidth;
                for (int blockY = minY; blockY < maxY; blockY++) {
                    BlockPos pos = new BlockPos(blockX, blockY, blockZ);
                    if (MinestuckConfig.entryCrater) {
                        if (worldserver0.getBlockState(pos).getBlock() != Blocks.BEDROCK) {
                            if (isEdgeX || isEdgeZ || blockY == minY || blockY == maxY - 1) {
                                worldserver0.setBlockState(pos, Blocks.AIR.getDefaultState(), 3);
                            } else {
                                worldserver0.setBlockState(pos, Blocks.AIR.getDefaultState(), 2);
                            }
                        }
                    } else {
                        TileEntity tileEntity = worldserver0.getTileEntity(pos);
                        if (tileEntity != null)
                            if (!creative)
                                worldserver0.setBlockState(pos, Blocks.AIR.getDefaultState(), 3);
                            else if (// Avoid duplicating computer data when a computer is kept in the overworld
                            tileEntity instanceof TileEntityComputer)
                                ((TileEntityComputer) tileEntity).programData = new NBTTagCompound();
                            else if (tileEntity instanceof TileEntityTransportalizer)
                                worldserver0.removeTileEntity(pos);
                    }
                }
            }
        }
        SkaianetHandler.clearMovingList();
        if (!creative || MinestuckConfig.entryCrater) {
            // Normally only items in containers
            Debug.debug("Removing entities created from removing blocks...");
            list = worldserver0.getEntitiesWithinAABBExcludingEntity(entity, entityTeleportBB);
            iterator = list.iterator();
            while (iterator.hasNext()) if (MinestuckConfig.entryCrater)
                iterator.next().setDead();
            else {
                Entity e = iterator.next();
                if (e instanceof EntityItem)
                    e.setDead();
            }
        }
        Debug.debug("Placing gates...");
        GateHandler.findGatePlacement(worldserver1);
        placeGate(1, new BlockPos(x, GateHandler.gateHeight1, z), worldserver1);
        placeGate(2, new BlockPos(x, GateHandler.gateHeight2, z), worldserver1);
        ServerEventHandler.tickTasks.add(new PostEntryTask(worldserver1.provider.getDimension(), x, y + yDiff, z, artifactRange, (byte) 0));
        Debug.info("Entry finished");
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) TileEntityComputer(com.mraof.minestuck.tileentity.TileEntityComputer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Chunk(net.minecraft.world.chunk.Chunk) PostEntryTask(com.mraof.minestuck.util.PostEntryTask) TileEntityTransportalizer(com.mraof.minestuck.tileentity.TileEntityTransportalizer) TileEntity(net.minecraft.tileentity.TileEntity) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BlockPos(net.minecraft.util.math.BlockPos) EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

PostEntryTask (com.mraof.minestuck.util.PostEntryTask)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 File (java.io.File)2 IOException (java.io.IOException)2 NBTTagList (net.minecraft.nbt.NBTTagList)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 TileEntityComputer (com.mraof.minestuck.tileentity.TileEntityComputer)1 TileEntityTransportalizer (com.mraof.minestuck.tileentity.TileEntityTransportalizer)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IBlockState (net.minecraft.block.state.IBlockState)1 Entity (net.minecraft.entity.Entity)1 EntityItem (net.minecraft.entity.item.EntityItem)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 TileEntity (net.minecraft.tileentity.TileEntity)1 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)1 BlockPos (net.minecraft.util.math.BlockPos)1 Chunk (net.minecraft.world.chunk.Chunk)1 ISaveHandler (net.minecraft.world.storage.ISaveHandler)1