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.

Announcements



×
×
  • Create New...

Important Information

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