2020-04-16 04:52:39 +02:00
/ *
* _____ _ _ _____ _
* | __ \ | | | | / ____ | | |
* | | __ ) | | ___ | | _ | ( ___ __ _ _ _ __ _ _ __ ___ __ | |
* | ___ / | | / _ \ | __ | \ ___ \ / _ ` | | | | / _ ` | ' __ / _ \ / _ ` |
* | | | | ( _ ) | | _ ____ ) | ( _ | | | _ | | ( _ | | | | __ / ( _ | |
* | _ | | _ | \ ___ / \ __ | _____ / \ __ , | \ __ , _ | \ __ , _ | _ | \ ___ | \ __ , _ |
* | |
* | _ |
* PlotSquared plot management system for Minecraft
* Copyright ( C ) 2020 IntellectualSites
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.command ;
2015-07-18 19:18:15 +02:00
2020-04-30 12:33:59 +02:00
import com.google.gson.JsonObject ;
import com.google.gson.JsonParser ;
2020-07-14 18:49:40 +02:00
import com.google.inject.Inject ;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.PlotSquared ;
2020-07-10 22:12:37 +02:00
import com.plotsquared.core.inject.annotations.ConfigFile ;
import com.plotsquared.core.inject.annotations.WorldFile ;
2020-04-16 06:14:33 +02:00
import com.plotsquared.core.configuration.Captions ;
import com.plotsquared.core.configuration.Settings ;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.player.PlotPlayer ;
import com.plotsquared.core.util.MainUtil ;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.util.PremiumVerification ;
import com.plotsquared.core.util.net.IncendoPaster ;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.util.task.TaskManager ;
2020-07-14 18:49:40 +02:00
import javax.annotation.Nonnull ;
2016-03-23 02:41:37 +01:00
2019-01-09 23:12:36 +01:00
import java.io.BufferedReader ;
2016-02-22 05:29:33 +01:00
import java.io.File ;
2019-01-09 23:12:36 +01:00
import java.io.FileReader ;
2016-02-22 05:29:33 +01:00
import java.io.IOException ;
2019-07-25 20:02:39 +02:00
import java.lang.management.ManagementFactory ;
import java.lang.management.RuntimeMXBean ;
2019-01-09 23:12:36 +01:00
import java.nio.file.Files ;
import java.util.List ;
2019-11-25 23:38:03 +01:00
import java.util.Map ;
2019-12-27 16:53:38 +01:00
import java.util.concurrent.TimeUnit ;
2019-02-08 20:49:37 +01:00
import java.util.stream.Collectors ;
2016-02-22 05:29:33 +01:00
2020-02-24 18:42:02 +01:00
@CommandDeclaration ( command = " debugpaste " ,
aliases = " dp " ,
usage = " /plot debugpaste " ,
2019-10-01 14:46:19 +02:00
description = " Upload settings.yml, worlds.yml, PlotSquared.use_THIS.yml your latest.log and Multiverse's worlds.yml (if being used) to https://athion.net/ISPaster/paste " ,
2020-02-24 18:42:02 +01:00
permission = " plots.debugpaste " ,
category = CommandCategory . DEBUG ,
confirmation = true ,
requiredType = RequiredType . NONE )
2019-10-03 01:12:08 +02:00
public class DebugPaste extends SubCommand {
2016-03-22 18:53:17 +01:00
2020-07-10 19:25:05 +02:00
private final File configFile ;
private final File worldfile ;
2020-07-14 18:49:40 +02:00
@Inject public DebugPaste ( @ConfigFile @Nonnull final File configFile ,
@WorldFile @Nonnull final File worldFile ) {
2020-07-10 19:25:05 +02:00
this . configFile = configFile ;
this . worldfile = worldFile ;
}
2020-07-14 18:49:40 +02:00
private static String readFile ( @Nonnull final File file ) throws IOException {
2019-02-08 20:49:37 +01:00
final List < String > lines ;
2019-02-04 14:59:11 +01:00
try ( final BufferedReader reader = new BufferedReader ( new FileReader ( file ) ) ) {
2019-02-08 20:49:37 +01:00
lines = reader . lines ( ) . collect ( Collectors . toList ( ) ) ;
2019-02-04 14:59:11 +01:00
}
2019-05-14 03:57:41 +02:00
final StringBuilder content = new StringBuilder ( ) ;
2019-02-04 14:59:11 +01:00
for ( int i = Math . max ( 0 , lines . size ( ) - 1000 ) ; i < lines . size ( ) ; i + + ) {
content . append ( lines . get ( i ) ) . append ( " \ n " ) ;
}
return content . toString ( ) ;
}
2020-05-27 14:08:48 +02:00
@Override public boolean onCommand ( final PlotPlayer < ? > player , String [ ] args ) {
2019-01-09 23:12:36 +01:00
TaskManager . runTaskAsync ( ( ) - > {
try {
StringBuilder b = new StringBuilder ( ) ;
b . append (
" # Welcome to this paste \ n# It is meant to provide us at IntellectualSites with better information about your "
+ " problem \ n \ n " ) ;
2020-02-19 23:55:43 +01:00
b . append ( " # PlotSquared Information \ n " ) ;
2020-05-13 14:13:14 +02:00
b . append ( " PlotSquared Version: " ) . append ( PlotSquared . get ( ) . getVersion ( ) )
. append ( " \ n " ) ;
2020-04-21 23:38:56 +02:00
b . append ( " Resource ID: " ) . append ( PremiumVerification . getResourceID ( ) ) . append ( " \ n " ) ;
b . append ( " Download ID: " ) . append ( PremiumVerification . getDownloadID ( ) ) . append ( " \ n " ) ;
2020-04-30 12:33:59 +02:00
b . append ( " This PlotSquared version is licensed to the spigot user " )
. append ( PremiumVerification . getUserID ( ) ) . append ( " \ n \ n " ) ;
2019-01-09 23:12:36 +01:00
b . append ( " # Server Information \ n " ) ;
2020-07-07 12:56:43 +02:00
b . append ( " Server Version: " ) . append ( PlotSquared . platform ( ) . getServerImplementation ( ) )
2019-02-04 14:59:11 +01:00
. append ( " \ n " ) ;
2020-05-19 23:05:36 +02:00
b . append ( " online_mode: " ) . append ( ! Settings . UUID . OFFLINE ) . append ( ';' )
2019-01-09 23:12:36 +01:00
. append ( ! Settings . UUID . OFFLINE ) . append ( '\n' ) ;
2019-07-25 20:02:39 +02:00
b . append ( " Plugins: " ) ;
2020-02-24 18:42:02 +01:00
for ( Map . Entry < Map . Entry < String , String > , Boolean > pluginInfo : PlotSquared
2020-07-07 12:56:43 +02:00
. platform ( ) . getPluginIds ( ) ) {
2019-11-25 23:40:50 +01:00
Map . Entry < String , String > nameVersion = pluginInfo . getKey ( ) ;
String name = nameVersion . getKey ( ) ;
String version = nameVersion . getValue ( ) ;
boolean enabled = pluginInfo . getValue ( ) ;
2019-01-09 23:12:36 +01:00
b . append ( " \ n " ) . append ( name ) . append ( " : \ n " ) . append ( " version: ' " )
. append ( version ) . append ( '\'' ) . append ( " \ n enabled: " ) . append ( enabled ) ;
}
b . append ( " \ n \ n# YAY! Now, let's see what we can find in your JVM \ n " ) ;
Runtime runtime = Runtime . getRuntime ( ) ;
2019-07-25 20:02:39 +02:00
RuntimeMXBean rb = ManagementFactory . getRuntimeMXBean ( ) ;
2020-02-24 18:42:02 +01:00
b . append ( " Uptime: " ) . append (
TimeUnit . MINUTES . convert ( rb . getUptime ( ) , TimeUnit . MILLISECONDS ) + " minutes " )
. append ( '\n' ) ;
2019-12-27 16:53:38 +01:00
b . append ( " JVM Flags: " ) . append ( rb . getInputArguments ( ) ) . append ( '\n' ) ;
2020-02-24 18:42:02 +01:00
b . append ( " Free Memory: " ) . append ( runtime . freeMemory ( ) / 1024 / 1024 + " MB " )
. append ( '\n' ) ;
b . append ( " Max Memory: " ) . append ( runtime . maxMemory ( ) / 1024 / 1024 + " MB " )
. append ( '\n' ) ;
2019-07-25 20:02:39 +02:00
b . append ( " Java Name: " ) . append ( rb . getVmName ( ) ) . append ( '\n' ) ;
2020-02-24 18:42:02 +01:00
b . append ( " Java Version: ' " ) . append ( System . getProperty ( " java.version " ) )
. append ( " ' \ n " ) ;
2019-07-25 20:02:39 +02:00
b . append ( " Java Vendor: ' " ) . append ( System . getProperty ( " java.vendor " ) ) . append ( " ' \ n " ) ;
b . append ( " Operating System: ' " ) . append ( System . getProperty ( " os.name " ) ) . append ( " ' \ n " ) ;
b . append ( " OS Version: " ) . append ( System . getProperty ( " os.version " ) ) . append ( '\n' ) ;
b . append ( " OS Arch: " ) . append ( System . getProperty ( " os.arch " ) ) . append ( '\n' ) ;
2019-01-09 23:12:36 +01:00
b . append ( " # Okay :D Great. You are now ready to create your bug report! " ) ;
b . append (
2020-05-24 18:48:15 +02:00
" \ n# You can do so at https://issues.intellectualsites.com/projects/ps " ) ;
2020-03-09 23:50:22 +01:00
b . append ( " \ n# or via our Discord at https://discord.gg/KxkjDVg " ) ;
2019-01-09 23:12:36 +01:00
2019-05-14 03:57:41 +02:00
final IncendoPaster incendoPaster = new IncendoPaster ( " plotsquared " ) ;
2019-01-09 23:12:36 +01:00
incendoPaster . addFile ( new IncendoPaster . PasteFile ( " information " , b . toString ( ) ) ) ;
2015-09-13 06:04:31 +02:00
try {
2019-02-04 14:59:11 +01:00
final File logFile =
2020-07-07 12:56:43 +02:00
new File ( PlotSquared . platform ( ) . getDirectory ( ) , " ../../logs/latest.log " ) ;
2019-01-09 23:12:36 +01:00
if ( Files . size ( logFile . toPath ( ) ) > 14_000_000 ) {
throw new IOException ( " Too big... " ) ;
2018-12-27 17:29:35 +01:00
}
2019-02-04 14:59:11 +01:00
incendoPaster
. addFile ( new IncendoPaster . PasteFile ( " latest.log " , readFile ( logFile ) ) ) ;
2019-01-09 23:12:36 +01:00
} catch ( IOException ignored ) {
2019-02-04 14:59:11 +01:00
MainUtil
. sendMessage ( player , " &clatest.log is too big to be pasted, will ignore " ) ;
2019-01-09 23:12:36 +01:00
}
2018-12-26 22:18:22 +01:00
2019-01-10 21:34:52 +01:00
try {
2019-02-04 14:59:11 +01:00
incendoPaster . addFile ( new IncendoPaster . PasteFile ( " settings.yml " ,
2020-07-10 19:25:05 +02:00
readFile ( this . configFile ) ) ) ;
2019-01-10 21:34:52 +01:00
} catch ( final IllegalArgumentException ignored ) {
MainUtil . sendMessage ( player , " &cSkipping settings.yml because it's empty " ) ;
}
try {
2019-02-04 14:59:11 +01:00
incendoPaster . addFile ( new IncendoPaster . PasteFile ( " worlds.yml " ,
2020-07-10 19:25:05 +02:00
readFile ( this . worldfile ) ) ) ;
2019-01-10 21:34:52 +01:00
} catch ( final IllegalArgumentException ignored ) {
MainUtil . sendMessage ( player , " &cSkipping worlds.yml because it's empty " ) ;
}
try {
incendoPaster . addFile ( new IncendoPaster . PasteFile ( " PlotSquared.use_THIS.yml " ,
readFile ( PlotSquared . get ( ) . translationFile ) ) ) ;
} catch ( final IllegalArgumentException ignored ) {
2019-02-04 14:59:11 +01:00
MainUtil . sendMessage ( player ,
" &cSkipping PlotSquared.use_THIS.yml because it's empty " ) ;
2019-01-10 21:34:52 +01:00
}
2019-10-01 12:12:42 +02:00
try {
2020-07-07 12:56:43 +02:00
final File MultiverseWorlds = new File ( PlotSquared . platform ( ) . getDirectory ( ) ,
2020-02-24 18:42:02 +01:00
" ../Multiverse-Core/worlds.yml " ) ;
incendoPaster . addFile ( new IncendoPaster . PasteFile ( " MultiverseCore/worlds.yml " ,
readFile ( MultiverseWorlds ) ) ) ;
2019-10-01 12:12:42 +02:00
} catch ( final IOException ignored ) {
2020-02-24 18:42:02 +01:00
MainUtil . sendMessage ( player ,
" &cSkipping Multiverse worlds.yml because the plugin is not in use " ) ;
2019-10-01 12:12:42 +02:00
}
2019-01-09 23:12:36 +01:00
try {
final String rawResponse = incendoPaster . upload ( ) ;
2019-02-04 14:59:11 +01:00
final JsonObject jsonObject =
new JsonParser ( ) . parse ( rawResponse ) . getAsJsonObject ( ) ;
2019-01-09 23:12:36 +01:00
if ( jsonObject . has ( " created " ) ) {
final String pasteId = jsonObject . get ( " paste_id " ) . getAsString ( ) ;
2019-02-04 14:59:11 +01:00
final String link =
2019-05-01 20:52:26 +02:00
String . format ( " https://athion.net/ISPaster/paste/view/%s " , pasteId ) ;
2019-08-07 02:35:36 +02:00
player . sendMessage (
Captions . DEBUG_REPORT_CREATED . getTranslated ( ) . replace ( " %url% " , link ) ) ;
2019-01-09 23:12:36 +01:00
} else {
final String responseMessage = jsonObject . get ( " response " ) . getAsString ( ) ;
2019-02-04 14:59:11 +01:00
MainUtil . sendMessage ( player , String
. format ( " &cFailed to create the debug paste: %s " , responseMessage ) ) ;
2019-01-09 23:12:36 +01:00
}
} catch ( final Throwable throwable ) {
throwable . printStackTrace ( ) ;
2019-02-04 14:59:11 +01:00
MainUtil . sendMessage ( player ,
" &cFailed to create the debug paste: " + throwable . getMessage ( ) ) ;
2018-12-27 17:29:35 +01:00
}
2019-01-09 23:12:36 +01:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
2015-07-18 19:18:15 +02:00
}
} ) ;
return true ;
}
}