Jump to content

Saving a list of BlockEvents


gpouliot

Recommended Posts

I'm working on a mod to Record and then playback block events.  Specifically, it records place events and break events and then lets me replay the events either in forward or reverse order.

 

I've got that working simply by making a list of block events and adding the break or place events to the list as they occur.  It's then trivial to replay the events.

 

Now, I'm at the point where I want to save these block event lists to disk so that I can load them and recreate the block events at a later date.  When it comes to Java, I'm a little rusty so I was trying to find an example that I could work with. However, most tutorials are about creating new block types.  There doesn't seem to be a lot of examples of people wanting to save block events to disk (and then reload them).

 

As a test, when I run my Stop Recording command, it opens a file (in the Minecraft game world directory) and writes the x,y,z coordinates of the effected blocks.  That's easy.  Where I'm getting hung up is on the best way to go from a BlockEvent to a Text File and then back to a Block Event when I reload the data.

 

Does anyone know of any examples that might help?  Baring that, what would you guys suggest?  I don't need someone to do it for me, just point me in the right direction and I can take it from there.  Thanks.

 

 

Link to comment
Share on other sites

You only need to save the pertinent data(the block, the position and whether it is breaking or placing the block) which means you need to write that data to a text file or some other type of file then parse the fata when loading.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I guess where I'm getting hung up is the meta data.  If it wasn't for the meta data, I would just save the x,y,z coordinates, whether it was a place or break event and what type of block it is or was.  However, given that there is all sorts of meta data to contend with, I'm just having trouble wrapping my head around it.

Link to comment
Share on other sites

I guess where I'm getting hung up is the meta data.  If it wasn't for the meta data, I would just save the x,y,z coordinates, whether it was a place or break event and what type of block it is or was.  However, given that there is all sorts of meta data to contend with, I'm just having trouble wrapping my head around it.

Grab the blockstate and handle the data from that or in some cases where they override getStateFromMeta you could just write the meta then use that method to grab the correct blockstate.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I'll put a little code here to better show what I'm up against...

 

The three classes below do the lion's share of the work.  What makes them so easy and nice to work with is the fact that I don't have to create any BlockEvents myself.  When an event happens I simply add it to the list.  When I want to play or rewind the list, I simply iterate through the list and use setBlockState to make the needed changes.

 

Since I can't directly save and then reload BlockEvent's (that would be nice), I think I have to pull out the needed data, save it, reload it and then somehow create my own list of BlockEvents based on that loaded data.  It's much more complicated than simply adding existing block events to a list and replaying them when needed.

 

Alternatively, I'm thinking that instead of using a list of BlockEvents, maybe the better option is to create my own list with a custom data format that only stores the block position, block type, metadata and event type (place or break).  That way, saving and reloading the data would be relatively easy.  The only tricky part is making sure I properly handle the meta data and figure out how to correctly send the needed state information in the second argument of the setBlockState() command.

 

If anyone has done something similar, I'd love to see an example...  :)

 

public void record(BlockEvent event)
{
if(isRecording == true)
{
	if(event instanceof BreakEvent || event instanceof PlaceEvent)
	{
		events.add(event);
	}
}
}

 

 

public void play(EntityPlayer player)
{
if(!events.isEmpty())
{
	for(BlockEvent event : events) {
		if(event instanceof BreakEvent)
		{
			player.worldObj.setBlockState(event.getPos(), Blocks.AIR.getDefaultState());
		}
		else if(event instanceof PlaceEvent) 
		{
			player.worldObj.setBlockState(event.getPos(), ((PlaceEvent) event).getState());
		}
	}
}
}

 

public void rewind(EntityPlayer player)
{
if(!events.isEmpty())
{
	ListIterator li = events.listIterator(events.size()); 

	while(li.hasPrevious()) {
		Object current = li.previous();
		if(current instanceof BreakEvent)
		{
			player.worldObj.setBlockState(((BlockEvent) current).getPos(), ((BlockEvent) current).getState());

		}
		else if(current instanceof PlaceEvent) 
		{
			player.worldObj.setBlockState(((BlockEvent) current).getPos(), ((PlaceEvent) current).getBlockSnapshot().getReplacedBlock());
		}
	}
}
}

 

 

Link to comment
Share on other sites

There is no direct way of saving BlockEvents or any class like that, because it is not a base field like ints and doubles are. You will have to save the data yourself. Meaning you need the blocks registryName, any blockstate information ie metadata or its properties, the position, and the type of event. Use WorldSavedData to save it to the world or alternatively an event for when the world loads/saves or capabilites for players.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • So the world dosen't even show on my world's list, it's totally broken, right?
    • Hey guys, I have following error for like three hours when joining my friends GPORTAL-Server: Connection Lost Internal Exception: io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: index: 9, lenght: 955 (expected: range(0, 30))   So could anybody help me out with this one? Log: Minecraft Log PS: I already updated Java, reinstalled the Modpack, restarted my PC, updated my Browser [...]
    • Seemingly fixed, I had to delete the advancements json file for every player in the server as this seemed to be the error. After deleting the files and reloading the server, we were able to join back in.
    • Incompatible mods found! net.fabricmc.loader.impl.FormattedException: Some of your mods are incompatible with the game or each other! A potential solution has been determined, this may resolve your problem:      - Replace mod 'Moonlight Lib' (moonlight) 1.21-2.16.0 with version 1.21-2.17.0 or later. More details:      - Mod 'Supplementaries' (supplementaries) 1.21-3.0.14-alpha requires version 1.21-2.17.0 or later of mod 'Moonlight Lib' (moonlight), but only the wrong version is present: 1.21-2.16.0!     at net.fabricmc.loader.impl.FormattedException.ofLocalized(FormattedException.java:51)     at net.fabricmc.loader.impl.FabricLoaderImpl.load(FabricLoaderImpl.java:196)     at net.fabricmc.loader.impl.launch.knot.Knot.init(Knot.java:146)     at net.fabricmc.loader.impl.launch.knot.Knot.launch(Knot.java:68)     at net.fabricmc.loader.impl.launch.knot.KnotClient.main(KnotClient.java:23) this si my error code, and yes i have the latest version of moonlight lib
×
×
  • Create New...

Important Information

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