Switched to an adapter system. Currently has adapters for 1.10 and 1.11.

Currently, due to the small number of changes it's loading the 1.11 adapter for 1.10. This needs fixing.
This commit is contained in:
Me4502 2016-11-17 17:58:34 +10:00 committed by wizjany
parent a438347604
commit e967ddf393
19 changed files with 362 additions and 710 deletions

4
.gitignore vendored
View File

@ -17,4 +17,6 @@ out
run
/dependency-reduced-pom.xml
*-private.sh
*-private.sh
!impl/*.class

View File

@ -21,7 +21,6 @@ package com.sk89q.worldedit.sponge;
import com.flowpowered.math.vector.Vector3d;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.sponge.nms.SpongeNMSWorld;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;
@ -31,7 +30,7 @@ final class SpongeAdapter {
}
public static World adapt(org.spongepowered.api.world.World world) {
return new SpongeNMSWorld(world);
return SpongeWorldEdit.inst().getAdapter().getWorld(world);
}
public static Location adapt(org.spongepowered.api.world.Location<org.spongepowered.api.world.World> loc, Vector3d rot) {

View File

@ -19,7 +19,6 @@
package com.sk89q.worldedit.sponge;
import com.sk89q.worldedit.sponge.nms.IDHelper;
import com.sk89q.worldedit.world.biome.BaseBiome;
import com.sk89q.worldedit.world.biome.BiomeData;
import com.sk89q.worldedit.world.registry.BiomeRegistry;
@ -45,7 +44,7 @@ class SpongeBiomeRegistry implements BiomeRegistry {
public List<BaseBiome> getBiomes() {
List<BaseBiome> list = new ArrayList<BaseBiome>();
for (BiomeType biome : Sponge.getGame().getRegistry().getAllOf(BiomeType.class)) {
list.add(new BaseBiome(IDHelper.resolve(biome)));
list.add(new BaseBiome(SpongeWorldEdit.inst().getAdapter().resolve(biome)));
}
return list;
}
@ -53,7 +52,7 @@ class SpongeBiomeRegistry implements BiomeRegistry {
@Nullable
@Override
public BiomeData getData(BaseBiome biome) {
return new SpongeBiomeData(IDHelper.resolveBiome(biome.getId()));
return new SpongeBiomeData(SpongeWorldEdit.inst().getAdapter().resolveBiome(biome.getId()));
}
private static class SpongeBiomeData implements BiomeData {

View File

@ -24,7 +24,6 @@ import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.entity.metadata.EntityType;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.sponge.nms.NMSHelper;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.NullWorld;
import org.spongepowered.api.world.World;
@ -47,7 +46,7 @@ class SpongeEntity implements Entity {
public BaseEntity getState() {
org.spongepowered.api.entity.Entity entity = entityRef.get();
if (entity != null) {
return NMSHelper.createBaseEntity(entity);
return SpongeWorldEdit.inst().getAdapter().createBaseEntity(entity);
} else {
return null;
}

View File

@ -19,8 +19,14 @@
package com.sk89q.worldedit.sponge;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.service.context.Contextual;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.service.permission.PermissionService;
import java.util.stream.Collectors;
public class SpongePermissionsProvider {
@ -28,5 +34,17 @@ public class SpongePermissionsProvider {
return player.hasPermission(permission);
}
public void registerPermission(CommandCallable command, String permission) { }
public void registerPermission(CommandCallable command, String permission) {
Sponge.getGame().getServiceManager().getRegistration(PermissionService.class).ifPresent((permissionService -> {
PermissionDescription.Builder permissionBuilder = permissionService.getProvider().newDescriptionBuilder(SpongeWorldEdit.inst()).get();
permissionBuilder.id(permission).register();
}));
}
public String[] getGroups(Player player) {
PermissionService permissionService = Sponge.getGame().getServiceManager().getRegistration(PermissionService.class).get().getProvider();
return player.getParents().stream()
.filter(subject -> subject.getContainingCollection().equals(permissionService.getGroupSubjects()))
.map(Contextual::getIdentifier).collect(Collectors.toList()).toArray(new String[0]);
}
}

View File

@ -25,8 +25,6 @@ import com.sk89q.worldedit.event.platform.CommandEvent;
import com.sk89q.worldedit.event.platform.CommandSuggestionEvent;
import com.sk89q.worldedit.extension.platform.*;
import com.sk89q.worldedit.sponge.config.SpongeConfiguration;
import com.sk89q.worldedit.sponge.nms.IDHelper;
import com.sk89q.worldedit.sponge.nms.SpongeNMSWorld;
import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.world.World;
@ -61,11 +59,8 @@ class SpongePlatform extends AbstractPlatform implements MultiUserPlatform {
Optional<ItemType> optType = Sponge.getRegistry().getType(ItemType.class, name);
if (optType.isPresent()) {
return IDHelper.resolve(optType.get());
}
return optType.map(itemType -> SpongeWorldEdit.inst().getAdapter().resolve(itemType)).orElse(0);
return 0;
}
@Override
@ -89,7 +84,7 @@ class SpongePlatform extends AbstractPlatform implements MultiUserPlatform {
Collection<org.spongepowered.api.world.World> worlds = Sponge.getServer().getWorlds();
List<com.sk89q.worldedit.world.World> ret = new ArrayList<>(worlds.size());
for (org.spongepowered.api.world.World world : worlds) {
ret.add(new SpongeNMSWorld(world));
ret.add(SpongeWorldEdit.inst().getAdapter().getWorld(world));
}
return ret;
}
@ -113,7 +108,7 @@ class SpongePlatform extends AbstractPlatform implements MultiUserPlatform {
} else {
for (org.spongepowered.api.world.World ws : Sponge.getServer().getWorlds()) {
if (ws.getName().equals(world.getName())) {
return new SpongeNMSWorld(ws);
return SpongeWorldEdit.inst().getAdapter().getWorld(ws);
}
}

View File

@ -29,7 +29,6 @@ import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.internal.LocalWorldAdapter;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.sponge.nms.IDHelper;
import com.sk89q.worldedit.util.Location;
import org.spongepowered.api.data.type.HandTypes;
import org.spongepowered.api.entity.living.player.Player;
@ -62,7 +61,7 @@ public class SpongePlayer extends AbstractPlayerActor {
@Override
public int getItemInHand() {
Optional<ItemStack> is = this.player.getItemInHand(HandTypes.MAIN_HAND);
return is.isPresent() ? IDHelper.resolve(is.get().getItem()) : 0;
return is.isPresent() ? SpongeWorldEdit.inst().getAdapter().resolve(is.get().getItem()) : 0;
}
@Override
@ -106,7 +105,7 @@ public class SpongePlayer extends AbstractPlayerActor {
@Override
public void giveItem(int type, int amt) {
this.player.getInventory().offer(ItemStack.of(IDHelper.resolveItem(type), amt));
this.player.getInventory().offer(ItemStack.of(SpongeWorldEdit.inst().getAdapter().resolveItem(type), amt));
}
@Override
@ -160,7 +159,7 @@ public class SpongePlayer extends AbstractPlayerActor {
@Override
public String[] getGroups() {
return new String[]{}; // WorldEditMod.inst.getPermissionsResolver().getGroups(this.player.username);
return SpongeWorldEdit.inst().getPermissionsProvider().getGroups(this.player);
}
@Override

View File

@ -30,7 +30,6 @@ import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.sponge.nms.IDHelper;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.AbstractWorld;
import com.sk89q.worldedit.world.biome.BaseBiome;
@ -142,10 +141,7 @@ public abstract class SpongeWorld extends AbstractWorld {
// Create the TileEntity
if (block.hasNbtData()) {
// Kill the old TileEntity
Optional<TileEntity> optTile = world.getTileEntity(pos);
if (optTile.isPresent()) {
applyTileEntityData(optTile.get(), block);
}
world.getTileEntity(pos).ifPresent(tileEntity -> applyTileEntityData(tileEntity, block));
}
return true;
@ -177,7 +173,7 @@ public abstract class SpongeWorld extends AbstractWorld {
@Override
public BaseBiome getBiome(Vector2D position) {
checkNotNull(position);
return new BaseBiome(IDHelper.resolve(getWorld().getBiome(position.getBlockX(), 0, position.getBlockZ())));
return new BaseBiome(SpongeWorldEdit.inst().getAdapter().resolve(getWorld().getBiome(position.getBlockX(), 0, position.getBlockZ())));
}
@Override
@ -185,7 +181,7 @@ public abstract class SpongeWorld extends AbstractWorld {
checkNotNull(position);
checkNotNull(biome);
getWorld().setBiome(position.getBlockX(), 0, position.getBlockZ(), IDHelper.resolveBiome(biome.getId()));
getWorld().setBiome(position.getBlockX(), 0, position.getBlockZ(), SpongeWorldEdit.inst().getAdapter().resolveBiome(biome.getId()));
return true;
}
@ -214,7 +210,7 @@ public abstract class SpongeWorld extends AbstractWorld {
@Override
public boolean isValidBlockType(int id) {
return (id == 0) || (IDHelper.resolveBlock(id) != null);
return id == 0 || SpongeWorldEdit.inst().getAdapter().resolveBlock(id) != null;
}
@Override
@ -231,10 +227,9 @@ public abstract class SpongeWorld extends AbstractWorld {
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;
return o instanceof com.sk89q.worldedit.world.World
&& ((com.sk89q.worldedit.world.World) o).getName().equals(getName());
}
}

View File

@ -26,11 +26,13 @@ import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.internal.LocalWorldAdapter;
import com.sk89q.worldedit.sponge.adapter.AdapterLoadException;
import com.sk89q.worldedit.sponge.adapter.SpongeImplAdapter;
import com.sk89q.worldedit.sponge.adapter.SpongeImplLoader;
import com.sk89q.worldedit.sponge.config.SpongeConfiguration;
import com.sk89q.worldedit.sponge.nms.NMSHelper;
import com.sk89q.worldedit.sponge.nms.SpongeNMSWorld;
import org.slf4j.Logger;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.block.BlockType;
@ -50,6 +52,7 @@ import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@ -84,6 +87,7 @@ public class SpongeWorldEdit {
}
private SpongePlatform platform;
private SpongeImplAdapter spongeAdapter;
@Inject
private SpongeConfiguration config;
@ -134,6 +138,44 @@ public class SpongeWorldEdit {
@Listener
public void serverStarted(GameStartedServerEvent event) {
WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent());
loadAdapter();
}
private void loadAdapter() {
WorldEdit worldEdit = WorldEdit.getInstance();
// Attempt to load a Sponge adapter
SpongeImplLoader adapterLoader = new SpongeImplLoader();
try {
adapterLoader.addFromPath(getClass().getClassLoader());
} catch (IOException e) {
logger.warn("Failed to search path for Sponge adapters");
}
try {
adapterLoader.addFromJar(container.getSource().get().toFile());
} catch (IOException e) {
logger.warn("Failed to search " + container.getSource().get().toFile() + " for Sponge adapters", e);
}
try {
spongeAdapter = adapterLoader.loadAdapter();
logger.info("Using " + spongeAdapter.getClass().getCanonicalName() + " as the Sponge adapter");
} catch (AdapterLoadException e) {
Platform platform = worldEdit.getPlatformManager().queryCapability(Capability.WORLD_EDITING);
if (platform instanceof SpongePlatform) {
logger.warn(e.getMessage());
} else {
logger.info("WorldEdit could not find a Sponge adapter for this MC version, " +
"but it seems that you have another implementation of WorldEdit installed (" + platform.getPlatformName() + ") " +
"that handles the world editing.");
}
}
}
public SpongeImplAdapter getAdapter() {
return this.spongeAdapter;
}
@Listener
@ -199,7 +241,7 @@ public class SpongeWorldEdit {
}
public static ItemStack toSpongeItemStack(BaseItemStack item) {
return NMSHelper.makeSpongeStack(item);
return inst().getAdapter().makeSpongeStack(item);
}
/**
@ -249,7 +291,7 @@ public class SpongeWorldEdit {
*/
public SpongeWorld getWorld(World world) {
checkNotNull(world);
return new SpongeNMSWorld(world);
return getAdapter().getWorld(world);
}
/**

View File

@ -17,25 +17,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.sponge.nms;
package com.sk89q.worldedit.sponge.adapter;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.TileEntityBlock;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
/**
* Thrown when no adapter can be found.
*/
public class AdapterLoadException extends Exception {
@Deprecated
public class TileEntityBaseBlock extends BaseBlock implements TileEntityBlock {
public TileEntityBaseBlock(int type, int data, TileEntity tile) {
super(type, data);
setNbtData(NBTConverter.fromNative(copyNbtData(tile)));
public AdapterLoadException() {
}
private static NBTTagCompound copyNbtData(TileEntity tile) {
NBTTagCompound tag = new NBTTagCompound();
tile.writeToNBT(tag);
return tag;
public AdapterLoadException(String message) {
super(message);
}
}
public AdapterLoadException(String message, Throwable cause) {
super(message, cause);
}
public AdapterLoadException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,105 @@
package com.sk89q.worldedit.sponge.adapter;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.sponge.SpongeWorld;
import net.minecraft.nbt.*;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.biome.BiomeType;
/**
* An interface for various things that can't be done through the Sponge API.
*/
public interface SpongeImplAdapter {
/**
* Resolves the numerical ID from this {@link ItemType}
*
* @param type The itemtype
* @return The numerical ID
*/
int resolve(ItemType type);
/**
* Resolves the numerical ID from this {@link BlockType}
*
* @param type The blocktype
* @return The numerical ID
*/
int resolve(BlockType type);
/**
* Resolves the numerical ID from this {@link BiomeType}
*
* @param type The biometype
* @return The numerical ID
*/
int resolve(BiomeType type);
ItemType resolveItem(int intID);
BlockType resolveBlock(int intID);
BiomeType resolveBiome(int intID);
NBTBase toNative(Tag tag);
NBTTagIntArray toNative(IntArrayTag tag);
NBTTagList toNative(ListTag tag);
NBTTagLong toNative(LongTag tag);
NBTTagString toNative(StringTag tag);
NBTTagInt toNative(IntTag tag);
NBTTagByte toNative(ByteTag tag);
NBTTagByteArray toNative(ByteArrayTag tag);
NBTTagCompound toNative(CompoundTag tag);
NBTTagFloat toNative(FloatTag tag);
NBTTagShort toNative(ShortTag tag);
NBTTagDouble toNative(DoubleTag tag);
Tag fromNative(NBTBase other);
IntArrayTag fromNative(NBTTagIntArray other);
ListTag fromNative(NBTTagList other);
EndTag fromNative(NBTTagEnd other);
LongTag fromNative(NBTTagLong other);
StringTag fromNative(NBTTagString other);
IntTag fromNative(NBTTagInt other);
ByteTag fromNative(NBTTagByte other);
ByteArrayTag fromNative(NBTTagByteArray other);
CompoundTag fromNative(NBTTagCompound other);
FloatTag fromNative(NBTTagFloat other);
ShortTag fromNative(NBTTagShort other);
DoubleTag fromNative(NBTTagDouble other);
BaseEntity createBaseEntity(Entity entity);
ItemStack makeSpongeStack(BaseItemStack itemStack);
SpongeWorld getWorld(World world);
}

View File

@ -0,0 +1,157 @@
package com.sk89q.worldedit.sponge.adapter;
import com.sk89q.worldedit.util.io.Closer;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Loads Sponge implementation adapters.
*/
public class SpongeImplLoader {
private static final Logger log = Logger.getLogger(SpongeImplLoader.class.getCanonicalName());
private final List<String> adapterCandidates = new ArrayList<>();
private String customCandidate;
private static final String SEARCH_PACKAGE = "com.sk89q.worldedit.sponge.adapter.impl";
private static final String SEARCH_PACKAGE_DOT = SEARCH_PACKAGE + ".";
private static final String SEARCH_PATH = SEARCH_PACKAGE.replace(".", "/");
private static final String CLASS_SUFFIX = ".class";
private static final String LOAD_ERROR_MESSAGE =
"\n**********************************************\n" +
"** This WorldEdit version does not support your version of Sponge.\n" +
"** WorldEdit will not function! \n" +
"** \n" +
"** Please ensure you are running the latest version\n" +
"**********************************************\n";
/**
* Create a new instance.
*/
public SpongeImplLoader() {
addDefaults();
}
/**
* Add default candidates, such as any defined with
* {@code -Dworldedit.sponge.adapter}.
*/
private void addDefaults() {
String className = System.getProperty("worldedit.sponge.adapter");
if (className != null) {
customCandidate = className;
adapterCandidates.add(className);
log.log(Level.INFO, "-Dworldedit.sponge.adapter used to add " + className + " to the list of available Sponge adapters");
}
}
/**
* Search the given JAR for candidate implementations.
*
* @param file the file
* @throws IOException thrown on I/O error
*/
public void addFromJar(File file) throws IOException {
Closer closer = Closer.create();
JarFile jar = closer.register(new JarFile(file));
try {
Enumeration entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) entries.nextElement();
String className = jarEntry.getName().replaceAll("[/\\\\]+", ".");
if (!className.startsWith(SEARCH_PACKAGE_DOT) || jarEntry.isDirectory() || className.contains("$")) continue;
int beginIndex = 0;
int endIndex = className.length() - CLASS_SUFFIX.length();
className = className.substring(beginIndex, endIndex);
adapterCandidates.add(className);
}
} finally {
closer.close();
}
}
/**
* Search for classes stored as separate files available via the given
* class loader.
*
* @param classLoader the class loader
* @throws IOException thrown on error
*/
public void addFromPath(ClassLoader classLoader) throws IOException {
Enumeration<URL> resources = classLoader.getResources(SEARCH_PATH);
while (resources.hasMoreElements()) {
File file = new File(resources.nextElement().getFile());
addFromPath(file);
}
}
/**
* Search for classes stored as separate files available via the given
* path.
*
* @param file the path
*/
private void addFromPath(File file) {
String resource = SEARCH_PACKAGE_DOT + file.getName();
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File child : files) {
addFromPath(child);
}
}
} else if (resource.endsWith(CLASS_SUFFIX)) {
int beginIndex = 0;
int endIndex = resource.length() - CLASS_SUFFIX.length();
String className = resource.substring(beginIndex, endIndex);
if (!className.contains("$")) {
adapterCandidates.add(className);
}
}
}
/**
* Iterate through the list of candidates and load an adapter.
*
* @return an adapter
* @throws AdapterLoadException thrown if no adapter could be found
*/
public SpongeImplAdapter loadAdapter() throws AdapterLoadException {
for (String className : adapterCandidates) {
try {
Class<?> cls = Class.forName(className);
if (SpongeImplAdapter.class.isAssignableFrom(cls)) {
return (SpongeImplAdapter) cls.newInstance();
} else {
log.log(Level.WARNING, "Failed to load the Sponge adapter class '" + className +
"' because it does not implement " + SpongeImplAdapter.class.getCanonicalName());
}
} catch (ClassNotFoundException e) {
log.log(Level.WARNING, "Failed to load the Sponge adapter class '" + className +
"' that is not supposed to be missing", e);
} catch (IllegalAccessException e) {
log.log(Level.WARNING, "Failed to load the Sponge adapter class '" + className +
"' that is not supposed to be raising this error", e);
} catch (Throwable e) {
if (className.equals(customCandidate)) {
log.log(Level.WARNING, "Failed to load the Sponge adapter class '" + className + "'", e);
}
}
}
throw new AdapterLoadException(LOAD_ERROR_MESSAGE);
}
}

