Resource Send Throwables to Players

Discussion in 'Spigot Plugin Development' started by ramidzk, Jun 12, 2017.

?

Is this even useful

  1. No (Good choice)

    13 vote(s)
    100.0%
  2. Yes (Reply how)

    0 vote(s)
    0.0%
Thread Status:
Not open for further replies.
  1. I just made some *DEBUG* code that makes it so that you can send Throwables* to a player (more like a CommandExecutor). I find no practical use for it, but I already made it
    *If you don't know what a Throwable or Exception is, I suggest you go back to the Oracle tutorials

    Code (Text):
    /*
    * MIT License
    *
    * Copyright (c) 2017 ramidzkh
    *
    * Permission is hereby granted, free of charge, to any person obtaining a copy
    * of this software and associated documentation files (the "Software"), to deal
    * in the Software without restriction, including without limitation the rights
    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    * copies of the Software, and to permit persons to whom the Software is
    * furnished to do so, subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included in all
    * copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    * SOFTWARE.
    */
    package me.ramidzkh.spigot.tag.util;

    import java.util.function.Consumer;

    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;

    /**
    * An internal error, made for uses with the Bukkit API.
    * Extends {@link RuntimeException} for simplicity
    *
    * @author ramidzkh
    */
    public class InternalError extends RuntimeException {

        private static final long serialVersionUID = -381479618743268720L;

        public InternalError() {
            super();
        }

        public InternalError(String message) {
            super(message);
        }

        public InternalError(String message, Throwable cause) {
            super(message, cause);
        }

        public InternalError(Throwable cause) {
            super(cause);
        }

        protected InternalError(String message, Throwable cause,
                                   boolean enableSuppression,
                                   boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
       
        public void sendBukkit(CommandSender sender) {
            send((s) -> sender.sendMessage(ChatColor.RED + s));
        }
       
        public void send(Consumer<String> s) {
            String ie = "An internal error occured";
            if(getMessage() != null) ie += ": " + getMessage();
            s.accept(ie);
           
            for(StackTraceElement ele : getStackTrace())
                s.accept("  at " + ele.toString());
           
            for(Throwable thr : getSuppressed())
                printThr(s, thr);
        }
       
        private static void printThr(Consumer<String> s, Throwable thr) {
            s.accept("Caused by: " + thr.getClass().getName() +
                    ((thr.getMessage() != null) ? ": " + thr.getMessage() : ""));
           
            for(StackTraceElement ele : thr.getStackTrace())
                s.accept("  at " + ele.toString());
        }
    }
     
    MIT license if you didn't realise????

    Usage:
    Code (Text):
    // Lets just say we got to deal with an exception
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        try {
            // Lets say you made some exception and didn't make some luck thing
            throw new OutOfLuckException("You are very unlucky");
        } catch (Throwable thr) {
            new InternalError(thr).sendBukkit(sender);
        }
        return true;
    }
    That would return EXACLY with Throwable#printStackTrace(PrintStream) would. Just to the player.
     
  2. What is a throwable?

    jks

    Nice I can make OutofluckExceptions now
     
  3. Why misusing the Exception system? It's not designed to act as an message system. Sending the exception to the user doesn't improve the situation.
    1. the player can't interpret the error
    2. most time the error will be split into parts -> is unreadable in chat and spams
     
    • Agree Agree x 5
  4. MiniDigger

    Supporter

    I think he uses this for debug purposes, you can read the error in game rather than in the console. could be useful if you work on a network and don't want to open 100 console windows I guess.
     
    • Like Like x 2
    • Agree Agree x 1
  5. How many lines can Minecraft chat support until the top lines replaced with the most recent message? The error printed could be split as it can't fit the limited line count.
     
  6. Choco

    Moderator

    As far as I'm aware, Minecraft's chat window can hold 100 lines of history... if it's not 100, then I believe it's 250. I forget which one :p
     
  7. a better idea would be to identify the error caused and show the class and line number which contains the error
     
    • Agree Agree x 1
  8. I was going to make it also take in a Boolean for minimal output. I'll add that back in
     
  9. StackTraceElement#toString is the thing I'm using

    And no, the source code isn't attached with the compiled class
     
    #9 ramidzk, Jun 12, 2017
    Last edited: Jun 12, 2017
Thread Status:
Not open for further replies.