From fe0ecccd4f079efb24dc99938f747a5c8c8fb6c7 Mon Sep 17 00:00:00 2001 From: Nameless-123 <53367549+Nameless-123@users.noreply.github.com> Date: Mon, 20 Apr 2020 13:44:49 +0200 Subject: [PATCH] Made drawCone WAY more fficient and code WAY less messy --- .../java/carpet/commands/DrawCommand.java | 214 +++++------------- 1 file changed, 57 insertions(+), 157 deletions(-) diff --git a/src/main/java/carpet/commands/DrawCommand.java b/src/main/java/carpet/commands/DrawCommand.java index 8f5a9858..9c0efe4c 100644 --- a/src/main/java/carpet/commands/DrawCommand.java +++ b/src/main/java/carpet/commands/DrawCommand.java @@ -36,13 +36,13 @@ public class DrawCommand { .then(literal("sphere").then(argument("center", BlockPosArgumentType.blockPos()) .then(argument("radius", IntegerArgumentType.integer(1)) .then(argument("block", BlockStateArgumentType.blockState()) - .executes((c) -> drawCircle(c.getSource(), + .executes((c) -> drawSphere(c.getSource(), BlockPosArgumentType.getBlockPos(c, "center"), IntegerArgumentType.getInteger(c, "radius"), BlockStateArgumentType.getBlockState(c, "block"), null)) .then(literal("replace") .then(argument("filter", BlockPredicateArgumentType.blockPredicate()) - .executes((c) -> drawCircle(c.getSource(), + .executes((c) -> drawSphere(c.getSource(), BlockPosArgumentType.getBlockPos(c, "center"), IntegerArgumentType.getInteger(c, "radius"), BlockStateArgumentType.getBlockState(c, "block"), @@ -77,20 +77,20 @@ public class DrawCommand { .then(argument("pointing up?",BoolArgumentType.bool()) .then(literal("x") .then(argument("block", BlockStateArgumentType.blockState()) - .executes((c) -> drawConex(c.getSource(), + .executes((c) -> drawCone(c.getSource(), BlockPosArgumentType.getBlockPos(c, "center"), IntegerArgumentType.getInteger(c, "radius"), IntegerArgumentType.getInteger(c, "height"), - BoolArgumentType.getBool(c,"pointing up?"), + BoolArgumentType.getBool(c,"pointing up?"),"x", BlockStateArgumentType.getBlockState(c, "block"), null) ) .then(literal("replace") .then(argument("filter", BlockPredicateArgumentType.blockPredicate()) - .executes((c) -> drawConex(c.getSource(), + .executes((c) -> drawCone(c.getSource(), BlockPosArgumentType.getBlockPos(c, "center"), IntegerArgumentType.getInteger(c, "radius"), IntegerArgumentType.getInteger(c, "height"), - BoolArgumentType.getBool(c,"pointing up?"), + BoolArgumentType.getBool(c,"pointing up?"),"x", BlockStateArgumentType.getBlockState(c, "block"), BlockPredicateArgumentType.getBlockPredicate(c,"filter") ) @@ -101,20 +101,20 @@ public class DrawCommand { ) .then(literal("y") .then(argument("block", BlockStateArgumentType.blockState()) - .executes((c) -> drawConey(c.getSource(), + .executes((c) -> drawCone(c.getSource(), BlockPosArgumentType.getBlockPos(c, "center"), IntegerArgumentType.getInteger(c, "radius"), IntegerArgumentType.getInteger(c, "height"), - BoolArgumentType.getBool(c,"pointing up?"), + BoolArgumentType.getBool(c,"pointing up?"),"y", BlockStateArgumentType.getBlockState(c, "block"), null) ) .then(literal("replace") .then(argument("filter", BlockPredicateArgumentType.blockPredicate()) - .executes((c) -> drawConey(c.getSource(), + .executes((c) -> drawCone(c.getSource(), BlockPosArgumentType.getBlockPos(c, "center"), IntegerArgumentType.getInteger(c, "radius"), IntegerArgumentType.getInteger(c, "height"), - BoolArgumentType.getBool(c,"pointing up?"), + BoolArgumentType.getBool(c,"pointing up?"),"y", BlockStateArgumentType.getBlockState(c, "block"), BlockPredicateArgumentType.getBlockPredicate(c,"filter") ) @@ -125,20 +125,20 @@ public class DrawCommand { ) .then(literal("z") .then(argument("block", BlockStateArgumentType.blockState()) - .executes((c) -> drawConez(c.getSource(), + .executes((c) -> drawCone(c.getSource(), BlockPosArgumentType.getBlockPos(c, "center"), IntegerArgumentType.getInteger(c, "radius"), IntegerArgumentType.getInteger(c, "height"), - BoolArgumentType.getBool(c,"pointing up?"), + BoolArgumentType.getBool(c,"pointing up?"),"z", BlockStateArgumentType.getBlockState(c, "block"), null) ) .then(literal("replace") .then(argument("filter", BlockPredicateArgumentType.blockPredicate()) - .executes((c) -> drawConez(c.getSource(), + .executes((c) -> drawCone(c.getSource(), BlockPosArgumentType.getBlockPos(c, "center"), IntegerArgumentType.getInteger(c, "radius"), IntegerArgumentType.getInteger(c, "height"), - BoolArgumentType.getBool(c,"pointing up?"), + BoolArgumentType.getBool(c,"pointing up?"),"z", BlockStateArgumentType.getBlockState(c, "block"), BlockPredicateArgumentType.getBlockPredicate(c,"filter") ) @@ -155,12 +155,12 @@ public class DrawCommand { dispatcher.register(command); } - private static int drawCircle(ServerCommandSource source, BlockPos pos, int radius, BlockStateArgument block, + private static int drawSphere(ServerCommandSource source, BlockPos pos, int radius, BlockStateArgument block, Predicate replacement) { - return drawCircle(source, pos, (double) radius, (double) radius, (double) radius, block, replacement, false); + return drawSphere(source, pos, (double) radius, (double) radius, (double) radius, block, replacement, false); } - private static int drawCircle(ServerCommandSource source, BlockPos pos, double radiusX, double radiusY, + private static int drawSphere(ServerCommandSource source, BlockPos pos, double radiusX, double radiusY, double radiusZ, BlockStateArgument block, Predicate replacement, boolean solid) { int affected = 0; ServerWorld world = source.getWorld(); @@ -314,80 +314,56 @@ public class DrawCommand { return 1; } - private static int drawConey(ServerCommandSource source, BlockPos pos, int radius, int height, boolean pointup, BlockStateArgument block, - Predicate replacement) { - return (int) drawConey(source, pos, radius, height, block, replacement, pointup, false); - } + private static int drawCircle(ServerCommandSource source, BlockPos pos, int offset, int radius, int height, boolean pointup, BlockStateArgument block, String orientation, + Predicate replacement, List list, BlockPos.Mutable mbpos){ + + if(orientation=="x"){ + for(int y=-radius;y<=radius;++y){ - private static double drawConey(ServerCommandSource source, BlockPos pos, int radius, int height,BlockStateArgument block, - Predicate replacement, boolean pointup, boolean solid) { - int affected = 0; - int offset = 0; - if(pointup){ - offset=height-1; - } - BlockPos.Mutable mbpos = new BlockPos.Mutable(pos); + for(int z=-radius;z<=radius;++z){ - List list = Lists.newArrayList(); - - ServerWorld world = source.getWorld(); - - CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates; - - for (int x = -radius; x <= radius; ++x) { - - for (int y = -offset; y <= height-offset-1; ++y) { - - for (int z = -radius; z <= radius; ++z) { - - if (!(x*x+z*z<=(radius/height*y)*(radius/height*y))) { - continue; - } - - mbpos.set(pos.getX() + x, pos.getY() + y + offset, pos.getZ() + z); - if (replacement == null || replacement.test(new CachedBlockPosition(world, mbpos, true))) { - BlockEntity tileentity = world.getBlockEntity(mbpos); - - if (tileentity instanceof Inventory) { - ((Inventory) tileentity).clear(); - } - - if (block.setBlockState(world, mbpos, 2)) { - list.add(mbpos.toImmutable()); - ++affected; - } + if(y*y+z*z<=radius*radius){ + blockset(source, pos.getX()+offset, pos.getY()+y, pos.getZ()+z, replacement, list, mbpos, block); } } } } - CarpetSettings.impendingFillSkipUpdates = false; + if(orientation=="y"){ + for(int x=-radius;x<=radius;++x){ - if (CarpetSettings.fillUpdates) { + for(int z=-radius;z<=radius;++z){ - for (BlockPos blockpos1 : list) { - Block blokc = world.getBlockState(blockpos1).getBlock(); - world.updateNeighbors(blockpos1, blokc); + if(x*x+z*z<=radius*radius){ + blockset(source, pos.getX()+x, pos.getY()+offset, pos.getZ()+z, replacement, list, mbpos, block); + } + } } } - Messenger.m(source, "gi Filled " + affected + " blocks"); + if(orientation=="z"){ + for(int y=-radius;y<=radius;++y){ + + for(int x=-radius;x<=radius;++x){ + + if(y*y+x*x<=radius*radius){ + blockset(source, pos.getX()+x, pos.getY()+y, pos.getZ()+offset, replacement, list, mbpos, block); + } + } + } + } return 1; } - - private static int drawConex(ServerCommandSource source, BlockPos pos, int radius, int height, boolean pointup, BlockStateArgument block, + + private static int drawCone(ServerCommandSource source, BlockPos pos, int radius, int height, boolean pointup, String orientation, BlockStateArgument block, Predicate replacement) { - return (int) drawConex(source, pos, radius, height, block, replacement, pointup, false); + return (int) drawCone(source, pos, radius, height, block, replacement, pointup, orientation, false); } - private static int drawConex(ServerCommandSource source, BlockPos pos, int radius, int height,BlockStateArgument block, - Predicate replacement, boolean pointup, boolean solid) { + private static double drawCone(ServerCommandSource source, BlockPos pos, int radius, int height,BlockStateArgument block, + Predicate replacement, boolean pointup, String orientation, boolean solid) { int affected = 0; - int offset = 0; - if(pointup){ - offset=height-1; - } BlockPos.Mutable mbpos = new BlockPos.Mutable(pos); List list = Lists.newArrayList(); @@ -396,95 +372,19 @@ public class DrawCommand { CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates; - for (int x = -offset; x <= height-offset-1; ++x) { + int r = radius; - for (int y = -radius; y <= radius; ++y) { - - for (int z = -radius; z <= radius; ++z) { - - if (!(y*y+z*z<=(radius/height*x)*(radius/height*x))) { - continue; - } - - mbpos.set(pos.getX() + x+ offset, pos.getY() + y, pos.getZ() + z); - if (replacement == null || replacement.test(new CachedBlockPosition(world, mbpos, true))) { - BlockEntity tileentity = world.getBlockEntity(mbpos); - - if (tileentity instanceof Inventory) { - ((Inventory) tileentity).clear(); - } - - if (block.setBlockState(world, mbpos, 2)) { - list.add(mbpos.toImmutable()); - ++affected; - } - } - } + for(int i =0; i replacement) { - return (int) drawConez(source, pos, radius, height, block, replacement, pointup, false); - } - - private static int drawConez(ServerCommandSource source, BlockPos pos, int radius, int height,BlockStateArgument block, - Predicate replacement, boolean pointup, boolean solid) { - int affected = 0; - int offset = 0; - if(pointup){ - offset=height-1; - } - BlockPos.Mutable mbpos = new BlockPos.Mutable(pos); - - List list = Lists.newArrayList(); - - ServerWorld world = source.getWorld(); - - CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates; - - for (int z = -offset; z <= height-offset-1; ++z) { - - for (int y = -radius; y <= radius; ++y) { - - for (int x = -radius; x <= radius; ++x) { - - if (!(x*x+y*y<=(radius/height*z)*(radius/height*z))) { - continue; - } - - mbpos.set(pos.getX() + x, pos.getY() + y, pos.getZ() + z + offset); - if (replacement == null || replacement.test(new CachedBlockPosition(world, mbpos, true))) { - BlockEntity tileentity = world.getBlockEntity(mbpos); - - if (tileentity instanceof Inventory) { - ((Inventory) tileentity).clear(); - } - - if (block.setBlockState(world, mbpos, 2)) { - list.add(mbpos.toImmutable()); - ++affected; - } - } - } - } - } - + CarpetSettings.impendingFillSkipUpdates = false; if (CarpetSettings.fillUpdates) {