Navigation Menu

Skip to content

Commit

Permalink
Bypass Bukkit in more situations (#532)
Browse files Browse the repository at this point in the history
* Bypass Bukkit in more situations

* Use orElseGet

* Apply the same optimisation in the reverse BlockData adapter, and use lambdas instead of AIC

* Remove bukkit type checks

* Improve reliability of fallbacks
  • Loading branch information
me4502 committed Nov 15, 2019
1 parent 89bc664 commit 77ef0ae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
Expand Up @@ -21,14 +21,14 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Function;
import com.sk89q.worldedit.NotABlockException;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Location;
Expand All @@ -45,13 +45,16 @@
import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -349,18 +352,19 @@ public static org.bukkit.entity.EntityType adapt(EntityType entityType) {
return org.bukkit.entity.EntityType.fromName(entityType.getId().substring(10));
}

private static EnumMap<Material, BlockType> materialBlockTypeCache = new EnumMap<>(Material.class);
private static EnumMap<Material, ItemType> materialItemTypeCache = new EnumMap<>(Material.class);

/**
* Converts a Material to a BlockType
*
* @param material The material
* @return The blocktype
*/
@Nullable
public static BlockType asBlockType(Material material) {
checkNotNull(material);
if (!material.isBlock()) {
throw new IllegalArgumentException(material.getKey().toString() + " is not a block!");
}
return BlockTypes.get(material.getKey().toString());
return materialBlockTypeCache.computeIfAbsent(material, input -> BlockTypes.get(material.getKey().toString()));
}

/**
Expand All @@ -369,15 +373,14 @@ public static BlockType asBlockType(Material material) {
* @param material The material
* @return The itemtype
*/
@Nullable
public static ItemType asItemType(Material material) {
checkNotNull(material);
if (!material.isItem()) {
throw new IllegalArgumentException(material.getKey().toString() + " is not an item!");
}
return ItemTypes.get(material.getKey().toString());
return materialItemTypeCache.computeIfAbsent(material, input -> ItemTypes.get(material.getKey().toString()));
}

private static Map<String, BlockState> blockStateCache = new HashMap<>();
private static Int2ObjectMap<BlockState> blockStateCache = new Int2ObjectOpenHashMap<>();
private static Map<String, BlockState> blockStateStringCache = new HashMap<>();

/**
* Create a WorldEdit BlockState from a Bukkit BlockData
Expand All @@ -387,21 +390,32 @@ public static ItemType asItemType(Material material) {
*/
public static BlockState adapt(BlockData blockData) {
checkNotNull(blockData);
return blockStateCache.computeIfAbsent(blockData.getAsString(), new Function<String, BlockState>() {
@Nullable
@Override
public BlockState apply(@Nullable String input) {

if (WorldEditPlugin.getInstance().getBukkitImplAdapter() == null) {
return blockStateStringCache.computeIfAbsent(blockData.getAsString(), input -> {
try {
return WorldEdit.getInstance().getBlockFactory().parseFromInput(input, TO_BLOCK_CONTEXT).toImmutableState();
} catch (InputParseException e) {
e.printStackTrace();
return null;
}
}
});
});
} else {
return blockStateCache.computeIfAbsent(
WorldEditPlugin.getInstance().getBukkitImplAdapter().getInternalBlockStateId(blockData).orElseGet(
() -> blockData.getAsString().hashCode()
), input -> {
try {
return WorldEdit.getInstance().getBlockFactory().parseFromInput(blockData.getAsString(), TO_BLOCK_CONTEXT).toImmutableState();
} catch (InputParseException e) {
e.printStackTrace();
return null;
}
});
}
}

private static Map<String, BlockData> blockDataCache = new HashMap<>();
private static Int2ObjectMap<BlockData> blockDataCache = new Int2ObjectOpenHashMap<>();

/**
* Create a Bukkit BlockData from a WorldEdit BlockStateHolder
Expand All @@ -411,13 +425,9 @@ public BlockState apply(@Nullable String input) {
*/
public static <B extends BlockStateHolder<B>> BlockData adapt(B block) {
checkNotNull(block);
return blockDataCache.computeIfAbsent(block.getAsString(), new Function<String, BlockData>() {
@Nullable
@Override
public BlockData apply(@Nullable String input) {
return Bukkit.createBlockData(block.getAsString());
}
}).clone();
// Should never not have an ID for this BlockState.
int cacheKey = BlockStateIdAccess.getBlockStateId(block.toImmutableState()).orElseGet(block::hashCode);
return blockDataCache.computeIfAbsent(cacheKey, input -> Bukkit.createBlockData(block.getAsString())).clone();
}

/**
Expand Down
Expand Up @@ -33,6 +33,7 @@
import com.sk89q.worldedit.world.block.BlockType;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -175,6 +176,9 @@ default boolean simulateItemUse(World world, BlockVector3 position, BaseItem ite
*/
BaseItemStack adapt(ItemStack itemStack);

default OptionalInt getInternalBlockStateId(BlockData data) {
return OptionalInt.empty();
}

/**
* Retrieve the internal ID for a given state, if possible.
Expand Down
Binary file modified worldedit-bukkit/src/main/resources/worldedit-adapters.jar
Binary file not shown.

0 comments on commit 77ef0ae

Please sign in to comment.