Posted May 11, 201510 yr I am trying to use several different types of events (world tick, world load, and world save) to all work on the same data. All my functions are in the same class, but for some reason, the variable dayCount seems to be a different instance for each function. Here is my code. public class TimeHandler { private boolean flag; public int dayCount; public TimeHandler() { this.flag = false; } @SubscribeEvent public void incrementDayCount(WorldTickEvent event) { if(flag == true && getTimeFromWorldTime(event.world.getWorldInfo().getWorldTime()) > 0) { flag = false; } else if(getTimeFromWorldTime(event.world.getWorldInfo().getWorldTime()) == 0 && flag == false) { flag = true; ++this.dayCount; ChatComponentText message = new ChatComponentText("Days elapsed since spawn: " + this.dayCount); Minecraft.getMinecraft().thePlayer.addChatMessage(message); } //System.out.println(getTimeFromWorldTime(event.world.getWorldInfo().getWorldTime())); } @SubscribeEvent public void createTimeSaves(WorldEvent.Load event) throws IOException { String pathToDir = FileIO.getMcDir().toString() + "/xpansion_data"; System.out.println(pathToDir); byte[] dayCountBinary = {0}; File testDir = new File(pathToDir); if(testDir.exists() && testDir.isDirectory()) { String pathToDat = FileIO.getMcDir().toString() + "/xpansion_data/day_count.dat"; File testFile = new File(pathToDat); if(testFile.exists() && !testFile.isDirectory()) { dayCountBinary = FileIO.readBinary(pathToDat); CharBuffer cb = ByteBuffer.wrap(dayCountBinary).asCharBuffer(); if(!StringUtils.isEmpty(cb.toString())) { System.out.println(cb.toString()); this.dayCount = Integer.parseInt(cb.toString(), 2); } else { this.dayCount = 0; } } else { this.dayCount = 0; FileIO.writeBinary(pathToDat, dayCountBinary); } } else { testDir.mkdir(); String pathToDat = FileIO.getMcDir().toString() + "/xpansion_data/day_count.dat"; File testFile = new File(pathToDat); this.dayCount = 0; FileIO.writeBinary(pathToDat, dayCountBinary); } } @SubscribeEvent public void saveDayCount(WorldEvent.Save event) throws IOException { System.out.println(this.dayCount); byte[] dayCountBinary = toBinary(this.dayCount); String path = FileIO.getMcDir().toString() + "/xpansion_data/day_count.dat"; FileIO.writeBinary(path, dayCountBinary); } private static long getTimeFromWorldTime(long time) { return (time%24000); } public byte[] toBinary(int n) { byte[] binary = {}; if (n == 0) { System.out.println("n is zero"); return binary; }; while (n > 0) { int rem = n % 2; System.out.println(rem); binary[binary.length] = (byte) rem; n = n / 2; } return binary; } } public class FileIO { public static byte[] readBinary(String filePath) throws IOException { Path path = Paths.get(filePath); return Files.readAllBytes(path); } public static void writeBinary(String fileName, byte[] contents) throws IOException { Path path = Paths.get(fileName); Files.write(path, contents); } public static File getMcDir() { if (MinecraftServer.getServer() != null && MinecraftServer.getServer().isDedicatedServer()) { return new File("."); } return Minecraft.getMinecraft().mcDataDir; } } When I run this, I do get a chat message that tells me the correct number of elapsed days, but when I hit the escape key and the save event is fired (this triggers the saveDayCount function) the dayCount variable is now 0 again. I don't know what I am doing wrong.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.