2021-04-18 18:47:43 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of the SteamWar software.
|
|
|
|
*
|
2022-01-29 15:28:28 +01:00
|
|
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
2021-04-18 18:47:43 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-01-29 15:28:28 +01:00
|
|
|
package de.steamwar.bausystem.utils;
|
2021-04-18 18:47:43 +02:00
|
|
|
|
|
|
|
import com.sk89q.worldedit.EditSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.bukkit.BukkitWorld;
|
2022-01-29 15:28:28 +01:00
|
|
|
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
|
2021-07-05 19:32:10 +02:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
|
2021-04-18 18:47:43 +02:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
2021-07-05 19:32:10 +02:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
|
2021-04-18 18:47:43 +02:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
|
2021-07-05 19:32:10 +02:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
|
2022-05-18 17:33:25 +02:00
|
|
|
import com.sk89q.worldedit.function.mask.Mask;
|
2021-08-01 22:01:25 +02:00
|
|
|
import com.sk89q.worldedit.function.mask.Mask2D;
|
2021-07-05 19:32:10 +02:00
|
|
|
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
|
2021-04-18 18:47:43 +02:00
|
|
|
import com.sk89q.worldedit.function.operation.Operations;
|
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
|
|
|
import com.sk89q.worldedit.math.transform.AffineTransform;
|
|
|
|
import com.sk89q.worldedit.regions.CuboidRegion;
|
2022-01-29 15:28:28 +01:00
|
|
|
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
|
2021-04-18 18:47:43 +02:00
|
|
|
import com.sk89q.worldedit.session.ClipboardHolder;
|
2022-01-29 15:28:28 +01:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2021-04-18 18:47:43 +02:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2022-01-29 15:28:28 +01:00
|
|
|
import de.steamwar.bausystem.region.Point;
|
2021-07-30 23:00:32 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2022-01-29 15:28:28 +01:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.data.BlockData;
|
|
|
|
import org.bukkit.block.data.Waterlogged;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.util.Vector;
|
2021-07-30 23:00:32 +02:00
|
|
|
|
2021-08-01 22:01:25 +02:00
|
|
|
import javax.annotation.Nullable;
|
2021-04-18 18:47:43 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2021-07-05 19:32:10 +02:00
|
|
|
import java.io.FileOutputStream;
|
2021-04-18 18:47:43 +02:00
|
|
|
import java.io.IOException;
|
2023-02-06 21:38:12 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Set;
|
2023-08-19 23:25:52 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2023-07-29 12:47:48 +02:00
|
|
|
import java.util.function.BiPredicate;
|
2021-07-05 19:32:10 +02:00
|
|
|
import java.util.logging.Level;
|
2021-04-18 18:47:43 +02:00
|
|
|
|
2022-08-03 19:10:26 +02:00
|
|
|
public class FlatteningWrapper15 implements FlatteningWrapper {
|
2022-01-29 15:28:28 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isNoBook(ItemStack item) {
|
|
|
|
return item.getType() != Material.WRITABLE_BOOK && item.getType() != Material.WRITTEN_BOOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Set<Material> unpushable = new HashSet<>(Arrays.asList(Material.BARRIER, Material.BEACON, Material.COMMAND_BLOCK, Material.CHAIN_COMMAND_BLOCK, Material.REPEATING_COMMAND_BLOCK, Material.ENCHANTING_TABLE, Material.END_GATEWAY, Material.END_PORTAL, Material.ENDER_CHEST, Material.GRINDSTONE, Material.JIGSAW, Material.JUKEBOX, Material.NETHER_PORTAL, Material.OBSIDIAN, Material.STRUCTURE_VOID, Material.BARREL, Material.BEEHIVE, Material.BEE_NEST, Material.BLAST_FURNACE, Material.BREWING_STAND, Material.CHEST, Material.DAYLIGHT_DETECTOR, Material.DISPENSER, Material.DROPPER, Material.FURNACE, Material.HOPPER, Material.LECTERN, Material.SMOKER, Material.TRAPPED_CHEST));
|
|
|
|
|
|
|
|
// TODO: FLOWER
|
|
|
|
private static final Set<Material> breaking = new HashSet<>(Arrays.asList(Material.BAMBOO, Material.CACTUS, Material.CAKE, Material.CARVED_PUMPKIN, Material.CHORUS_FLOWER, Material.CHORUS_PLANT, Material.COBWEB, Material.COCOA, Material.DRAGON_EGG, Material.FIRE, Material.FLOWER_POT, Material.JACK_O_LANTERN, Material.LADDER, Material.LAVA, Material.LAVA, Material.LEVER, Material.LILY_PAD, Material.MELON, Material.NETHER_WART, Material.PUMPKIN, Material.COMPARATOR, Material.REDSTONE_WIRE, Material.REPEATER, Material.TORCH, Material.STRUCTURE_VOID, Material.SCAFFOLDING, Material.SEA_PICKLE, Material.SNOW, Material.SUGAR_CANE, Material.TORCH, Material.TRIPWIRE, Material.TRIPWIRE_HOOK, Material.TURTLE_EGG, Material.VINE, Material.WATER, Material.WHEAT));
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isUnpusheable(Material material) {
|
|
|
|
if (unpushable.contains(material)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
String name = material.name();
|
|
|
|
return name.contains("BANNER") || name.contains("SIGN");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isBreakingOnPush(Material material) {
|
|
|
|
if (breaking.contains(material)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
String name = material.name();
|
|
|
|
return name.contains("BED") || name.contains("BUTTON") || name.contains("CARPET") || (name.contains("DOOR") && !name.contains("TRAPDOOR")) || name.contains("HEAD") || name.contains("LEAVES") || name.contains("MUSHROOM") || name.contains("PRESSURE_PLATE") || name.contains("SHULKER_BOX");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isWorldEditCommand(String command) {
|
|
|
|
if (command.startsWith("/")) {
|
|
|
|
command = command.replaceFirst("/", "");
|
|
|
|
}
|
|
|
|
command = command.toLowerCase();
|
|
|
|
return WorldEdit.getInstance().getPlatformManager().getPlatformCommandManager().getCommandManager().containsCommand(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final WorldEditPlugin WORLDEDIT_PLUGIN = Objects.requireNonNull((WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"));
|
|
|
|
private static final World BUKKITWORLD = new BukkitWorld(Bukkit.getWorlds().get(0));
|
|
|
|
@Override
|
|
|
|
public void setSelection(Player p, Point minPoint, Point maxPoint) {
|
|
|
|
WORLDEDIT_PLUGIN.getSession(p).setRegionSelector(BUKKITWORLD, new CuboidRegionSelector(BUKKITWORLD, toBlockVector3(minPoint), toBlockVector3(maxPoint)));
|
|
|
|
}
|
2021-07-31 12:08:53 +02:00
|
|
|
|
2023-07-29 12:47:48 +02:00
|
|
|
@Override
|
|
|
|
public Clipboard loadSchematic(File file) {
|
|
|
|
Clipboard clipboard;
|
|
|
|
try (ClipboardReader reader = Objects.requireNonNull(ClipboardFormats.findByFile(file)).getReader(new FileInputStream(file))) {
|
|
|
|
clipboard = reader.read();
|
|
|
|
} catch (NullPointerException | IOException e) {
|
|
|
|
throw new SecurityException("Bausystem schematic not found", e);
|
|
|
|
}
|
|
|
|
return clipboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EditSession paste(PasteBuilder pasteBuilder) {
|
|
|
|
try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) {
|
|
|
|
Clipboard clipboard = pasteBuilder.getClipboard();
|
|
|
|
|
2023-07-30 21:02:33 +02:00
|
|
|
if (!pasteBuilder.getMappers().isEmpty()) {
|
|
|
|
BlockVector3 minimum = clipboard.getRegion().getMinimumPoint();
|
|
|
|
for (int x = 0; x < clipboard.getDimensions().getX(); x++) {
|
|
|
|
for (int y = 0; y < clipboard.getDimensions().getY(); y++) {
|
|
|
|
for (int z = 0; z < clipboard.getDimensions().getZ(); z++) {
|
|
|
|
BlockVector3 pos = minimum.add(x, y, z);
|
|
|
|
pasteBuilder.getMappers().forEach(mapper -> mapper.accept(clipboard, pos));
|
|
|
|
}
|
2023-07-29 12:47:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-19 23:25:52 +02:00
|
|
|
AtomicReference<BlockVector3> pastePoint = new AtomicReference<>();
|
2023-07-30 21:02:33 +02:00
|
|
|
if (!pasteBuilder.getPredicates().isEmpty()) {
|
|
|
|
e.setMask(new Mask() {
|
|
|
|
@Override
|
|
|
|
public boolean test(BlockVector3 blockVector3) {
|
2023-08-19 23:25:52 +02:00
|
|
|
BaseBlock block = clipboard.getFullBlock(blockVector3.subtract(pastePoint.get()).add(clipboard.getRegion().getMinimumPoint()));
|
|
|
|
String blockName = block.getBlockType().toString().toLowerCase();
|
2023-07-30 21:02:33 +02:00
|
|
|
for (BiPredicate<BaseBlock, String> predicate : pasteBuilder.getPredicates()) {
|
|
|
|
if (!predicate.test(block, blockName)) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2023-07-29 12:47:48 +02:00
|
|
|
}
|
|
|
|
|
2023-08-19 23:25:52 +02:00
|
|
|
public Mask copy() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-07-30 21:02:33 +02:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Mask2D toMask2D() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-07-29 12:47:48 +02:00
|
|
|
|
|
|
|
ClipboardHolder ch = new ClipboardHolder(clipboard);
|
|
|
|
BlockVector3 dimensions = clipboard.getDimensions();
|
|
|
|
BlockVector3 v = BlockVector3.at(pasteBuilder.getPastPoint().getX(), pasteBuilder.getPastPoint().getY(), pasteBuilder.getPastPoint().getZ());
|
|
|
|
BlockVector3 offset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
|
|
|
|
if (pasteBuilder.isRotate()) {
|
|
|
|
ch.setTransform(new AffineTransform().rotateY(180));
|
|
|
|
v = v.add(dimensions.getX() / 2, 0, dimensions.getZ() / 2).subtract(offset.multiply(-1, 1, -1)).subtract(0, 0, 1);
|
|
|
|
} else {
|
|
|
|
v = v.subtract(dimensions.getX() / 2, 0, dimensions.getZ() / 2).subtract(offset);
|
|
|
|
}
|
2023-08-19 23:25:52 +02:00
|
|
|
pastePoint.set(v);
|
2023-07-29 12:47:48 +02:00
|
|
|
|
|
|
|
if (pasteBuilder.isReset()) {
|
|
|
|
e.setBlocks(new CuboidRegion(toBlockVector3(pasteBuilder.getMinPoint()), toBlockVector3(pasteBuilder.getMaxPoint())), Objects.requireNonNull(BlockTypes.AIR).getDefaultState().toBaseBlock());
|
|
|
|
if (pasteBuilder.getWaterLevel() != 0) {
|
|
|
|
e.setBlocks(new CuboidRegion(toBlockVector3(pasteBuilder.getMinPoint()), toBlockVector3(pasteBuilder.getMaxPoint()).withY(pasteBuilder.getWaterLevel())), Objects.requireNonNull(BlockTypes.WATER).getDefaultState().toBaseBlock());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Operations.completeBlindly(ch.createPaste(e).to(v).ignoreAirBlocks(pasteBuilder.isIgnoreAir()).build());
|
|
|
|
return e;
|
|
|
|
} catch (WorldEditException e) {
|
|
|
|
throw new SecurityException(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:28:28 +01:00
|
|
|
@Override
|
|
|
|
public boolean backup(Point minPoint, Point maxPoint, File file) {
|
2021-07-31 12:57:01 +02:00
|
|
|
BukkitWorld bukkitWorld = new BukkitWorld(Bukkit.getWorlds().get(0));
|
2022-01-29 15:28:28 +01:00
|
|
|
CuboidRegion region = new CuboidRegion(bukkitWorld, toBlockVector3(minPoint), toBlockVector3(maxPoint));
|
2021-07-31 12:57:01 +02:00
|
|
|
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
|
|
|
|
try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(bukkitWorld, -1)) {
|
|
|
|
ForwardExtentCopy copy = new ForwardExtentCopy(
|
|
|
|
e, region, clipboard, region.getMinimumPoint()
|
|
|
|
);
|
|
|
|
|
|
|
|
copy.setCopyingEntities(false);
|
|
|
|
copy.setCopyingBiomes(false);
|
|
|
|
|
|
|
|
Operations.complete(copy);
|
|
|
|
|
|
|
|
try (ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) {
|
|
|
|
writer.write(clipboard);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (WorldEditException | IOException e) {
|
|
|
|
Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 15:28:28 +01:00
|
|
|
|
|
|
|
private BlockVector3 toBlockVector3(Point point) {
|
|
|
|
return BlockVector3.at(point.getX(), point.getY(), point.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean inWater(org.bukkit.World world, Vector tntPosition) {
|
|
|
|
Block block = world.getBlockAt(tntPosition.getBlockX(), tntPosition.getBlockY(), tntPosition.getBlockZ());
|
|
|
|
if (block.getType() == Material.WATER)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
BlockData data = block.getBlockData();
|
|
|
|
if (!(data instanceof Waterlogged))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ((Waterlogged) data).isWaterlogged();
|
|
|
|
}
|
|
|
|
}
|