Search in sources :

Example 1 with Entity

use of com.sk89q.worldedit.entity.Entity in project FastAsyncWorldEdit by IntellectualSites.

the class PlatformCommandManager method handleCommandOnCurrentThread.

public void handleCommandOnCurrentThread(CommandEvent event) {
    Actor actor = platformManager.createProxyActor(event.getActor());
    String[] split = parseArgs(event.getArguments()).map(Substring::getSubstring).toArray(String[]::new);
    // No command found!
    if (!commandManager.containsCommand(split[0])) {
        return;
    }
    LocalSession session = worldEdit.getSessionManager().get(actor);
    Request.request().setSession(session);
    if (actor instanceof Entity) {
        Extent extent = ((Entity) actor).getExtent();
        if (extent instanceof World) {
            Request.request().setWorld(((World) extent));
        }
    }
    MemoizingValueAccess context = initializeInjectedValues(event::getArguments, actor, event, false);
    ThrowableSupplier<Throwable> task = () -> commandManager.execute(context, ImmutableList.copyOf(split));
    handleCommandTask(task, context, session, event);
}
Also used : Entity(com.sk89q.worldedit.entity.Entity) MemoizingValueAccess(org.enginehub.piston.inject.MemoizingValueAccess) Extent(com.sk89q.worldedit.extent.Extent) LocalSession(com.sk89q.worldedit.LocalSession) World(com.sk89q.worldedit.world.World)

Example 2 with Entity

use of com.sk89q.worldedit.entity.Entity in project FastAsyncWorldEdit by IntellectualSites.

the class BlockArrayClipboard method getEntities.

@Override
public List<? extends Entity> getEntities(Region region) {
    region = region.clone();
    region.shift(BlockVector3.ZERO.subtract(offset));
    return getParent().getEntities(region).stream().map(e -> {
        if (e instanceof ClipboardEntity) {
            ClipboardEntity ce = (ClipboardEntity) e;
            Location oldloc = ce.getLocation();
            Location loc = new Location(oldloc.getExtent(), oldloc.getX() + offset.getBlockX(), oldloc.getY() + offset.getBlockY(), oldloc.getZ() + offset.getBlockZ(), oldloc.getYaw(), oldloc.getPitch());
            return new ClipboardEntity(loc, ce.entity);
        }
        return e;
    }).collect(Collectors.toList());
}
Also used : OffsetBlockVector3(com.fastasyncworldedit.core.math.OffsetBlockVector3) BlockVector2(com.sk89q.worldedit.math.BlockVector2) BlockTypes(com.sk89q.worldedit.world.block.BlockTypes) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Iterators(com.google.common.collect.Iterators) WorldEditException(com.sk89q.worldedit.WorldEditException) Location(com.sk89q.worldedit.util.Location) MutableBlockVector2(com.fastasyncworldedit.core.math.MutableBlockVector2) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) Nonnull(javax.annotation.Nonnull) Region(com.sk89q.worldedit.regions.Region) BlockStateHolder(com.sk89q.worldedit.world.block.BlockStateHolder) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) SimpleClipboard(com.fastasyncworldedit.core.extent.clipboard.SimpleClipboard) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Entity(com.sk89q.worldedit.entity.Entity) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) CompoundTag(com.sk89q.jnbt.CompoundTag) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) Order(com.fastasyncworldedit.core.function.visitor.Order) BlockState(com.sk89q.worldedit.world.block.BlockState) Extent(com.sk89q.worldedit.extent.Extent) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) Location(com.sk89q.worldedit.util.Location)

Example 3 with Entity

use of com.sk89q.worldedit.entity.Entity in project FastAsyncWorldEdit by IntellectualSites.

the class MinecraftStructure method write.

