2023-08-24 09:43:16 +00:00
package dev.plex.extras ;
2022-07-09 14:26:57 +00:00
import com.google.common.collect.ImmutableSet ;
import com.google.common.reflect.ClassPath ;
2023-08-24 09:43:16 +00:00
import dev.plex.extras.hook.SlimeWorldHook ;
import dev.plex.extras.listener.PlayerListener ;
2023-03-08 19:30:51 +00:00
import dev.plex.command.PlexCommand ;
2022-07-09 14:26:57 +00:00
import dev.plex.command.annotation.CommandParameters ;
import dev.plex.command.annotation.CommandPermissions ;
import dev.plex.config.ModuleConfig ;
2023-08-24 09:43:16 +00:00
import dev.plex.extras.jumppads.JumpPads ;
import dev.plex.extras.listener.JumpPadsListener ;
2022-07-09 14:26:57 +00:00
import dev.plex.listener.PlexListener ;
import dev.plex.module.PlexModule ;
import dev.plex.util.PlexLog ;
2023-03-08 19:30:51 +00:00
import lombok.Getter ;
import org.bukkit.Location ;
import org.bukkit.World ;
2022-07-09 14:26:57 +00:00
import java.io.IOException ;
import java.lang.reflect.InvocationTargetException ;
import java.util.Collections ;
import java.util.HashSet ;
import java.util.Set ;
import java.util.concurrent.ThreadLocalRandom ;
public class TFMExtras extends PlexModule
{
@Getter
private static TFMExtras module ;
public JumpPads jumpPads ;
@Getter
private ModuleConfig config ;
2023-08-24 09:43:16 +00:00
@Getter
private final SlimeWorldHook slimeWorldHook = new SlimeWorldHook ( ) ;
2022-07-09 14:26:57 +00:00
@Override
public void load ( )
{
module = this ;
config = new ModuleConfig ( this , " tfmextras/config.yml " , " config.yml " ) ;
config . load ( ) ;
jumpPads = new JumpPads ( ) ;
PlexLog . debug ( String . valueOf ( config . getInt ( " server.jumppad_strength " ) ) ) ;
}
@Override
public void enable ( )
{
2023-08-24 09:43:16 +00:00
if ( slimeWorldHook . plugin ( ) ! = null )
{
slimeWorldHook . onEnable ( this ) ;
}
2022-07-09 14:26:57 +00:00
2023-08-24 09:43:16 +00:00
getClassesFrom ( " dev.plex.extras.command " ) . forEach ( aClass - >
2023-03-08 19:30:51 +00:00
{
2023-08-24 09:43:16 +00:00
if ( PlexCommand . class . isAssignableFrom ( aClass ) & & aClass . isAnnotationPresent ( CommandParameters . class ) & & aClass . isAnnotationPresent ( CommandPermissions . class ) )
2022-07-09 14:26:57 +00:00
{
2023-03-08 19:30:51 +00:00
try
{
2022-07-09 14:26:57 +00:00
PlexCommand plexCommand = ( PlexCommand ) aClass . getConstructors ( ) [ 0 ] . newInstance ( ) ;
registerCommand ( plexCommand ) ;
2023-03-08 19:30:51 +00:00
}
catch ( InstantiationException | IllegalAccessException | InvocationTargetException e )
{
2022-07-09 14:26:57 +00:00
throw new RuntimeException ( e ) ;
}
}
} ) ;
2023-08-24 09:43:16 +00:00
getClassesFrom ( " dev.plex.extras.listener " ) . forEach ( aClass - >
2023-03-08 19:30:51 +00:00
{
2023-08-24 09:43:16 +00:00
if ( PlexListener . class . isAssignableFrom ( aClass ) )
2022-07-09 14:26:57 +00:00
{
2023-03-08 19:30:51 +00:00
try
{
2022-07-09 14:26:57 +00:00
PlexListener plexListener = ( PlexListener ) aClass . getConstructors ( ) [ 0 ] . newInstance ( ) ;
registerListener ( plexListener ) ;
2023-03-08 19:30:51 +00:00
}
catch ( InstantiationException | IllegalAccessException | InvocationTargetException e )
{
2022-07-09 14:26:57 +00:00
throw new RuntimeException ( e ) ;
}
}
} ) ;
addDefaultMessage ( " emptyAdminInfo " , " <red>The admin information section of the config.yml file has not been configured. " ) ;
addDefaultMessage ( " cakeLyrics " , " <rainbow>But there's no sense crying over every mistake. You just keep on trying till you run out of cake. " ) ;
addDefaultMessage ( " areaEffectCloudClear " , " <red>{0} - Removing all area effect clouds " , " 0 - The command sender " ) ;
addDefaultMessage ( " chatCleared " , " <red>{0} - Cleared the chat " , " 0 - The command sender " ) ;
addDefaultMessage ( " attributeList " , " <gold>All possible attributes: <yellow>{0} " , " 0 - The attribute list, each split by a new line " ) ;
addDefaultMessage ( " modifiedAutoClear " , " <gold>{0} will {1} have their inventory cleared when they join. " , " 0 - The player who will have their inventory cleared on join " , " 1 - Whether they had this option toggled (returns: 'no longer', 'now') " ) ;
2023-03-08 19:30:51 +00:00
addDefaultMessage ( " modifiedAutoTeleport " , " <gold>{0} will {1} be teleported automatically when they join. " , " 0 - The player to be teleported automatically " , " 1 - Whether they had this option toggled (returns: 'no longer', 'now') " ) ;
2023-08-24 09:43:16 +00:00
addDefaultMessage ( " createdPlayerWorld " , " <green>Welcome to the server! We've created you a new private world where you can invite your friends! View how to use this using /myworld! " ) ;
2022-07-09 14:26:57 +00:00
}
@Override
public void disable ( )
{
// Unregistering listeners / commands is handled by Plex
2023-08-24 09:43:16 +00:00
if ( slimeWorldHook . plugin ( ) ! = null )
{
slimeWorldHook . onDisable ( this ) ;
}
2022-07-09 14:26:57 +00:00
}
public static Location getRandomLocation ( World world )
{
double x = ThreadLocalRandom . current ( ) . nextDouble ( - 100000 , 100000 ) ;
double z = ThreadLocalRandom . current ( ) . nextDouble ( - 100000 , 100000 ) ;
2023-03-08 19:30:51 +00:00
double y = world . getHighestBlockYAt ( ( int ) x , ( int ) z ) + 1 ;
2022-07-09 14:26:57 +00:00
return new Location ( world , x , y , z ) ;
}
2023-03-08 19:30:51 +00:00
private Set < Class < ? > > getClassesFrom ( String packageName )
{
2022-07-09 14:26:57 +00:00
Set < Class < ? > > classes = new HashSet ( ) ;
2023-03-08 19:30:51 +00:00
try
{
2022-07-09 14:26:57 +00:00
ClassPath path = ClassPath . from ( TFMExtras . class . getClassLoader ( ) ) ;
ImmutableSet < ClassPath . ClassInfo > infoSet = path . getTopLevelClasses ( packageName ) ;
2023-03-08 19:30:51 +00:00
infoSet . forEach ( ( info ) - >
{
try
{
2022-07-09 14:26:57 +00:00
Class < ? > clazz = Class . forName ( info . getName ( ) ) ;
classes . add ( clazz ) ;
2023-03-08 19:30:51 +00:00
}
catch ( ClassNotFoundException var4 )
{
2022-07-09 14:26:57 +00:00
PlexLog . error ( " Unable to find class " + info . getName ( ) + " in " + packageName ) ;
}
} ) ;
2023-03-08 19:30:51 +00:00
}
catch ( IOException var4 )
{
2022-07-09 14:26:57 +00:00
PlexLog . error ( " Something went wrong while fetching classes from " + packageName ) ;
throw new RuntimeException ( var4 ) ;
}
return Collections . unmodifiableSet ( classes ) ;
}
}