mirror of
https://github.com/YouHaveTrouble/BlockEdit.git
synced 2026-06-29 21:46:19 +00:00
allow set and replace commands to take block data along with materials as args
This commit is contained in:
@@ -2,6 +2,9 @@ package me.youhavetrouble.blockedit.commands;
|
|||||||
|
|
||||||
import me.youhavetrouble.blockedit.BEPlayer;
|
import me.youhavetrouble.blockedit.BEPlayer;
|
||||||
import me.youhavetrouble.blockedit.api.BlockEditAPI;
|
import me.youhavetrouble.blockedit.api.BlockEditAPI;
|
||||||
|
import me.youhavetrouble.blockedit.commands.arguments.BlockDataArgument;
|
||||||
|
import me.youhavetrouble.blockedit.commands.arguments.InvalidDataException;
|
||||||
|
import me.youhavetrouble.blockedit.commands.arguments.InvalidMaterialException;
|
||||||
import me.youhavetrouble.blockedit.operations.ReplaceOperation;
|
import me.youhavetrouble.blockedit.operations.ReplaceOperation;
|
||||||
import me.youhavetrouble.blockedit.util.Selection;
|
import me.youhavetrouble.blockedit.util.Selection;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
@@ -28,8 +31,7 @@ public class ReplaceCommand extends Command {
|
|||||||
if (args.length == 1 || args.length == 2) {
|
if (args.length == 1 || args.length == 2) {
|
||||||
ArrayList<String> suggestions = new ArrayList<>();
|
ArrayList<String> suggestions = new ArrayList<>();
|
||||||
for (Material material : Material.values()) {
|
for (Material material : Material.values()) {
|
||||||
if (material.isBlock())
|
if (material.isBlock()) suggestions.add(material.name().toLowerCase());
|
||||||
suggestions.add(material.name().toLowerCase());
|
|
||||||
}
|
}
|
||||||
return StringUtil.copyPartialMatches(args[args.length-1], suggestions, new ArrayList<>());
|
return StringUtil.copyPartialMatches(args[args.length-1], suggestions, new ArrayList<>());
|
||||||
}
|
}
|
||||||
@@ -47,26 +49,39 @@ public class ReplaceCommand extends Command {
|
|||||||
player.sendMessage(Component.text("You need to provide block type to replace"));
|
player.sendMessage(Component.text("You need to provide block type to replace"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Material materialToReplace = Material.getMaterial(args[1].toUpperCase());
|
|
||||||
if (materialToReplace == null) {
|
BlockData blockData = null;
|
||||||
player.sendMessage(Component.text("Provided material does not exist"));
|
BlockData blockDataToReplaceWith = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
blockData = BlockDataArgument.getBlockData(args[0]);
|
||||||
|
} catch (InvalidMaterialException e) {
|
||||||
|
player.sendMessage(Component.text("Provided block type does not exist"));
|
||||||
return true;
|
return true;
|
||||||
}
|
} catch (InvalidDataException e) {
|
||||||
Material material = Material.getMaterial(args[0].toUpperCase());
|
player.sendMessage(Component.text("Provided block data is invalid"));
|
||||||
if (material == null) {
|
return true;
|
||||||
player.sendMessage(Component.text("Provided material does not exist"));
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
blockDataToReplaceWith = BlockDataArgument.getBlockData(args[1]);
|
||||||
|
} catch (InvalidMaterialException e) {
|
||||||
|
player.sendMessage(Component.text("Provided block type does not exist"));
|
||||||
|
return true;
|
||||||
|
} catch (InvalidDataException e) {
|
||||||
|
player.sendMessage(Component.text("Provided block data is invalid"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockData blockData = material.createBlockData();
|
|
||||||
BlockData blockDataToReplaceWith = materialToReplace.createBlockData();
|
|
||||||
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
|
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
|
||||||
Selection selection = bePlayer.getSelection();
|
Selection selection = bePlayer.getSelection();
|
||||||
if (selection == null) {
|
if (selection == null) {
|
||||||
player.sendMessage(Component.text("You need to select 2 points to do this"));
|
player.sendMessage(Component.text("You need to select 2 points to do this"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockEditAPI.runOperation(selection, 1, new ReplaceOperation(blockData, blockDataToReplaceWith));
|
BlockEditAPI.runOperation(selection, 1, new ReplaceOperation(blockData, blockDataToReplaceWith));
|
||||||
|
player.sendMessage(Component.text("Replacing blocks..."));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package me.youhavetrouble.blockedit.commands;
|
|||||||
|
|
||||||
import me.youhavetrouble.blockedit.BEPlayer;
|
import me.youhavetrouble.blockedit.BEPlayer;
|
||||||
import me.youhavetrouble.blockedit.api.BlockEditAPI;
|
import me.youhavetrouble.blockedit.api.BlockEditAPI;
|
||||||
|
import me.youhavetrouble.blockedit.commands.arguments.BlockDataArgument;
|
||||||
|
import me.youhavetrouble.blockedit.commands.arguments.InvalidDataException;
|
||||||
|
import me.youhavetrouble.blockedit.commands.arguments.InvalidMaterialException;
|
||||||
import me.youhavetrouble.blockedit.operations.SetOperation;
|
import me.youhavetrouble.blockedit.operations.SetOperation;
|
||||||
import me.youhavetrouble.blockedit.util.Selection;
|
import me.youhavetrouble.blockedit.util.Selection;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
@@ -33,37 +36,6 @@ public class SetCommand extends Command {
|
|||||||
}
|
}
|
||||||
return StringUtil.copyPartialMatches(args[0], suggestions, new ArrayList<>());
|
return StringUtil.copyPartialMatches(args[0], suggestions, new ArrayList<>());
|
||||||
}
|
}
|
||||||
// TODO refactor and/or abstract this
|
|
||||||
if (args.length > 1) {
|
|
||||||
Material material = Material.getMaterial(args[0].toUpperCase());
|
|
||||||
if (material == null) return suggestions;
|
|
||||||
if (!material.isBlock()) return suggestions;
|
|
||||||
BlockData blockData = material.createBlockData();
|
|
||||||
String[] split = args[args.length-1].split("=");
|
|
||||||
if (split.length == 1) {
|
|
||||||
String datas = blockData.getAsString(false);
|
|
||||||
String[] nameAndDatas = datas.split("\\[");
|
|
||||||
if (nameAndDatas.length != 2) return suggestions;
|
|
||||||
datas = nameAndDatas[1].substring(0, nameAndDatas[1].length()-2);
|
|
||||||
String[] splitDatas = datas.split(",");
|
|
||||||
for (String data : splitDatas) {
|
|
||||||
String[] splitData = data.split("=");
|
|
||||||
if (splitData.length != 2) continue;
|
|
||||||
String suggestion = splitData[0]+"=";
|
|
||||||
boolean alreadyUsed = false;
|
|
||||||
for (String arg : args) {
|
|
||||||
if (arg.startsWith(suggestion)) {
|
|
||||||
alreadyUsed = true;
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (alreadyUsed) continue;
|
|
||||||
suggestions.add(suggestion);
|
|
||||||
}
|
|
||||||
return StringUtil.copyPartialMatches(split[0], suggestions, new ArrayList<>());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,37 +51,29 @@ public class SetCommand extends Command {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length != 1) {
|
||||||
player.sendMessage(Component.text("You need to provide block type"));
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockData blockData;
|
BlockData blockData;
|
||||||
try {
|
try {
|
||||||
blockData = getBlockData(args);
|
blockData = BlockDataArgument.getBlockData(args[0]);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (InvalidMaterialException e) {
|
||||||
|
player.sendMessage(Component.text("Provided block type does not exist"));
|
||||||
|
return true;
|
||||||
|
} catch (InvalidDataException e) {
|
||||||
player.sendMessage(Component.text("Provided block data is invalid"));
|
player.sendMessage(Component.text("Provided block data is invalid"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (blockData == null) {
|
|
||||||
player.sendMessage(Component.text("Provided material does not exist"));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockEditAPI.runOperation(selection, 1, new SetOperation(blockData));
|
BlockEditAPI.runOperation(selection, 1, new SetOperation(blockData));
|
||||||
|
player.sendMessage(Component.text("Setting blocks..."));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
private BlockData getBlockData(String[] args) throws IllegalArgumentException {
|
public @NotNull String getUsage() {
|
||||||
ArrayList<String> argsList = new ArrayList<>(List.of(args));
|
return "/set <block[block=data]>";
|
||||||
if (argsList.size() == 0) return null;
|
|
||||||
Material material = Material.getMaterial(argsList.get(0).toUpperCase());
|
|
||||||
if (material == null) return null;
|
|
||||||
argsList.remove(0);
|
|
||||||
if (argsList.size() == 0) return material.createBlockData();
|
|
||||||
String dataString = "[" + String.join(",", argsList) + "]";
|
|
||||||
return material.createBlockData(dataString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package me.youhavetrouble.blockedit.commands.arguments;
|
||||||
|
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.block.data.BlockData;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
|
||||||
|
public class BlockDataArgument {
|
||||||
|
|
||||||
|
public static BlockData getBlockData(String string) throws InvalidMaterialException, InvalidDataException {
|
||||||
|
|
||||||
|
String[] split = string.split("\\[");
|
||||||
|
Material material = null;
|
||||||
|
BlockData blockData = null;
|
||||||
|
if (split.length >= 1) {
|
||||||
|
String materialString = split[0].toUpperCase(Locale.ENGLISH);
|
||||||
|
material = Material.getMaterial(materialString);
|
||||||
|
if (material == null) throw new InvalidMaterialException();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (split.length == 1) {
|
||||||
|
blockData = material.createBlockData();
|
||||||
|
}
|
||||||
|
if (split.length == 2) {
|
||||||
|
try {
|
||||||
|
blockData = material.createBlockData("[" + split[1]);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw new InvalidDataException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return blockData;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package me.youhavetrouble.blockedit.commands.arguments;
|
||||||
|
|
||||||
|
public class InvalidDataException extends IllegalArgumentException {
|
||||||
|
|
||||||
|
public InvalidDataException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package me.youhavetrouble.blockedit.commands.arguments;
|
||||||
|
|
||||||
|
public class InvalidMaterialException extends IllegalArgumentException {
|
||||||
|
|
||||||
|
public InvalidMaterialException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ public record ReplaceOperation(BlockData blockToReplace, BlockData blockToSet) i
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void transformBlock(Block block) {
|
public void transformBlock(Block block) {
|
||||||
if (block.getBlockData().matches(blockToReplace))
|
if (!block.getBlockData().matches(blockToReplace)) return;
|
||||||
block.setBlockData(blockToSet);
|
block.setBlockData(blockToSet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user