|
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2021 Alexander Söderberg & Contributors |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | +package cloud.commandframework.examples.bukkit; |
| 25 | + |
| 26 | +import cloud.commandframework.Command; |
| 27 | +import cloud.commandframework.arguments.standard.IntegerArgument; |
| 28 | +import cloud.commandframework.bukkit.BukkitCommandManager; |
| 29 | +import cloud.commandframework.bukkit.data.BlockPredicate; |
| 30 | +import cloud.commandframework.bukkit.data.ItemStackPredicate; |
| 31 | +import cloud.commandframework.bukkit.data.ProtoItemStack; |
| 32 | +import cloud.commandframework.bukkit.parsers.BlockPredicateArgument; |
| 33 | +import cloud.commandframework.bukkit.parsers.ItemStackArgument; |
| 34 | +import cloud.commandframework.bukkit.parsers.ItemStackPredicateArgument; |
| 35 | +import cloud.commandframework.bukkit.parsers.MaterialArgument; |
| 36 | +import cloud.commandframework.context.CommandContext; |
| 37 | +import org.bukkit.Location; |
| 38 | +import org.bukkit.Material; |
| 39 | +import org.bukkit.block.Block; |
| 40 | +import org.bukkit.block.data.BlockData; |
| 41 | +import org.bukkit.command.CommandSender; |
| 42 | +import org.bukkit.entity.Player; |
| 43 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 44 | +import org.checkerframework.framework.qual.DefaultQualifier; |
| 45 | + |
| 46 | +@DefaultQualifier(NonNull.class) |
| 47 | +final class Mc113 { |
| 48 | + private final BukkitCommandManager<CommandSender> manager; |
| 49 | + |
| 50 | + Mc113(final BukkitCommandManager<CommandSender> manager) { |
| 51 | + this.manager = manager; |
| 52 | + } |
| 53 | + |
| 54 | + void registerCommands() { |
| 55 | + final Command.Builder<CommandSender> builder = this.manager.commandBuilder("example") |
| 56 | + .literal("mc113"); |
| 57 | + |
| 58 | + this.manager.command(builder.literal("replace") |
| 59 | + .senderType(Player.class) |
| 60 | + .argument(BlockPredicateArgument.of("predicate")) |
| 61 | + .literal("with") |
| 62 | + .argument(MaterialArgument.of("block")) // todo: use BlockDataArgument |
| 63 | + .argument(IntegerArgument.<CommandSender>newBuilder("radius").withMin(1)) |
| 64 | + .handler(this::executeReplace)); |
| 65 | + |
| 66 | + this.manager.command(builder.literal("test_item") |
| 67 | + .argument(ItemStackArgument.of("item")) |
| 68 | + .literal("is") |
| 69 | + .argument(ItemStackPredicateArgument.of("predicate")) |
| 70 | + .handler(Mc113::executeTestItem)); |
| 71 | + } |
| 72 | + |
| 73 | + private void executeReplace(final CommandContext<CommandSender> ctx) { |
| 74 | + final BlockData block = ctx.<Material>get("block").createBlockData(); |
| 75 | + final BlockPredicate predicate = ctx.get("predicate"); |
| 76 | + final int radius = ctx.get("radius"); |
| 77 | + |
| 78 | + final Player player = (Player) ctx.getSender(); |
| 79 | + final Location loc = player.getLocation(); |
| 80 | + |
| 81 | + this.manager.taskRecipe().begin(ctx).synchronous(context -> { |
| 82 | + for (double x = loc.getX() - radius; x < loc.getX() + radius; x++) { |
| 83 | + for (double y = loc.getY() - radius; y < loc.getY() + radius; y++) { |
| 84 | + for (double z = loc.getZ() - radius; z < loc.getZ() + radius; z++) { |
| 85 | + final Block blockAt = player.getWorld().getBlockAt((int) x, (int) y, (int) z); |
| 86 | + if (predicate.test(blockAt)) { |
| 87 | + blockAt.setBlockData(block); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }).execute(); |
| 93 | + } |
| 94 | + |
| 95 | + private static void executeTestItem(final CommandContext<CommandSender> ctx) { |
| 96 | + final ProtoItemStack protoItemStack = ctx.get("item"); |
| 97 | + final ItemStackPredicate predicate = ctx.get("predicate"); |
| 98 | + ctx.getSender().sendMessage("result: " + predicate.test( |
| 99 | + protoItemStack.createItemStack(1, true))); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments