2022-10-25 13:03:07 +00:00
package net.t2code.t2codelib.SPIGOT.api.update ;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend ;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain ;
2023-05-10 19:07:36 +00:00
import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig ;
2022-11-14 17:45:54 +00:00
import net.t2code.t2codelib.UpdateType ;
2022-11-14 13:41:45 +00:00
import net.t2code.t2codelib.T2CupdateObject ;
import net.t2code.t2codelib.T2CupdateWebData ;
2022-11-20 19:17:43 +00:00
import org.bukkit.Bukkit ;
2022-10-25 13:03:07 +00:00
import org.bukkit.entity.Player ;
import org.bukkit.plugin.Plugin ;
import org.bukkit.plugin.java.JavaPlugin ;
import org.bukkit.scheduler.BukkitRunnable ;
import java.util.HashMap ;
public class T2CupdateAPI {
public static HashMap < String , T2CupdateObject > pluginVersions = new HashMap < > ( ) ;
public static void join ( Plugin plugin , String prefix , String perm , Player player , Integer spigotID , String discord ) {
2023-04-23 11:50:53 +00:00
if ( pluginVersions . get ( plugin . getName ( ) ) = = null ) {
2022-11-20 19:17:43 +00:00
Bukkit . getScheduler ( ) . runTaskLaterAsynchronously ( T2CodeLibMain . getPlugin ( ) , new Runnable ( ) {
@Override
public void run ( ) {
2023-04-23 11:50:53 +00:00
join ( plugin , prefix , perm , player , spigotID , discord ) ;
2022-11-20 19:17:43 +00:00
}
2023-04-23 11:50:53 +00:00
} , 2 * 20L ) ;
2022-12-22 00:43:40 +00:00
return ;
2022-11-20 19:17:43 +00:00
}
2022-11-19 01:51:28 +00:00
if ( ! pluginVersions . get ( plugin . getName ( ) ) . updateCheckOnJoin ) {
2022-10-25 13:03:07 +00:00
return ;
}
if ( ! player . hasPermission ( perm ) & & ! player . isOp ( ) ) {
return ;
}
if ( pluginVersions . get ( plugin . getName ( ) ) = = null ) {
new BukkitRunnable ( ) {
@Override
public void run ( ) {
join ( plugin , prefix , perm , player , spigotID , discord ) ;
}
} . runTaskLaterAsynchronously ( plugin , 20L ) ;
return ;
}
2022-11-10 13:00:59 +00:00
T2CupdateWebData webData = pluginVersions . get ( plugin . getName ( ) ) . webData ;
2022-11-14 00:06:44 +00:00
if ( ! pluginVersions . get ( plugin . getName ( ) ) . updateAvailable ) return ;
2022-10-25 13:03:07 +00:00
new BukkitRunnable ( ) {
@Override
public void run ( ) {
2022-11-10 13:00:59 +00:00
sendUpdateMsg ( prefix , webData , discord , plugin , player ) ;
2022-10-25 13:03:07 +00:00
}
} . runTaskLaterAsynchronously ( T2CodeLibMain . getPlugin ( ) , 200L ) ;
}
2022-11-10 13:00:59 +00:00
public static void sendUpdateMsg ( String prefix , T2CupdateWebData webData , String discord , Plugin plugin , Player player ) {
2022-11-14 00:06:44 +00:00
String publicVersion = webData . getVersion ( ) ;
2022-10-25 13:03:07 +00:00
String pluginVersion = plugin . getDescription ( ) . getVersion ( ) ;
2022-11-14 00:06:44 +00:00
if ( ! pluginVersions . get ( plugin . getName ( ) ) . updateAvailable ) {
2022-10-25 13:03:07 +00:00
return ;
}
String st = " [prefix]<br> " +
2022-11-14 21:37:36 +00:00
" <click:open_url:'[link]'><hover:show_text:'<color:#6e90ff>You can download it here: <yellow>[link]</yellow></color>'>[prefix] <color:#6e90ff>A new</color> [value] <color:#6e90ff>version was " +
" found!</color></hover></click><br> " +
" <click:open_url:'[link]'><hover:show_text:'<color:#6e90ff>You can download it here: <yellow>[link]</yellow></color>'>[prefix] <red>[plv]</red> <gray>-></gray> " +
2022-11-14 00:06:44 +00:00
" <green>[puv]</green></hover></click><br> " +
2022-11-14 21:37:36 +00:00
" <click:open_url:'[dc]'><hover:show_text:'<yellow>[dc]</yellow>'>[prefix] <color:#6e90ff>You can find more information on Discord.</color></hover></click><br> " +
" [prefix] <color:#ff9499><hover:show_text:'<red>Click for more information</red>'><click:run_command:'/t2c updateinfo " + plugin . getName ( ) + " '>Update information</click></hover></color><br> " +
2022-10-25 13:03:07 +00:00
" [prefix] " ;
2022-11-14 00:06:44 +00:00
String value ;
2022-11-10 13:00:59 +00:00
if ( webData . isPreRelease ( ) ) {
2022-11-14 23:36:25 +00:00
//todo if (!SelectLibConfig.getSeePreReleaseUpdates()) return;
2022-11-14 00:06:44 +00:00
value = UpdateType . PRERELEASE . text ;
2022-10-25 13:03:07 +00:00
if ( publicVersion . toLowerCase ( ) . contains ( " dev " ) ) {
2022-11-14 00:06:44 +00:00
value = UpdateType . DEVELOPMENT . text ;
2022-10-25 13:03:07 +00:00
}
if ( publicVersion . toLowerCase ( ) . contains ( " beta " ) ) {
2022-11-14 00:06:44 +00:00
value = UpdateType . BETA . text ;
2022-10-25 13:03:07 +00:00
}
if ( publicVersion . toLowerCase ( ) . contains ( " snapshot " ) ) {
2022-11-14 00:06:44 +00:00
value = UpdateType . SNAPSHOT . text ;
2022-10-25 13:03:07 +00:00
}
2022-11-14 00:06:44 +00:00
} else value = UpdateType . STABLE . text ;
2022-11-10 13:00:59 +00:00
T2Csend . player ( player , st . replace ( " [prefix] " , prefix ) . replace ( " [value] " , value ) . replace ( " [link] " , webData . getUpdateUrl ( ) )
2022-10-25 13:03:07 +00:00
. replace ( " [plv] " , pluginVersion ) . replace ( " [puv] " , publicVersion ) . replace ( " [dc] " , discord ) ) ;
}
2022-11-10 13:00:59 +00:00
public static void sendUpdateMsg ( String prefix , String discord , T2CupdateWebData webData , Plugin plugin ) {
2022-11-14 00:06:44 +00:00
String publicVersion = webData . getVersion ( ) ;
2022-10-25 13:03:07 +00:00
String pluginVersion = plugin . getDescription ( ) . getVersion ( ) ;
2022-11-14 00:06:44 +00:00
String value ;
2022-11-10 13:00:59 +00:00
if ( webData . isPreRelease ( ) ) {
2022-11-14 00:06:44 +00:00
value = UpdateType . PRERELEASE . text ;
2022-10-25 13:03:07 +00:00
if ( publicVersion . toLowerCase ( ) . contains ( " dev " ) ) {
2022-11-14 00:06:44 +00:00
value = UpdateType . DEVELOPMENT . text ;
2022-10-25 13:03:07 +00:00
}
if ( publicVersion . toLowerCase ( ) . contains ( " beta " ) ) {
2022-11-14 00:06:44 +00:00
value = UpdateType . BETA . text ;
2022-10-25 13:03:07 +00:00
}
if ( publicVersion . toLowerCase ( ) . contains ( " snapshot " ) ) {
2022-11-14 00:06:44 +00:00
value = UpdateType . SNAPSHOT . text ;
2022-10-25 13:03:07 +00:00
}
2022-11-14 00:06:44 +00:00
} else value = UpdateType . STABLE . text ;
2022-11-14 17:45:54 +00:00
String h = " <br><dark_red>╔══════════════</dark_red> " + prefix + " <dark_red>══════════════</dark_red> " ;
2022-11-14 21:37:36 +00:00
String s1 = " <br><dark_red>║</dark_red> <color:#6e90ff>A new [value] version was found!</color> " . replace ( " [value] " , value ) ;
String s2 = " <br><dark_red>║</dark_red> <color:#6e90ff>Your version: <red> " + pluginVersion + " </red> <gray>-</gray> Current version:</color> <green> " + webData . getVersion ( ) + " </green> " ;
String s3 = " <br><dark_red>║</dark_red> <color:#6e90ff>You can download it here:</color> <yellow> " + webData . getUpdateUrl ( ) + " </yellow> " ;
String s4 = " <br><dark_red>║</dark_red> <color:#6e90ff>You can find more information on Discord:</color> <yellow> " + discord + " </yellow> " ;
2022-11-14 17:45:54 +00:00
String f = " <br><dark_red>╚══════════════</dark_red> " + prefix + " <dark_red>══════════════</dark_red> " ;
String text = h + s1 + s2 + s3 + s4 + f ;
2022-11-03 06:57:53 +00:00
T2Csend . console ( text ) ;
2022-10-25 13:03:07 +00:00
}
2022-11-14 17:45:54 +00:00
public static String updateInfo ( String [ ] args , Boolean player ) {
2022-11-14 00:06:44 +00:00
T2CupdateObject object ;
try {
object = T2CupdateAPI . pluginVersions . get ( args [ 1 ] ) ;
} catch ( Exception e ) {
return " Das Plugin " + args [ 1 ] + " gibts net " ; // todo
}
T2CupdateWebData webData = object . webData ;
String pluginName = T2CupdateAPI . pluginVersions . get ( args [ 1 ] ) . pluginName ;
String pluginVersion = T2CupdateAPI . pluginVersions . get ( args [ 1 ] ) . pluginVersion ;
String updateTitle = webData . getUpdateTitle ( ) ;
String updateVersion = webData . getVersion ( ) ;
String updateType ;
if ( webData . isPreRelease ( ) ) {
updateType = UpdateType . PRERELEASE . text ;
if ( updateVersion . toLowerCase ( ) . contains ( " dev " ) ) {
updateType = UpdateType . DEVELOPMENT . text ;
}
if ( updateVersion . toLowerCase ( ) . contains ( " beta " ) ) {
updateType = UpdateType . BETA . text ;
}
if ( updateVersion . toLowerCase ( ) . contains ( " snapshot " ) ) {
updateType = UpdateType . SNAPSHOT . text ;
}
} else updateType = UpdateType . STABLE . text ;
String updateAt = webData . getPublishedAt ( ) ;
String updateUpdate = webData . getUpdateUrl ( ) ;
String updateDescription = webData . getUpdateDescription ( ) ;
2022-11-14 21:37:36 +00:00
String pluginNameString = " <br><dark_red>║</dark_red> <color:#6e90ff>Plugin:</color> <color:#5eff89> " + pluginName + " </color> " ;
String pluginVersionString = " <br><dark_red>║</dark_red> <color:#6e90ff>Your version:</color> <color:#5eff89> " + pluginVersion + " </color> " ;
String updateInfoString = " <br><dark_red>║</dark_red> <color:#09ff00>[value]:</color> " ;
String updateTitleString = " <br><dark_red>║</dark_red> <color:#6e90ff>Title:</color> <color:#5eff89> " + updateTitle + " </color> " ;
String updateVersionString = " <br><dark_red>║</dark_red> <color:#6e90ff>Version:</color> <color:#5eff89> " + updateVersion + " </color> " ;
String updateAtString = " <br><dark_red>║</dark_red> <color:#6e90ff>Published on:</color> <color:#5eff89> " + updateAt + " </color> " ;
String updateTypeString = " <br><dark_red>║</dark_red> <color:#6e90ff>Version type:</color> <color:#5eff89> " + updateType + " </color> " ;
2022-11-14 00:06:44 +00:00
String updateButton ;
if ( player ) {
2022-11-14 21:37:36 +00:00
updateButton = " <br><dark_red>║</dark_red> <color:#5eff89><hover:show_text:'<gold> " + updateUpdate + " </gold>'><click:open_url:' " + updateUpdate
2023-04-23 12:33:42 +00:00
+ " '>Download</click></hover></color> <dark_gray>|</dark_gray> <color:#ff9499><hover:show_text:' " + updateDescription + " '><click:open_url:' " + webData . getGitURL ( ) + " '>Update Description</click></hover></color> " ;
2022-11-14 00:06:44 +00:00
} else updateButton = " <br><dark_red>║</dark_red> <color:#5eff89>Download:</color> <gold> " + updateUpdate + " </gold> " ;
String text ;
text = " <dark_red>╔══════════════════════</dark_red> " ;
text = text + pluginNameString ;
text = text + pluginVersionString ;
text = text + updateInfoString . replace ( " [value] " , object . updateAvailable ? " Update available " : " Info about your version " ) ;
if ( ! updateTitle . equals ( " OLD " ) ) text = text + updateTitleString ;
if ( object . updateAvailable ) text = text + updateVersionString ;
if ( ! updateTitle . equals ( " OLD " ) ) text = text + updateAtString ;
if ( ! updateTitle . equals ( " OLD " ) ) text = text + updateTypeString ;
2022-11-14 21:37:36 +00:00
text = text + updateButton ;
2022-11-14 00:06:44 +00:00
if ( updateTitle . equals ( " OLD " ) ) text = text + " <br><dark_gray>This plugin does not have the new update checker yet, so there is no exact update / " +
" version information available!</dark_gray> " ;
text = text + " <br><dark_red>╚══════════════════════</dark_red> " ;
return text ;
}
2022-11-14 23:36:25 +00:00
public static void onUpdateCheck ( Plugin plugin , String prefix , String gitKey , Integer spigotID , String discord , Boolean updateCheckOnJoin , Boolean seePreReleaseUpdates , Integer timeInterval ) {
2023-05-10 19:07:36 +00:00
if ( SelectLibConfig . getUpdateCheckFullDisable ( ) ) return ;
2022-11-19 01:51:28 +00:00
new T2CupdateCheckerGit ( ( JavaPlugin ) plugin , prefix , gitKey , spigotID , discord , updateCheckOnJoin , seePreReleaseUpdates , timeInterval ) ;
2022-10-25 13:03:07 +00:00
}
}