001/* 002 * PermissionsEx 003 * Copyright (C) zml and PermissionsEx contributors 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package ca.stellardrift.permissionsex.minecraft.command; 018 019import net.kyori.adventure.text.Component; 020import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; 021import net.kyori.adventure.translation.GlobalTranslator; 022import net.kyori.adventure.util.ComponentMessageThrowable; 023import org.checkerframework.checker.nullness.qual.Nullable; 024 025import java.util.Locale; 026 027import static java.util.Objects.requireNonNull; 028 029/** 030 * An exception caused during command execuction. 031 */ 032public class CommandException extends RuntimeException implements ComponentMessageThrowable { 033 034 private static final long serialVersionUID = -4818903806299876921L; 035 private final Component message; 036 037 public CommandException(final Component message) { 038 super(null, null, true, false); 039 this.message = requireNonNull(message, "message"); 040 } 041 042 public CommandException(final Component message, final @Nullable Throwable cause) { 043 super(null, cause, true, false); 044 this.message = requireNonNull(message, "message"); 045 } 046 047 @Override 048 public String getMessage() { 049 return PlainComponentSerializer.plain().serialize(this.message); 050 } 051 052 @Override 053 public String getLocalizedMessage() { 054 return PlainComponentSerializer.plain().serialize(GlobalTranslator.render(this.message, Locale.getDefault())); 055 } 056 057 @Override 058 public Component componentMessage() { 059 return this.message; 060 } 061}