mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-14 23:48:36 +00:00
some adapter refactoring
This commit is contained in:
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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()};
|
||||
|
Reference in New Issue
Block a user