some adapter refactoring

This commit is contained in:
Jesse Boyd
2019-11-19 04:40:40 +00:00
parent 751765cc01
commit 1b07846746
30 changed files with 1855 additions and 4779 deletions

View File

@ -353,10 +353,6 @@ public enum BukkitAdapter {
public static BlockState adapt(@NotNull BlockData blockData) {
return getAdapter().adapt(blockData);
}
public static BlockType adapt(Material material) {
return getAdapter().adapt(material);
}
/*
private static Map<String, BlockData> blockDataCache = new HashMap<>();
*/

View File

@ -127,7 +127,7 @@ public class BukkitBlockRegistry extends BundledBlockRegistry {
}
@Override
public Collection<String> registerBlocks() {
public Collection<String> values() {
ArrayList<String> blocks = new ArrayList<>();
for (Material m : Material.values()) {
if (!m.isLegacy() && m.isBlock()) {

View File

@ -1,6 +1,22 @@
package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.world.registry.BundledItemRegistry;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import java.util.ArrayList;
import java.util.Collection;
public class BukkitItemRegistry extends BundledItemRegistry {
@Override
public Collection<String> values() {
ArrayList<String> values = new ArrayList<>();
for (Material m : Material.values()) {
if (!m.isLegacy() && (m.isBlock() || m.isItem())) {
String id = m.getKey().toString();
values.add(id);
}
}
return values;
}
}

View File

@ -22,6 +22,7 @@ package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.world.registry.BiomeRegistry;
import com.sk89q.worldedit.world.registry.BlockCategoryRegistry;
import com.sk89q.worldedit.world.registry.BlockRegistry;
import com.sk89q.worldedit.world.registry.BundledItemRegistry;
import com.sk89q.worldedit.world.registry.BundledRegistries;
import com.sk89q.worldedit.world.registry.EntityRegistry;
import com.sk89q.worldedit.world.registry.ItemCategoryRegistry;
@ -34,6 +35,7 @@ class BukkitRegistries extends BundledRegistries {
private static final BukkitRegistries INSTANCE = new BukkitRegistries();
private final BlockRegistry blockRegistry = new BukkitBlockRegistry();
private final ItemRegistry itemRegistry = new BukkitItemRegistry();
private final BiomeRegistry biomeRegistry = new BukkitBiomeRegistry();
private final EntityRegistry entityRegistry = new BukkitEntityRegistry();
private final BlockCategoryRegistry blockCategoryRegistry = new BukkitBlockCategoryRegistry();
@ -79,4 +81,8 @@ class BukkitRegistries extends BundledRegistries {
return INSTANCE;
}
@Override
public ItemRegistry getItemRegistry() {
return itemRegistry;
}
}

View File

@ -22,10 +22,9 @@ package com.sk89q.worldedit.bukkit;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.internal.anvil.ChunkDeleter.DELCHUNKS_FILE_NAME;
import com.bekvon.bukkit.residence.commands.message;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.bukkit.FaweBukkit;
import com.boydti.fawe.bukkit.adapter.mc1_14.Spigot_v1_14_R4;
import com.boydti.fawe.bukkit.adapter.mc1_14.FAWE_Spigot_v1_14_R4;
import com.boydti.fawe.util.MainUtil;
import com.google.common.base.Joiner;
import com.sk89q.util.yaml.YAMLProcessor;
@ -50,7 +49,6 @@ import com.sk89q.worldedit.world.block.BlockCategory;
import com.sk89q.worldedit.world.entity.EntityType;
import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldedit.world.item.ItemCategory;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.weather.WeatherTypes;
import io.papermc.lib.PaperLib;
import java.io.File;
@ -84,7 +82,6 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.WorldInitEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
@ -251,9 +248,8 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter
String lowerCaseBiomeName = biome.name().toLowerCase(Locale.ROOT);
BiomeType.REGISTRY.register("minecraft:" + lowerCaseBiomeName, new BiomeType("minecraft:" + lowerCaseBiomeName));
}
// Block & Item
/*// Block & Item
for (Material material : Material.values()) {
/*
if (material.isBlock() && !material.isLegacy()) {
BlockType.REGISTRY.register(material.getKey().toString(), new BlockType(material.getKey().toString(), blockState -> {
// TODO Use something way less hacky than this.
@ -277,11 +273,11 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter
}
}));
}
*/
if (material.isItem() && !material.isLegacy()) {
ItemType.REGISTRY.register(material.getKey().toString(), new ItemType(material.getKey().toString()));
}
}
*/
// Entity
for (org.bukkit.entity.EntityType entityType : org.bukkit.entity.EntityType.values()) {
String mcid = entityType.getName();
@ -371,7 +367,7 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter
// Attempt to load a Bukkit adapter
BukkitImplLoader adapterLoader = new BukkitImplLoader();
try {
adapterLoader.addClass(Spigot_v1_14_R4.class);
adapterLoader.addClass(FAWE_Spigot_v1_14_R4.class);
} catch (Throwable throwable) {
throwable.printStackTrace();
}

View File

@ -26,6 +26,7 @@ import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property;
@ -36,17 +37,24 @@ 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.registry.BlockMaterial;
import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable;
/**
* An interface for adapters of various Bukkit implementations.
*/
@ -67,6 +75,19 @@ public interface BukkitImplAdapter<T> extends IBukkitAdapter {
@Nullable
DataFixer getDataFixer();
/**
* @return {@code true} if {@link #tickWatchdog()} is implemented
*/
default boolean supportsWatchdog() {
return false;
}
/**
* Tick the server watchdog, if possible.
*/
default void tickWatchdog() {
}
/**
* Get the block at the given location.
*
@ -83,11 +104,7 @@ public interface BukkitImplAdapter<T> extends IBukkitAdapter {
* @param notifyAndLight notify and light if set
* @return true if a block was likely changed
*/
default boolean setBlock(Location location, BlockStateHolder<?> state, boolean notifyAndLight) {
return this.setBlock(location.getChunk(), location.getBlockX(), location.getBlockY(), location.getBlockZ(), state, notifyAndLight);
}
boolean setBlock(Chunk chunk, int x, int y, int z, BlockStateHolder<?> state, boolean update);
boolean setBlock(Location location, BlockStateHolder<?> state, boolean notifyAndLight);
/**
* Notifies the simulation that the block at the given location has
@ -142,13 +159,6 @@ public interface BukkitImplAdapter<T> extends IBukkitAdapter {
*/
void sendFakeOP(Player player);
/**
* Send a fake chunk packet to a player
* @param player
* @param packet
*/
void sendFakeChunk(org.bukkit.World world, Player player, ChunkPacket packet);
/**
* Simulates a player using an item.
*
@ -178,10 +188,24 @@ public interface BukkitImplAdapter<T> extends IBukkitAdapter {
*/
BaseItemStack adapt(ItemStack itemStack);
boolean isChunkInUse(Chunk chunk);
default OptionalInt getInternalBlockStateId(BlockData data) {
return getInternalBlockStateId(BukkitAdapter.adapt(data));
}
/**
* Retrieve the internal ID for a given state, if possible.
*
* @param state The block state
* @return the internal ID of the state
*/
default OptionalInt getInternalBlockStateId(BlockState state) {
return OptionalInt.empty();
}
// FAWE ADDITIONS
default BlockMaterial getMaterial(BlockType blockType) {
return null;
return getMaterial(blockType.getDefaultState());
}
default BlockMaterial getMaterial(BlockState blockState) {
@ -201,12 +225,11 @@ public interface BukkitImplAdapter<T> extends IBukkitAdapter {
}
/**
* Retrieve the internal ID for a given state, if possible.
*
* @param state The block state
* @return the internal ID of the state
* Send a fake chunk packet to a player
* @param player
* @param packet
*/
default OptionalInt getInternalBlockStateId(BlockState state) {
return OptionalInt.empty();
default void sendFakeChunk(org.bukkit.World world, Player player, ChunkPacket packet) {
throw new UnsupportedOperationException("Cannot send fake chunks");
}
}
}

View File

@ -155,6 +155,7 @@ public class BukkitImplLoader {
*/
public BukkitImplAdapter loadAdapter() throws AdapterLoadException {
for (String className : adapterCandidates) {
System.out.println("Try load " + className);
try {
Class<?> cls = Class.forName(className);
if (cls.isSynthetic()) continue;
@ -168,6 +169,7 @@ public class BukkitImplLoader {
log.warn("Failed to load the Bukkit adapter class '" + className +
"' that is not supposed to be raising this error", e);
} catch (Throwable e) {
e.printStackTrace();
if (className.equals(customCandidate)) {
log.warn("Failed to load the Bukkit adapter class '" + className + "'", e);
}

View File

@ -57,11 +57,11 @@ public abstract class CachedBukkitAdapter implements IBukkitAdapter {
}
@Override
public BlockType adapt(Material material) {
public BlockType asBlockType(Material material) {
try {
return BlockTypesCache.values[blockTypes[material.ordinal()]];
} catch (NullPointerException e) {
if (init()) return adapt(material);
if (init()) return asBlockType(material);
throw e;
}
}

View File

@ -26,6 +26,8 @@ import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldedit.world.item.ItemType;
import java.util.Locale;
import com.sk89q.worldedit.world.item.ItemTypes;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Biome;
@ -208,7 +210,9 @@ public interface IBukkitAdapter {
* @param material The material
* @return The itemtype
*/
ItemType asItemType(Material material);
default ItemType asItemType(Material material) {
return ItemTypes.get(material.getKey().toString());
}
/**
* Create a WorldEdit BlockStateHolder from a Bukkit BlockData
@ -216,9 +220,10 @@ public interface IBukkitAdapter {
* @param blockData The Bukkit BlockData
* @return The WorldEdit BlockState
*/
BlockState adapt(BlockData blockData);
BlockType adapt(Material material);
default BlockState adapt(BlockData blockData) {
String id = blockData.getAsString();
return BlockState.get(id);
}
/**
* Create a Bukkit BlockData from a WorldEdit BlockStateHolder
@ -226,7 +231,9 @@ public interface IBukkitAdapter {
* @param block The WorldEdit BlockStateHolder
* @return The Bukkit BlockData
*/
BlockData adapt(BlockStateHolder block);
default BlockData adapt(BlockStateHolder block) {
return Bukkit.createBlockData(block.getAsString());
}
/**
* Create a WorldEdit BaseItemStack from a Bukkit ItemStack

View File

@ -0,0 +1,278 @@
package com.sk89q.worldedit.bukkit.adapter;
import com.boydti.fawe.beta.implementation.packet.ChunkPacket;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.bukkit.BukkitPlayer;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.world.DataFixer;
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 com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.OptionalInt;
public interface IDelegateBukkitImplAdapter<T> extends BukkitImplAdapter<T> {
BukkitImplAdapter<T> getParent();
@Override
default int getDataVersion() {
return getParent().getDataVersion();
}
@Override
@Nullable
default DataFixer getDataFixer() {
return getParent().getDataFixer();
}
@Override
default boolean supportsWatchdog() {
return getParent().supportsWatchdog();
}
@Override
default void tickWatchdog() {
getParent().tickWatchdog();
}
@Override
default BaseBlock getBlock(Location location) {
return getParent().getBlock(location);
}
default boolean setBlock(Location location, BlockStateHolder state, boolean notifyAndLight) {
return getParent().setBlock(location, state, notifyAndLight);
}
@Override
default void notifyAndLightBlock(Location position, BlockState previousType) {
getParent().notifyAndLightBlock(position, previousType);
}
@Override
@Nullable
default BaseEntity getEntity(Entity entity) {
return getParent().getEntity(entity);
}
@Override
@Nullable
default Entity createEntity(Location location, BaseEntity state) {
return getParent().createEntity(location, state);
}
@Override
default Map<String, ? extends Property<?>> getProperties(BlockType blockType) {
return getParent().getProperties(blockType);
}
@Override
default void sendFakeNBT(Player player, BlockVector3 pos, CompoundTag nbtData) {
getParent().sendFakeNBT(player, pos, nbtData);
}
@Override
default void sendFakeOP(Player player) {
getParent().sendFakeOP(player);
}
@Override
default boolean simulateItemUse(World world, BlockVector3 position, BaseItem item, Direction face) {
return getParent().simulateItemUse(world, position, item, face);
}
@Override
default ItemStack adapt(BaseItemStack item) {
return getParent().adapt(item);
}
@Override
default BaseItemStack adapt(ItemStack itemStack) {
return getParent().adapt(itemStack);
}
@Override
default OptionalInt getInternalBlockStateId(BlockData data) {
return getParent().getInternalBlockStateId(data);
}
@Override
default OptionalInt getInternalBlockStateId(BlockState state) {
return getParent().getInternalBlockStateId(state);
}
@Override
default BlockMaterial getMaterial(BlockType blockType) {
return getParent().getMaterial(blockType);
}
@Override
default BlockMaterial getMaterial(BlockState blockState) {
return getParent().getMaterial(blockState);
}
default Tag toNative(T foreign) {
return getParent().toNative(foreign);
}
@Override
default T fromNative(Tag foreign) {
return getParent().fromNative(foreign);
}
@Override
@Nullable
default World createWorld(WorldCreator creator) {
return getParent().createWorld(creator);
}
@Override
default void sendFakeChunk(World world, Player player, ChunkPacket packet) {
getParent().sendFakeChunk(world, player, packet);
}
@Override
default BukkitWorld asBukkitWorld(com.sk89q.worldedit.world.World world) {
return getParent().asBukkitWorld(world);
}
@Override
default World adapt(com.sk89q.worldedit.world.World world) {
return getParent().adapt(world);
}
@Override
default Location adapt(World world, Vector3 position) {
return getParent().adapt(world, position);
}
@Override
default Location adapt(World world, BlockVector3 position) {
return getParent().adapt(world, position);
}
@Override
default Location adapt(World world, com.sk89q.worldedit.util.Location location) {
return getParent().adapt(world, location);
}
@Override
default Vector3 asVector(Location location) {
return getParent().asVector(location);
}
@Override
default BlockVector3 asBlockVector(Location location) {
return getParent().asBlockVector(location);
}
@Override
default com.sk89q.worldedit.entity.Entity adapt(Entity entity) {
return getParent().adapt(entity);
}
@Override
default Material adapt(ItemType itemType) {
return getParent().adapt(itemType);
}
@Override
default Material adapt(BlockType blockType) {
return getParent().adapt(blockType);
}
@Override
default EntityType adapt(com.sk89q.worldedit.world.entity.EntityType entityType) {
return getParent().adapt(entityType);
}
@Override
default BlockType asBlockType(Material material) {
return getParent().asBlockType(material);
}
@Override
default ItemType asItemType(Material material) {
return getParent().asItemType(material);
}
@Override
default BlockState adapt(BlockData blockData) {
return getParent().adapt(blockData);
}
@Override
default BlockData adapt(BlockStateHolder block) {
return getParent().adapt(block);
}
@Override
default BukkitPlayer adapt(Player player) {
return getParent().adapt(player);
}
@Override
default Player adapt(com.sk89q.worldedit.entity.Player player) {
return getParent().adapt(player);
}
@Override
default Biome adapt(BiomeType biomeType) {
return getParent().adapt(biomeType);
}
@Override
default BiomeType adapt(Biome biome) {
return getParent().adapt(biome);
}
@Override
default boolean equals(BlockType blockType, Material type) {
return getParent().equals(blockType, type);
}
@Override
default com.sk89q.worldedit.world.World adapt(World world) {
return getParent().adapt(world);
}
@Override
default GameMode adapt(org.bukkit.GameMode gameMode) {
return getParent().adapt(gameMode);
}
@Override
default com.sk89q.worldedit.world.entity.EntityType adapt(EntityType entityType) {
return getParent().adapt(entityType);
}
@Override
default BlockState asBlockState(ItemStack itemStack) {
return getParent().asBlockState(itemStack);
}
}

View File

@ -12,7 +12,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
public class SimpleBukkitAdapter extends CachedBukkitAdapter {
private BlockData[][] blockDataCache;
public boolean init() {
private boolean init() {
if (blockDataCache != null) return false;
this.blockDataCache = new BlockData[BlockTypes.size()][];
blockDataCache[0] = new BlockData[] {Material.AIR.createBlockData()};