Skip to content

Commit

Permalink
Fix ServerLevel proxies not including entities (#2308)
Browse files Browse the repository at this point in the history
* Fix ServerLevel proxies not including entities

* Also include the entity call that the village generator uses
  • Loading branch information
me4502 committed May 7, 2023
1 parent cd95d20 commit 5638a28
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
Expand Up @@ -21,14 +21,24 @@

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.concurrency.LazyReference;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.entity.EntityTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import org.enginehub.linbus.tree.LinCompoundTag;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationHandler;
Expand Down Expand Up @@ -87,13 +97,28 @@ private boolean removeBlock(BlockPos blockPos, boolean bl) {
}
}

private boolean addEntity(Entity entity) {
Vec3 pos = entity.getPosition(0.0f);
Location location = new Location(BukkitAdapter.adapt(serverLevel.getWorld()), pos.x(), pos.y(), pos.z());

ResourceLocation id = serverLevel.registryAccess().registryOrThrow(Registries.ENTITY_TYPE).getKey(entity.getType());
CompoundTag tag = new CompoundTag();
entity.saveWithoutId(tag);
BaseEntity baseEntity = new BaseEntity(EntityTypes.get(id.toString()), LazyReference.from(() -> (LinCompoundTag) adapter.toNative(tag)));

return editSession.createEntity(location, baseEntity) != null;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "a_", "getBlockState" -> {
case "a_", "getBlockState", "addFreshEntityWithPassengers" -> {
if (args.length == 1 && args[0] instanceof BlockPos blockPos) {
// getBlockState
return getBlockState(blockPos);
} else if (args.length >= 1 && args[0] instanceof Entity entity) {
// addFreshEntityWithPassengers
return addEntity(entity);
}
}
case "c_", "getBlockEntity" -> {
Expand All @@ -111,6 +136,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return removeBlock(blockPos, bl);
}
}
case "j", "addEntity" -> {
if (args.length >= 1 && args[0] instanceof Entity entity) {
return addEntity(entity);
}
}
default -> { }
}

Expand Down
Expand Up @@ -38,9 +38,7 @@
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.StringRepresentable;
Expand Down
Expand Up @@ -34,6 +34,7 @@
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.fabric.internal.ExtendedMinecraftServer;
import com.sk89q.worldedit.fabric.internal.FabricEntity;
import com.sk89q.worldedit.fabric.internal.FabricServerLevelDelegateProxy;
import com.sk89q.worldedit.fabric.internal.FabricWorldNativeAccess;
import com.sk89q.worldedit.fabric.internal.NBTConverter;
Expand Down
Expand Up @@ -17,12 +17,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.fabric;
package com.sk89q.worldedit.fabric.internal;

import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.entity.metadata.EntityProperties;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.fabric.FabricAdapter;
import com.sk89q.worldedit.fabric.FabricEntityProperties;
import com.sk89q.worldedit.fabric.FabricWorldEdit;
import com.sk89q.worldedit.fabric.internal.NBTConverter;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Location;
Expand All @@ -38,11 +41,11 @@

import static com.google.common.base.Preconditions.checkNotNull;

class FabricEntity implements Entity {
public class FabricEntity implements Entity {

private final WeakReference<net.minecraft.world.entity.Entity> entityRef;

FabricEntity(net.minecraft.world.entity.Entity entity) {
public FabricEntity(net.minecraft.world.entity.Entity entity) {
checkNotNull(entity);
this.entityRef = new WeakReference<>(entity);
}
Expand Down
Expand Up @@ -21,10 +21,14 @@

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.fabric.FabricAdapter;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.block.BlockTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -33,7 +37,6 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class FabricServerLevelDelegateProxy implements InvocationHandler {

Expand Down Expand Up @@ -85,6 +88,13 @@ private boolean removeBlock(BlockPos blockPos, boolean bl) {
}
}

private boolean addEntity(Entity entity) {
Vector3 pos = FabricAdapter.adapt(entity.getPosition(0.0f));
Location location = new Location(FabricAdapter.adapt(serverLevel), pos.getX(), pos.getY(), pos.getZ());
BaseEntity baseEntity = new FabricEntity(entity).getState();
return editSession.createEntity(location, baseEntity) != null;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
Expand All @@ -108,6 +118,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return removeBlock(blockPos, bl);
}
}
case "addEntity", "method_14175", "addFreshEntityWithPassengers", "method_30771" -> {
if (args.length >= 1 && args[0] instanceof Entity entity) {
return addEntity(entity);
}
}
default -> { }
}

Expand Down
Expand Up @@ -33,6 +33,7 @@
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.forge.internal.ForgeEntity;
import com.sk89q.worldedit.forge.internal.ForgeServerLevelDelegateProxy;
import com.sk89q.worldedit.forge.internal.ForgeWorldNativeAccess;
import com.sk89q.worldedit.forge.internal.NBTConverter;
Expand Down
Expand Up @@ -17,12 +17,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.forge;
package com.sk89q.worldedit.forge.internal;

import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.entity.metadata.EntityProperties;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.forge.ForgeAdapter;
import com.sk89q.worldedit.forge.ForgeEntityProperties;
import com.sk89q.worldedit.forge.internal.NBTConverter;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Location;
Expand All @@ -39,11 +41,11 @@

import static com.google.common.base.Preconditions.checkNotNull;

class ForgeEntity implements Entity {
public class ForgeEntity implements Entity {

private final WeakReference<net.minecraft.world.entity.Entity> entityRef;

ForgeEntity(net.minecraft.world.entity.Entity entity) {
public ForgeEntity(net.minecraft.world.entity.Entity entity) {
checkNotNull(entity);
this.entityRef = new WeakReference<>(entity);
}
Expand Down
Expand Up @@ -21,10 +21,14 @@

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.forge.ForgeAdapter;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.block.BlockTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -33,7 +37,6 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class ForgeServerLevelDelegateProxy implements InvocationHandler {

Expand Down Expand Up @@ -85,6 +88,13 @@ private boolean removeBlock(BlockPos blockPos, boolean bl) {
}
}

private boolean addEntity(Entity entity) {
Vector3 pos = ForgeAdapter.adapt(entity.getPosition(0.0f));
Location location = new Location(ForgeAdapter.adapt(serverLevel), pos.getX(), pos.getY(), pos.getZ());
BaseEntity baseEntity = new ForgeEntity(entity).getState();
return editSession.createEntity(location, baseEntity) != null;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
Expand All @@ -108,6 +118,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return removeBlock(blockPos, bl);
}
}
case "addEntity", "m_8872_", "addFreshEntityWithPassengers", "m_47205_" -> {
if (args.length >= 1 && args[0] instanceof Entity entity) {
return addEntity(entity);
}
}
default -> { }
}

Expand Down

0 comments on commit 5638a28

Please sign in to comment.