Skip to content

Commit

Permalink
Get it to a point where it works minimally on 1.13 Spigot.
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Aug 4, 2018
1 parent 59ca295 commit 3e1d438
Show file tree
Hide file tree
Showing 16 changed files with 2,751 additions and 70 deletions.
4 changes: 2 additions & 2 deletions worldedit-bukkit/build.gradle
Expand Up @@ -10,7 +10,7 @@ repositories {
dependencies {
compile project(':worldedit-core')
compile 'com.sk89q:dummypermscompat:1.8'
compile 'org.bukkit:bukkit:1.13-pre5-R0.1-SNAPSHOT' // zzz
compile 'org.bukkit:bukkit:1.13-pre7-R0.1-SNAPSHOT' // zzz
// compile 'org.bukkit:bukkit:1.9.4-R0.1-SNAPSHOT' // zzz
testCompile 'org.mockito:mockito-core:1.9.0-rc1'
}
Expand All @@ -36,7 +36,7 @@ jar {
shadowJar {
dependencies {
include(dependency(':worldedit-core'))
include(dependency('com.google.code.gson:gson:2.2.4'))
include(dependency('com.google.code.gson:gson'))
}

relocate('com.google.gson', 'com.sk89q.worldedit.internal.gson')
Expand Down
@@ -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();
}
}
}
Expand Up @@ -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;

/**
Expand All @@ -28,6 +29,7 @@
class BukkitRegistries extends BundledRegistries {

private static final BukkitRegistries INSTANCE = new BukkitRegistries();
private final BlockRegistry blockRegistry = new BukkitBlockRegistry();
private final BiomeRegistry biomeRegistry = new BukkitBiomeRegistry();

/**
Expand All @@ -36,6 +38,11 @@ class BukkitRegistries extends BundledRegistries {
BukkitRegistries() {
}

@Override
public BlockRegistry getBlockRegistry() {
return blockRegistry;
}

@Override
public BiomeRegistry getBiomeRegistry() {
return biomeRegistry;
Expand Down
Expand Up @@ -21,14 +21,18 @@

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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -130,12 +140,31 @@ public static World toWorld(final Extent world) {
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 {
Expand All @@ -151,7 +180,6 @@ public static BaseItemStack toBaseItemStack(ItemStack itemStack) {
}

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());
}
}
Expand Up @@ -284,10 +284,8 @@ public static TreeType toBukkitTreeType(TreeGenerator.TreeType type) {
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
Expand Down Expand Up @@ -368,7 +366,7 @@ public boolean setBlock(Vector position, BlockStateHolder block, boolean notifyA
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;
}
}
Expand Down
Expand Up @@ -20,62 +20,46 @@
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;

public EditSessionBlockChangeDelegate(EditSession editSession) {
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 BlockData getBlockData(int x, int y, int z) {
return BukkitUtil.toBlock(editSession.getBlock(new Vector(x, y, z)));
}

// @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 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;
}

}
1 change: 1 addition & 0 deletions worldedit-bukkit/src/main/resources/plugin.yml
Expand Up @@ -2,6 +2,7 @@ name: WorldEdit
main: com.sk89q.worldedit.bukkit.WorldEditPlugin
version: "${internalVersion}"
softdepend: [Spout] #hack to fix trove errors
api-version: 1.13

# Permissions aren't here. Read http://wiki.sk89q.com/wiki/WEPIF/DinnerPerms
# for how WorldEdit permissions actually work.
Expand Up @@ -22,6 +22,7 @@
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.util.logging.LogFormat;
import com.sk89q.worldedit.world.registry.LegacyMapper;
import com.sk89q.worldedit.world.snapshot.SnapshotRepository;

import java.io.File;
Expand Down Expand Up @@ -148,4 +149,23 @@ public File getWorkingDirectory() {
return new File(".");
}

public String convertLegacyItem(String legacy) {
String item = legacy;
try {
String[] splitter = item.split(":", 2);
int id = 0;
byte data = 0;
if (splitter.length == 1) {
id = Integer.parseInt(item);
} else {
id = Integer.parseInt(splitter[0]);
data = Byte.parseByte(splitter[1]);
}
item = LegacyMapper.getInstance().getItemFromLegacy(id, data).getId();
} catch (Throwable e) {
}

return item;
}

}
Expand Up @@ -56,11 +56,7 @@ public void load() {
}

profile = config.getBoolean("debug", profile);
wandItem = config.getString("wand-item", wandItem);
try {
wandItem = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(wandItem)).getId();
} catch (Throwable e) {
}
wandItem = convertLegacyItem(config.getString("wand-item", wandItem));

defaultChangeLimit = Math.max(-1, config.getInt(
"limits.max-blocks-changed.default", defaultChangeLimit));
Expand Down Expand Up @@ -104,11 +100,7 @@ public void load() {
useInventoryCreativeOverride = config.getBoolean("use-inventory.creative-mode-overrides",
useInventoryCreativeOverride);

navigationWand = config.getString("navigation-wand.item", navigationWand);
try {
navigationWand = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(navigationWand)).getId();
} catch (Throwable e) {
}
navigationWand = convertLegacyItem(config.getString("navigation-wand.item", navigationWand));
navigationWandMaxDistance = config.getInt("navigation-wand.max-distance", navigationWandMaxDistance);
navigationUseGlass = config.getBoolean("navigation.use-glass", navigationUseGlass);

Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.sk89q.worldedit.registry.state.Property;

import java.util.Map;
import java.util.stream.Collectors;

public interface BlockStateHolder<T extends BlockStateHolder> {

Expand Down Expand Up @@ -70,4 +71,13 @@ public interface BlockStateHolder<T extends BlockStateHolder> {
* @return A BlockState
*/
BlockState toImmutableState();

default String getAsString() {
if (getStates().isEmpty()) {
return this.getBlockType().getId();
} else {
String properties = getStates().entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining(","));
return this.getBlockType().getId() + "[" + properties + "]";
}
}
}

0 comments on commit 3e1d438

Please sign in to comment.