001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util; 020 021import com.google.common.base.Preconditions; 022import com.plotsquared.core.command.Command; 023import com.plotsquared.core.configuration.caption.TranslatableCaption; 024import com.plotsquared.core.player.PlotPlayer; 025import com.sk89q.worldedit.WorldEdit; 026import com.sk89q.worldedit.entity.Player; 027import com.sk89q.worldedit.extension.input.InputParseException; 028import com.sk89q.worldedit.extension.input.ParserContext; 029import com.sk89q.worldedit.extension.platform.Actor; 030import com.sk89q.worldedit.function.pattern.BlockPattern; 031import com.sk89q.worldedit.function.pattern.Pattern; 032import com.sk89q.worldedit.math.BlockVector3; 033import com.sk89q.worldedit.world.block.BaseBlock; 034import com.sk89q.worldedit.world.block.BlockState; 035import com.sk89q.worldedit.world.block.BlockType; 036import net.kyori.adventure.text.minimessage.Template; 037import org.checkerframework.checker.nullness.qual.NonNull; 038 039import java.util.ArrayList; 040import java.util.List; 041 042public class PatternUtil { 043 044 public static BaseBlock apply(@NonNull Pattern pattern, int x, int y, int z) { 045 Preconditions.checkNotNull(pattern, "Pattern may not be null"); 046 if (pattern instanceof BlockPattern 047 || pattern instanceof BlockState || pattern instanceof BlockType 048 || pattern instanceof BaseBlock) { 049 return pattern.applyBlock(BlockVector3.ZERO); 050 } 051 return pattern.applyBlock(BlockVector3.at(x, y, z)); 052 } 053 054 public static Pattern parse(PlotPlayer<?> plotPlayer, String input) { 055 return parse(plotPlayer, input, true); 056 } 057 058 public static List<String> getSuggestions(String input) { 059 try { 060 return WorldEdit.getInstance().getPatternFactory().getSuggestions(input); 061 } catch (final Exception ignored) { 062 } 063 return new ArrayList<>(); 064 } 065 066 public static Pattern parse(PlotPlayer<?> plotPlayer, String input, boolean allowLegacy) { 067 ParserContext context = new ParserContext(); 068 if (plotPlayer != null) { 069 Actor actor = plotPlayer.toActor(); 070 context.setActor(actor); 071 if (actor instanceof Player) { 072 context.setWorld(((Player) actor).getWorld()); 073 } 074 context.setSession(WorldEdit.getInstance().getSessionManager().get(actor)); 075 context.setRestricted(true); 076 } else { 077 context.setRestricted(false); 078 } 079 context.setPreferringWildcard(false); 080 context.setTryLegacy(allowLegacy); 081 try { 082 return WorldEdit.getInstance().getPatternFactory().parseFromInput(input, context); 083 } catch (InputParseException e) { 084 throw new Command.CommandException( 085 TranslatableCaption.of("invalid.not_valid_block"), 086 Template.of("value", e.getMessage()) 087 ); 088 } 089 } 090 091 public static boolean isAir(Pattern pattern) { 092 if (pattern instanceof BlockPattern) { 093 return ((BlockPattern) pattern).getBlock().getBlockType().getMaterial().isAir(); 094 } 095 if (pattern instanceof BlockState) { 096 return ((BlockState) pattern).getBlockType().getMaterial().isAir(); 097 } 098 if (pattern instanceof BlockType) { 099 return ((BlockType) pattern).getMaterial().isAir(); 100 } 101 if (pattern instanceof BaseBlock) { 102 return ((BaseBlock) pattern).getBlockType().getMaterial().isAir(); 103 } 104 return false; 105 } 106 107}