public void write(Clipboard clipboard, String owner) throws IOException {
    Region region = clipboard.getRegion();
    int width = region.getWidth();
    int height = region.getHeight();
    int length = region.getLength();
    if (width > WARN_SIZE || height > WARN_SIZE || length > WARN_SIZE) {
        LOGGER.info("A structure longer than 32 is unsupported by minecraft (but probably still works)");
    }
    Map<String, Object> structure = FaweCache.INSTANCE.asMap("version", 1, "author", owner);
    // ignored: version / owner
    Int2ObjectArrayMap<Integer> indexes = new Int2ObjectArrayMap<>();
    // Size
    structure.put("size", Arrays.asList(width, height, length));
    // Palette
    ArrayList<HashMap<String, Object>> palette = new ArrayList<>();
    for (BlockVector3 point : region) {
        BlockState block = clipboard.getBlock(point);
        int combined = block.getInternalId();
        BlockType type = block.getBlockType();
        if (type == BlockTypes.STRUCTURE_VOID || indexes.containsKey(combined)) {
            continue;
        }
        indexes.put(combined, (Integer) palette.size());
        HashMap<String, Object> paletteEntry = new HashMap<>();
        paletteEntry.put("Name", type.getId());
        if (block.getInternalId() != type.getInternalId()) {
            Map<String, Object> properties = null;
            for (AbstractProperty property : (List<AbstractProperty<?>>) type.getProperties()) {
                int propIndex = property.getIndex(block.getInternalId());
                if (propIndex != 0) {
                    if (properties == null) {
                        properties = new HashMap<>();
                    }
                    Object value = property.getValues().get(propIndex);
                    properties.put(property.getName(), value.toString());
                }
            }
            if (properties != null) {
                paletteEntry.put("Properties", properties);
            }
        }
        palette.add(paletteEntry);
    }
    if (!palette.isEmpty()) {
        structure.put("palette", palette);
    }
    // Blocks
    ArrayList<Map<String, Object>> blocks = new ArrayList<>();
    BlockVector3 min = region.getMinimumPoint();
    for (BlockVector3 point : region) {
        BaseBlock block = clipboard.getFullBlock(point);
        if (block.getBlockType() != BlockTypes.STRUCTURE_VOID) {
            int combined = block.getInternalId();
            int index = indexes.get(combined);
            List<Integer> pos = Arrays.asList(point.getX() - min.getX(), point.getY() - min.getY(), point.getZ() - min.getZ());
            if (!block.hasNbtData()) {
                blocks.add(FaweCache.INSTANCE.asMap("state", index, "pos", pos));
            } else {
                blocks.add(FaweCache.INSTANCE.asMap("state", index, "pos", pos, "nbt", block.getNbtData()));
            }
        }
    }
    if (!blocks.isEmpty()) {
        structure.put("blocks", blocks);
    }
    // Entities
    ArrayList<Map<String, Object>> entities = new ArrayList<>();
    for (Entity entity : clipboard.getEntities()) {
        Location loc = entity.getLocation();
        List<Double> pos = Arrays.asList(loc.getX(), loc.getY(), loc.getZ());
        List<Integer> blockPos = Arrays.asList(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
        BaseEntity state = entity.getState();
        if (state != null) {
            CompoundTag nbt = state.getNbtData();
            Map<String, Tag> nbtMap = nbt.getValue();
            // Replace rotation data
            nbtMap.put("Rotation", writeRotation(entity.getLocation()));
            nbtMap.put("id", new StringTag(state.getType().getId()));
            Map<String, Object> entityMap = FaweCache.INSTANCE.asMap("pos", pos, "blockPos", blockPos, "nbt", nbt);
            entities.add(entityMap);
        }
    }
    if (!entities.isEmpty()) {
        structure.put("entities", entities);
    }
    out.writeNamedTag("", FaweCache.INSTANCE.asTag(structure));
    close();
}
Also used : StringTag(com.sk89q.jnbt.StringTag) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) Entity(com.sk89q.worldedit.entity.Entity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) ArrayList(java.util.ArrayList) List(java.util.List) CompoundTag(com.sk89q.jnbt.CompoundTag) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) AbstractProperty(com.sk89q.worldedit.registry.state.AbstractProperty) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) StringTag(com.sk89q.jnbt.StringTag) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) NamedTag(com.sk89q.jnbt.NamedTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) HashMap(java.util.HashMap) Map(java.util.Map) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap) Location(com.sk89q.worldedit.util.Location)

