From 8137f7dc0272628925729556e888d305d8a502a0 Mon Sep 17 00:00:00 2001 From: ROM Date: Fri, 4 Mar 2022 10:46:14 -0600 Subject: [PATCH] change: update chunk-loading internals (non-final) --- .../core/event/EventHandlerGC.java | 44 +- .../mods/galacticraft/core/util/MapUtil.java | 229 +++++----- .../core/world/ChunkLoadingCallback.java | 406 ++++++++---------- .../mars/tile/TileEntityLaunchController.java | 54 ++- .../mars/tile/TileEntityTerraformer.java | 93 ++-- 5 files changed, 389 insertions(+), 437 deletions(-) diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/event/EventHandlerGC.java b/src/main/java/micdoodle8/mods/galacticraft/core/event/EventHandlerGC.java index 13b30a3d2a..80afc35303 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/event/EventHandlerGC.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/event/EventHandlerGC.java @@ -43,7 +43,6 @@ import micdoodle8.mods.galacticraft.core.util.GCCoreUtil; import micdoodle8.mods.galacticraft.core.util.OxygenUtil; import micdoodle8.mods.galacticraft.core.util.PlayerUtil; -import micdoodle8.mods.galacticraft.core.world.ChunkLoadingCallback; import micdoodle8.mods.galacticraft.core.wrappers.PlayerGearData; import micdoodle8.mods.galacticraft.planets.asteroids.AsteroidsModule; import micdoodle8.mods.galacticraft.planets.mars.network.PacketSimpleMars; @@ -103,9 +102,6 @@ import net.minecraftforge.event.terraingen.PopulateChunkEvent; import net.minecraftforge.event.terraingen.TerrainGen; import net.minecraftforge.event.world.BlockEvent; -import net.minecraftforge.event.world.ChunkDataEvent; -import net.minecraftforge.event.world.ChunkEvent.Load; -import net.minecraftforge.event.world.WorldEvent.Save; import net.minecraftforge.fml.client.FMLClientHandler; import net.minecraftforge.fml.client.event.ConfigChangedEvent; import net.minecraftforge.fml.common.eventhandler.Event; @@ -144,26 +140,26 @@ public void onPortalSpawn(BlockEvent.PortalSpawnEvent event) } } - @SubscribeEvent - public void onWorldSave(Save event) - { - ChunkLoadingCallback.save((WorldServer) event.getWorld()); - } - - @SubscribeEvent - public void onChunkDataLoad(ChunkDataEvent.Load event) - { - ChunkLoadingCallback.load((WorldServer) event.getWorld()); - } - - @SubscribeEvent - public void onWorldLoad(Load event) - { - if (!event.getWorld().isRemote) - { - ChunkLoadingCallback.load((WorldServer) event.getWorld()); - } - } + // @SubscribeEvent(priority = EventPriority.LOWEST) + // public void onWorldSave(Save event) + // { + // ChunkLoadingCallback.save((WorldServer) event.getWorld()); + // } + // + // @SubscribeEvent(priority = EventPriority.LOWEST) + // public void onChunkDataLoad(ChunkDataEvent.Load event) + // { + // ChunkLoadingCallback.load((WorldServer) event.getWorld()); + // } + // + // @SubscribeEvent(priority = EventPriority.LOWEST) + // public void onWorldLoad(Load event) + // { + // if (!event.getWorld().isRemote) + // { + // ChunkLoadingCallback.load((WorldServer) event.getWorld()); + // } + // } @SubscribeEvent public void onEntityDamaged(LivingHurtEvent event) diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/util/MapUtil.java b/src/main/java/micdoodle8/mods/galacticraft/core/util/MapUtil.java index a10e6438fd..544382768a 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/util/MapUtil.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/util/MapUtil.java @@ -38,6 +38,7 @@ import net.minecraft.client.renderer.texture.DynamicTexture; import net.minecraft.client.resources.IResourceManager; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.init.Biomes; import net.minecraft.init.Blocks; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; @@ -56,62 +57,35 @@ public class MapUtil { // Mapgen management - public static AtomicBoolean calculatingMap = new AtomicBoolean(); - public static AtomicBoolean resetClientFlag = new AtomicBoolean(); - private static MapGen currentMap = null; - private static MapGen slowMap = null; - private static Thread threadCurrentMap = null; - private static Thread threadSlowMap = null; - public static boolean doneOverworldTexture = false; - private static LinkedList queuedMaps = new LinkedList<>(); - public static LinkedList clientRequests = new LinkedList<>(); - - public static ArrayList biomeColours = new ArrayList(40); - private static Random rand = new Random(); - private static byte[] overworldImageBytesPart; // Used client side only - private static byte[] overworldImageCompressed = null; + public static AtomicBoolean calculatingMap = new AtomicBoolean(); + public static AtomicBoolean resetClientFlag = new AtomicBoolean(); + private static MapGen currentMap = null; + private static MapGen slowMap = null; + private static Thread threadCurrentMap = null; + private static Thread threadSlowMap = null; + public static boolean doneOverworldTexture = false; + private static LinkedList queuedMaps = new LinkedList<>(); + public static LinkedList clientRequests = new LinkedList<>(); + + public static ArrayList biomeColours = new ArrayList(40); + private static Random rand = new Random(); + private static byte[] overworldImageBytesPart; // Used client side only + private static byte[] overworldImageCompressed = null; // Map size definitions - private static final int SIZE_STD = 176; - public static final int SIZE_STD2 = SIZE_STD * 2; - public static final int OVERWORLD_LARGEMAP_WIDTH = 1536; // Do not make a - // large map whose - // raw binary - // exceeds 2MB - // otherwise - // sendMapPacket() - // will not send - // it. This raw - // binary is 576kB - private static final int OVERWORLD_LARGEMAP_HEIGHT = 960; - private static final int OVERWORLD_MAP_SCALE = 4; // Recommended is 4. This - // gives a large overworld - // map of size (1536 x 16) - // by (960 x 16): that - // extends 12000 blocks - // from spawn in both EW - // directions and 7600 - // blocks from spawn north - // and south - private static final int OVERWORLD_TEXTURE_WIDTH = 192; // Do not change - - // planet texture - // needs to be this - // size - private static final int OVERWORLD_TEXTURE_HEIGHT = 48; // Do not change - - // planet texture - // needs to be this - // size - private static final int OVERWORLD_TEXTURE_SCALE = 7; - - private static final int LARGEMAP_MARKER = 30000001; // This is a marker to - // flag world map - // packets, it must be - // an impossible cx - // coordinate + private static final int SIZE_STD = 176; + public static final int SIZE_STD2 = SIZE_STD * 2; + public static final int OVERWORLD_LARGEMAP_WIDTH = 1536; + private static final int OVERWORLD_LARGEMAP_HEIGHT = 960; + private static final int OVERWORLD_MAP_SCALE = 4; + private static final int OVERWORLD_TEXTURE_WIDTH = 192; + private static final int OVERWORLD_TEXTURE_HEIGHT = 48; + private static final int OVERWORLD_TEXTURE_SCALE = 7; + private static final int LARGEMAP_MARKER = 30000001; // Color related constants - private static final int OCEAN_HEIGHT = 63; - private static final int DEEP_OCEAN = 56; + private static final int OCEAN_HEIGHT = 63; + private static final int DEEP_OCEAN = 56; static { @@ -157,9 +131,7 @@ public static void resetClientBody() } } } - GalacticraftCore.packetPipeline - .sendToServer(new PacketSimple(PacketSimple.EnumSimplePacket.S_REQUEST_OVERWORLD_IMAGE, GCCoreUtil.getDimensionID(FMLClientHandler.instance().getClient().world), new Object[] - {})); + GalacticraftCore.packetPipeline.sendToServer(new PacketSimple(PacketSimple.EnumSimplePacket.S_REQUEST_OVERWORLD_IMAGE, GCCoreUtil.getDimensionID(FMLClientHandler.instance().getClient().world), new Object[] {})); ClientProxyCore.overworldTextureRequestSent = true; DrawGameScreen.reusableMap = new DynamicTexture(MapUtil.SIZE_STD2, MapUtil.SIZE_STD2); MapUtil.biomeColours.clear(); @@ -269,7 +241,8 @@ public static void sendOverworldToClient(EntityPlayerMP client) { sendMapPacket(LARGEMAP_MARKER, 0, client, FileUtils.readFileToByteArray(file)); } - } catch (Exception ex) + } + catch (Exception ex) { System.err.println("Error sending overworld image to player."); ex.printStackTrace(); @@ -302,7 +275,8 @@ public static void sendOrCreateMap(World world, int cx, int cz, EntityPlayerMP c return; } sendMapPacket(cx, cz, client, FileUtils.readFileToByteArray(file)); - } catch (Exception ex) + } + catch (Exception ex) { System.err.println("Error sending map image to player."); ex.printStackTrace(); @@ -319,7 +293,8 @@ public static void sendMapPacket(int cx, int cz, EntityPlayerMP client, byte[] l overworldImageCompressed = zipCompress(largeMap); } compressed = overworldImageCompressed; - } else + } + else { compressed = zipCompress(largeMap); } @@ -336,7 +311,8 @@ public static void sendMapPacketToAll(int cx, int cz, byte[] largeMap) overworldImageCompressed = zipCompress(largeMap); } compressed = overworldImageCompressed; - } else + } + else { compressed = zipCompress(largeMap); } @@ -354,8 +330,9 @@ private static void sendMapPacketCompressed(int cx, int cz, EntityPlayerMP clien {cx, map.length, largeMapPartA}), client); GalacticraftCore.packetPipeline.sendTo(new PacketSimple(EnumSimplePacket.C_SEND_OVERWORLD_IMAGE, GCCoreUtil.getDimensionID(client.world), new Object[] {cx + 1, map.length, largeMapPartB}), client); - } else if (map.length < 1040000) // That's about the limit on a Forge - // packet length + } + else if (map.length < 1040000) // That's about the limit on a Forge + // packet length { GalacticraftCore.packetPipeline.sendTo(new PacketSimple(EnumSimplePacket.C_SEND_OVERWORLD_IMAGE, GCCoreUtil.getDimensionID(client.world), new Object[] {cx, cz, map}), client); @@ -373,8 +350,9 @@ private static void sendMapPacketAllCompressed(int cx, int cz, byte[] map) {cx, map.length, largeMapPartA}); GCCoreUtil.sendToAllDimensions(EnumSimplePacket.C_SEND_OVERWORLD_IMAGE, new Object[] {cx + 1, map.length, largeMapPartB}); - } else if (map.length < 1040000) // That's about the limit on a Forge - // packet length + } + else if (map.length < 1040000) // That's about the limit on a Forge + // packet length { GCCoreUtil.sendToAllDimensions(EnumSimplePacket.C_SEND_OVERWORLD_IMAGE, new Object[] {cx, cz, map}); @@ -447,7 +425,8 @@ public static boolean getBiomeMapForCoords(World world, int cx, int cz, int scal } return false; } - } else + } + else { outputFile = makeFileName(baseFolder, cx, cz); } @@ -458,7 +437,8 @@ public static boolean getBiomeMapForCoords(World world, int cx, int cz, int scal if (calculatingMap.getAndSet(true)) { queuedMaps.add(newGen); - } else + } + else { currentMap = newGen; } @@ -474,10 +454,10 @@ public static void saveMapProgress() slowMap.abort(); try { - Thread.currentThread().sleep(90); - } catch (InterruptedException e) - { + Thread.sleep(90); } + catch (InterruptedException e) + {} slowMap.writeOutputFile(false); slowMap = null; } @@ -505,7 +485,8 @@ public static void BiomeMapNextTick_MultiThreaded() threadCurrentMap.setName("Background world mapping"); threadCurrentMap.setPriority(Thread.NORM_PRIORITY - 1); threadCurrentMap.start(); - } else if (currentMap.finishedCalculating.get()) + } + else if (currentMap.finishedCalculating.get()) { // Finished the current map threadCurrentMap = null; @@ -514,7 +495,8 @@ public static void BiomeMapNextTick_MultiThreaded() if (queuedMaps.size() > 0) { currentMap = queuedMaps.removeFirst(); - } else + } + else { currentMap = null; @@ -553,7 +535,8 @@ public static void BiomeMapNextTick_MultiThreaded() threadSlowMap.setName("Background world mapping"); threadSlowMap.setPriority(Thread.NORM_PRIORITY - 1); threadSlowMap.start(); - } else if (slowMap.finishedCalculating.get()) + } + else if (slowMap.finishedCalculating.get()) { // Finished the current map threadSlowMap = null; @@ -575,11 +558,13 @@ public static void BiomeMapNextTick_SingleThreaded() if (currentMap != null) { map = currentMap; - } else if (slowMap != null) + } + else if (slowMap != null) { map = slowMap; doingSlow = true; - } else + } + else { return; } @@ -597,7 +582,8 @@ public static void BiomeMapNextTick_SingleThreaded() if (doingSlow) { slowMap = null; - } else + } + else { currentMap = null; if (queuedMaps.size() > 0) @@ -624,9 +610,9 @@ public static boolean backgroundMapping(Thread currentThread) * only from the colours in the paletteImage * * @param overworldImage Output image already created as a blank image, - * dimensions biomeMapSizeX x biomeMapSizeY - * @param paletteImage Palette image, dimensions must be a square with sides - * biomeMapSizeZ / 4 + * dimensions biomeMapSizeX x biomeMapSizeY + * @param paletteImage Palette image, dimensions must be a square with + * sides biomeMapSizeZ / 4 */ public static BufferedImage convertTo12pxTexture(BufferedImage overworldImage, BufferedImage paletteImage) { @@ -638,7 +624,7 @@ public static BufferedImage convertTo12pxTexture(BufferedImage overworldImage, B { for (int z = 0; z < overworldImage.getHeight(); z += 4) { - int r = 0; + //int r = 0; int g = 0; int b = 0; for (int xx = 0; xx < 4; xx++) @@ -646,7 +632,7 @@ public static BufferedImage convertTo12pxTexture(BufferedImage overworldImage, B for (int zz = 0; zz < 4; zz++) { int col = overworldImage.getRGB(xx + x, zz + z); - r += (col >> 16); + //r += (col >> 16); g += (col >> 8) & 255; b += col & 255; } @@ -659,7 +645,7 @@ public static BufferedImage convertTo12pxTexture(BufferedImage overworldImage, B if (x < overworldImage.getHeight()) { int col = paletteImage.getRGB(x + 1, z + 1); - r = (col >> 16); + //r = (col >> 16); g = (col >> 8) & 255; b = col & 255; while (mapColPosB.containsKey(g - b)) @@ -712,7 +698,7 @@ public static BufferedImage readImage(Object source) throws IOException ImageReadParam param = reader.getDefaultReadParam(); ImageTypeSpecifier typeToUse = null; - for (Iterator i = reader.getImageTypes(0); i.hasNext();) + for (Iterator i = reader.getImageTypes(0); i.hasNext();) { ImageTypeSpecifier type = (ImageTypeSpecifier) i.next(); if (type.getColorModel().getColorSpace().isCS_sRGB()) @@ -744,9 +730,9 @@ public static void writeImgToFile(BufferedImage img, String name) GalacticraftCore.jpgWriter.setOutput(outputStreamA); GalacticraftCore.jpgWriter.write(null, new IIOImage(img, null, null), GalacticraftCore.writeParam); outputStreamA.close(); - } catch (Exception e) - { } + catch (Exception e) + {} } } @@ -790,7 +776,8 @@ public static void receiveOverworldImageCompressed(int cx, int cz, byte[] raw) t { overworldImageBytesPart = raw; return; - } else + } + else { byte[] overWorldImageComplete = Arrays.copyOf(raw, cz); int offsetPartB = cz / 2; @@ -799,14 +786,16 @@ public static void receiveOverworldImageCompressed(int cx, int cz, byte[] raw) t overworldImageBytesPart = null; raw = overWorldImageComplete; } - } else if (cx == LARGEMAP_MARKER + 1) + } + else if (cx == LARGEMAP_MARKER + 1) { // Received large map part B if (overworldImageBytesPart == null) { overworldImageBytesPart = raw; return; - } else + } + else { byte[] overWorldImageComplete = Arrays.copyOf(overworldImageBytesPart, cz); int offsetPartB = cz / 2; @@ -820,7 +809,8 @@ public static void receiveOverworldImageCompressed(int cx, int cz, byte[] raw) t try { getOverworldImageFromRaw(cx, cz, zipDeCompress(raw)); - } catch (DataFormatException e) + } + catch (DataFormatException e) { GalacticraftCore.logger.debug(e.toString()); GalacticraftCore.logger.debug("Client received a corrupted map image data packet from server " + cx + "_" + cz); @@ -841,11 +831,13 @@ public static void getOverworldImageFromRaw(int cx, int cz, byte[] raw) throws I if (!file0.exists() || (file0.canRead() && file0.canWrite())) { FileUtils.writeByteArrayToFile(file0, raw); - } else + } + else { System.err.println("Cannot write to file %minecraft%/assets/galacticraftMaps/overworldRaw.bin"); } - } else + } + else { System.err.println("No folder for file %minecraft%/assets/galacticraftMaps/overworldRaw.bin"); } @@ -856,9 +848,7 @@ public static void getOverworldImageFromRaw(int cx, int cz, byte[] raw) throws I // pixels for each data point, it just looks better that way when // the texture is used BufferedImage worldImageLarge = new BufferedImage(OVERWORLD_LARGEMAP_WIDTH * 2, OVERWORLD_LARGEMAP_HEIGHT * 2, BufferedImage.TYPE_INT_RGB); - ArrayList cols = new ArrayList(); - int lastcol = -1; - int idx = 0; + for (int x = 0; x < OVERWORLD_LARGEMAP_WIDTH; x++) { for (int z = 0; z < OVERWORLD_LARGEMAP_HEIGHT; z++) @@ -884,13 +874,6 @@ public static void getOverworldImageFromRaw(int cx, int cz, byte[] raw) throws I } } -//overworldTextureLarge is currently unused in beta -// if (ClientProxyCore.overworldTextureLarge == null) -// { -// ClientProxyCore.overworldTextureLarge = new DynamicTextureProper(WIDTH_WORLD * 2, HEIGHT_WORLD * 2); -// } -// ClientProxyCore.overworldTextureLarge.update(worldImageLarge); - // Write it to a .jpg file on client for beta preview if (GalacticraftCore.enableJPEG && folder != null) { @@ -906,9 +889,7 @@ else if (raw.length == OVERWORLD_TEXTURE_WIDTH * OVERWORLD_TEXTURE_HEIGHT * 2) // raw is a WIDTH_STD x HEIGHT_STD array of 2 byte entries: biome // type followed by height BufferedImage worldImage = new BufferedImage(OVERWORLD_TEXTURE_WIDTH, OVERWORLD_TEXTURE_HEIGHT, BufferedImage.TYPE_INT_RGB); - ArrayList cols = new ArrayList(); - int lastcol = -1; - int idx = 0; + for (int x = 0; x < OVERWORLD_TEXTURE_WIDTH; x++) { for (int z = 0; z < OVERWORLD_TEXTURE_HEIGHT; z++) @@ -939,7 +920,8 @@ else if (raw.length == OVERWORLD_TEXTURE_WIDTH * OVERWORLD_TEXTURE_HEIGHT * 2) paletteImage = ImageIO.read(in); in.close(); paletteImage.getHeight(); - } catch (Exception e) + } + catch (Exception e) { e.printStackTrace(); return; @@ -961,7 +943,8 @@ else if (raw.length == OVERWORLD_TEXTURE_WIDTH * OVERWORLD_TEXTURE_HEIGHT * 2) ClientProxyCore.overworldTextureClient.update(result); ClientProxyCore.overworldTexturesValid = true; } - } else if (folder != null) + } + else if (folder != null) { File file0 = makeFileName(folder, cx, cz); @@ -969,7 +952,8 @@ else if (raw.length == OVERWORLD_TEXTURE_WIDTH * OVERWORLD_TEXTURE_HEIGHT * 2) { FileUtils.writeByteArrayToFile(file0, raw); } - } else + } + else { System.err.println("No folder %minecraft%/assets/galacticraftMaps for local map file."); } @@ -1041,14 +1025,14 @@ private static boolean makeRGBimage(int[] array, File baseFolder, int cx, int cz { // GalacticraftCore.logger.debug("Info: Server not yet ready to send map file " + // baseFolder.getName() + "/" + filename.getName()); - } else + } + else { clientRequests.add(filename.getName()); // GalacticraftCore.logger.debug("Info: Client requested map file" + // filename.getName()); - GalacticraftCore.packetPipeline - .sendToServer(new PacketSimple(PacketSimple.EnumSimplePacket.S_REQUEST_MAP_IMAGE, GCCoreUtil.getDimensionID(FMLClientHandler.instance().getClient().world), new Object[] - {dim, cx, cz})); + GalacticraftCore.packetPipeline.sendToServer(new PacketSimple(PacketSimple.EnumSimplePacket.S_REQUEST_MAP_IMAGE, GCCoreUtil.getDimensionID(FMLClientHandler.instance().getClient().world), new Object[] + {dim, cx, cz})); } return true; } @@ -1065,7 +1049,8 @@ private static boolean makeRGBimage(int[] array, File baseFolder, int cx, int cz try { raw = FileUtils.readFileToByteArray(filename); - } catch (IOException e) + } + catch (IOException e) { GalacticraftCore.logger.error("Problem reading map file: " + baseFolder.getAbsolutePath() + filename.getName()); return true; @@ -1110,7 +1095,8 @@ private static boolean makeRGBimage(int[] array, File baseFolder, int cx, int cz if (imagex < 0 || imageZ < 0) { GalacticraftCore.logger.debug("Outside image " + imagex + "," + imageZ + " - " + "x=" + x + " z=" + z + " offsetX=" + offsetX + " offsetZ = " + offsetZ + " ox=" + ox + " oz=" + oz); - } else + } + else { array[imagex + SIZE_STD2 * imageZ] = convertBiomeColour(biome, height) + 0xff000000; } @@ -1127,8 +1113,7 @@ private static File makeFileName(File folder, int cx, int cz) private static int getBiomeBaseColour(int biomeId) { Biome biomegenbase = Biome.REGISTRY.getObjectById(biomeId); -// return biomegenbase == null ? Biomes.OCEAN.color : biomegenbase.color; TODO Fix vanilla biome colors - return 0; + return biomegenbase == null ? Biomes.OCEAN.getWaterColor() : biomegenbase.getWaterColor(); } public static int convertBiomeColour(int in, int height) @@ -1142,18 +1127,21 @@ public static int convertBiomeColour(int in, int height) if (in >= s) { rv = getBiomeBaseColour(in); - } else + } + else { BlockVec3 bv = MapUtil.biomeColours.get(in); if (bv == null) { rv = getBiomeBaseColour(in); - } else + } + else { if (bv.z > 0 && MapUtil.rand.nextInt(100) < bv.z) { rv = bv.y; - } else + } + else { rv = bv.x; } @@ -1309,7 +1297,8 @@ public static void makeVanillaMap(int dim, int chunkXPos, int chunkZPos, File ba { ImageIO.write(image, "jpg", outputFile); } - } catch (IOException e) + } + catch (IOException e) { e.printStackTrace(); } @@ -1324,11 +1313,13 @@ public static File getClientMapsFolder() if (folder.exists() || folder.mkdirs()) { return folder; - } else + } + else { System.err.println("Cannot create directory %minecraft%/assets/galacticraftMaps! : " + folder.toString()); } - } catch (Exception e) + } + catch (Exception e) { System.err.println(folder.toString()); e.printStackTrace(); diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/world/ChunkLoadingCallback.java b/src/main/java/micdoodle8/mods/galacticraft/core/world/ChunkLoadingCallback.java index 25bec87fec..05eafbef48 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/world/ChunkLoadingCallback.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/world/ChunkLoadingCallback.java @@ -1,17 +1,10 @@ package micdoodle8.mods.galacticraft.core.world; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; +import com.google.common.collect.ListMultimap; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map.Entry; -import micdoodle8.mods.galacticraft.core.GalacticraftCore; import micdoodle8.mods.galacticraft.core.util.GCCoreUtil; import micdoodle8.mods.galacticraft.core.util.PlayerUtil; import net.minecraft.entity.player.EntityPlayer; @@ -20,26 +13,18 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.World; -import net.minecraft.world.WorldServer; -import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.ForgeChunkManager; import net.minecraftforge.common.ForgeChunkManager.LoadingCallback; +import net.minecraftforge.common.ForgeChunkManager.PlayerOrderedLoadingCallback; import net.minecraftforge.common.ForgeChunkManager.Ticket; -import net.minecraftforge.common.config.Configuration; -public class ChunkLoadingCallback implements LoadingCallback +public class ChunkLoadingCallback implements LoadingCallback, PlayerOrderedLoadingCallback { - private static boolean loaded; + //private static boolean loaded; private static HashMap>> chunkLoaderList = new HashMap>>(); - // private static HashMap> loadedChunks = new - // HashMap>(); - - private static boolean configLoaded; - private static Configuration config; - // private static boolean keepLoadedOffline; private static boolean loadOnLogin; - private static boolean dirtyData; + //private static boolean dirtyData; @Override public void ticketsLoaded(List tickets, World world) @@ -63,31 +48,6 @@ public void ticketsLoaded(List tickets, World world) } } - public static void loadConfig(File file) - { - if (!ChunkLoadingCallback.configLoaded) - { - ChunkLoadingCallback.config = new Configuration(file); - } - - try - { - ChunkLoadingCallback.loadOnLogin = - ChunkLoadingCallback.config.get("CHUNKLOADING", "LoadOnLogin", true, "If you don't want each player's chunks to load when they log in, set to false.").getBoolean(true); - } catch (final Exception e) - { - GalacticraftCore.logger.error("Problem loading chunkloading config (\"core.conf\")"); - } finally - { - if (ChunkLoadingCallback.config.hasChanged()) - { - ChunkLoadingCallback.config.save(); - } - - ChunkLoadingCallback.configLoaded = true; - } - } - public static void addToList(World world, int x, int y, int z, String playerName) { HashMap> dimensionMap = ChunkLoadingCallback.chunkLoaderList.get(playerName); @@ -108,7 +68,7 @@ public static void addToList(World world, int x, int y, int z, String playerName chunkLoaders.add(new BlockPos(x, y, z)); dimensionMap.put(GCCoreUtil.getDimensionID(world), chunkLoaders); ChunkLoadingCallback.chunkLoaderList.put(playerName, dimensionMap); - ChunkLoadingCallback.dirtyData = true; + //ChunkLoadingCallback.dirtyData = true; } public static void forceChunk(Ticket ticket, World world, int x, int y, int z, String playerName) @@ -118,180 +78,180 @@ public static void forceChunk(Ticket ticket, World world, int x, int y, int z, S ForgeChunkManager.forceChunk(ticket, chunkPos); } - public static void save(WorldServer world) - { - if (!ChunkLoadingCallback.dirtyData) - { - return; - } - - File saveDir = ChunkLoadingCallback.getSaveDir(); - - if (saveDir != null) - { - File saveFile = new File(saveDir, "chunkloaders.dat"); - - if (!saveFile.exists()) - { - try - { - if (!saveFile.createNewFile()) - { - GalacticraftCore.logger.error("Could not create chunk loader data file: " + saveFile.getAbsolutePath()); - } - } catch (IOException e) - { - GalacticraftCore.logger.error("Could not create chunk loader data file: " + saveFile.getAbsolutePath()); - e.printStackTrace(); - } - } - - FileOutputStream fos = null; - try - { - fos = new FileOutputStream(saveFile); - } catch (FileNotFoundException e) - { - e.printStackTrace(); - } - if (fos != null) - { - DataOutputStream dataStream = new DataOutputStream(fos); - try - { - dataStream.writeInt(ChunkLoadingCallback.chunkLoaderList.size()); - - for (Entry>> playerEntry : ChunkLoadingCallback.chunkLoaderList.entrySet()) - { - dataStream.writeUTF(playerEntry.getKey()); - dataStream.writeInt(playerEntry.getValue().size()); - - for (Entry> dimensionEntry : playerEntry.getValue().entrySet()) - { - dataStream.writeInt(dimensionEntry.getKey()); - dataStream.writeInt(dimensionEntry.getValue().size()); - - for (BlockPos coords : dimensionEntry.getValue()) - { - dataStream.writeInt(coords.getX()); - dataStream.writeInt(coords.getY()); - dataStream.writeInt(coords.getZ()); - } - } - } - } catch (IOException e) - { - e.printStackTrace(); - } - try - { - dataStream.close(); - fos.close(); - } catch (IOException e) - { - e.printStackTrace(); - } - } - } - ChunkLoadingCallback.dirtyData = false; - } - - private static File getSaveDir() - { - if (DimensionManager.getWorld(0) != null) - { - File saveDir = new File(DimensionManager.getCurrentSaveRootDirectory(), "galacticraft"); - - if (!saveDir.exists()) - { - if (!saveDir.mkdirs()) - { - GalacticraftCore.logger.error("Could not create chunk loader save data folder: " + saveDir.getAbsolutePath()); - } - } - - return saveDir; - } - - return null; - } - - public static void load(WorldServer world) - { - if (ChunkLoadingCallback.loaded) - { - return; - } - - DataInputStream dataStream = null; - - try - { - File saveDir = ChunkLoadingCallback.getSaveDir(); - - if (saveDir != null) - { - if (!saveDir.exists()) - { - if (!saveDir.mkdirs()) - { - GalacticraftCore.logger.error("Could not create chunk loader save data folder: " + saveDir.getAbsolutePath()); - } - } - - File saveFile = new File(saveDir, "chunkloaders.dat"); - - if (saveFile.exists()) - { - dataStream = new DataInputStream(new FileInputStream(saveFile)); - - int playerCount = dataStream.readInt(); - - for (int l = 0; l < playerCount; l++) - { - String ownerName = dataStream.readUTF(); - - int mapSize = dataStream.readInt(); - HashMap> dimensionMap = new HashMap>(); - - for (int i = 0; i < mapSize; i++) - { - int dimensionID = dataStream.readInt(); - HashSet coords = new HashSet(); - dimensionMap.put(dimensionID, coords); - int coordSetSize = dataStream.readInt(); - - for (int j = 0; j < coordSetSize; j++) - { - coords.add(new BlockPos(dataStream.readInt(), dataStream.readInt(), dataStream.readInt())); - } - } - - ChunkLoadingCallback.chunkLoaderList.put(ownerName, dimensionMap); - } - - dataStream.close(); - } - } - } catch (Exception e) - { - e.printStackTrace(); - - if (dataStream != null) - { - try - { - dataStream.close(); - } catch (IOException e1) - { - e1.printStackTrace(); - } - } - } - - ChunkLoadingCallback.loaded = true; - ChunkLoadingCallback.dirtyData = false; - } + // public static void save(WorldServer world) + // { + // if (!ChunkLoadingCallback.dirtyData) + // { + // return; + // } + // + // File saveDir = ChunkLoadingCallback.getSaveDir(); + // + // if (saveDir != null) + // { + // File saveFile = new File(saveDir, "chunkloaders.dat"); + // + // if (!saveFile.exists()) + // { + // try + // { + // if (!saveFile.createNewFile()) + // { + // GalacticraftCore.logger.error("Could not create chunk loader data file: " + saveFile.getAbsolutePath()); + // } + // } catch (IOException e) + // { + // GalacticraftCore.logger.error("Could not create chunk loader data file: " + saveFile.getAbsolutePath()); + // e.printStackTrace(); + // } + // } + // + // FileOutputStream fos = null; + // try + // { + // fos = new FileOutputStream(saveFile); + // } catch (FileNotFoundException e) + // { + // e.printStackTrace(); + // } + // if (fos != null) + // { + // DataOutputStream dataStream = new DataOutputStream(fos); + // try + // { + // dataStream.writeInt(ChunkLoadingCallback.chunkLoaderList.size()); + // + // for (Entry>> playerEntry : ChunkLoadingCallback.chunkLoaderList.entrySet()) + // { + // dataStream.writeUTF(playerEntry.getKey()); + // dataStream.writeInt(playerEntry.getValue().size()); + // + // for (Entry> dimensionEntry : playerEntry.getValue().entrySet()) + // { + // dataStream.writeInt(dimensionEntry.getKey()); + // dataStream.writeInt(dimensionEntry.getValue().size()); + // + // for (BlockPos coords : dimensionEntry.getValue()) + // { + // dataStream.writeInt(coords.getX()); + // dataStream.writeInt(coords.getY()); + // dataStream.writeInt(coords.getZ()); + // } + // } + // } + // } catch (IOException e) + // { + // e.printStackTrace(); + // } + // try + // { + // dataStream.close(); + // fos.close(); + // } catch (IOException e) + // { + // e.printStackTrace(); + // } + // } + // } + // ChunkLoadingCallback.dirtyData = false; + // } + // + // private static File getSaveDir() + // { + // if (DimensionManager.getWorld(0) != null) + // { + // File saveDir = new File(DimensionManager.getCurrentSaveRootDirectory(), "galacticraft"); + // + // if (!saveDir.exists()) + // { + // if (!saveDir.mkdirs()) + // { + // GalacticraftCore.logger.error("Could not create chunk loader save data folder: " + saveDir.getAbsolutePath()); + // } + // } + // + // return saveDir; + // } + // + // return null; + // } + // + // public static void load(WorldServer world) + // { + // if (ChunkLoadingCallback.loaded) + // { + // return; + // } + // + // DataInputStream dataStream = null; + // + // try + // { + // File saveDir = ChunkLoadingCallback.getSaveDir(); + // + // if (saveDir != null) + // { + // if (!saveDir.exists()) + // { + // if (!saveDir.mkdirs()) + // { + // GalacticraftCore.logger.error("Could not create chunk loader save data folder: " + saveDir.getAbsolutePath()); + // } + // } + // + // File saveFile = new File(saveDir, "chunkloaders.dat"); + // + // if (saveFile.exists()) + // { + // dataStream = new DataInputStream(new FileInputStream(saveFile)); + // + // int playerCount = dataStream.readInt(); + // + // for (int l = 0; l < playerCount; l++) + // { + // String ownerName = dataStream.readUTF(); + // + // int mapSize = dataStream.readInt(); + // HashMap> dimensionMap = new HashMap>(); + // + // for (int i = 0; i < mapSize; i++) + // { + // int dimensionID = dataStream.readInt(); + // HashSet coords = new HashSet(); + // dimensionMap.put(dimensionID, coords); + // int coordSetSize = dataStream.readInt(); + // + // for (int j = 0; j < coordSetSize; j++) + // { + // coords.add(new BlockPos(dataStream.readInt(), dataStream.readInt(), dataStream.readInt())); + // } + // } + // + // ChunkLoadingCallback.chunkLoaderList.put(ownerName, dimensionMap); + // } + // + // dataStream.close(); + // } + // } + // } catch (Exception e) + // { + // e.printStackTrace(); + // + // if (dataStream != null) + // { + // try + // { + // dataStream.close(); + // } catch (IOException e1) + // { + // e1.printStackTrace(); + // } + // } + // } + // + // ChunkLoadingCallback.loaded = true; + // ChunkLoadingCallback.dirtyData = false; + // } public static void onPlayerLogin(EntityPlayer player) { @@ -311,4 +271,10 @@ public static void onPlayerLogin(EntityPlayer player) } } } + + @Override + public ListMultimap playerTicketsLoaded(ListMultimap tickets, World world) + { + return tickets; + } } diff --git a/src/main/java/micdoodle8/mods/galacticraft/planets/mars/tile/TileEntityLaunchController.java b/src/main/java/micdoodle8/mods/galacticraft/planets/mars/tile/TileEntityLaunchController.java index ab9d557a2a..422af1dcd3 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/planets/mars/tile/TileEntityLaunchController.java +++ b/src/main/java/micdoodle8/mods/galacticraft/planets/mars/tile/TileEntityLaunchController.java @@ -44,24 +44,31 @@ public class TileEntityLaunchController extends TileBaseElectricBlockWithInventory implements IChunkLoader, ISidedInventory, ILandingPadAttachable { - public static final int WATTS_PER_TICK = 1; - @NetworkedField(targetSide = Side.CLIENT) public boolean launchPadRemovalDisabled = true; - private Ticket chunkLoadTicket; - private List connectedPads = new ArrayList(); - @NetworkedField(targetSide = Side.CLIENT) public int frequency = -1; - @NetworkedField(targetSide = Side.CLIENT) public int destFrequency = -1; - @NetworkedField(targetSide = Side.CLIENT) public String ownerName = ""; - @NetworkedField(targetSide = Side.CLIENT) public boolean frequencyValid; - @NetworkedField(targetSide = Side.CLIENT) public boolean destFrequencyValid; - @NetworkedField(targetSide = Side.CLIENT) public int launchDropdownSelection; - @NetworkedField(targetSide = Side.CLIENT) public boolean launchSchedulingEnabled; - @NetworkedField(targetSide = Side.CLIENT) public boolean controlEnabled; - public boolean hideTargetDestination = true; - public boolean requiresClientUpdate; - public Object attachedDock = null; - private boolean frequencyCheckNeeded = false; -// private static Map tickCounts = new HashMap<>(); -// private static Map instanceCounts = new HashMap<>(); + public static final int WATTS_PER_TICK = 1; + @NetworkedField(targetSide = Side.CLIENT) + public boolean launchPadRemovalDisabled = true; + private Ticket chunkLoadTicket; + private List connectedPads = new ArrayList(); + @NetworkedField(targetSide = Side.CLIENT) + public int frequency = -1; + @NetworkedField(targetSide = Side.CLIENT) + public int destFrequency = -1; + @NetworkedField(targetSide = Side.CLIENT) + public String ownerName = ""; + @NetworkedField(targetSide = Side.CLIENT) + public boolean frequencyValid; + @NetworkedField(targetSide = Side.CLIENT) + public boolean destFrequencyValid; + @NetworkedField(targetSide = Side.CLIENT) + public int launchDropdownSelection; + @NetworkedField(targetSide = Side.CLIENT) + public boolean launchSchedulingEnabled; + @NetworkedField(targetSide = Side.CLIENT) + public boolean controlEnabled; + public boolean hideTargetDestination = true; + public boolean requiresClientUpdate; + public Object attachedDock = null; + private boolean frequencyCheckNeeded = false; public TileEntityLaunchController() { @@ -116,7 +123,8 @@ public void update() } } } - } else + } + else { if (this.frequency == -1 && this.destFrequency == -1) { @@ -184,7 +192,8 @@ public void onTicketLoaded(Ticket ticket, boolean placed) if (placed) { ChunkLoadingCallback.forceChunk(this.chunkLoadTicket, this.world, this.getPos().getX() + x, this.getPos().getY(), this.getPos().getZ() + z, this.getOwnerName()); - } else + } + else { ChunkLoadingCallback.addToList(this.world, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), this.getOwnerName()); } @@ -359,7 +368,8 @@ public void setFrequency(int frequency) } } } - } else + } + else { this.frequencyValid = false; } @@ -474,7 +484,7 @@ public EnumFacing getElectricInputDirection() { return byIndex().rotateY(); } - + @Override @Deprecated @ForRemoval(deadline = "4.1.0") diff --git a/src/main/java/micdoodle8/mods/galacticraft/planets/mars/tile/TileEntityTerraformer.java b/src/main/java/micdoodle8/mods/galacticraft/planets/mars/tile/TileEntityTerraformer.java index cd26c5fb0e..d9b59d2569 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/planets/mars/tile/TileEntityTerraformer.java +++ b/src/main/java/micdoodle8/mods/galacticraft/planets/mars/tile/TileEntityTerraformer.java @@ -46,29 +46,29 @@ public class TileEntityTerraformer extends TileBaseElectricBlockWithInventory implements ISidedInventory, IDisableableMachine, IBubbleProviderColored, IFluidHandlerWrapper { - private final int tankCapacity = 2000; - @NetworkedField(targetSide = Side.CLIENT) public FluidTank waterTank = new FluidTank(this.tankCapacity); - public boolean active; - public boolean lastActive; - public static final int WATTS_PER_TICK = 1; - private ArrayList terraformableBlocksList = new ArrayList(); - private ArrayList grassBlockList = new ArrayList(); - private ArrayList grownTreesList = new ArrayList(); - @NetworkedField(targetSide = Side.CLIENT) public int terraformableBlocksListSize = 0; // used - // for - // server->client - // ease - @NetworkedField(targetSide = Side.CLIENT) public int grassBlocksListSize = 0; // used - // for - // server->client - // ease - @NetworkedField(targetSide = Side.CLIENT) public boolean treesDisabled; - @NetworkedField(targetSide = Side.CLIENT) public boolean grassDisabled; - public final double MAX_SIZE = 15.0D; - private int[] useCount = new int[2]; - private int saplingIndex = 6; - public float bubbleSize; - @NetworkedField(targetSide = Side.CLIENT) public boolean shouldRenderBubble = true; + private final int tankCapacity = 2000; + @NetworkedField(targetSide = Side.CLIENT) + public FluidTank waterTank = new FluidTank(this.tankCapacity); + public boolean active; + public boolean lastActive; + public static final int WATTS_PER_TICK = 1; + private ArrayList terraformableBlocksList = new ArrayList<>(); + private ArrayList grassBlockList = new ArrayList<>(); + private ArrayList grownTreesList = new ArrayList<>(); + @NetworkedField(targetSide = Side.CLIENT) + public int terraformableBlocksListSize = 0; + @NetworkedField(targetSide = Side.CLIENT) + public int grassBlocksListSize = 0; + @NetworkedField(targetSide = Side.CLIENT) + public boolean treesDisabled; + @NetworkedField(targetSide = Side.CLIENT) + public boolean grassDisabled; + public final double MAX_SIZE = 15.0D; + private int[] useCount = new int[2]; + private int saplingIndex = 6; + public float bubbleSize; + @NetworkedField(targetSide = Side.CLIENT) + public boolean shouldRenderBubble = true; public TileEntityTerraformer() { @@ -103,15 +103,6 @@ public void update() { super.update(); -// if (this.terraformBubble == null) -// { -// if (!this.world.isRemote) -// { -// this.terraformBubble = new EntityTerraformBubble(this.world, new Vector3(this), this); -// this.world.spawnEntity(this.terraformBubble); -// } -// } - if (!this.world.isRemote) { final FluidStack liquid = FluidUtil.getFluidContained(this.getInventory().get(0)); @@ -120,8 +111,7 @@ public void update() FluidUtil.loadFromContainer(waterTank, FluidRegistry.WATER, this.getInventory(), 0, liquid.amount); } - this.active = - this.bubbleSize == this.MAX_SIZE && this.hasEnoughEnergyToRun && !this.getFirstBonemealStack().isEmpty() && this.waterTank.getFluid() != null && this.waterTank.getFluid().amount > 0; + this.active = this.bubbleSize == this.MAX_SIZE && this.hasEnoughEnergyToRun && !this.getFirstBonemealStack().isEmpty() && this.waterTank.getFluid() != null && this.waterTank.getFluid().amount > 0; } if (!this.world.isRemote && (this.active != this.lastActive || this.ticks % 60 == 0)) @@ -154,7 +144,8 @@ public void update() if (doGrass && blockID instanceof ITerraformableBlock && ((ITerraformableBlock) blockID).isTerraformable(this.world, pos)) { this.terraformableBlocksList.add(new BlockPos(x, y, z)); - } else if (doTrees) + } + else if (doTrees) { Block blockIDAbove = this.world.getBlockState(pos.up()).getBlock(); if (blockID == Blocks.GRASS && blockIDAbove.isAir(this.world.getBlockState(pos.up()), this.world, pos.up())) @@ -171,7 +162,7 @@ public void update() if (!this.world.isRemote && this.terraformableBlocksList.size() > 0 && this.ticks % 15 == 0) { - ArrayList terraformableBlocks2 = new ArrayList(this.terraformableBlocksList); + ArrayList terraformableBlocks2 = new ArrayList<>(this.terraformableBlocksList); int randomIndex = this.world.rand.nextInt(this.terraformableBlocksList.size()); BlockPos vec = terraformableBlocks2.get(randomIndex); @@ -183,11 +174,12 @@ public void update() switch (this.world.rand.nextInt(40)) { case 0: - if (this.world.isBlockFullCube(new BlockPos(vec.getX() - 1, vec.getY(), vec.getZ())) && this.world.isBlockFullCube(new BlockPos(vec.getX() + 1, vec.getY(), vec.getZ())) - && this.world.isBlockFullCube(new BlockPos(vec.getX(), vec.getY(), vec.getZ() - 1)) && this.world.isBlockFullCube(new BlockPos(vec.getX(), vec.getY(), vec.getZ() + 1))) + if (this.world.isBlockFullCube(new BlockPos(vec.getX() - 1, vec.getY(), vec.getZ())) && this.world.isBlockFullCube(new BlockPos(vec.getX() + 1, vec.getY(), vec.getZ())) && this.world.isBlockFullCube(new BlockPos(vec.getX(), vec.getY(), vec.getZ() - 1)) + && this.world.isBlockFullCube(new BlockPos(vec.getX(), vec.getY(), vec.getZ() + 1))) { id = Blocks.FLOWING_WATER; - } else + } + else { id = Blocks.GRASS; } @@ -204,7 +196,8 @@ public void update() this.useCount[0]++; this.waterTank.drain(1, true); this.checkUsage(1); - } else if (id == Blocks.FLOWING_WATER) + } + else if (id == Blocks.FLOWING_WATER) { this.checkUsage(2); } @@ -245,7 +238,8 @@ public void update() ((BlockSapling) b).grow(this.world, vecSapling, this.world.getBlockState(vecSapling), this.world.rand); this.grownTreesList.add(new BlockPos(vecSapling.getX(), vecSapling.getY(), vecSapling.getZ())); } - } else if (b instanceof BlockBush) + } + else if (b instanceof BlockBush) { if (this.world.getLightFromNeighbors(vecSapling) >= 5) // Hammer the update tick a few times to try to get it @@ -256,7 +250,8 @@ public void update() if (this.world.getBlockState(vecSapling).getBlock() == b) { ((BlockBush) b).updateTick(this.world, vecSapling, this.world.getBlockState(vecSapling), this.world.rand); - } else + } + else { this.grownTreesList.add(new BlockPos(vecSapling.getX(), vecSapling.getY(), vecSapling.getZ())); break; @@ -283,7 +278,8 @@ public void update() if (this.hasEnoughEnergyToRun && (!this.grassDisabled || !this.treesDisabled)) { this.bubbleSize = (float) Math.min(Math.max(0, this.bubbleSize + 0.1F), this.MAX_SIZE); - } else + } + else { this.bubbleSize = (float) Math.min(Math.max(0, this.bubbleSize - 0.1F), this.MAX_SIZE); } @@ -584,12 +580,6 @@ public boolean getDisabled(int index) return false; } -// @Override -// public IBubble getBubble() -// { -// return this.terraformBubble; -// } - @Override public void setBubbleVisible(boolean shouldRender) { @@ -651,8 +641,7 @@ public FluidTankInfo[] getTankInfo(EnumFacing from) @SideOnly(Side.CLIENT) public AxisAlignedBB getRenderBoundingBox() { - return new AxisAlignedBB(this.getPos().getX() - this.bubbleSize, this.getPos().getY() - this.bubbleSize, this.getPos().getZ() - this.bubbleSize, this.getPos().getX() + this.bubbleSize, - this.getPos().getY() + this.bubbleSize, this.getPos().getZ() + this.bubbleSize); + return new AxisAlignedBB(this.getPos().getX() - this.bubbleSize, this.getPos().getY() - this.bubbleSize, this.getPos().getZ() - this.bubbleSize, this.getPos().getX() + this.bubbleSize, this.getPos().getY() + this.bubbleSize, this.getPos().getZ() + this.bubbleSize); } @Override @@ -724,7 +713,7 @@ public boolean canConnect(EnumFacing direction, NetworkType type) } return false; } - + @Override @Deprecated @ForRemoval(deadline = "4.1.0")