Get it to a point where it works minimally on 1.13 Spigot.

This commit is contained in:
Matthew Miller
2018-07-16 00:21:32 +10:00
parent 59ca29577c
commit 3e1d438565
16 changed files with 2751 additions and 70 deletions

View File

@ -0,0 +1,68 @@
/*
* 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.bukkit;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.registry.BundledBlockRegistry;
import com.sk89q.worldedit.world.registry.PassthroughBlockMaterial;
import org.bukkit.Material;
import java.util.EnumMap;
import java.util.Map;
import javax.annotation.Nullable;
public class BukkitBlockRegistry extends BundledBlockRegistry {
private Map<Material, BukkitBlockMaterial> materialMap = new EnumMap<>(Material.class);
@Nullable
@Override
public BlockMaterial getMaterial(String id) {
return materialMap.computeIfAbsent(BukkitUtil.toMaterial(BlockTypes.get(id)),
material -> new BukkitBlockMaterial(BukkitBlockRegistry.super.getMaterial(id), material));
}
public static class BukkitBlockMaterial extends PassthroughBlockMaterial {
private final Material material;
public BukkitBlockMaterial(@Nullable BlockMaterial material, Material bukkitMaterial) {
super(material);
this.material = bukkitMaterial;
}
@Override
public boolean isSolid() {
return material.isSolid();
}
@Override
public boolean isBurnable() {
return material.isBurnable();
}
@Override
public boolean isTranslucent() {
return material.isTransparent();
}
}
}

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.world.registry.BiomeRegistry;
import com.sk89q.worldedit.world.registry.BlockRegistry;
import com.sk89q.worldedit.world.registry.BundledRegistries;
/**
@ -28,6 +29,7 @@ import com.sk89q.worldedit.world.registry.BundledRegistries;
class BukkitRegistries extends BundledRegistries {
private static final BukkitRegistries INSTANCE = new BukkitRegistries();
private final BlockRegistry blockRegistry = new BukkitBlockRegistry();
private final BiomeRegistry biomeRegistry = new BukkitBiomeRegistry();
/**
@ -36,6 +38,11 @@ class BukkitRegistries extends BundledRegistries {
BukkitRegistries() {
}
@Override
public BlockRegistry getBlockRegistry() {
return blockRegistry;
}
@Override
public BiomeRegistry getBiomeRegistry() {
return biomeRegistry;

View File

@ -21,14 +21,18 @@ package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -48,6 +52,12 @@ public final class BukkitUtil {
private BukkitUtil() {
}
private static final ParserContext TO_BLOCK_CONTEXT = new ParserContext();
static {
TO_BLOCK_CONTEXT.setRestricted(false);
}
public static com.sk89q.worldedit.world.World getWorld(World w) {
return new BukkitWorld(w);
}
@ -130,12 +140,31 @@ public final class BukkitUtil {
return ((BukkitWorld) world).getWorld();
}
public static Material toMaterial(ItemType itemType) {
if (!itemType.getId().startsWith("minecraft:")) {
throw new IllegalArgumentException("Bukkit only supports Minecraft items");
}
return Material.getMaterial(itemType.getId().replace("minecraft:", "").toUpperCase());
}
public static Material toMaterial(BlockType blockType) {
if (!blockType.getId().startsWith("minecraft:")) {
throw new IllegalArgumentException("Bukkit only supports Minecraft blocks");
}
return Material.getMaterial(blockType.getId().replace("minecraft:", "").toUpperCase());
}
public static BlockState toBlock(BlockData blockData) {
return null; // TODO BLOCKING
try {
return WorldEdit.getInstance().getBlockFactory().parseFromInput(blockData.getAsString(), TO_BLOCK_CONTEXT).toImmutableState();
} catch (InputParseException e) {
e.printStackTrace();
}
return null;
}
public static BlockData toBlock(BlockStateHolder block) {
return Bukkit.createBlockData(block.toString()); // TODO BLOCKING
return Bukkit.createBlockData(block.getAsString());
}
public static BlockState toBlock(ItemStack itemStack) throws WorldEditException {
@ -151,7 +180,6 @@ public final class BukkitUtil {
}
public static ItemStack toItemStack(BaseItemStack item) {
BlockData blockData = Bukkit.createBlockData(item.getType().getId());
return new ItemStack(blockData.getMaterial(), item.getAmount());
return new ItemStack(toMaterial(item.getType()), item.getAmount());
}
}

View File

@ -284,10 +284,8 @@ public class BukkitWorld extends AbstractWorld {
public boolean generateTree(TreeGenerator.TreeType type, EditSession editSession, Vector pt) {
World world = getWorld();
TreeType bukkitType = toBukkitTreeType(type);
return type != null && world.generateTree(BukkitUtil.toLocation(world, pt), bukkitType);
// return type != null && world.generateTree(BukkitUtil.toLocation(world, pt), bukkitType,
// new EditSessionBlockChangeDelegate(editSession));
// TODO
return type != null && world.generateTree(BukkitUtil.toLocation(world, pt), bukkitType,
new EditSessionBlockChangeDelegate(editSession));
}
@Override
@ -368,7 +366,7 @@ public class BukkitWorld extends AbstractWorld {
return adapter.setBlock(BukkitAdapter.adapt(getWorld(), position), block, notifyAndLight);
} else {
Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
bukkitBlock.setData(BukkitUtil.toBlock(block), notifyAndLight);
bukkitBlock.setBlockData(BukkitUtil.toBlock(block), notifyAndLight);
return true;
}
}

View File

@ -20,11 +20,16 @@
package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.world.block.BlockTypes;
import org.bukkit.BlockChangeDelegate;
import org.bukkit.block.data.BlockData;
/**
* Proxy class to catch calls to set blocks.
*/
public class EditSessionBlockChangeDelegate {//implements BlockChangeDelegate {
public class EditSessionBlockChangeDelegate implements BlockChangeDelegate {
private EditSession editSession;
@ -32,50 +37,29 @@ public class EditSessionBlockChangeDelegate {//implements BlockChangeDelegate {
this.editSession = editSession;
}
// TODO This needs a fix in Spigot itself
@Override
public boolean setBlockData(int x, int y, int z, BlockData blockData) {
try {
editSession.setBlock(new Vector(x, y, z), BukkitUtil.toBlock(blockData));
} catch (MaxChangedBlocksException e) {
return false;
}
return true;
}
// @Override
// public boolean setRawTypeId(int x, int y, int z, int typeId) {
// try {
// return editSession.setBlock(new Vector(x, y, z), LegacyMapper.getInstance().getBlockFromLegacy(typeId));
// } catch (MaxChangedBlocksException ex) {
// return false;
// }
// }
//
// @Override
// public boolean setRawTypeIdAndData(int x, int y, int z, int typeId, int data) {
// try {
// return editSession.setBlock(new Vector(x, y, z), LegacyMapper.getInstance().getBlockFromLegacy(typeId, data));
// } catch (MaxChangedBlocksException ex) {
// return false;
// }
// }
//
// @Override
// public boolean setTypeId(int x, int y, int z, int typeId) {
// return setRawTypeId(x, y, z, typeId);
// }
//
// @Override
// public boolean setTypeIdAndData(int x, int y, int z, int typeId, int data) {
// return setRawTypeIdAndData(x, y, z, typeId, data);
// }
//
// @Override
// public int getTypeId(int x, int y, int z) {
// int[] datas = LegacyMapper.getInstance().getLegacyFromBlock(editSession.getBlock(new Vector(x, y, z)));
// return datas[0];
// }
//
// @Override
// public int getHeight() {
// return editSession.getWorld().getMaxY() + 1;
// }
//
// @Override
// public boolean isEmpty(int x, int y, int z) {
// return editSession.getBlock(new Vector(x, y, z)).getBlockType() == BlockTypes.AIR;
// }
@Override
public BlockData getBlockData(int x, int y, int z) {
return BukkitUtil.toBlock(editSession.getBlock(new Vector(x, y, z)));
}
@Override
public int getHeight() {
return editSession.getWorld().getMaxY() + 1;
}
@Override
public boolean isEmpty(int x, int y, int z) {
return editSession.getBlock(new Vector(x, y, z)).getBlockType() == BlockTypes.AIR;
}
}