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.Component; 037import net.kyori.adventure.text.minimessage.tag.Tag; 038import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; 039import org.checkerframework.checker.nullness.qual.NonNull; 040 041import java.util.ArrayList; 042import java.util.List; 043 044public class PatternUtil { 045 046 public static BaseBlock apply(@NonNull Pattern pattern, int x, int y, int z) { 047 Preconditions.checkNotNull(pattern, "Pattern may not be null"); 048 if (pattern instanceof BlockPattern 049 || pattern instanceof BlockState || pattern instanceof BlockType 050 || pattern instanceof BaseBlock) { 051 return pattern.applyBlock(BlockVector3.ZERO); 052 } 053 return pattern.applyBlock(BlockVector3.at(x, y, z)); 054 } 055 056 public static Pattern parse(PlotPlayer<?> plotPlayer, String input) { 057 return parse(plotPlayer, input, true); 058 } 059 060 public static List<String> getSuggestions(String input) { 061 try { 062 return WorldEdit.getInstance().getPatternFactory().getSuggestions(input); 063 } catch (final Exception ignored) { 064 } 065 return new ArrayList<>(); 066 } 067 068 public static Pattern parse(PlotPlayer<?> plotPlayer, String input, boolean allowLegacy) { 069 ParserContext context = new ParserContext(); 070 if (plotPlayer != null) { 071 Actor actor = plotPlayer.toActor(); 072 context.setActor(actor); 073 if (actor instanceof Player) { 074 context.setWorld(((Player) actor).getWorld()); 075 } 076 context.setSession(WorldEdit.getInstance().getSessionManager().get(actor)); 077 context.setRestricted(true); 078 } else { 079 context.setRestricted(false); 080 } 081 context.setPreferringWildcard(false); 082 context.setTryLegacy(allowLegacy); 083 try { 084 return WorldEdit.getInstance().getPatternFactory().parseFromInput(input, context); 085 } catch (InputParseException e) { 086 throw new Command.CommandException( 087 TranslatableCaption.of("invalid.not_valid_block"), 088 TagResolver.resolver("value", Tag.inserting(Component.text(e.getMessage()))) 089 ); 090 } 091 } 092 093 public static boolean isAir(Pattern pattern) { 094 if (pattern instanceof BlockPattern) { 095 return ((BlockPattern) pattern).getBlock().getBlockType().getMaterial().isAir(); 096 } 097 if (pattern instanceof BlockState) { 098 return ((BlockState) pattern).getBlockType().getMaterial().isAir(); 099 } 100 if (pattern instanceof BlockType) { 101 return ((BlockType) pattern).getMaterial().isAir(); 102 } 103 if (pattern instanceof BaseBlock) { 104 return ((BaseBlock) pattern).getBlockType().getMaterial().isAir(); 105 } 106 return false; 107 } 108 109}