use of com.sk89q.worldedit.extent.inventory.BlockBag in project FastAsyncWorldEdit by IntellectualSites.
the class DefaultBlockParser method parseLogic.
private BaseBlock parseLogic(String input, ParserContext context) throws InputParseException {
// FAWE start
String[] blockAndExtraData = input.trim().split("\\|", 2);
blockAndExtraData[0] = woolMapper(blockAndExtraData[0]);
Map<Property<?>, Object> blockStates = new HashMap<>();
// FAWE end
BlockState state = null;
// Legacy matcher
if (context.isTryingLegacy()) {
try {
String[] split = blockAndExtraData[0].split(":", 2);
if (split.length == 0) {
throw new InputParseException(Caption.of("worldedit.error.parser.invalid-colon"));
} else if (split.length == 1) {
state = LegacyMapper.getInstance().getBlockFromLegacy(Integer.parseInt(split[0]));
} else if (MathMan.isInteger(split[0])) {
int id = Integer.parseInt(split[0]);
int data = Integer.parseInt(split[1]);
// FAWE start
if (data < 0 || data >= 16) {
throw new InputParseException(Caption.of("fawe.error.parser.invalid-data", TextComponent.of(data)));
}
state = LegacyMapper.getInstance().getBlockFromLegacy(id, data);
} else {
BlockType type = BlockTypes.get(split[0].toLowerCase(Locale.ROOT));
if (type != null) {
int data = Integer.parseInt(split[1]);
if (data < 0 || data >= 16) {
throw new InputParseException(Caption.of("fawe.error.parser.invalid-data", TextComponent.of(data)));
}
state = LegacyMapper.getInstance().getBlockFromLegacy(type.getLegacyCombinedId() >> 4, data);
}
}
} catch (NumberFormatException ignored) {
}
}
CompoundTag nbt = null;
// FAWE end
if (state == null) {
String typeString;
String stateString = null;
int stateStart = blockAndExtraData[0].indexOf('[');
if (stateStart == -1) {
typeString = blockAndExtraData[0];
} else {
typeString = blockAndExtraData[0].substring(0, stateStart);
if (stateStart + 1 >= blockAndExtraData[0].length()) {
throw new InputParseException(Caption.of("worldedit.error.parser.hanging-lbracket", TextComponent.of(stateStart)));
}
int stateEnd = blockAndExtraData[0].lastIndexOf(']');
if (stateEnd < 0) {
throw new InputParseException(Caption.of("worldedit.error.parser.missing-rbracket"));
}
stateString = blockAndExtraData[0].substring(stateStart + 1, blockAndExtraData[0].length() - 1);
}
String[] stateProperties = EMPTY_STRING_ARRAY;
if (stateString != null) {
stateProperties = stateString.split(",");
}
if (typeString.isEmpty()) {
throw new InputParseException(Caption.of("worldedit.error.parser.bad-state-format", TextComponent.of(blockAndExtraData[0])));
}
if ("hand".equalsIgnoreCase(typeString)) {
// Get the block type from the item in the user's hand.
final BaseBlock blockInHand = getBlockInHand(context.requireActor(), HandSide.MAIN_HAND);
// FAWE start
state = blockInHand.toBlockState();
nbt = blockInHand.getNbtData();
// FAWE end
} else if ("offhand".equalsIgnoreCase(typeString)) {
// Get the block type from the item in the user's off hand.
final BaseBlock blockInHand = getBlockInHand(context.requireActor(), HandSide.OFF_HAND);
// FAWE start
state = blockInHand.toBlockState();
nbt = blockInHand.getNbtData();
// FAWE end
} else if (typeString.matches("pos[0-9]+")) {
int index = Integer.parseInt(typeString.replaceAll("[a-z]+", ""));
// Get the block type from the "primary position"
final World world = context.requireWorld();
final BlockVector3 primaryPosition;
try {
primaryPosition = context.requireSession().getRegionSelector(world).getVertices().get(index - 1);
} catch (IncompleteRegionException e) {
throw new InputParseException(Caption.of("worldedit.error.incomplete-region"));
}
state = world.getBlock(primaryPosition);
nbt = state.getNbtData();
// FAWE start
} else if (typeString.matches("slot[0-9]+")) {
int slot = Integer.parseInt(typeString.substring(4)) - 1;
Actor actor = context.requireActor();
if (!(actor instanceof Player)) {
throw new InputParseException(Caption.of("worldedit.command.player-only"));
}
Player player = (Player) actor;
BlockBag bag = player.getInventoryBlockBag();
if (!(bag instanceof SlottableBlockBag)) {
throw new InputParseException(Caption.of("fawe.error.unsupported"));
}
SlottableBlockBag slottable = (SlottableBlockBag) bag;
BaseItem item = slottable.getItem(slot);
if (!item.getType().hasBlockType()) {
throw new InputParseException(Caption.of("worldedit.error.not-a-block"));
}
state = item.getType().getBlockType().getDefaultState();
nbt = item.getNbtData();
} else {
BlockType type = BlockTypes.parse(typeString.toLowerCase(Locale.ROOT));
if (type != null) {
state = type.getDefaultState();
}
if (state == null) {
throw new NoMatchException(Caption.of("fawe.error.invalid-block-type", TextComponent.of(input)));
}
}
// FAWE end
// FAWE start - Not null if nullNotError false.
blockStates.putAll(parseProperties(state.getBlockType(), stateProperties, context, false));
// FAWE end
if (context.isPreferringWildcard()) {
if (stateString == null || stateString.isEmpty()) {
state = new FuzzyBlockState(state);
} else {
FuzzyBlockState.Builder fuzzyBuilder = FuzzyBlockState.builder();
fuzzyBuilder.type(state.getBlockType());
for (Map.Entry<Property<?>, Object> blockState : blockStates.entrySet()) {
@SuppressWarnings("unchecked") Property<Object> objProp = (Property<Object>) blockState.getKey();
fuzzyBuilder.withProperty(objProp, blockState.getValue());
}
state = fuzzyBuilder.build();
}
} else {
for (Map.Entry<Property<?>, Object> blockState : blockStates.entrySet()) {
@SuppressWarnings("unchecked") Property<Object> objProp = (Property<Object>) blockState.getKey();
state = state.with(objProp, blockState.getValue());
}
}
}
// this should be impossible but IntelliJ isn't that smart
if (state == null) {
throw new NoMatchException(Caption.of("worldedit.error.unknown-block", TextComponent.of(input)));
}
// FAWE start
if (blockAndExtraData.length > 1 && blockAndExtraData[1].startsWith("{")) {
String joined = StringMan.join(Arrays.copyOfRange(blockAndExtraData, 1, blockAndExtraData.length), "|");
try {
nbt = JSON2NBT.getTagFromJson(joined);
} catch (NBTException e) {
throw new NoMatchException(TextComponent.of(e.getMessage()));
}
}
// FAWE end
// Check if the item is allowed
BlockType blockType = state.getBlockType();
if (context.isRestricted()) {
Actor actor = context.requireActor();
// FAWE start - per-limit disallowed blocks
if (actor != null) {
if (!actor.hasPermission("worldedit.anyblock") && worldEdit.getConfiguration().disallowedBlocks.contains(blockType.getId().toLowerCase(Locale.ROOT))) {
throw new DisallowedUsageException(Caption.of("worldedit.error.disallowed-block", TextComponent.of(blockType.getId())));
}
FaweLimit limit = actor.getLimit();
if (!limit.isUnlimited()) {
// during contains.
if (limit.DISALLOWED_BLOCKS.contains(blockType.getId().toLowerCase(Locale.ROOT))) {
throw new DisallowedUsageException(Caption.of("fawe.error.limit.disallowed-block", TextComponent.of(blockType.getId())));
}
}
}
// FAWE end
}
if (nbt != null) {
BaseBlock result = blockStates.size() > 0 ? state.toBaseBlock(nbt) : new BlanketBaseBlock(state, nbt);
return validate(context, result);
}
if (blockType == BlockTypes.SIGN || blockType == BlockTypes.WALL_SIGN || BlockCategories.SIGNS.contains(blockType)) {
// Allow special sign text syntax
String[] text = new String[4];
text[0] = blockAndExtraData.length > 1 ? blockAndExtraData[1] : "";
text[1] = blockAndExtraData.length > 2 ? blockAndExtraData[2] : "";
text[2] = blockAndExtraData.length > 3 ? blockAndExtraData[3] : "";
text[3] = blockAndExtraData.length > 4 ? blockAndExtraData[4] : "";
return validate(context, new SignBlock(state, text));
} else if (blockType == BlockTypes.SPAWNER) {
// Allow setting mob spawn type
if (blockAndExtraData.length > 1) {
String mobName = blockAndExtraData[1];
EntityType ent = EntityTypes.get(mobName.toLowerCase(Locale.ROOT));
if (ent == null) {
throw new NoMatchException(Caption.of("worldedit.error.unknown-entity", TextComponent.of(mobName)));
}
mobName = ent.getId();
if (!worldEdit.getPlatformManager().queryCapability(Capability.USER_COMMANDS).isValidMobType(mobName)) {
throw new NoMatchException(Caption.of("worldedit.error.unknown-mob", TextComponent.of(mobName)));
}
return validate(context, new MobSpawnerBlock(state, mobName));
} else {
// noinspection ConstantConditions
return validate(context, new MobSpawnerBlock(state, EntityTypes.PIG.getId()));
}
} else if (blockType == BlockTypes.PLAYER_HEAD || blockType == BlockTypes.PLAYER_WALL_HEAD) {
// allow setting type/player/rotation
if (blockAndExtraData.length <= 1) {
return validate(context, new SkullBlock(state));
}
String type = blockAndExtraData[1];
// valid MC usernames
return validate(context, new SkullBlock(state, type.replace(" ", "_")));
} else {
// FAWE start
nbt = state.getNbtData();
BaseBlock result;
if (nbt != null) {
result = blockStates.size() > 0 ? state.toBaseBlock(nbt) : new BlanketBaseBlock(state, nbt);
} else {
result = blockStates.size() > 0 ? new BaseBlock(state) : state.toBaseBlock();
}
return validate(context, result);
// FAWE end
}
}
use of com.sk89q.worldedit.extent.inventory.BlockBag in project FastAsyncWorldEdit by IntellectualSites.
the class WorldEdit method flushBlockBag.
/**
* Flush a block bag's changes to a player.
*
* @param actor the actor
* @param editSession the edit session
*/
public void flushBlockBag(Actor actor, EditSession editSession) {
BlockBag blockBag = editSession.getBlockBag();
if (blockBag != null) {
blockBag.flushChanges();
}
Map<BlockType, Integer> missingBlocks = editSession.popMissingBlocks();
if (!missingBlocks.isEmpty()) {
TextComponent.Builder str = TextComponent.builder();
str.append("Missing these blocks: ");
int size = missingBlocks.size();
int i = 0;
for (Map.Entry<BlockType, Integer> blockTypeIntegerEntry : missingBlocks.entrySet()) {
str.append((blockTypeIntegerEntry.getKey()).getRichName());
str.append(" [Amt: ").append(String.valueOf(blockTypeIntegerEntry.getValue())).append("]");
++i;
if (i != size) {
str.append(", ");
}
}
actor.printError(str.build());
}
}
use of com.sk89q.worldedit.extent.inventory.BlockBag in project FastAsyncWorldEdit by IntellectualSites.
the class HistoryCommands method redo.
@Command(name = "redo", aliases = { "/redo" }, desc = "Redoes the last action (from history)")
@CommandPermissions({ "worldedit.history.redo", "worldedit.history.redo.self" })
public void redo(Actor actor, LocalSession session, @Confirm(Confirm.Processor.LIMIT) @Arg(desc = "Number of redoes to perform", def = "1") int times, @Arg(name = "player", desc = "Redo this player's operations", def = "") String playerName) throws WorldEditException {
times = Math.max(1, times);
LocalSession redoSession = session;
if (playerName != null) {
actor.checkPermission("worldedit.history.redo.other");
redoSession = worldEdit.getSessionManager().findByName(playerName);
if (redoSession == null) {
actor.print(Caption.of("worldedit.session.cant-find-session", TextComponent.of(playerName)));
return;
}
}
int timesRedone = 0;
for (int i = 0; i < times; ++i) {
BlockBag blockBag = actor instanceof Player ? redoSession.getBlockBag((Player) actor) : null;
EditSession redone = redoSession.redo(blockBag, actor);
if (redone != null) {
timesRedone++;
worldEdit.flushBlockBag(actor, redone);
} else {
break;
}
}
if (timesRedone > 0) {
actor.print(Caption.of("worldedit.redo.redone", TextComponent.of(timesRedone)));
} else {
actor.print(Caption.of("worldedit.redo.none"));
}
}
use of com.sk89q.worldedit.extent.inventory.BlockBag in project FastAsyncWorldEdit by IntellectualSites.
the class HistoryCommands method undo.
@Command(name = "undo", aliases = { "/undo" }, desc = "Undoes the last action (from history)")
@CommandPermissions({ "worldedit.history.undo", "worldedit.history.undo.self" })
public void undo(Actor actor, LocalSession session, @Confirm(Confirm.Processor.LIMIT) @Arg(desc = "Number of undoes to perform", def = "1") int times, @Arg(name = "player", desc = "Undo this player's operations", def = "") String playerName) throws WorldEditException {
times = Math.max(1, times);
LocalSession undoSession = session;
// FAWE start - Add fastmode check
if (session.hasFastMode()) {
actor.print(Caption.of("fawe.worldedit.history.command.undo.disabled"));
return;
}
// FAWE end
if (playerName != null) {
actor.checkPermission("worldedit.history.undo.other");
undoSession = worldEdit.getSessionManager().findByName(playerName);
if (undoSession == null) {
actor.print(Caption.of("worldedit.session.cant-find-session", TextComponent.of(playerName)));
return;
}
}
int timesUndone = 0;
for (int i = 0; i < times; ++i) {
BlockBag blockBag = actor instanceof Player ? undoSession.getBlockBag((Player) actor) : null;
EditSession undone = undoSession.undo(blockBag, actor);
if (undone != null) {
timesUndone++;
worldEdit.flushBlockBag(actor, undone);
} else {
break;
}
}
if (timesUndone > 0) {
actor.print(Caption.of("worldedit.undo.undone", TextComponent.of(timesUndone)));
} else {
actor.print(Caption.of("worldedit.undo.none"));
}
}
use of com.sk89q.worldedit.extent.inventory.BlockBag in project FastAsyncWorldEdit by IntellectualSites.
the class EditSession method popMissingBlocks.
/**
* Gets the list of missing blocks and clears the list for the next
* operation.
*
* @return a map of missing blocks
*/
public Map<BlockType, Integer> popMissingBlocks() {
BlockBag bag = getBlockBag();
if (bag != null) {
bag.flushChanges();
Map<BlockType, Integer> missingBlocks;
ChangeSet changeSet = getChangeSet();
if (changeSet instanceof BlockBagChangeSet) {
missingBlocks = ((BlockBagChangeSet) changeSet).popMissing();
} else {
ExtentTraverser<BlockBagExtent> find = new ExtentTraverser<>(getExtent()).find(BlockBagExtent.class);
if (find != null && find.get() != null) {
missingBlocks = find.get().popMissing();
} else {
missingBlocks = null;
}
}
if (missingBlocks != null && !missingBlocks.isEmpty()) {
StringBuilder str = new StringBuilder();
int size = missingBlocks.size();
int i = 0;
for (Map.Entry<BlockType, Integer> entry : missingBlocks.entrySet()) {
BlockType type = entry.getKey();
int amount = entry.getValue();
str.append((type.getName())).append((amount != 1 ? "x" + amount : ""));
++i;
if (i != size) {
str.append(", ");
}
}
actor.print(Caption.of("fawe.error.worldedit.some.fails.blockbag", str.toString()));
}
}
return Collections.emptyMap();
}
Aggregations