Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up the brush command #348

Open
wants to merge 3 commits into
base: sponge-1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/com/thevoxelbox/voxelsniper/Brushes.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.spongepowered.api.Sponge;

import java.util.Map;
import java.util.Set;

/**
* Brush registration manager.
Expand Down Expand Up @@ -78,8 +79,8 @@ public static int getBrushCount() {
return brushes.size();
}

public static String getAllBrushes() {
return String.join(", ", brushes.keySet());
public static Set<String> getAllBrushes() {
return brushes.keySet();
}

private Brushes() {
Expand Down
30 changes: 13 additions & 17 deletions src/main/java/com/thevoxelbox/voxelsniper/brush/PerformBrush.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,36 @@ public abstract class PerformBrush extends Brush {
protected PerformerType replace = PerformerType.NONE;
protected boolean physics = true;

public void parse(String[] args, SnipeData v) {
String handle = args[0];
public boolean setPerformer(String handle) {
handle = handle.toLowerCase();

if (PERFORMER.matcher(handle).matches()) {
char p = handle.charAt(0);
if (p == 'm' || p == 'M') {
if (p == 'm') {
this.place = PerformerType.TYPE;
} else if (p == 'c' || p == 'C') {
} else if (p == 'c') {
this.place = PerformerType.COMBO;
}

int i = 1;
if (handle.length() >= 2) {
char r = handle.charAt(i);
i = 2;
if (r == 'm' || r == 'M') {
if (r == 'm') {
this.replace = PerformerType.TYPE;
} else if (r == 'c' || r == 'C') {
} else if (r == 'c') {
this.replace = PerformerType.COMBO;
} else {
i = 1;
this.replace = PerformerType.NONE;
}
}
if (handle.length() >= i + 1) {
char e = handle.charAt(i);
if (e == 'p' || e == 'P') {
this.physics = false;
} else {
this.physics = true;
}
}
parameters(Arrays.copyOfRange(args, 1, args.length), v);
} else {
parameters(args, v);

this.physics = handle.length() <= i || handle.charAt(i) != p;
return true;
}

return false;
}

public void showInfo(Message vm) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* This file is part of VoxelSniper, licensed under the MIT License (MIT).
*
* Copyright (c) The VoxelBox <http://thevoxelbox.com>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.thevoxelbox.voxelsniper.command;

import com.thevoxelbox.voxelsniper.Brushes;
import com.thevoxelbox.voxelsniper.brush.Brush;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.ArgumentParseException;
import org.spongepowered.api.command.args.CommandArgs;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.CommandElement;
import org.spongepowered.api.text.Text;

import java.util.ArrayList;
import java.util.List;

public class BrushCommandElement extends CommandElement {
private List<String> brushes;

protected BrushCommandElement(Text t) {
super(t);
}

@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
CommandArgs.Snapshot argSnapshot = args.getSnapshot();
String brushInput = args.next();
Class<? extends Brush> brush = Brushes.getBrushForHandle(brushInput);

if (brush == null) {
args.applySnapshot(argSnapshot);
throw args.createError(Text.of("Cannot find brush \"" + brushInput + "\""));
}

return brush;
}

@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
if (brushes == null) {
brushes = new ArrayList<>(Brushes.getAllBrushes());
}

String nextArg = "";
try {
nextArg = args.peek();
} catch (ArgumentParseException ignored) {}

List<String> possibleBrushes = new ArrayList<>();
for (String brush : brushes) {
if (brush.startsWith(nextArg)) {
possibleBrushes.add(brush);
}
}

return possibleBrushes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
import com.thevoxelbox.voxelsniper.SniperManager;
import com.thevoxelbox.voxelsniper.VoxelSniperConfiguration;
import com.thevoxelbox.voxelsniper.brush.Brush;
import com.thevoxelbox.voxelsniper.brush.PerformBrush;
import com.thevoxelbox.voxelsniper.event.sniper.ChangeBrushEvent;
import com.thevoxelbox.voxelsniper.event.sniper.ChangeBrushSizeEvent;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
Expand All @@ -53,68 +52,61 @@ public static void setup(Object plugin) {
.register(plugin,
CommandSpec.builder()
.arguments(
GenericArguments.optional(GenericArguments.string(Text.of("brush"))),
GenericArguments.playerOrSource(Text.of("sniper")),
GenericArguments.optional(new BrushCommandElement(Text.of("brush"))),
GenericArguments.optionalWeak(GenericArguments.doubleNum(Text.of("brush_size"))),
GenericArguments.optional(GenericArguments.remainingJoinedStrings(Text.of("brush_args"))))
.executor(new VoxelBrushCommand())
.permission(VoxelSniperConfiguration.PERMISSION_SNIPER)
.description(Text.of("VoxelSniper brush settings")).build(),
.description(Text.of("Set VoxelSniper brush")).build(),
"b", "brush");
}

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("Player only."));
return CommandResult.success();
}
Player player = (Player) src;
public CommandResult execute(CommandSource src, CommandContext args) {
Player player = (Player) args.getOne("sniper").get();
Sniper sniper = SniperManager.get().getSniperForPlayer(player);
String currentToolId = sniper.getCurrentToolId();
SnipeData snipeData = sniper.getSnipeData(currentToolId);

Optional<String> brush_selection = args.getOne("brush");
if (!brush_selection.isPresent()) {
sniper.displayInfo();
return CommandResult.success();
Optional<Class<? extends Brush>> optBrush = args.getOne("brush");
if (optBrush.isPresent()) {
Class<? extends Brush> newBrushClass = optBrush.get();
Brush oldBrush = sniper.getBrush(currentToolId);

if (!oldBrush.getClass().equals(newBrushClass)) {
sniper.setBrush(currentToolId, newBrushClass);
Brush newBrush = sniper.getBrush(currentToolId);

ChangeBrushEvent event = new ChangeBrushEvent(Sponge.getCauseStackManager().getCurrentCause(), snipeData, newBrush);
Sponge.getEventManager().post(event);
}
}

try {
double newBrushSize = Double.parseDouble(brush_selection.get());
Optional<Double> optBrushSize = args.getOne("brush_size");
if (optBrushSize.isPresent()) {
double newBrushSize = optBrushSize.get();
if (!player.hasPermission(VoxelSniperConfiguration.PERMISSION_IGNORE_SIZE_LIMITS)
&& newBrushSize > VoxelSniperConfiguration.LITESNIPER_MAX_BRUSH_SIZE) {
player.sendMessage(
Text.of(TextColors.RED, "Size is restricted to " + VoxelSniperConfiguration.LITESNIPER_MAX_BRUSH_SIZE + " for you."));
newBrushSize = VoxelSniperConfiguration.LITESNIPER_MAX_BRUSH_SIZE;
}

ChangeBrushSizeEvent event = new ChangeBrushSizeEvent(Sponge.getCauseStackManager().getCurrentCause(), snipeData, newBrushSize);
Sponge.getEventManager().post(event);
snipeData.setBrushSize(newBrushSize);
snipeData.getVoxelMessage().size();
return CommandResult.success();
} catch (NumberFormatException ingored) {
}
Optional<String> brush_args = args.getOne("brush_args");
Class<? extends Brush> brush = Brushes.getBrushForHandle(brush_selection.get());
if (brush != null) {
Brush existing = sniper.getBrush(currentToolId);
sniper.setBrush(currentToolId, brush);

if (brush_args.isPresent()) {
String[] bargs = brush_args.get().split(" ");
Brush currentBrush = sniper.getBrush(currentToolId);
if (currentBrush instanceof PerformBrush) {
((PerformBrush) currentBrush).parse(bargs, snipeData);
} else {
// @Cleanup parse out flags and pass as separate set
currentBrush.parameters(bargs, snipeData);
}
}
if (existing == null || existing.getClass() != brush) {
sniper.displayInfo();
}
} else {
player.sendMessage(Text.of(TextColors.RED, "Couldn't find Brush for brush handle \"" + brush_selection.get() + "\""));
Optional<String> brush_args = args.getOne("brush_args");
if (brush_args.isPresent()) {
String[] bargs = brush_args.get().split(" ");
Brush currentBrush = sniper.getBrush(currentToolId);
currentBrush.parameters(bargs, snipeData);
}

sniper.displayInfo();
return CommandResult.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.thevoxelbox.voxelsniper.command;

import com.thevoxelbox.voxelsniper.Message;
import com.thevoxelbox.voxelsniper.SnipeData;
import com.thevoxelbox.voxelsniper.Sniper;
import com.thevoxelbox.voxelsniper.SniperManager;
Expand Down Expand Up @@ -62,7 +63,11 @@ public CommandResult execute(CommandSource src, CommandContext gargs) throws Com

Brush brush = sniper.getBrush(sniper.getCurrentToolId());
if (brush instanceof PerformBrush) {
((PerformBrush) brush).parse(new String[] { performer }, snipeData);
if (((PerformBrush) brush).setPerformer(performer)) {
(new Message(snipeData)).performerName(performer);
} else {
player.sendMessage(Text.of(TextColors.RED, "Problem parsing performers \"" + performer + "\""));
}
} else {
player.sendMessage(Text.of(TextColors.RED, "This brush is not a performer brush."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.Iterator;
import java.util.Optional;

public class VoxelSniperCommand implements CommandExecutor {
Expand All @@ -64,7 +65,7 @@ public CommandResult execute(CommandSource src, CommandContext gargs) throws Com
String[] args = oargs.get().split(" ");
if (args[0].equalsIgnoreCase("brushes")) {
player.sendMessage(Text.of(TextColors.AQUA, "All available brushes:"));
player.sendMessage(Text.of(Brushes.getAllBrushes()));
player.sendMessage(Text.of(String.join(", ", Brushes.getAllBrushes())));
return CommandResult.success();
} else if (args[0].equalsIgnoreCase("version")) {
player.sendMessage(Text.of(TextColors.AQUA, "VoxelSniper version " + VoxelSniperConfiguration.PLUGIN_VERSION));
Expand Down