2016-02-02 06:28:27 +00:00
|
|
|
/*
|
|
|
|
* 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 Lesser 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 Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.sponge;
|
|
|
|
|
|
|
|
import com.flowpowered.math.vector.Vector3d;
|
|
|
|
import com.flowpowered.math.vector.Vector3i;
|
2016-02-06 00:30:11 +00:00
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
2016-02-02 06:28:27 +00:00
|
|
|
import com.sk89q.worldedit.Vector;
|
|
|
|
import com.sk89q.worldedit.Vector2D;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
|
|
|
import com.sk89q.worldedit.blocks.BaseItemStack;
|
|
|
|
import com.sk89q.worldedit.entity.BaseEntity;
|
|
|
|
import com.sk89q.worldedit.entity.Entity;
|
2016-02-06 00:30:11 +00:00
|
|
|
import com.sk89q.worldedit.internal.Constants;
|
2016-02-02 06:28:27 +00:00
|
|
|
import com.sk89q.worldedit.regions.Region;
|
|
|
|
import com.sk89q.worldedit.util.Location;
|
|
|
|
import com.sk89q.worldedit.world.AbstractWorld;
|
|
|
|
import com.sk89q.worldedit.world.biome.BaseBiome;
|
|
|
|
import com.sk89q.worldedit.world.registry.WorldData;
|
2016-02-06 00:30:11 +00:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.EntityList;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.BlockPos;
|
|
|
|
import net.minecraft.world.chunk.Chunk;
|
|
|
|
import org.spongepowered.api.Sponge;
|
2016-02-02 06:28:27 +00:00
|
|
|
import org.spongepowered.api.block.BlockSnapshot;
|
|
|
|
import org.spongepowered.api.block.BlockState;
|
2016-02-06 00:30:11 +00:00
|
|
|
import org.spongepowered.api.block.tileentity.TileEntity;
|
2016-02-02 06:28:27 +00:00
|
|
|
import org.spongepowered.api.data.key.Keys;
|
|
|
|
import org.spongepowered.api.data.property.block.GroundLuminanceProperty;
|
|
|
|
import org.spongepowered.api.data.property.block.SkyLuminanceProperty;
|
|
|
|
import org.spongepowered.api.entity.EntitySnapshot;
|
2016-02-06 00:30:11 +00:00
|
|
|
import org.spongepowered.api.entity.EntityType;
|
2016-02-02 06:28:27 +00:00
|
|
|
import org.spongepowered.api.entity.EntityTypes;
|
|
|
|
import org.spongepowered.api.event.cause.Cause;
|
2016-02-06 00:30:11 +00:00
|
|
|
import org.spongepowered.api.util.PositionOutOfBoundsException;
|
2016-02-02 06:28:27 +00:00
|
|
|
import org.spongepowered.api.world.World;
|
|
|
|
|
2016-02-03 00:28:05 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2016-02-02 06:28:27 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An adapter to Minecraft worlds for WorldEdit.
|
|
|
|
*/
|
|
|
|
public abstract class SpongeWorld extends AbstractWorld {
|
|
|
|
|
|
|
|
protected static final Random random = new Random();
|
|
|
|
protected static final int UPDATE = 1, NOTIFY = 2, NOTIFY_CLIENT = 4;
|
|
|
|
protected static final Logger logger = Logger.getLogger(SpongeWorld.class.getCanonicalName());
|
|
|
|
|
|
|
|
private final WeakReference<World> worldRef;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new world.
|
|
|
|
*
|
|
|
|
* @param world the world
|
|
|
|
*/
|
|
|
|
SpongeWorld(World world) {
|
|
|
|
checkNotNull(world);
|
|
|
|
this.worldRef = new WeakReference<World>(world);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the underlying handle to the world.
|
|
|
|
*
|
|
|
|
* @return the world
|
|
|
|
* @throws WorldEditException thrown if a reference to the world was lost (i.e. world was unloaded)
|
|
|
|
*/
|
|
|
|
public World getWorldChecked() throws WorldEditException {
|
|
|
|
World world = worldRef.get();
|
|
|
|
if (world != null) {
|
|
|
|
return world;
|
|
|
|
} else {
|
|
|
|
throw new WorldReferenceLostException("The reference to the world was lost (i.e. the world may have been unloaded)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the underlying handle to the world.
|
|
|
|
*
|
|
|
|
* @return the world
|
|
|
|
* @throws RuntimeException thrown if a reference to the world was lost (i.e. world was unloaded)
|
|
|
|
*/
|
|
|
|
public World getWorld() {
|
|
|
|
World world = worldRef.get();
|
|
|
|
if (world != null) {
|
|
|
|
return world;
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("The reference to the world was lost (i.e. the world may have been unloaded)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return getWorld().getName();
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:30:11 +00:00
|
|
|
protected abstract BlockState getBlockState(BaseBlock block);
|
|
|
|
|
|
|
|
protected abstract void applyTileEntityData(TileEntity entity, BaseBlock block);
|
2016-02-02 06:28:27 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean setBlock(Vector position, BaseBlock block, boolean notifyAndLight) throws WorldEditException {
|
|
|
|
checkNotNull(position);
|
|
|
|
checkNotNull(block);
|
|
|
|
|
2016-02-06 00:30:11 +00:00
|
|
|
World world = getWorldChecked();
|
|
|
|
|
|
|
|
// First set the block
|
|
|
|
Vector3i pos = new Vector3i(position.getX(), position.getY(), position.getZ());
|
|
|
|
BlockState newState = getBlockState(block);
|
|
|
|
|
|
|
|
try {
|
|
|
|
world.setBlock(pos, newState, notifyAndLight, Cause.of(SpongeWorldEdit.container()));
|
|
|
|
} catch (PositionOutOfBoundsException ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the TileEntity
|
|
|
|
if (block.hasNbtData()) {
|
|
|
|
// Kill the old TileEntity
|
|
|
|
Optional<TileEntity> optTile = world.getTileEntity(pos);
|
|
|
|
if (optTile.isPresent()) {
|
|
|
|
applyTileEntityData(optTile.get(), block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-02-02 06:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getBlockLightLevel(Vector position) {
|
|
|
|
checkNotNull(position);
|
|
|
|
|
|
|
|
BlockState state = getWorld().getBlock(new Vector3i(position.getX(), position.getY(), position.getZ()));
|
|
|
|
|
|
|
|
Optional<GroundLuminanceProperty> groundLuminanceProperty = state.getProperty(GroundLuminanceProperty.class);
|
|
|
|
Optional<SkyLuminanceProperty> skyLuminanceProperty = state.getProperty(SkyLuminanceProperty.class);
|
|
|
|
|
|
|
|
if (!groundLuminanceProperty.isPresent() || !skyLuminanceProperty.isPresent()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
return (int) Math.max(groundLuminanceProperty.get().getValue(), skyLuminanceProperty.get().getValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BaseBiome getBiome(Vector2D position) {
|
|
|
|
checkNotNull(position);
|
|
|
|
return new BaseBiome(IDHelper.resolve(getWorld().getBiome(position.getBlockX(), position.getBlockZ())));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean setBiome(Vector2D position, BaseBiome biome) {
|
|
|
|
checkNotNull(position);
|
|
|
|
checkNotNull(biome);
|
|
|
|
|
|
|
|
getWorld().setBiome(position.getBlockX(), position.getBlockZ(), IDHelper.resolveBiome(biome.getId()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void dropItem(Vector position, BaseItemStack item) {
|
|
|
|
checkNotNull(position);
|
|
|
|
checkNotNull(item);
|
|
|
|
|
|
|
|
if (item.getType() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<org.spongepowered.api.entity.Entity> optItem = getWorld().createEntity(
|
|
|
|
EntityTypes.ITEM,
|
|
|
|
new Vector3d(position.getX(), position.getY(), position.getZ())
|
|
|
|
);
|
|
|
|
|
|
|
|
if (optItem.isPresent()) {
|
|
|
|
org.spongepowered.api.entity.Entity entity = optItem.get();
|
|
|
|
entity.offer(Keys.REPRESENTED_ITEM, SpongeWorldEdit.toSpongeItemStack(item).createSnapshot());
|
2016-02-06 00:30:11 +00:00
|
|
|
getWorld().spawnEntity(entity, Cause.of(SpongeWorldEdit.container()));
|
2016-02-02 06:28:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public WorldData getWorldData() {
|
2016-02-02 23:15:47 +00:00
|
|
|
return SpongeWorldData.getInstance();
|
2016-02-02 06:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isValidBlockType(int id) {
|
2016-02-02 23:15:47 +00:00
|
|
|
return (id == 0) || (IDHelper.resolveBlock(id) != null);
|
2016-02-02 06:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return getWorld().hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o == null) {
|
|
|
|
return false;
|
|
|
|
} else if ((o instanceof SpongeWorld)) {
|
|
|
|
SpongeWorld other = ((SpongeWorld) o);
|
|
|
|
World otherWorld = other.worldRef.get();
|
|
|
|
World thisWorld = worldRef.get();
|
|
|
|
return otherWorld != null && thisWorld != null && otherWorld.equals(thisWorld);
|
|
|
|
} else if (o instanceof com.sk89q.worldedit.world.World) {
|
|
|
|
return ((com.sk89q.worldedit.world.World) o).getName().equals(getName());
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<? extends Entity> getEntities(Region region) {
|
|
|
|
List<Entity> entities = new ArrayList<>();
|
|
|
|
for (org.spongepowered.api.entity.Entity entity : getWorld().getEntities()) {
|
|
|
|
org.spongepowered.api.world.Location<World> loc = entity.getLocation();
|
|
|
|
if (region.contains(new Vector(loc.getX(), loc.getY(), loc.getZ()))) {
|
|
|
|
entities.add(new SpongeEntity(entity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<? extends Entity> getEntities() {
|
|
|
|
List<Entity> entities = new ArrayList<>();
|
|
|
|
for (org.spongepowered.api.entity.Entity entity : getWorld().getEntities()) {
|
|
|
|
entities.add(new SpongeEntity(entity));
|
|
|
|
}
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:30:11 +00:00
|
|
|
protected abstract void applyEntityData(org.spongepowered.api.entity.Entity entity, BaseEntity data);
|
2016-02-02 06:28:27 +00:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Entity createEntity(Location location, BaseEntity entity) {
|
2016-02-06 00:30:11 +00:00
|
|
|
World world = getWorld();
|
|
|
|
|
|
|
|
EntityType entityType = Sponge.getRegistry().getType(EntityType.class, entity.getTypeId()).get();
|
|
|
|
Vector3d pos = new Vector3d(location.getX(), location.getY(), location.getZ());
|
|
|
|
|
|
|
|
Optional<org.spongepowered.api.entity.Entity> optNewEnt = world.createEntity(entityType, pos);
|
|
|
|
if (optNewEnt.isPresent()) {
|
|
|
|
org.spongepowered.api.entity.Entity newEnt = optNewEnt.get();
|
|
|
|
if (entity.hasNbtData()) {
|
|
|
|
applyEntityData(newEnt, entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite any data set by the NBT application
|
|
|
|
Vector dir = location.getDirection();
|
|
|
|
|
|
|
|
newEnt.setLocationAndRotation(
|
|
|
|
new org.spongepowered.api.world.Location<>(getWorld(), pos),
|
|
|
|
new Vector3d(dir.getX(), dir.getY(), dir.getZ())
|
|
|
|
);
|
|
|
|
|
|
|
|
if (world.spawnEntity(newEnt, Cause.of(SpongeWorldEdit.container()))) {
|
|
|
|
return new SpongeEntity(newEnt);
|
2016-02-02 06:28:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-06 00:30:11 +00:00
|
|
|
|
2016-02-02 06:28:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Thrown when the reference to the world is lost.
|
|
|
|
*/
|
|
|
|
private static class WorldReferenceLostException extends WorldEditException {
|
|
|
|
private WorldReferenceLostException(String message) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|