Added support for Custom Blocks in WorldEdit

This commit is contained in:
ASangarin 2020-08-15 05:50:41 +02:00
parent 2612a4902e
commit 2695a76f90
5 changed files with 98 additions and 1 deletions

Binary file not shown.

View File

@ -257,7 +257,7 @@
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.0.0</version>
<version>7.2.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/WorldEdit.jar</systemPath>
</dependency>

View File

@ -16,6 +16,8 @@ import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.worldedit.WorldEdit;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.ItemTier;
import net.Indyuce.mmoitems.api.SoulboundInfo;
@ -30,6 +32,7 @@ import net.Indyuce.mmoitems.comp.AdvancedEnchantmentsHook;
import net.Indyuce.mmoitems.comp.MMOItemsMetrics;
import net.Indyuce.mmoitems.comp.MMOItemsRewardTypes;
import net.Indyuce.mmoitems.comp.RealDualWieldHook;
import net.Indyuce.mmoitems.comp.WECustomBlockInputParser;
import net.Indyuce.mmoitems.comp.eco.VaultSupport;
import net.Indyuce.mmoitems.comp.flags.DefaultFlags;
import net.Indyuce.mmoitems.comp.flags.FlagPlugin;
@ -126,6 +129,15 @@ public class MMOItems extends JavaPlugin {
getLogger().log(Level.WARNING, "Could not initialize support with WorldGuard 7+");
}
try {
if (getServer().getPluginManager().getPlugin("WorldEdit") != null) {
WorldEdit.getInstance().getBlockFactory().register(new WECustomBlockInputParser());
getLogger().log(Level.INFO, "Hooked onto WorldEdit");
}
} catch (Exception e) {
getLogger().log(Level.WARNING, "Could not initialize support with WorldEdit 7+");
}
/*
* stat manager must be initialized before MMOCore compatibility
* initializes so that MMOCore can register its stats

View File

@ -52,4 +52,24 @@ public class MushroomState {
return blockData;
}
public boolean getSide(String side) {
side = side.toLowerCase();
switch(side) {
case "up":
return up;
case "down":
return down;
case "north":
return north;
case "south":
return south;
case "east":
return east;
case "west":
return west;
default:
return false;
}
}
}

View File

@ -0,0 +1,65 @@
package net.Indyuce.mmoitems.comp;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.block.CustomBlock;
import net.Indyuce.mmoitems.api.util.MushroomState;
public class WECustomBlockInputParser extends InputParser<BaseBlock> {
public WECustomBlockInputParser() {
super(WorldEdit.getInstance());
}
@Override
@SuppressWarnings("unchecked")
public BaseBlock parseFromInput(String input, ParserContext context) throws InputParseException {
input = input.toLowerCase();
BlockType type = null;
if(!input.startsWith("mmoitems-"))
return null;
int id;
try {
id = Integer.parseInt(input.split("\\-")[1]);
} catch(NumberFormatException e) {
return null;
}
CustomBlock block = MMOItems.plugin.getCustomBlocks().getBlock(id);
if(block == null) return null;
MushroomState mush = block.getState();
switch(mush.getType()) {
case MUSHROOM_STEM:
type = BlockTypes.MUSHROOM_STEM;
break;
case BROWN_MUSHROOM_BLOCK:
type = BlockTypes.BROWN_MUSHROOM_BLOCK;
break;
case RED_MUSHROOM_BLOCK:
type = BlockTypes.RED_MUSHROOM_BLOCK;
break;
default:
return null;
//throw new NoMatchException(TranslatableComponent.of("worldedit.error.unknown-block", TextComponent.of(input)));
}
BlockState state = type.getDefaultState()
.with((Property<Boolean>) type.getPropertyMap().get("up"), mush.getSide("up"))
.with((Property<Boolean>) type.getPropertyMap().get("down"), mush.getSide("down"))
.with((Property<Boolean>) type.getPropertyMap().get("north"), mush.getSide("north"))
.with((Property<Boolean>) type.getPropertyMap().get("south"), mush.getSide("south"))
.with((Property<Boolean>) type.getPropertyMap().get("east"), mush.getSide("east"))
.with((Property<Boolean>) type.getPropertyMap().get("west"), mush.getSide("west"));
return state.toBaseBlock();
}
}