Example 4 with Entity

use of com.sk89q.worldedit.entity.Entity in project FastAsyncWorldEdit by IntellectualSites.

the class FastSchematicWriter method write2.

/**
 * Writes a version 2 schematic file.
 *
 * @param clipboard The clipboard
 */
private void write2(Clipboard clipboard) throws IOException {
    Region region = clipboard.getRegion();
    BlockVector3 origin = clipboard.getOrigin();
    BlockVector3 min = region.getMinimumPoint();
    BlockVector3 offset = min.subtract(origin);
    int width = region.getWidth();
    int height = region.getHeight();
    int length = region.getLength();
    if (width > MAX_SIZE) {
        throw new IllegalArgumentException("Width of region too large for a .schematic");
    }
    if (height > MAX_SIZE) {
        throw new IllegalArgumentException("Height of region too large for a .schematic");
    }
    if (length > MAX_SIZE) {
        throw new IllegalArgumentException("Length of region too large for a .schematic");
    }
    final DataOutput rawStream = outputStream.getOutputStream();
    outputStream.writeLazyCompoundTag("Schematic", out -> {
        out.writeNamedTag("DataVersion", WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion());
        out.writeNamedTag("Version", CURRENT_VERSION);
        out.writeNamedTag("Width", (short) width);
        out.writeNamedTag("Height", (short) height);
        out.writeNamedTag("Length", (short) length);
        // The Sponge format Offset refers to the 'min' points location in the world. That's our 'Origin'
        out.writeNamedTag("Offset", new int[] { min.getBlockX(), min.getBlockY(), min.getBlockZ() });
        out.writeLazyCompoundTag("Metadata", out1 -> {
            out1.writeNamedTag("WEOffsetX", offset.getBlockX());
            out1.writeNamedTag("WEOffsetY", offset.getBlockY());
            out1.writeNamedTag("WEOffsetZ", offset.getBlockZ());
            out1.writeNamedTag("FAWEVersion", Fawe.instance().getVersion().build);
        });
        ByteArrayOutputStream blocksCompressed = new ByteArrayOutputStream();
        FaweOutputStream blocksOut = new FaweOutputStream(new DataOutputStream(new LZ4BlockOutputStream(blocksCompressed)));
        ByteArrayOutputStream tilesCompressed = new ByteArrayOutputStream();
        NBTOutputStream tilesOut = new NBTOutputStream(new LZ4BlockOutputStream(tilesCompressed));
        List<Integer> paletteList = new ArrayList<>();
        char[] palette = new char[BlockTypesCache.states.length];
        Arrays.fill(palette, Character.MAX_VALUE);
        int paletteMax = 0;
        int numTiles = 0;
        Clipboard finalClipboard;
        if (clipboard instanceof BlockArrayClipboard) {
            finalClipboard = ((BlockArrayClipboard) clipboard).getParent();
        } else {
            finalClipboard = clipboard;
        }
        Iterator<BlockVector3> iterator = finalClipboard.iterator(Order.YZX);
        while (iterator.hasNext()) {
            BlockVector3 pos = iterator.next();
            BaseBlock block = pos.getFullBlock(finalClipboard);
            CompoundTag nbt = block.getNbtData();
            if (nbt != null) {
                Map<String, Tag> values = new HashMap<>(nbt.getValue());
                // Positions are kept in NBT, we don't want that.
                values.remove("x");
                values.remove("y");
                values.remove("z");
                values.put("Id", new StringTag(block.getNbtId()));
                // Remove 'id' if it exists. We want 'Id'.
                // Do this after we get "getNbtId" cos otherwise "getNbtId" doesn't work.
                // Dum.
                values.remove("id");
                values.put("Pos", new IntArrayTag(new int[] { pos.getX(), pos.getY(), pos.getZ() }));
                numTiles++;
                tilesOut.writeTagPayload(new CompoundTag(values));
            }
            int ordinal = block.getOrdinal();
            if (ordinal == 0) {
                ordinal = 1;
            }
            char value = palette[ordinal];
            if (value == Character.MAX_VALUE) {
                int size = paletteMax++;
                palette[ordinal] = value = (char) size;
                paletteList.add(ordinal);
            }
            blocksOut.writeVarInt(value);
        }
        // close
        tilesOut.close();
        blocksOut.close();
        out.writeNamedTag("PaletteMax", paletteMax);
        out.writeLazyCompoundTag("Palette", out12 -> {
            for (int i = 0; i < paletteList.size(); i++) {
                int stateOrdinal = paletteList.get(i);
                BlockState state = BlockTypesCache.states[stateOrdinal];
                out12.writeNamedTag(state.getAsString(), i);
            }
        });
        out.writeNamedTagName("BlockData", NBTConstants.TYPE_BYTE_ARRAY);
        rawStream.writeInt(blocksOut.size());
        try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(blocksCompressed.toByteArray()))) {
            IOUtil.copy(in, rawStream);
        }
        if (numTiles != 0) {
            out.writeNamedTagName("BlockEntities", NBTConstants.TYPE_LIST);
            rawStream.write(NBTConstants.TYPE_COMPOUND);
            rawStream.writeInt(numTiles);
            try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(tilesCompressed.toByteArray()))) {
                IOUtil.copy(in, rawStream);
            }
        } else {
            out.writeNamedEmptyList("BlockEntities");
        }
        if (finalClipboard.hasBiomes()) {
            writeBiomes(finalClipboard, out);
        }
        List<Tag> entities = new ArrayList<>();
        for (Entity entity : finalClipboard.getEntities()) {
            BaseEntity state = entity.getState();
            if (state != null) {
                Map<String, Tag> values = new HashMap<>();
                // Put NBT provided data
                CompoundTag rawTag = state.getNbtData();
                if (rawTag != null) {
                    values.putAll(rawTag.getValue());
                }
                // Store our location data, overwriting any
                values.remove("id");
                Location loc = entity.getLocation();
                if (!brokenEntities) {
                    loc = loc.setPosition(loc.add(min.toVector3()));
                }
                values.put("Id", new StringTag(state.getType().getId()));
                values.put("Pos", writeVector(loc));
                values.put("Rotation", writeRotation(entity.getLocation()));
                CompoundTag entityTag = new CompoundTag(values);
                entities.add(entityTag);
            }
        }
        if (entities.isEmpty()) {
            out.writeNamedEmptyList("Entities");
        } else {
            out.writeNamedTag("Entities", new ListTag(CompoundTag.class, entities));
        }
    });
}
Also used : StringTag(com.sk89q.jnbt.StringTag) DataOutput(java.io.DataOutput) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) Entity(com.sk89q.worldedit.entity.Entity) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) CompoundTag(com.sk89q.jnbt.CompoundTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlockVector3(com.sk89q.worldedit.math.BlockVector3) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) ListTag(com.sk89q.jnbt.ListTag) BlockState(com.sk89q.worldedit.world.block.BlockState) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(com.sk89q.worldedit.regions.Region) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) StringTag(com.sk89q.jnbt.StringTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) ListTag(com.sk89q.jnbt.ListTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) FaweOutputStream(com.fastasyncworldedit.core.internal.io.FaweOutputStream) Location(com.sk89q.worldedit.util.Location)

