Implement UPDATE for Fabric (#1598)

* Implement UPDATE for Fabric

* Fix a minor threading problem
This commit is contained in:
Octavia Togami 2020-11-29 17:56:09 -08:00 committed by GitHub
parent d0e6f9f33b
commit 3637d94aef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 133 additions and 1 deletions

View File

@ -216,7 +216,8 @@ public Map<Capability, Preference> getCapabilities() {
SideEffect.VALIDATION,
SideEffect.ENTITY_AI,
SideEffect.LIGHTING,
SideEffect.NEIGHBORS
SideEffect.NEIGHBORS,
SideEffect.UPDATE
);
@Override

View File

@ -0,0 +1,43 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.fabric.internal;
import com.sk89q.worldedit.util.SideEffect;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.Chunk;
import javax.annotation.Nullable;
public interface ExtendedChunk {
/**
* {@link Chunk#setBlockState(BlockPos, BlockState, boolean)} with the extra
* {@link SideEffect#UPDATE} flag.
*
* @param pos the position to set
* @param state the state to set
* @param moved I honestly have no idea and can't be bothered to investigate, we pass {@code
* false}
* @param update the update flag, see side-effect for details
* @return the old block state, or {@code null} if unchanged
*/
@Nullable
BlockState setBlockState(BlockPos pos, BlockState state, boolean moved, boolean update);
}

View File

@ -22,6 +22,8 @@
import com.sk89q.worldedit.fabric.FabricAdapter;
import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
import com.sk89q.worldedit.internal.wna.WorldNativeAccess;
import com.sk89q.worldedit.util.SideEffect;
import com.sk89q.worldedit.util.SideEffectSet;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
@ -41,6 +43,7 @@ public class FabricWorldNativeAccess implements WorldNativeAccess<WorldChunk, Bl
private static final int NOTIFY = 2;
private final WeakReference<World> world;
private SideEffectSet sideEffectSet;
public FabricWorldNativeAccess(WeakReference<World> world) {
this.world = world;
@ -50,6 +53,11 @@ private World getWorld() {
return Objects.requireNonNull(world.get(), "The reference to the world was lost");
}
@Override
public void setCurrentSideEffectSet(SideEffectSet sideEffectSet) {
this.sideEffectSet = sideEffectSet;
}
@Override
public WorldChunk getChunk(int x, int z) {
return getWorld().getChunk(x, z);
@ -71,6 +79,11 @@ public BlockState getBlockState(WorldChunk chunk, BlockPos position) {
@Nullable
@Override
public BlockState setBlockState(WorldChunk chunk, BlockPos position, BlockState state) {
if (chunk instanceof ExtendedChunk) {
return ((ExtendedChunk) chunk).setBlockState(
position, state, false, sideEffectSet.shouldApply(SideEffect.UPDATE)
);
}
return chunk.setBlockState(position, state, false);
}

View File

@ -0,0 +1,74 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.fabric.mixin;
import com.sk89q.worldedit.fabric.internal.ExtendedChunk;
import net.minecraft.block.BlockState;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.WorldChunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.Slice;
import javax.annotation.Nullable;
@Mixin(WorldChunk.class)
public abstract class MixinWorldChunk implements Chunk, ExtendedChunk {
private boolean shouldUpdate = true;
@Nullable
@Override
public BlockState setBlockState(BlockPos pos, BlockState state, boolean moved, boolean update) {
// save the state for the hook
shouldUpdate = update;
try {
return setBlockState(pos, state, moved);
} finally {
// restore natural mode
shouldUpdate = true;
}
}
@Redirect(
method = "setBlockState",
slice = @Slice(
from = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/BlockEntity;resetBlock()V")
),
at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;onBlockAdded(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Z)V", ordinal = 0)
)
public void setBlockStateHook(BlockState target, World world, BlockPos pos, BlockState old, boolean move) {
boolean localShouldUpdate;
MinecraftServer server = world.getServer();
if (server == null || Thread.currentThread() != server.getThread()) {
// We're not on the server thread for some reason, WorldEdit will never be here
// so we'll just ignore our flag
localShouldUpdate = true;
} else {
localShouldUpdate = shouldUpdate;
}
if (localShouldUpdate) {
target.onBlockAdded(world, pos, old, move);
}
}
}

View File

@ -6,6 +6,7 @@
"MixinBiomeArray",
"MixinServerPlayerEntity",
"MixinMinecraftServer",
"MixinWorldChunk",
"AccessorClientSettingsC2SPacket",
"AccessorLevelProperties",
"AccessorServerChunkManager"