mirror of
https://github.com/EngineHub/WorldEdit.git
synced 2025-02-23 13:30:12 +08:00
Implement UPDATE for Fabric (#1598)
* Implement UPDATE for Fabric * Fix a minor threading problem
This commit is contained in:
parent
d0e6f9f33b
commit
3637d94aef
@ -216,7 +216,8 @@ public Map<Capability, Preference> getCapabilities() {
|
|||||||
SideEffect.VALIDATION,
|
SideEffect.VALIDATION,
|
||||||
SideEffect.ENTITY_AI,
|
SideEffect.ENTITY_AI,
|
||||||
SideEffect.LIGHTING,
|
SideEffect.LIGHTING,
|
||||||
SideEffect.NEIGHBORS
|
SideEffect.NEIGHBORS,
|
||||||
|
SideEffect.UPDATE
|
||||||
);
|
);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -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);
|
||||||
|
}
|
@ -22,6 +22,8 @@
|
|||||||
import com.sk89q.worldedit.fabric.FabricAdapter;
|
import com.sk89q.worldedit.fabric.FabricAdapter;
|
||||||
import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
|
import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
|
||||||
import com.sk89q.worldedit.internal.wna.WorldNativeAccess;
|
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.Block;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.block.entity.BlockEntity;
|
import net.minecraft.block.entity.BlockEntity;
|
||||||
@ -41,6 +43,7 @@ public class FabricWorldNativeAccess implements WorldNativeAccess<WorldChunk, Bl
|
|||||||
private static final int NOTIFY = 2;
|
private static final int NOTIFY = 2;
|
||||||
|
|
||||||
private final WeakReference<World> world;
|
private final WeakReference<World> world;
|
||||||
|
private SideEffectSet sideEffectSet;
|
||||||
|
|
||||||
public FabricWorldNativeAccess(WeakReference<World> world) {
|
public FabricWorldNativeAccess(WeakReference<World> world) {
|
||||||
this.world = world;
|
this.world = world;
|
||||||
@ -50,6 +53,11 @@ private World getWorld() {
|
|||||||
return Objects.requireNonNull(world.get(), "The reference to the world was lost");
|
return Objects.requireNonNull(world.get(), "The reference to the world was lost");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCurrentSideEffectSet(SideEffectSet sideEffectSet) {
|
||||||
|
this.sideEffectSet = sideEffectSet;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WorldChunk getChunk(int x, int z) {
|
public WorldChunk getChunk(int x, int z) {
|
||||||
return getWorld().getChunk(x, z);
|
return getWorld().getChunk(x, z);
|
||||||
@ -71,6 +79,11 @@ public BlockState getBlockState(WorldChunk chunk, BlockPos position) {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public BlockState setBlockState(WorldChunk chunk, BlockPos position, BlockState state) {
|
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);
|
return chunk.setBlockState(position, state, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@
|
|||||||
"MixinBiomeArray",
|
"MixinBiomeArray",
|
||||||
"MixinServerPlayerEntity",
|
"MixinServerPlayerEntity",
|
||||||
"MixinMinecraftServer",
|
"MixinMinecraftServer",
|
||||||
|
"MixinWorldChunk",
|
||||||
"AccessorClientSettingsC2SPacket",
|
"AccessorClientSettingsC2SPacket",
|
||||||
"AccessorLevelProperties",
|
"AccessorLevelProperties",
|
||||||
"AccessorServerChunkManager"
|
"AccessorServerChunkManager"
|
||||||
|
Loading…
Reference in New Issue
Block a user