Skip to content

Commit

Permalink
Fix saving Tile-Entities saving (GTNewHorizons#19)
Browse files Browse the repository at this point in the history
* make sure Tile-Entities have a world to prevent saving issues

* remove reference to client
  • Loading branch information
mist475 authored Feb 27, 2024
1 parent 54359a5 commit e72a792
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentTranslation;

import com.github.lunatrius.schematica.reference.Names;
Expand Down Expand Up @@ -74,7 +75,15 @@ private void processQueue() {
if (container.hasNext()) {
this.queue.offer(container);
} else {
final boolean success = SchematicFormat.writeToFile(container.file, container.schematic);
if (container.world != null) {
for (TileEntity entity : container.schematic.getTileEntities()) {
if (!entity.hasWorldObj()) {
entity.setWorldObj(container.world);
}
}
}

final boolean success = SchematicFormat.writeToFile(container.file, container.schematic, container.world);
final String message = success ? Names.Command.Save.Message.SAVE_SUCCESSFUL
: Names.Command.Save.Message.SAVE_FAILED;
container.player.addChatMessage(new ChatComponentTranslation(message, container.file.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void toBytes(ByteBuf buf) {
@Override
public IMessage onMessage(MessageDownloadEnd message, MessageContext ctx) {
File directory = Schematica.proxy.getPlayerSchematicDirectory(null, true);
boolean success = SchematicFormat.writeToFile(directory, message.name, DownloadHandler.INSTANCE.schematic);
boolean success = SchematicFormat
.writeToFile(directory, message.name, DownloadHandler.INSTANCE.schematic, null);

if (success) {
Minecraft.getMinecraft().thePlayer.addChatMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.Constants;

Expand Down Expand Up @@ -100,7 +101,7 @@ public ISchematic readFromNBT(NBTTagCompound tagCompound) {
}

@Override
public boolean writeToNBT(NBTTagCompound tagCompound, ISchematic schematic) {
public boolean writeToNBT(NBTTagCompound tagCompound, ISchematic schematic, World backupWorld) {
NBTTagCompound tagCompoundIcon = new NBTTagCompound();
ItemStack icon = schematic.getIcon();
icon.writeToNBT(tagCompoundIcon);
Expand Down Expand Up @@ -142,6 +143,9 @@ public boolean writeToNBT(NBTTagCompound tagCompound, ISchematic schematic) {
NBTTagList tileEntitiesList = new NBTTagList();
for (TileEntity tileEntity : schematic.getTileEntities()) {
try {
if (!tileEntity.hasWorldObj()) {
tileEntity.setWorldObj(backupWorld);
}
NBTTagCompound tileEntityTagCompound = NBTHelper.writeTileEntityToCompound(tileEntity);
tileEntitiesList.appendTag(tileEntityTagCompound);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.zip.GZIPOutputStream;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;

import com.github.lunatrius.schematica.api.ISchematic;
Expand All @@ -22,7 +23,7 @@ public abstract class SchematicFormat {

public abstract ISchematic readFromNBT(NBTTagCompound tagCompound);

public abstract boolean writeToNBT(NBTTagCompound tagCompound, ISchematic schematic);
public abstract boolean writeToNBT(NBTTagCompound tagCompound, ISchematic schematic, World backupWorld);

public static ISchematic readFromFile(File file) {
try {
Expand All @@ -46,14 +47,14 @@ public static ISchematic readFromFile(File directory, String filename) {
return readFromFile(new File(directory, filename));
}

public static boolean writeToFile(File file, ISchematic schematic) {
public static boolean writeToFile(File file, ISchematic schematic, World backupWorld) {
try {
final PostSchematicCaptureEvent event = new PostSchematicCaptureEvent(schematic);
MinecraftForge.EVENT_BUS.post(event);

NBTTagCompound tagCompound = new NBTTagCompound();

FORMATS.get(FORMAT_DEFAULT).writeToNBT(tagCompound, schematic);
FORMATS.get(FORMAT_DEFAULT).writeToNBT(tagCompound, schematic, backupWorld);

try (DataOutputStream dataOutputStream = new DataOutputStream(
new GZIPOutputStream(new FileOutputStream(file)))) {
Expand All @@ -68,8 +69,8 @@ public static boolean writeToFile(File file, ISchematic schematic) {
return false;
}

public static boolean writeToFile(File directory, String filename, ISchematic schematic) {
return writeToFile(new File(directory, filename), schematic);
public static boolean writeToFile(File directory, String filename, ISchematic schematic, World backupWorld) {
return writeToFile(new File(directory, filename), schematic, backupWorld);
}

static {
Expand Down

0 comments on commit e72a792

Please sign in to comment.