mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-11 20:13:55 +00:00
Merge pull request #518 from aurorasmiles/fixEntities
start reimplementing entities
This commit is contained in:
@ -2,12 +2,26 @@ package com.boydti.fawe.beta.implementation;
|
||||
|
||||
import com.boydti.fawe.beta.IChunk;
|
||||
import com.sk89q.jnbt.CompoundTag;
|
||||
import com.sk89q.jnbt.DoubleTag;
|
||||
import com.sk89q.jnbt.IntArrayTag;
|
||||
import com.sk89q.jnbt.ListTag;
|
||||
import com.sk89q.jnbt.LongTag;
|
||||
import com.sk89q.jnbt.StringTag;
|
||||
import com.sk89q.jnbt.Tag;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.entity.BaseEntity;
|
||||
import com.sk89q.worldedit.entity.Entity;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.util.Location;
|
||||
import com.sk89q.worldedit.world.biome.BiomeType;
|
||||
import com.sk89q.worldedit.world.block.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.jetbrains.annotations.Range;
|
||||
|
||||
public interface IChunkExtent<T extends IChunk> extends Extent {
|
||||
@ -91,4 +105,88 @@ public interface IChunkExtent<T extends IChunk> extends Extent {
|
||||
final IChunk chunk = getOrCreateChunk(x >> 4, z >> 4);
|
||||
return chunk.getOpacity(x & 15, y, z & 15);
|
||||
}
|
||||
|
||||
@Override
|
||||
default Entity createEntity(Location location, BaseEntity entity) {
|
||||
final IChunk chunk = getOrCreateChunk(location.getBlockX() >> 4, location.getBlockZ() >> 4);
|
||||
Map<String, Tag> map = new HashMap<>(entity.getNbtData().getValue()); //do not modify original entity data
|
||||
map.put("Id", new StringTag(entity.getType().getName()));
|
||||
|
||||
//Set pos
|
||||
List<DoubleTag> posList = new ArrayList<>();
|
||||
posList.add(new DoubleTag(location.getX()));
|
||||
posList.add(new DoubleTag(location.getY()));
|
||||
posList.add(new DoubleTag(location.getZ()));
|
||||
map.put("Pos", new ListTag(DoubleTag.class, posList));
|
||||
|
||||
//set new uuid
|
||||
UUID newuuid = UUID.randomUUID();
|
||||
int[] uuidArray = new int[4];
|
||||
uuidArray[0] = (int) (newuuid.getMostSignificantBits() >> 32);
|
||||
uuidArray[1] = (int) newuuid.getMostSignificantBits();
|
||||
uuidArray[2] = (int) (newuuid.getLeastSignificantBits() >> 32);
|
||||
uuidArray[3] = (int) newuuid.getLeastSignificantBits();
|
||||
map.put("UUID", new IntArrayTag(uuidArray));
|
||||
|
||||
map.put("UUIDMost", new LongTag(newuuid.getMostSignificantBits()));
|
||||
map.put("UUIDLeast", new LongTag(newuuid.getLeastSignificantBits()));
|
||||
|
||||
map.put("PersistentIDMSB", new LongTag(newuuid.getMostSignificantBits()));
|
||||
map.put("PersistentIDLSB", new LongTag(newuuid.getLeastSignificantBits()));
|
||||
|
||||
chunk.setEntity(new CompoundTag(map));
|
||||
return new IChunkEntity(this, location, newuuid, entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
default void removeEntity(int x, int y, int z, UUID uuid) {
|
||||
final IChunk chunk = getOrCreateChunk(x >> 4, z >> 4);
|
||||
chunk.removeEntity(uuid);
|
||||
}
|
||||
|
||||
class IChunkEntity implements Entity {
|
||||
|
||||
private final Extent extent;
|
||||
private final Location location;
|
||||
private final UUID uuid;
|
||||
private final BaseEntity base;
|
||||
|
||||
public IChunkEntity(Extent extent, Location location, UUID uuid, BaseEntity base) {
|
||||
this.extent = extent;
|
||||
this.location = location;
|
||||
this.uuid = uuid;
|
||||
this.base = base;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseEntity getState() {
|
||||
return base;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove() {
|
||||
extent.removeEntity(location.getBlockX(), location.getBlockY(), location.getBlockZ(), uuid);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getFacet(Class<? extends T> cls) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setLocation(Location location) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Extent getExtent() {
|
||||
return extent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,12 +78,12 @@ public class MutableEntityChange implements Change {
|
||||
return;
|
||||
}
|
||||
List<DoubleTag> pos = (List<DoubleTag>) posTag.getValue();
|
||||
int x = MathMan.roundInt(pos.get(0).getValue());
|
||||
int y = MathMan.roundInt(pos.get(1).getValue());
|
||||
int z = MathMan.roundInt(pos.get(2).getValue());
|
||||
double x = pos.get(0).getValue();
|
||||
double y = pos.get(1).getValue();
|
||||
double z = pos.get(2).getValue();
|
||||
Extent extent = context.getExtent();
|
||||
Location location = new Location(extent, x, y, z, 0, 0);
|
||||
String id = tag.getString("id");
|
||||
String id = tag.getString("Id");
|
||||
EntityType type = EntityTypes.parse(id);
|
||||
BaseEntity entity = new BaseEntity(type, tag);
|
||||
context.getExtent().createEntity(location, entity);
|
||||
|
@ -43,6 +43,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
@ -437,6 +438,11 @@ public class DiskOptimizedClipboard extends LinearClipboard implements Closeable
|
||||
public List<? extends Entity> getEntities() {
|
||||
return new ArrayList<>(entities);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Entity> getEntities(Region region) {
|
||||
return new ArrayList<>(entities.stream().filter(e -> region.contains(e.getLocation().toBlockPoint())).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeEntity(Entity entity) {
|
||||
|
@ -27,6 +27,7 @@ import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class MemoryOptimizedClipboard extends LinearClipboard {
|
||||
@ -292,11 +293,15 @@ public class MemoryOptimizedClipboard extends LinearClipboard {
|
||||
return new ArrayList<>(entities);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Entity> getEntities(Region region) {
|
||||
return new ArrayList<>(entities.stream().filter(e -> region.contains(e.getLocation().toBlockPoint())).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeEntity(Entity entity) {
|
||||
if (entity instanceof ClipboardEntity) {
|
||||
this.entities.remove(entity);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -40,6 +40,8 @@ import com.boydti.fawe.util.ExtentTraverser;
|
||||
import com.boydti.fawe.util.MaskTraverser;
|
||||
import com.boydti.fawe.util.MathMan;
|
||||
import com.boydti.fawe.util.TaskManager;
|
||||
import com.sk89q.worldedit.entity.BaseEntity;
|
||||
import com.sk89q.worldedit.entity.Entity;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.event.extent.EditSessionEvent;
|
||||
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
|
||||
@ -112,6 +114,7 @@ import com.sk89q.worldedit.regions.shape.RegionShape;
|
||||
import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment;
|
||||
import com.sk89q.worldedit.util.Countable;
|
||||
import com.sk89q.worldedit.util.Direction;
|
||||
import com.sk89q.worldedit.util.Location;
|
||||
import com.sk89q.worldedit.util.SideEffectSet;
|
||||
import com.sk89q.worldedit.util.TreeGenerator;
|
||||
import com.sk89q.worldedit.util.eventbus.EventBus;
|
||||
@ -147,6 +150,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.sk89q.worldedit.regions.Regions.asFlatRegion;
|
||||
import static com.sk89q.worldedit.regions.Regions.maximumBlockY;
|
||||
import static com.sk89q.worldedit.regions.Regions.minimumBlockY;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* An {@link Extent} that handles history, {@link BlockBag}s, change limits,
|
||||
@ -3084,4 +3088,32 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Entity> getEntities() {
|
||||
return world.getEntities();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Entity> getEntities(Region region) {
|
||||
return world.getEntities(region);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity createEntity(Location location, BaseEntity entity) {
|
||||
try {
|
||||
return this.getExtent().createEntity(location, entity);
|
||||
} catch (WorldEditException e) {
|
||||
throw new RuntimeException("Unexpected exception", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeEntity(int x, int y, int z, UUID uuid) {
|
||||
try {
|
||||
this.getExtent().removeEntity(x, y, z, uuid);
|
||||
} catch (WorldEditException e) {
|
||||
throw new RuntimeException("Unexpected exception", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,11 +40,15 @@ import com.sk89q.worldedit.world.biome.BiomeType;
|
||||
import com.sk89q.worldedit.world.block.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import org.jetbrains.annotations.Range;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
@ -153,6 +157,11 @@ public class AbstractDelegateExtent implements Extent {
|
||||
return extent.createEntity(location, entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeEntity(int x, int y, int z, UUID uuid) {
|
||||
extent.removeEntity(x, y, z, uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Entity> getEntities() {
|
||||
return extent.getEntities();
|
||||
|
@ -37,11 +37,6 @@ public class PassthroughExtent extends AbstractDelegateExtent {
|
||||
super(extent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeEntity(int x, int y, int z, UUID uuid) {
|
||||
getExtent().removeEntity(x, y, z, uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean regenerateChunk(int x, int z, @Nullable BiomeType type, @Nullable Long seed) {
|
||||
return getExtent().regenerateChunk(x, z, type, seed);
|
||||
|
@ -45,6 +45,7 @@ import java.util.UUID;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Stores block data as a multi-dimensional array of {@link BlockState}s and
|
||||
@ -197,13 +198,49 @@ public class BlockArrayClipboard implements Clipboard {
|
||||
public List<? extends Entity> getEntities(Region region) {
|
||||
region = region.clone();
|
||||
region.shift(BlockVector3.ZERO.subtract(origin));
|
||||
return getParent().getEntities(region);
|
||||
return getParent().getEntities(region).stream().map(e ->
|
||||
{
|
||||
if (e instanceof ClipboardEntity) {
|
||||
ClipboardEntity ce = (ClipboardEntity) e;
|
||||
Location oldloc = ce.getLocation();
|
||||
Location loc = new Location(oldloc.getExtent(),
|
||||
oldloc.getX() + origin.getBlockX(),
|
||||
oldloc.getY() + origin.getBlockY(),
|
||||
oldloc.getZ() + origin.getBlockZ(),
|
||||
oldloc.getYaw(), oldloc.getPitch());
|
||||
return new ClipboardEntity(loc, ce.entity);
|
||||
}
|
||||
return e;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Entity> getEntities() {
|
||||
return getParent().getEntities().stream().map(e ->
|
||||
{
|
||||
if (e instanceof ClipboardEntity) {
|
||||
ClipboardEntity ce = (ClipboardEntity) e;
|
||||
Location oldloc = ce.getLocation();
|
||||
Location loc = new Location(oldloc.getExtent(),
|
||||
oldloc.getX() + origin.getBlockX(),
|
||||
oldloc.getY() + origin.getBlockY(),
|
||||
oldloc.getZ() + origin.getBlockZ(),
|
||||
oldloc.getYaw(), oldloc.getPitch());
|
||||
return new ClipboardEntity(loc, ce.entity);
|
||||
}
|
||||
return e;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Entity createEntity(Location location, BaseEntity entity) {
|
||||
return getParent().createEntity(location, entity);
|
||||
Location l = new Location(location.getExtent(),
|
||||
location.getX() - origin.getBlockX(),
|
||||
location.getY() - origin.getBlockY(),
|
||||
location.getZ() - origin.getBlockZ(),
|
||||
location.getYaw(), location.getPitch());
|
||||
return getParent().createEntity(l, entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -286,7 +323,7 @@ public class BlockArrayClipboard implements Clipboard {
|
||||
private final float yaw, pitch;
|
||||
|
||||
public ClipboardEntity(Location loc, BaseEntity entity) {
|
||||
this((Clipboard) loc.getExtent(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), loc.getYaw(), loc.getPitch(), entity);
|
||||
this((Clipboard) loc.getExtent(), loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch(), entity);
|
||||
}
|
||||
|
||||
public ClipboardEntity(Clipboard clipboard, double x, double y, double z, float yaw, float pitch, BaseEntity entity) {
|
||||
|
@ -37,6 +37,7 @@ import com.sk89q.worldedit.util.Location;
|
||||
import com.sk89q.worldedit.world.entity.EntityTypes;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Copies entities provided to the function to the provided destination
|
||||
@ -44,6 +45,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||
*/
|
||||
public class ExtentEntityCopy implements EntityFunction {
|
||||
|
||||
private final Extent source;
|
||||
private final Extent destination;
|
||||
private final Vector3 from;
|
||||
private final Vector3 to;
|
||||
@ -58,11 +60,35 @@ public class ExtentEntityCopy implements EntityFunction {
|
||||
* @param to the destination position
|
||||
* @param transform the transformation to apply to both position and orientation
|
||||
*/
|
||||
@Deprecated
|
||||
public ExtentEntityCopy(Vector3 from, Extent destination, Vector3 to, Transform transform) {
|
||||
checkNotNull(from);
|
||||
checkNotNull(destination);
|
||||
checkNotNull(to);
|
||||
checkNotNull(transform);
|
||||
this.source = null;
|
||||
this.destination = destination;
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
this.transform = transform;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param source the source {@code Extent}
|
||||
* @param from the from position
|
||||
* @param destination the destination {@code Extent}
|
||||
* @param to the destination position
|
||||
* @param transform the transformation to apply to both position and orientation
|
||||
*/
|
||||
public ExtentEntityCopy(Extent source, Vector3 from, Extent destination, Vector3 to, Transform transform) {
|
||||
checkNotNull(source);
|
||||
checkNotNull(from);
|
||||
checkNotNull(destination);
|
||||
checkNotNull(to);
|
||||
checkNotNull(transform);
|
||||
this.source = source;
|
||||
this.destination = destination;
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
@ -119,7 +145,22 @@ public class ExtentEntityCopy implements EntityFunction {
|
||||
|
||||
// Remove
|
||||
if (isRemoving() && success) {
|
||||
entity.remove();
|
||||
UUID uuid = null;
|
||||
if (tag.containsKey("UUID")) {
|
||||
int[] arr = tag.getIntArray("UUID");
|
||||
uuid = new UUID((long)arr[0] << 32 | (arr[1] & 0xFFFFFFFFL), (long)arr[2] << 32 | (arr[3] & 0xFFFFFFFFL));
|
||||
} else if (tag.containsKey("UUIDMost")) {
|
||||
uuid = new UUID(tag.getLong("UUIDMost"), tag.getLong("UUIDLeast"));
|
||||
} else if (tag.containsKey("PersistentIDMSB")) {
|
||||
uuid = new UUID(tag.getLong("PersistentIDMSB"), tag.getLong("PersistentIDLSB"));
|
||||
}
|
||||
if (uuid != null) {
|
||||
if (source != null) {
|
||||
source.removeEntity(entity.getLocation().getBlockX(), entity.getLocation().getBlockY(), entity.getLocation().getBlockZ(), uuid);
|
||||
} else {
|
||||
entity.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
|
@ -393,7 +393,7 @@ public class ForwardExtentCopy implements Operation {
|
||||
Operations.completeBlindly(blockCopy);
|
||||
|
||||
if (!entities.isEmpty()) {
|
||||
ExtentEntityCopy entityCopy = new ExtentEntityCopy(from.toVector3(), destination, to.toVector3(), currentTransform);
|
||||
ExtentEntityCopy entityCopy = new ExtentEntityCopy(source, from.toVector3(), destination, to.toVector3(), currentTransform);
|
||||
entityCopy.setRemoving(removingEntities);
|
||||
List<? extends Entity> entities2 = Lists.newArrayList(source.getEntities(region));
|
||||
entities2.removeIf(entity -> {
|
||||
@ -402,6 +402,7 @@ public class ForwardExtentCopy implements Operation {
|
||||
});
|
||||
EntityVisitor entityVisitor = new EntityVisitor(entities.iterator(), entityCopy);
|
||||
Operations.completeBlindly(entityVisitor);
|
||||
affectedEntities += entityVisitor.getAffected();
|
||||
}
|
||||
|
||||
if (transExt != null) {
|
||||
|
@ -36,9 +36,9 @@ public final class Constants {
|
||||
|
||||
static {
|
||||
NO_COPY_ENTITY_NBT_FIELDS = Collections.unmodifiableList(Arrays.asList(
|
||||
"UUIDLeast", "UUIDMost", // Bukkit and Vanilla
|
||||
"WorldUUIDLeast", "WorldUUIDMost", // Bukkit and Vanilla
|
||||
"PersistentIDMSB", "PersistentIDLSB" // Forge
|
||||
//"UUIDLeast", "UUIDMost", // Bukkit and Vanilla //UUID values need to be set manully to a new UUID
|
||||
"WorldUUIDLeast", "WorldUUIDMost" // Bukkit and Vanilla
|
||||
//"PersistentIDMSB", "PersistentIDLSB" // Forge //UUID values need to be set manully to a new UUID
|
||||
));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user