View File

@ -1,57 +0,0 @@
/*
* 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.nms;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.world.biome.Biome;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.world.biome.BiomeType;
@Deprecated
public final class IDHelper {
private IDHelper() { }
public static int resolve(ItemType type) {
return Item.getIdFromItem((Item) type);
}
public static int resolve(BlockType type) {
return Block.getIdFromBlock((Block) type);
}
public static int resolve(BiomeType type) {
return Biome.getIdForBiome((Biome) type);
}
public static ItemType resolveItem(int intID) {
return (ItemType) Item.getItemById(intID);
}
public static BlockType resolveBlock(int intID) {
return (BlockType) Block.getBlockById(intID);
}
public static BiomeType resolveBiome(int intID) {
return (BiomeType) Biome.getBiome(intID);
}
}

View File

@ -1,237 +0,0 @@
/*
* 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.nms;
import com.sk89q.jnbt.*;
import net.minecraft.nbt.*;
import java.util.*;
import java.util.Map.Entry;
/**
* Converts between JNBT and Minecraft NBT classes.
*/
@Deprecated
final class NBTConverter {
private NBTConverter() {
}
public static NBTBase toNative(Tag tag) {
if (tag instanceof IntArrayTag) {
return toNative((IntArrayTag) tag);
} else if (tag instanceof ListTag) {
return toNative((ListTag) tag);
} else if (tag instanceof LongTag) {
return toNative((LongTag) tag);
} else if (tag instanceof StringTag) {
return toNative((StringTag) tag);
} else if (tag instanceof IntTag) {
return toNative((IntTag) tag);
} else if (tag instanceof ByteTag) {
return toNative((ByteTag) tag);
} else if (tag instanceof ByteArrayTag) {
return toNative((ByteArrayTag) tag);
} else if (tag instanceof CompoundTag) {
return toNative((CompoundTag) tag);
} else if (tag instanceof FloatTag) {
return toNative((FloatTag) tag);
} else if (tag instanceof ShortTag) {
return toNative((ShortTag) tag);
} else if (tag instanceof DoubleTag) {
return toNative((DoubleTag) tag);
} else {
throw new IllegalArgumentException("Can't convert tag of type " + tag.getClass().getCanonicalName());
}
}
public static NBTTagIntArray toNative(IntArrayTag tag) {
int[] value = tag.getValue();
return new NBTTagIntArray(Arrays.copyOf(value, value.length));
}
public static NBTTagList toNative(ListTag tag) {
NBTTagList list = new NBTTagList();
for (Tag child : tag.getValue()) {
if (child instanceof EndTag) {
continue;
}
list.appendTag(toNative(child));
}
return list;
}
public static NBTTagLong toNative(LongTag tag) {
return new NBTTagLong(tag.getValue());
}
public static NBTTagString toNative(StringTag tag) {
return new NBTTagString(tag.getValue());
}
public static NBTTagInt toNative(IntTag tag) {
return new NBTTagInt(tag.getValue());
}
public static NBTTagByte toNative(ByteTag tag) {
return new NBTTagByte(tag.getValue());
}
public static NBTTagByteArray toNative(ByteArrayTag tag) {
byte[] value = tag.getValue();
return new NBTTagByteArray(Arrays.copyOf(value, value.length));
}
public static NBTTagCompound toNative(CompoundTag tag) {
NBTTagCompound compound = new NBTTagCompound();
for (Entry<String, Tag> child : tag.getValue().entrySet()) {
compound.setTag(child.getKey(), toNative(child.getValue()));
}
return compound;
}
public static NBTTagFloat toNative(FloatTag tag) {
return new NBTTagFloat(tag.getValue());
}
public static NBTTagShort toNative(ShortTag tag) {
return new NBTTagShort(tag.getValue());
}
public static NBTTagDouble toNative(DoubleTag tag) {
return new NBTTagDouble(tag.getValue());
}
public static Tag fromNative(NBTBase other) {
if (other instanceof NBTTagIntArray) {
return fromNative((NBTTagIntArray) other);
} else if (other instanceof NBTTagList) {
return fromNative((NBTTagList) other);
} else if (other instanceof NBTTagEnd) {
return fromNative((NBTTagEnd) other);
} else if (other instanceof NBTTagLong) {
return fromNative((NBTTagLong) other);
} else if (other instanceof NBTTagString) {
return fromNative((NBTTagString) other);
} else if (other instanceof NBTTagInt) {
return fromNative((NBTTagInt) other);
} else if (other instanceof NBTTagByte) {
return fromNative((NBTTagByte) other);
} else if (other instanceof NBTTagByteArray) {
return fromNative((NBTTagByteArray) other);
} else if (other instanceof NBTTagCompound) {
return fromNative((NBTTagCompound) other);
} else if (other instanceof NBTTagFloat) {
return fromNative((NBTTagFloat) other);
} else if (other instanceof NBTTagShort) {
return fromNative((NBTTagShort) other);
} else if (other instanceof NBTTagDouble) {
return fromNative((NBTTagDouble) other);
} else {
throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName());
}
}
public static IntArrayTag fromNative(NBTTagIntArray other) {
int[] value = other.getIntArray();
return new IntArrayTag(Arrays.copyOf(value, value.length));
}
public static ListTag fromNative(NBTTagList other) {
other = (NBTTagList) other.copy();
List<Tag> list = new ArrayList<Tag>();
Class<? extends Tag> listClass = StringTag.class;
int tags = other.tagCount();
for (int i = 0; i < tags; i++) {
Tag child = fromNative(other.removeTag(0));
list.add(child);
listClass = child.getClass();
}
return new ListTag(listClass, list);
}
public static EndTag fromNative(NBTTagEnd other) {
return new EndTag();
}
public static LongTag fromNative(NBTTagLong other) {
return new LongTag(other.getLong());
}
public static StringTag fromNative(NBTTagString other) {
return new StringTag(other.getString());
}
public static IntTag fromNative(NBTTagInt other) {
return new IntTag(other.getInt());
}
public static ByteTag fromNative(NBTTagByte other) {
return new ByteTag(other.getByte());
}
public static ByteArrayTag fromNative(NBTTagByteArray other) {
byte[] value = other.getByteArray();
return new ByteArrayTag(Arrays.copyOf(value, value.length));
}
public static CompoundTag fromNative(NBTTagCompound other) {
@SuppressWarnings("unchecked") Set<String> tags = other.getKeySet();
Map<String, Tag> map = new HashMap<String, Tag>();
for (String tagName : tags) {
map.put(tagName, fromNative(other.getTag(tagName)));
}
return new CompoundTag(map);
}
public static FloatTag fromNative(NBTTagFloat other) {
return new FloatTag(other.getFloat());
}
public static ShortTag fromNative(NBTTagShort other) {
return new ShortTag(other.getShort());
}
public static DoubleTag fromNative(NBTTagDouble other) {
return new DoubleTag(other.getDouble());
}
}

