use of com.sk89q.worldedit.extension.input.ParserContext in project FastAsyncWorldEdit by IntellectualSites.
the class ConsumeBindings method baseBlock.
@Binding
public BaseBlock baseBlock(Actor actor, String argument) {
ParserContext parserContext = new ParserContext();
parserContext.setActor(actor);
if (actor instanceof Entity) {
Extent extent = ((Entity) actor).getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
}
parserContext.setSession(getWorldEdit().getSessionManager().get(actor));
try {
return getWorldEdit().getBlockFactory().parseFromInput(argument, parserContext);
} catch (NoMatchException e) {
throw new InputParseException(TextComponent.of(e.getMessage()));
}
}
use of com.sk89q.worldedit.extension.input.ParserContext in project FastAsyncWorldEdit by IntellectualSites.
the class FactoryConverter method convert.
@Override
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
Actor actor = context.injectedValue(Key.of(Actor.class)).orElseThrow(() -> new IllegalStateException("No actor"));
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
ParserContext parserContext = new ParserContext();
parserContext.setActor(actor);
if (actor instanceof Locatable) {
Extent extent = ((Locatable) actor).getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
parserContext.setExtent(new SupplyingExtent(((Locatable) actor)::getExtent));
} else if (session.hasWorldOverride()) {
parserContext.setWorld(session.getWorldOverride());
parserContext.setExtent(new SupplyingExtent(session::getWorldOverride));
}
parserContext.setSession(session);
parserContext.setRestricted(true);
parserContext.setInjected(context);
if (contextTweaker != null) {
contextTweaker.accept(parserContext);
}
try {
return SuccessfulConversion.fromSingle(factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext));
} catch (InputParseException e) {
return FailedConversion.from(e);
}
}
use of com.sk89q.worldedit.extension.input.ParserContext in project FastAsyncWorldEdit by IntellectualSites.
the class LegacyMapper method loadFromResource.
/**
* Attempt to load the data from file.
*
* @throws IOException thrown on I/O error
*/
private void loadFromResource() throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
Gson gson = gsonBuilder.disableHtmlEscaping().create();
URL url = resourceLoader.getResource(LegacyMapper.class, "legacy.json");
if (url == null) {
throw new IOException("Could not find legacy.json");
}
String data = Resources.toString(url, Charset.defaultCharset());
LegacyDataFile dataFile = gson.fromJson(data, new TypeToken<LegacyDataFile>() {
}.getType());
DataFixer fixer = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataFixer();
ParserContext parserContext = new ParserContext();
parserContext.setPreferringWildcard(false);
parserContext.setRestricted(false);
// This is legacy. Don't match itself.
parserContext.setTryLegacy(false);
for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
String id = blockEntry.getKey();
final String value = blockEntry.getValue();
// FAWE start
Integer combinedId = getCombinedId(blockEntry.getKey());
blockEntries.put(id, value);
// FAWE end
BlockState state = null;
// FAWE start
try {
state = BlockState.get(null, blockEntry.getValue());
BlockType type = state.getBlockType();
if (type.hasProperty(PropertyKey.WATERLOGGED)) {
state = state.with(PropertyKey.WATERLOGGED, false);
}
} catch (InputParseException f) {
BlockFactory blockFactory = WorldEdit.getInstance().getBlockFactory();
// if fixer is available, try using that first, as some old blocks that were renamed share names with new blocks
if (fixer != null) {
try {
String newEntry = fixer.fixUp(DataFixer.FixTypes.BLOCK_STATE, value, Constants.DATA_VERSION_MC_1_13_2);
state = blockFactory.parseFromInput(newEntry, parserContext).toImmutableState();
} catch (InputParseException ignored) {
}
}
// if it's still null, the fixer was unavailable or failed
if (state == null) {
try {
state = blockFactory.parseFromInput(value, parserContext).toImmutableState();
} catch (InputParseException ignored) {
}
}
// if it's still null, both fixer and default failed
if (state == null) {
LOGGER.error("Unknown block: {}. Neither the DataFixer nor defaulting worked to recognize this block.", value);
} else {
// it's not null so one of them succeeded, now use it
blockToStringMap.put(state, id);
stringToBlockMap.put(id, state);
}
}
// FAWE start
if (state != null) {
blockArr[combinedId] = state.getInternalId();
blockStateToLegacyId4Data.put(state.getInternalId(), combinedId);
blockStateToLegacyId4Data.putIfAbsent(state.getInternalBlockTypeId(), combinedId);
}
}
for (int id = 0; id < 256; id++) {
int combinedId = id << 4;
int base = blockArr[combinedId];
if (base != 0) {
for (int data_ = 0; data_ < 16; data_++, combinedId++) {
if (blockArr[combinedId] == 0) {
blockArr[combinedId] = base;
}
}
}
}
for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
String id = itemEntry.getKey();
String value = itemEntry.getValue();
ItemType type = ItemTypes.get(value);
if (type == null && fixer != null) {
value = fixer.fixUp(DataFixer.FixTypes.ITEM_TYPE, value, Constants.DATA_VERSION_MC_1_13_2);
type = ItemTypes.get(value);
}
if (type == null) {
LOGGER.error("Unknown item: {}. Neither the DataFixer nor defaulting worked to recognize this item.", value);
} else {
try {
itemMap.put(getCombinedId(id), type);
} catch (Exception ignored) {
}
}
}
}
use of com.sk89q.worldedit.extension.input.ParserContext in project bteConoSurCore by BTEConoSur.
the class Tree method place.
// PLACE
public EditSession place(Vector loc, Player player, EditSession editSession) {
if (editSession == null) {
editSession = getEditSession(player);
}
com.sk89q.worldedit.entity.Player actor = new BukkitPlayer((WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"), ((WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit")).getServerInterface(), player);
LocalSession localSession = WorldEdit.getInstance().getSessionManager().get(actor);
Clipboard clipboard;
ClipboardFormat format = ClipboardFormat.SCHEMATIC;
try {
ClipboardReader reader = format.getReader(new FileInputStream(schematic));
clipboard = reader.read(actor.getWorld().getWorldData());
clipboard.setOrigin(new Vector(xOffset, yOffset, zOffset));
Region region = clipboard.getRegion();
// PASTE SCHEMATIC
// Get Maxs and Mins
int xMax = clipboard.getMaximumPoint().getBlockX();
int yMax = clipboard.getMaximumPoint().getBlockY();
int zMax = clipboard.getMaximumPoint().getBlockZ();
// MASK
Mask mask = localSession.getMask();
if (mask == null) {
ParserContext parserContext = new ParserContext();
parserContext.setActor(actor);
Extent extent = actor.getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
parserContext.setSession(WorldEdit.getInstance().getSessionManager().get(actor));
mask = WorldEdit.getInstance().getMaskFactory().parseFromInput("0", parserContext);
}
// ROTATION
int degrees = new Random().nextInt(4);
// ORIGEN
int xOg = loc.getBlockX();
int zOg = loc.getBlockZ();
for (BlockVector p : region) {
if (clipboard.getBlock(p).getType() != 0) {
int x = loc.getBlockX() + p.getBlockX() - xMax + xOffset;
int y = loc.getBlockY() + p.getBlockY() - yMax + yOffset;
int z = loc.getBlockZ() + p.getBlockZ() - zMax + zOffset;
int x0 = x - xOg;
int z0 = z - zOg;
if (degrees == 1) {
x = z0 + xOg;
z = -x0 + zOg;
} else if (degrees == 2) {
x = -x0 + xOg;
z = -z0 + zOg;
} else if (degrees == 3) {
x = -z0 + xOg;
z = x0 + zOg;
}
Vector newVector = new Vector(x, y + clipboard.getDimensions().getBlockY() - 1, z);
if (mask.test(newVector) && getWorldGuard().canBuild(player, mainWorld.getBlockAt(newVector.getBlockX(), newVector.getBlockY(), newVector.getBlockZ()))) {
editSession.setBlock(newVector, clipboard.getBlock(p));
}
}
}
return editSession;
} catch (IOException | MaxChangedBlocksException | InputParseException e) {
e.printStackTrace();
return null;
}
}
use of com.sk89q.worldedit.extension.input.ParserContext in project bteConoSurCore by BTEConoSur.
the class Polywall method onCommand.
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equals("/polywalls")) {
if (sender instanceof Player) {
Player p = (Player) sender;
//
// GET REGION
Region region = null;
try {
region = getSelection(p);
} catch (IncompleteRegionException e) {
p.sendMessage(wePrefix + "Selecciona un área primero.");
return true;
}
if (args.length > 0) {
// PARSE PATTERN
com.sk89q.worldedit.entity.Player actor = new BukkitPlayer((WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"), ((WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit")).getServerInterface(), p);
LocalSession localSession = WorldEdit.getInstance().getSessionManager().get(actor);
Mask mask = localSession.getMask();
ParserContext parserContext = new ParserContext();
parserContext.setActor(actor);
Extent extent = ((Entity) actor).getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
parserContext.setSession(WorldEdit.getInstance().getSessionManager().get(actor));
Pattern pattern;
try {
pattern = WorldEdit.getInstance().getPatternFactory().parseFromInput(args[0], parserContext);
} catch (InputParseException e) {
p.sendMessage(wePrefix + "Patrón inválido.");
return true;
}
// GET POINTS
List<BlockVector2D> points = new ArrayList<>();
int maxY;
int minY;
if (region instanceof CuboidRegion) {
CuboidRegion cuboidRegion = (CuboidRegion) region;
Vector first = cuboidRegion.getPos1();
Vector second = cuboidRegion.getPos2();
maxY = cuboidRegion.getMaximumY();
minY = cuboidRegion.getMinimumY();
points.add(new BlockVector2D(first.getX(), first.getZ()));
points.add(new BlockVector2D(second.getX(), first.getZ()));
points.add(new BlockVector2D(second.getX(), second.getZ()));
points.add(new BlockVector2D(first.getX(), second.getZ()));
} else if (region instanceof Polygonal2DRegion) {
maxY = ((Polygonal2DRegion) region).getMaximumY();
minY = ((Polygonal2DRegion) region).getMinimumY();
points = ((Polygonal2DRegion) region).getPoints();
} else {
p.sendMessage(wePrefix + "Debes seleccionar una region cúbica o poligonal.");
return true;
}
if (points.size() < 3) {
p.sendMessage(wePrefix + "Selecciona un área primero.");
return true;
}
List<BlockVector2D> pointsFinal = new ArrayList<>(points);
pointsFinal.add(points.get(0));
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession((World) new BukkitWorld(mainWorld), WorldEdit.getInstance().getSessionManager().get(actor).getBlockChangeLimit());
for (int i = minY; i <= maxY; i++) {
for (int j = 0; j < pointsFinal.size() - 1; j++) {
BlockVector2D v1 = pointsFinal.get(j);
BlockVector2D v2 = pointsFinal.get(j + 1);
setBlocksInLine(p, actor, editSession, pattern, mask, v1.toVector(i), v2.toVector(i));
}
}
p.sendMessage(wePrefix + "Paredes de la selección creadas.");
} else {
p.sendMessage(wePrefix + "Introduce un patrón de bloques.");
}
}
}
return true;
}
Aggregations