Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.