Example 5 with Entity

use of com.sk89q.worldedit.entity.Entity in project WorldGuard by EngineHub.

the class ToggleCommands method stopLag.

@Command(aliases = { "halt-activity", "stoplag", "haltactivity" }, usage = "[confirm]", desc = "Attempts to cease as much activity in order to stop lag", flags = "cis", max = 1)
@CommandPermissions({ "worldguard.halt-activity" })
public void stopLag(CommandContext args, Actor sender) throws CommandException {
    ConfigurationManager configManager = WorldGuard.getInstance().getPlatform().getGlobalStateManager();
    if (args.hasFlag('i')) {
        if (configManager.activityHaltToggle) {
            sender.print("ALL intensive server activity is not allowed.");
        } else {
            sender.print("ALL intensive server activity is allowed.");
        }
    } else {
        boolean activityHaltToggle = !args.hasFlag('c');
        if (activityHaltToggle && (args.argsLength() == 0 || !args.getString(0).equalsIgnoreCase("confirm"))) {
            String confirmCommand = "/" + args.getCommand() + " confirm";
            TextComponent message = TextComponent.builder("").append(ErrorFormat.wrap("This command will ")).append(ErrorFormat.wrap("PERMANENTLY").decoration(TextDecoration.BOLD, TextDecoration.State.TRUE)).append(ErrorFormat.wrap(" erase ALL animals in ALL loaded chunks in ALL loaded worlds. ")).append(TextComponent.newline()).append(TextComponent.of("[Click]", TextColor.GREEN).clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, confirmCommand)).hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to confirm /" + args.getCommand())))).append(ErrorFormat.wrap(" or type ")).append(CodeFormat.wrap(confirmCommand).clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, confirmCommand))).append(ErrorFormat.wrap(" to confirm.")).build();
            sender.print(message);
            return;
        }
        configManager.activityHaltToggle = activityHaltToggle;
        if (activityHaltToggle) {
            if (!(sender instanceof LocalPlayer)) {
                sender.print("ALL intensive server activity halted.");
            }
            if (!args.hasFlag('s')) {
                worldGuard.getPlatform().broadcastNotification(LabelFormat.wrap("ALL intensive server activity halted by " + sender.getDisplayName() + "."));
            } else {
                sender.print("(Silent) ALL intensive server activity halted by " + sender.getDisplayName() + ".");
            }
            for (World world : WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS).getWorlds()) {
                int removed = 0;
                for (Entity entity : world.getEntities()) {
                    if (Entities.isIntensiveEntity(entity)) {
                        entity.remove();
                        removed++;
                    }
                }
                if (removed > 10) {
                    sender.printRaw("" + removed + " entities (>10) auto-removed from " + world.getName());
                }
            }
        } else {
            if (!args.hasFlag('s')) {
                worldGuard.getPlatform().broadcastNotification(LabelFormat.wrap("ALL intensive server activity is now allowed."));
                if (!(sender instanceof LocalPlayer)) {
                    sender.print("ALL intensive server activity is now allowed.");
                }
            } else {
                sender.print("(Silent) ALL intensive server activity is now allowed.");
            }
        }
    }
}
Also used : TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Entity(com.sk89q.worldedit.entity.Entity) LocalPlayer(com.sk89q.worldguard.LocalPlayer) World(com.sk89q.worldedit.world.World) ConfigurationManager(com.sk89q.worldguard.config.ConfigurationManager) Command(com.sk89q.minecraft.util.commands.Command) CommandPermissions(com.sk89q.minecraft.util.commands.CommandPermissions)

Aggregations

Entity (com.sk89q.worldedit.entity.Entity)9 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)4 Extent (com.sk89q.worldedit.extent.Extent)4 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)4 Region (com.sk89q.worldedit.regions.Region)4 World (com.sk89q.worldedit.world.World)4 CompoundTag (com.sk89q.jnbt.CompoundTag)3 Location (com.sk89q.worldedit.util.Location)3 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)3 BlockState (com.sk89q.worldedit.world.block.BlockState)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ListTag (com.sk89q.jnbt.ListTag)2 StringTag (com.sk89q.jnbt.StringTag)2 Tag (com.sk89q.jnbt.Tag)2 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)2 ParserContext (com.sk89q.worldedit.extension.input.ParserContext)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 HashMap (java.util.HashMap)2 SimpleClipboard (com.fastasyncworldedit.core.extent.clipboard.SimpleClipboard)1