|
| 1 | +package redart15.commandly.command; |
| 2 | + |
| 3 | +import com.mojang.brigadier.CommandDispatcher; |
| 4 | +import com.mojang.brigadier.arguments.ArgumentTypeInteger; |
| 5 | +import com.mojang.brigadier.builder.ArgumentBuilderLiteral; |
| 6 | +import com.mojang.brigadier.builder.ArgumentBuilderRequired; |
| 7 | +import com.mojang.brigadier.context.CommandContext; |
| 8 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 9 | +import net.minecraft.core.block.Block; |
| 10 | +import net.minecraft.core.entity.player.Player; |
| 11 | +import net.minecraft.core.net.command.CommandManager; |
| 12 | +import net.minecraft.core.net.command.CommandSource; |
| 13 | +import net.minecraft.core.net.command.arguments.ArgumentTypeVec3; |
| 14 | +import net.minecraft.core.net.command.helpers.DoubleCoordinate; |
| 15 | +import net.minecraft.core.net.command.helpers.DoubleCoordinates; |
| 16 | +import net.minecraft.core.world.World; |
| 17 | +import redart15.commandly.CommandlyConfig; |
| 18 | +import redart15.commandly.treecapitator.TreeCapitator; |
| 19 | +import redart15.commandly.veincapitator.VeinMining; |
| 20 | +import turniplabs.halplibe.helper.EnvironmentHelper; |
| 21 | + |
| 22 | +import java.util.function.Predicate; |
| 23 | + |
| 24 | +import static redart15.commandly.command.CommandlyCommands.ReturnValues.*; |
| 25 | +import static redart15.commandly.CommandlyMod.MASK; |
| 26 | + |
| 27 | +@SuppressWarnings("ALL") //cause this drives me nuts |
| 28 | +public class CommandProtect implements CommandManager.CommandRegistry { |
| 29 | + public static final int MAX_BLOCK_COUNT = 589_824; |
| 30 | + |
| 31 | + public void register(CommandDispatcher<CommandSource> dispatcher) { |
| 32 | + ArgumentBuilderLiteral<CommandSource> command = |
| 33 | + (ArgumentBuilderLiteral) ((ArgumentBuilderLiteral) ArgumentBuilderLiteral.literal("protect") |
| 34 | + .requires((t) -> ((CommandSource) t).hasAdmin()) |
| 35 | + .then(ArgumentBuilderLiteral.literal("chunk") |
| 36 | + .executes(CommandProtect::chunk) |
| 37 | + .then(ArgumentBuilderRequired.argument("point", ArgumentTypeVec3.vec3d()) |
| 38 | + .executes(CommandProtect::chunk))) |
| 39 | + .then(ArgumentBuilderLiteral.literal("radius") |
| 40 | + .then(ArgumentBuilderRequired.argument("radius", ArgumentTypeInteger.integer()) |
| 41 | + .executes(CommandProtect::radius)) |
| 42 | + .then(ArgumentBuilderRequired.argument("radius", ArgumentTypeInteger.integer()) |
| 43 | + .then(ArgumentBuilderRequired.argument("Point", ArgumentTypeVec3.vec3d()) |
| 44 | + .executes(CommandProtect::radius)))) |
| 45 | + .then(ArgumentBuilderLiteral.literal("points") |
| 46 | + .then(ArgumentBuilderRequired.argument("second point", ArgumentTypeVec3.vec3d()) |
| 47 | + .executes(CommandProtect::points)) |
| 48 | + .then(ArgumentBuilderRequired.argument("first point", ArgumentTypeVec3.vec3d()) |
| 49 | + .then(ArgumentBuilderRequired.argument("second point", ArgumentTypeVec3.vec3d()) |
| 50 | + .executes(CommandProtect::points))))); |
| 51 | + dispatcher.register(command); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + private static int chunk(CommandContext<Object> context) { |
| 56 | + if (!CommandlyConfig.SMART_VEINMINER && !CommandlyConfig.SMART_TREECAPITATOR) { |
| 57 | + ((CommandSource) context.getSource()).sendTranslatableMessage("commandly.all.inactive"); |
| 58 | + return code(CANNOT); |
| 59 | + } |
| 60 | + Predicate<Block<?>> treecapitator = (block) -> false; |
| 61 | + Predicate<Block<?>> veinmining = (block) -> false; |
| 62 | + if (CommandlyConfig.SMART_TREECAPITATOR) { |
| 63 | + treecapitator = (block) -> TreeCapitator.canTreecapitated(block); |
| 64 | + } |
| 65 | + if (CommandlyConfig.SMART_VEINMINER) { |
| 66 | + veinmining = (block) -> VeinMining.canBeVeinMinedCommand(block); |
| 67 | + } |
| 68 | + CommandSource source = (CommandSource) context.getSource(); |
| 69 | + World world = source.getWorld(); |
| 70 | + DoubleCoordinates p1; |
| 71 | + try { |
| 72 | + p1 = context.getArgument("point", DoubleCoordinates.class); |
| 73 | + } catch (IllegalArgumentException noargs) { |
| 74 | + Player player = source.getSender(); |
| 75 | + DoubleCoordinate x = new DoubleCoordinate(false, player.x); |
| 76 | + DoubleCoordinate y = new DoubleCoordinate(false, player.y); |
| 77 | + DoubleCoordinate z = new DoubleCoordinate(false, player.z); |
| 78 | + p1 = new DoubleCoordinates(x, y, z); |
| 79 | + } |
| 80 | + return chunks_protect(source, world, treecapitator, veinmining, p1); |
| 81 | + } |
| 82 | + |
| 83 | + private static int chunks_protect(CommandSource source, World world, Predicate<Block<?>> treecapitator, Predicate<Block<?>> veinmining, DoubleCoordinates p1) { |
| 84 | + int fx, fy, fz; |
| 85 | + try { |
| 86 | + fx = (int) Math.round(p1.getX(source)); |
| 87 | + fy = (int) Math.round(p1.getY(source, EnvironmentHelper.isServerEnvironment())); |
| 88 | + fz = (int) Math.round(p1.getZ(source)); |
| 89 | + } catch (CommandSyntaxException e) { |
| 90 | + throw new RuntimeException(e); |
| 91 | + } |
| 92 | + fx = Math.floorDiv(fx, 16) * 16; |
| 93 | + fz = Math.floorDiv(fz, 16) * 16; |
| 94 | + int count_protected = 0; |
| 95 | + for (int x = 0; x < 16; x++) { |
| 96 | + for (int y = 0; y <= world.getHeightBlocks(); y++) { |
| 97 | + for (int z = 0; z < 16; z++) { |
| 98 | + Block<?> block = world.getBlock(fx + x, y, fz + z); |
| 99 | + if (block == null) continue; |
| 100 | + if (treecapitator.test(block) || veinmining.test(block)) { |
| 101 | + world.setBlockMetadata(fx + x, y, fz + z, (1 << MASK)); |
| 102 | + count_protected++; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + if (count_protected == 0) { |
| 108 | + source.sendTranslatableMessage("commadly.protected.fail"); |
| 109 | + return code(FAIL); |
| 110 | + } |
| 111 | + source.sendTranslatableMessage("commadly.protected.active", count_protected); |
| 112 | + return code(OK); |
| 113 | + } |
| 114 | + |
| 115 | + private static int radius(CommandContext<Object> context) { |
| 116 | + if (!CommandlyConfig.SMART_VEINMINER && !CommandlyConfig.SMART_TREECAPITATOR) { |
| 117 | + ((CommandSource) context.getSource()).sendTranslatableMessage("commandly.all.inactive"); |
| 118 | + return code(CANNOT); |
| 119 | + } |
| 120 | + Predicate<Block<?>> treecapitator = (block) -> false; |
| 121 | + Predicate<Block<?>> veinmining = (block) -> false; |
| 122 | + if (CommandlyConfig.SMART_TREECAPITATOR) { |
| 123 | + treecapitator = (block) -> TreeCapitator.canTreecapitated(block); |
| 124 | + } |
| 125 | + if (CommandlyConfig.SMART_VEINMINER) { |
| 126 | + veinmining = (block) -> VeinMining.canBeVeinMinedCommand(block); |
| 127 | + } |
| 128 | + CommandSource source = (CommandSource) context.getSource(); |
| 129 | + World world = source.getWorld(); |
| 130 | + DoubleCoordinates p1; |
| 131 | + try { |
| 132 | + p1 = context.getArgument("point", DoubleCoordinates.class); |
| 133 | + } catch (IllegalArgumentException noargs) { |
| 134 | + Player player = source.getSender(); |
| 135 | + DoubleCoordinate x = new DoubleCoordinate(false, player.x); |
| 136 | + DoubleCoordinate y = new DoubleCoordinate(false, player.y); |
| 137 | + DoubleCoordinate z = new DoubleCoordinate(false, player.z); |
| 138 | + p1 = new DoubleCoordinates(x, y, z); |
| 139 | + } |
| 140 | + int radius = context.getArgument("radius", Integer.class); |
| 141 | + return radius_protect(source, world, treecapitator, veinmining, radius, p1); |
| 142 | + } |
| 143 | + |
| 144 | + private static int radius_protect( |
| 145 | + CommandSource source, World world, |
| 146 | + Predicate<Block<?>> treecapitator, |
| 147 | + Predicate<Block<?>> veinmining, |
| 148 | + int radius, |
| 149 | + DoubleCoordinates p1 |
| 150 | + ) { |
| 151 | + int fx, fy, fz; |
| 152 | + try { |
| 153 | + fx = (int) Math.round(p1.getX(source)); |
| 154 | + fy = (int) Math.round(p1.getY(source, EnvironmentHelper.isServerEnvironment())); |
| 155 | + fz = (int) Math.round(p1.getZ(source)); |
| 156 | + } catch (CommandSyntaxException e) { |
| 157 | + throw new RuntimeException(e); |
| 158 | + } |
| 159 | + |
| 160 | + if ((int) Math.floor(1.25 * Math.PI * Math.pow(radius, 3)) > MAX_BLOCK_COUNT) { |
| 161 | + source.sendTranslatableMessage("commadly.protected.tolarge"); |
| 162 | + return code(CANNOT); |
| 163 | + } |
| 164 | + int count_protected = 0; |
| 165 | + for (int x = -radius; x <= radius; x++) { |
| 166 | + for (int y = -radius; y <= radius; y++) { |
| 167 | + for (int z = -radius; z <= radius; z++) { |
| 168 | + if (x * x + y * y + z * z >= radius * radius) continue; |
| 169 | + Block<?> block = world.getBlock(fx + x, fy + y, fz + z); |
| 170 | + if (block == null) continue; |
| 171 | + if (treecapitator.test(block) || veinmining.test(block)) { |
| 172 | + world.setBlockMetadata(fx + x, fy + y, fz + z, (1 << MASK)); |
| 173 | + count_protected++; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + if (count_protected == 0) { |
| 179 | + source.sendTranslatableMessage("commadly.protected.fail"); |
| 180 | + return code(FAIL); |
| 181 | + } |
| 182 | + source.sendTranslatableMessage("commadly.protected.active", count_protected); |
| 183 | + return code(OK); |
| 184 | + } |
| 185 | + |
| 186 | + private static int points(CommandContext<Object> context) { |
| 187 | + if (!CommandlyConfig.SMART_VEINMINER && !CommandlyConfig.SMART_TREECAPITATOR) { |
| 188 | + ((CommandSource) context.getSource()).sendTranslatableMessage("commandly.all.inactive"); |
| 189 | + return code(CANNOT); |
| 190 | + } |
| 191 | + Predicate<Block<?>> treecapitator = (block) -> false; |
| 192 | + Predicate<Block<?>> veinmining = (block) -> false; |
| 193 | + if (CommandlyConfig.SMART_TREECAPITATOR) { |
| 194 | + treecapitator = (block) -> TreeCapitator.canTreecapitated(block); |
| 195 | + } |
| 196 | + if (CommandlyConfig.SMART_VEINMINER) { |
| 197 | + veinmining = (block) -> VeinMining.canBeVeinMinedCommand(block); |
| 198 | + } |
| 199 | + CommandSource sauce = (CommandSource) context.getSource(); |
| 200 | + World world = sauce.getWorld(); |
| 201 | + DoubleCoordinates p1; |
| 202 | + try { |
| 203 | + p1 = context.getArgument("first point", DoubleCoordinates.class); |
| 204 | + } catch (IllegalArgumentException noargs) { |
| 205 | + Player player = sauce.getSender(); |
| 206 | + DoubleCoordinate x = new DoubleCoordinate(false, player.x); |
| 207 | + DoubleCoordinate y = new DoubleCoordinate(false, player.y); |
| 208 | + DoubleCoordinate z = new DoubleCoordinate(false, player.z); |
| 209 | + p1 = new DoubleCoordinates(x, y, z); |
| 210 | + } |
| 211 | + DoubleCoordinates p2 = context.getArgument("second point", DoubleCoordinates.class); |
| 212 | + return point_protect(sauce, world, p1, p2, treecapitator, veinmining); |
| 213 | + } |
| 214 | + |
| 215 | + public static int point_protect( |
| 216 | + CommandSource source, World world, DoubleCoordinates p1, DoubleCoordinates p2, |
| 217 | + Predicate<Block<?>> treecapitator, |
| 218 | + Predicate<Block<?>> veinmining |
| 219 | + ) { |
| 220 | + int fx, fy, fz, sx, sy, sz; |
| 221 | + try { |
| 222 | + fx = (int) Math.round(p1.getX(source)); |
| 223 | + fy = (int) Math.round(p1.getY(source, EnvironmentHelper.isServerEnvironment())); |
| 224 | + fz = (int) Math.round(p1.getZ(source)); |
| 225 | + |
| 226 | + sx = (int) Math.round(p2.getX(source)); |
| 227 | + sy = (int) Math.round(p2.getY(source, EnvironmentHelper.isServerEnvironment())); |
| 228 | + sz = (int) Math.round(p2.getZ(source)); |
| 229 | + } catch (CommandSyntaxException e) { |
| 230 | + throw new RuntimeException(e); |
| 231 | + } |
| 232 | + if (Math.abs(fx - sx) * Math.abs(fy - sy) * Math.abs(fz - sz) > MAX_BLOCK_COUNT) { |
| 233 | + source.sendTranslatableMessage("commadly.protected.tolarge"); |
| 234 | + return code(CANNOT); |
| 235 | + } |
| 236 | + |
| 237 | + int count_protected = 0; |
| 238 | + for (int x = Math.min(fx, sx); x <= Math.max(fx, sx); x++) { |
| 239 | + for (int y = Math.min(fy, sy); y <= Math.max(fy, sy); y++) { |
| 240 | + for (int z = Math.min(fz, sz); z <= Math.max(fz, sz); z++) { |
| 241 | + Block<?> block = world.getBlock(x, y, z); |
| 242 | + if (block == null) continue; |
| 243 | + if (treecapitator.test(block) || veinmining.test(block)) { |
| 244 | + world.setBlockMetadata(x, y, z, (1 << MASK)); |
| 245 | + count_protected++; |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + if (count_protected == 0) { |
| 251 | + source.sendTranslatableMessage("commadly.protected.fail"); |
| 252 | + return code(FAIL); |
| 253 | + } |
| 254 | + source.sendTranslatableMessage("commadly.protected.active", count_protected); |
| 255 | + return code(OK); |
| 256 | + } |
| 257 | +} |
0 commit comments