View File

@ -1,50 +0,0 @@
/*
* 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.nms;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import net.minecraft.item.Item;
import net.minecraft.nbt.NBTTagCompound;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.item.inventory.ItemStack;
import java.util.Map;
@Deprecated
public final class NMSHelper {
private NMSHelper() { }
public static ItemStack makeSpongeStack(BaseItemStack itemStack) {
net.minecraft.item.ItemStack newStack = new net.minecraft.item.ItemStack(Item.getItemById(itemStack.getType()), itemStack.getAmount(), itemStack.getData());
for (Map.Entry<Integer, Integer> entry : itemStack.getEnchantments().entrySet()) {
newStack.addEnchantment(net.minecraft.enchantment.Enchantment.getEnchantmentByID(entry.getKey()), entry.getValue());
}
return (ItemStack) (Object) newStack;
}
public static BaseEntity createBaseEntity(Entity entity) {
String id = entity.getType().getId();
NBTTagCompound tag = new NBTTagCompound();
((net.minecraft.entity.Entity) entity).writeToNBT(tag);
return new BaseEntity(id, NBTConverter.fromNative(tag));
}
}

View File

@ -1,169 +0,0 @@
/*
* 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.nms;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.LazyBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.sponge.SpongeWorld;
import com.sk89q.worldedit.util.TreeGenerator;
import net.minecraft.block.*;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.gen.feature.*;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
@Deprecated
public class SpongeNMSWorld extends SpongeWorld {
private static final IBlockState JUNGLE_LOG = Blocks.LOG.getDefaultState().withProperty(BlockOldLog.VARIANT, BlockPlanks.EnumType.JUNGLE);
private static final IBlockState JUNGLE_LEAF = Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.JUNGLE).withProperty(BlockLeaves.CHECK_DECAY, Boolean.valueOf(false));
private static final IBlockState JUNGLE_SHRUB = Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.OAK).withProperty(BlockLeaves.CHECK_DECAY, Boolean.valueOf(false));
/**
* Construct a new world.
*
* @param world the world
*/
public SpongeNMSWorld(World world) {
super(world);
}
@Override
protected BlockState getBlockState(BaseBlock block) {
return (BlockState) Block.getBlockById(block.getId()).getStateFromMeta(block.getData());
}
private NBTTagCompound updateForSet(NBTTagCompound tag, Vector position) {
checkNotNull(tag);
checkNotNull(position);
tag.setTag("x", new NBTTagInt(position.getBlockX()));
tag.setTag("y", new NBTTagInt(position.getBlockY()));
tag.setTag("z", new NBTTagInt(position.getBlockZ()));
return tag;
}
@Override
protected void applyTileEntityData(org.spongepowered.api.block.tileentity.TileEntity entity, BaseBlock block) {
NBTTagCompound tag = NBTConverter.toNative(block.getNbtData());
Location<World> loc = entity.getLocation();
updateForSet(tag, new Vector(loc.getX(), loc.getY(), loc.getZ()));
((TileEntity) entity).readFromNBT(tag);
}
@Override
protected void applyEntityData(Entity entity, BaseEntity data) {
NBTTagCompound tag = NBTConverter.toNative(data.getNbtData());
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.removeTag(name);
}
((net.minecraft.entity.Entity) entity).readFromNBT(tag);
}
@Override
public boolean clearContainerBlockContents(Vector position) {
BlockPos pos = new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ());
TileEntity tile =((net.minecraft.world.World) getWorld()).getTileEntity(pos);
if (tile instanceof IInventory) {
IInventory inv = (IInventory) tile;
int size = inv.getSizeInventory();
for (int i = 0; i < size; i++) {
inv.setInventorySlotContents(i, null);
}
return true;
}
return false;
}
@Nullable
private static WorldGenerator createWorldGenerator(TreeGenerator.TreeType type) {
switch (type) {
case TREE: return new WorldGenTrees(true);
case BIG_TREE: return new WorldGenBigTree(true);
case REDWOOD: return new WorldGenTaiga2(true);
case TALL_REDWOOD: return new WorldGenTaiga1();
case BIRCH: return new WorldGenBirchTree(true, false);
case JUNGLE: return new WorldGenMegaJungle(true, 10, 20, JUNGLE_LOG, JUNGLE_LEAF);
case SMALL_JUNGLE: return new WorldGenTrees(true, 4 + random.nextInt(7), JUNGLE_LOG, JUNGLE_LEAF, false);
case SHORT_JUNGLE: return new WorldGenTrees(true, 4 + random.nextInt(7), JUNGLE_LOG, JUNGLE_LEAF, true);
case JUNGLE_BUSH: return new WorldGenShrub(JUNGLE_LOG, JUNGLE_SHRUB);
case RED_MUSHROOM: return new WorldGenBigMushroom(Blocks.BROWN_MUSHROOM_BLOCK);
case BROWN_MUSHROOM: return new WorldGenBigMushroom(Blocks.RED_MUSHROOM_BLOCK);
case SWAMP: return new WorldGenSwamp();
case ACACIA: return new WorldGenSavannaTree(true);
case DARK_OAK: return new WorldGenCanopyTree(true);
case MEGA_REDWOOD: return new WorldGenMegaPineTree(false, random.nextBoolean());
case TALL_BIRCH: return new WorldGenBirchTree(true, true);
case RANDOM:
case PINE:
case RANDOM_REDWOOD:
default:
return null;
}
}
@Override
public boolean generateTree(TreeGenerator.TreeType type, EditSession editSession, Vector pos) throws MaxChangedBlocksException {
WorldGenerator generator = createWorldGenerator(type);
return generator != null && generator.generate((net.minecraft.world.World) getWorld(), random, new BlockPos(pos.getX(), pos.getY(), pos.getZ()));
}
@Override
public BaseBlock getBlock(Vector position) {
World world = getWorld();
BlockPos pos = new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ());
IBlockState state = ((net.minecraft.world.World) world).getBlockState(pos);
TileEntity tile = ((net.minecraft.world.World) world).getTileEntity(pos);
if (tile != null) {
return new TileEntityBaseBlock(Block.getIdFromBlock(state.getBlock()), state.getBlock().getMetaFromState(state), tile);
} else {
return new BaseBlock(Block.getIdFromBlock(state.getBlock()), state.getBlock().getMetaFromState(state));
}
}
@Override
public BaseBlock getLazyBlock(Vector position) {
World world = getWorld();
BlockPos pos = new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ());
IBlockState state = ((net.minecraft.world.World) world).getBlockState(pos);
return new LazyBlock(Block.getIdFromBlock(state.getBlock()), state.getBlock().getMetaFromState(state), this, position);
}
}

View File

@ -1,145 +0,0 @@
/*
* 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.nms;
import com.sk89q.worldedit.Vector;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Utility methods for setting tile entities in the world.
*/
@Deprecated
final class TileEntityUtils {
private TileEntityUtils() {
}
/**
* Update the given tag compound with position information.
*
* @param tag the tag
* @param position the position
* @return a tag compound
*/
private static NBTTagCompound updateForSet(NBTTagCompound tag, Vector position) {
checkNotNull(tag);
checkNotNull(position);
tag.setTag("x", new NBTTagInt(position.getBlockX()));
tag.setTag("y", new NBTTagInt(position.getBlockY()));
tag.setTag("z", new NBTTagInt(position.getBlockZ()));
return tag;
}
/**
* Set a tile entity at the given location.
*
* @param world the world
* @param position the position
* @param clazz the tile entity class
* @param tag the tag for the tile entity (may be null to not set NBT data)
*/
static void setTileEntity(World world, Vector position, Class<? extends TileEntity> clazz, @Nullable NBTTagCompound tag) {
checkNotNull(world);
checkNotNull(position);
checkNotNull(clazz);
TileEntity tileEntity = constructTileEntity(world, position, clazz);
if (tileEntity == null) {
return;
}
if (tag != null) {
// Set X, Y, Z
updateForSet(tag, position);
tileEntity.readFromNBT(tag);
}
world.setTileEntity(new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ()), tileEntity);
}
/**
* Set a tile entity at the given location using the tile entity ID from
* the tag.
*
* @param world the world
* @param position the position
* @param tag the tag for the tile entity (may be null to do nothing)
*/
static void setTileEntity(World world, Vector position, @Nullable NBTTagCompound tag) {
if (tag != null) {
updateForSet(tag, position);
TileEntity tileEntity = TileEntity.create(world, tag);
if (tileEntity != null) {
world.setTileEntity(new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ()), tileEntity);
}
}
}
/**
* Construct a tile entity from the given class.
*
* @param world the world
* @param position the position
* @param clazz the class
* @return a tile entity (may be null if it failed)
*/
@Nullable
static TileEntity constructTileEntity(World world, Vector position, Class<? extends TileEntity> clazz) {
Constructor<? extends TileEntity> baseConstructor;
try {
baseConstructor = clazz.getConstructor(); // creates "blank" TE
} catch (Throwable e) {
return null; // every TE *should* have this constructor, so this isn't necessary
}
TileEntity genericTE;
try {
// Downcast here for return while retaining the type
genericTE = (TileEntity) baseConstructor.newInstance();
} catch (Throwable e) {
return null;
}
/*
genericTE.blockType = Block.blocksList[block.getId()];
genericTE.blockMetadata = block.getData();
genericTE.xCoord = pt.getBlockX();
genericTE.yCoord = pt.getBlockY();
genericTE.zCoord = pt.getBlockZ();
genericTE.worldObj = world;
*/ // handled by internal code
return genericTE;
}
}