Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getWorldUUID to SaveChunkEvent. #3164

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
import net.minecraft.world.chunk.storage.RegionFileCache;
import net.minecraft.world.storage.ThreadedFileIOBase;
Expand All @@ -59,6 +60,7 @@
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.spongepowered.common.SpongeImpl;
import org.spongepowered.common.SpongeImplHooks;
import org.spongepowered.common.bridge.world.WorldBridge;
import org.spongepowered.common.bridge.world.chunk.ChunkBridge;
import org.spongepowered.common.bridge.world.chunk.storage.AnvilChunkLoaderBridge;
import org.spongepowered.common.entity.PlayerTracker;
Expand All @@ -67,12 +69,15 @@
import org.spongepowered.common.util.Constants;
import org.spongepowered.common.util.QueuedChunk;
import org.spongepowered.common.util.VecHelper;
import org.spongepowered.common.util.WorldChunkPos;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;

@Mixin(AnvilChunkLoader.class)
Expand Down Expand Up @@ -268,14 +273,14 @@ public boolean writeNextIO() {
this.writeChunkData(chunkpos, nbttagcompound);
laste = null;
break;
} catch (Exception exception) {
} catch (final Exception exception) {
// LOGGER.error((String)"Failed to save chunk",
// (Throwable)exception);
laste = exception;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -305,11 +310,24 @@ public boolean writeNextIO() {
return this.chunkSaveLocation.toPath();
}

@Redirect(method = "saveChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/chunk/storage/AnvilChunkLoader.addChunkToPending"
+ "(Lnet/minecraft/util/math/ChunkPos;Lnet/minecraft/nbt/NBTTagCompound;)V"))
private void impl$useWorldChunkPosWhenQueuing(final AnvilChunkLoader anvilChunkLoader, final ChunkPos pos, final NBTTagCompound compound,
final World worldIn, final Chunk chunkIn) {
this.addChunkToPending(new WorldChunkPos(((org.spongepowered.api.world.World) worldIn).getUniqueId(), pos.x, pos.z), compound);
}

@Inject(method = "writeChunkData", at = @At("RETURN"))
private void impl$callSaveChunkEventPost(ChunkPos pos, NBTTagCompound compound, CallbackInfo ci) {
private void impl$callSaveChunkEventPost(final ChunkPos pos, final NBTTagCompound compound, final CallbackInfo ci) {
if (ShouldFire.SAVE_CHUNK_EVENT_POST) {
final Cause cause = Cause.of(EventContext.empty(), Collections.singleton(SpongeImpl.getServer()));
SpongeImpl.postEvent(SpongeEventFactory.createSaveChunkEventPost(cause, VecHelper.toVec3i(pos)));
final Optional<UUID> worldUUID;
if (pos instanceof WorldChunkPos) {
worldUUID = Optional.of(((WorldChunkPos) pos).getWorldUUID());
} else {
worldUUID = Optional.empty();
}
SpongeImpl.postEvent(SpongeEventFactory.createSaveChunkEventPost(cause, VecHelper.toVec3i(pos), worldUUID));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -358,7 +359,7 @@ public boolean tick()
if (ShouldFire.SAVE_CHUNK_EVENT_PRE) {
final org.spongepowered.api.world.Chunk apiChunk = (org.spongepowered.api.world.Chunk) chunkIn;
if (SpongeImpl.postEvent(SpongeEventFactory.createSaveChunkEventPre(SpongeImpl.getCauseStackManager().getCurrentCause(),
apiChunk.getPosition(), apiChunk))) {
apiChunk.getPosition(), Optional.of(apiChunk.getWorld().getUniqueId()), apiChunk))) {
ci.cancel();
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/spongepowered/common/util/WorldChunkPos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.util;

import net.minecraft.util.math.ChunkPos;

import java.util.UUID;

// Used in AnvilChunkLoaderMixin
public final class WorldChunkPos extends ChunkPos {

private final UUID uuid;

public WorldChunkPos(final UUID uuid, final int x, final int z) {
super(x, z);
this.uuid = uuid;
}

public UUID getWorldUUID() {
return this.uuid;
}

}