2017-10-13 18:35:11 +00:00
package me.totalfreedom.totalfreedommod.bridge ;
2018-07-31 07:01:29 +00:00
import java.io.File ;
2021-10-01 05:21:50 +00:00
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
2020-11-30 23:17:27 +00:00
import java.text.DecimalFormat ;
2018-07-31 07:01:29 +00:00
import java.util.Arrays ;
2020-10-17 03:33:33 +00:00
import java.util.Collections ;
2020-11-30 23:17:27 +00:00
import java.util.HashMap ;
2018-07-31 07:01:29 +00:00
import java.util.List ;
2020-11-30 23:17:27 +00:00
import java.util.Map ;
2017-10-13 18:35:11 +00:00
import me.totalfreedom.totalfreedommod.FreedomService ;
import me.totalfreedom.totalfreedommod.config.ConfigEntry ;
2020-11-30 23:17:27 +00:00
import me.totalfreedom.totalfreedommod.player.PlayerData ;
2018-02-22 12:55:04 +00:00
import me.totalfreedom.totalfreedommod.util.FLog ;
2020-11-30 23:17:27 +00:00
import me.totalfreedom.totalfreedommod.util.FUtil ;
2017-10-13 18:35:11 +00:00
import net.coreprotect.CoreProtect ;
import net.coreprotect.CoreProtectAPI ;
import org.bukkit.Bukkit ;
2020-11-30 23:17:27 +00:00
import org.bukkit.ChatColor ;
2017-10-13 18:35:11 +00:00
import org.bukkit.World ;
2020-11-30 23:17:27 +00:00
import org.bukkit.block.Block ;
import org.bukkit.block.BlockState ;
import org.bukkit.block.data.BlockData ;
import org.bukkit.entity.Player ;
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
import org.bukkit.event.block.Action ;
import org.bukkit.event.player.PlayerInteractEvent ;
2018-02-22 12:55:04 +00:00
import org.bukkit.plugin.Plugin ;
import org.bukkit.scheduler.BukkitRunnable ;
import org.bukkit.scheduler.BukkitTask ;
2017-10-13 18:35:11 +00:00
public class CoreProtectBridge extends FreedomService
{
2020-12-25 19:46:43 +00:00
public static Map < Player , FUtil . PaginationList < String > > HISTORY_MAP = new HashMap < > ( ) ;
2017-10-13 18:35:11 +00:00
private final List < String > tables = Arrays . asList ( " co_sign " , " co_session " , " co_container " , " co_block " ) ;
2018-07-25 02:44:00 +00:00
2020-11-30 23:17:27 +00:00
private final HashMap < String , Long > cooldown = new HashMap < > ( ) ;
2020-12-25 19:46:43 +00:00
private CoreProtectAPI coreProtectAPI = null ;
private BukkitTask wiper ;
2020-11-30 23:17:27 +00:00
2020-12-25 19:46:43 +00:00
public static Long getSecondsLeft ( long prevTime , int timeAdd )
{
return prevTime / 1000L + timeAdd - System . currentTimeMillis ( ) / 1000L ;
}
2020-11-30 23:17:27 +00:00
2020-12-25 19:46:43 +00:00
// Unix timestamp converter taken from Functions class in CoreProtect, not my code
public static String getTimeAgo ( int logTime , int currentTime )
{
StringBuilder message = new StringBuilder ( ) ;
double timeSince = ( double ) currentTime - ( ( double ) logTime + 0 . 0D ) ;
timeSince / = 60 . 0D ;
if ( timeSince < 60 . 0D )
{
message . append ( ( new DecimalFormat ( " 0.00 " ) ) . format ( timeSince ) ) . append ( " /m ago " ) ;
}
if ( message . length ( ) = = 0 )
{
timeSince / = 60 . 0D ;
if ( timeSince < 24 . 0D )
{
message . append ( ( new DecimalFormat ( " 0.00 " ) ) . format ( timeSince ) ) . append ( " /h ago " ) ;
}
}
if ( message . length ( ) = = 0 )
{
timeSince / = 24 . 0D ;
message . append ( ( new DecimalFormat ( " 0.00 " ) ) . format ( timeSince ) ) . append ( " /d ago " ) ;
}
return message . toString ( ) ;
}
2017-10-13 18:35:11 +00:00
@Override
2020-07-01 01:51:06 +00:00
public void onStart ( )
2017-10-13 18:35:11 +00:00
{
}
@Override
2020-07-01 01:51:06 +00:00
public void onStop ( )
2017-10-13 18:35:11 +00:00
{
}
public CoreProtect getCoreProtect ( )
{
CoreProtect coreProtect = null ;
try
{
final Plugin coreProtectPlugin = Bukkit . getServer ( ) . getPluginManager ( ) . getPlugin ( " CoreProtect " ) ;
2020-12-25 19:46:43 +00:00
assert coreProtectPlugin ! = null ;
if ( coreProtectPlugin instanceof CoreProtect )
2017-10-13 18:35:11 +00:00
{
2018-07-31 07:01:29 +00:00
coreProtect = ( CoreProtect ) coreProtectPlugin ;
2017-10-13 18:35:11 +00:00
}
}
catch ( Exception ex )
{
FLog . severe ( ex ) ;
}
return coreProtect ;
}
public CoreProtectAPI getCoreProtectAPI ( )
{
if ( coreProtectAPI = = null )
{
try
{
final CoreProtect coreProtect = getCoreProtect ( ) ;
2018-07-25 02:44:00 +00:00
2017-10-13 18:35:11 +00:00
coreProtectAPI = coreProtect . getAPI ( ) ;
2018-07-25 02:44:00 +00:00
2017-10-13 18:35:11 +00:00
// Check if the plugin or api is not enabled, if so, return null
if ( ! coreProtect . isEnabled ( ) | | ! coreProtectAPI . isEnabled ( ) )
{
return null ;
}
}
catch ( Exception ex )
{
FLog . severe ( ex ) ;
}
}
return coreProtectAPI ;
}
2018-07-25 02:44:00 +00:00
2017-10-13 18:35:11 +00:00
public boolean isEnabled ( )
{
final CoreProtect coreProtect = getCoreProtect ( ) ;
return coreProtect ! = null & & coreProtect . isEnabled ( ) ;
}
2018-07-28 07:11:48 +00:00
// Rollback the specified player's edits that were in the last 24 hours.
2017-10-13 18:35:11 +00:00
public void rollback ( final String name )
{
final CoreProtectAPI coreProtect = getCoreProtectAPI ( ) ;
if ( ! isEnabled ( ) )
{
return ;
}
new BukkitRunnable ( )
{
@Override
public void run ( )
{
2020-10-17 03:33:33 +00:00
coreProtect . performRollback ( 86400 , Collections . singletonList ( name ) , null , null , null , null , 0 , null ) ;
2017-10-13 18:35:11 +00:00
}
} . runTaskAsynchronously ( plugin ) ;
}
2018-07-25 02:44:00 +00:00
2018-07-28 07:11:48 +00:00
// Reverts a rollback for the specified player's edits that were in the last 24 hours.
2018-07-26 22:55:55 +00:00
public void restore ( final String name )
2017-10-13 18:35:11 +00:00
{
final CoreProtectAPI coreProtect = getCoreProtectAPI ( ) ;
if ( ! isEnabled ( ) )
{
return ;
}
new BukkitRunnable ( )
{
@Override
public void run ( )
{
2020-10-17 03:33:33 +00:00
coreProtect . performRestore ( 86400 , Collections . singletonList ( name ) , null , null , null , null , 0 , null ) ;
2017-10-13 18:35:11 +00:00
}
} . runTaskAsynchronously ( plugin ) ;
}
2018-07-25 02:44:00 +00:00
2017-10-13 18:35:11 +00:00
public File getDatabase ( )
{
if ( ! isEnabled ( ) )
{
return null ;
}
2018-07-25 02:44:00 +00:00
return ( new File ( getCoreProtect ( ) . getDataFolder ( ) , " database.db " ) ) ;
2017-10-13 18:35:11 +00:00
}
2018-07-25 02:44:00 +00:00
2017-10-13 18:35:11 +00:00
public double getDBSize ( )
{
double bytes = getDatabase ( ) . length ( ) ;
double kilobytes = ( bytes / 1024 ) ;
double megabytes = ( kilobytes / 1024 ) ;
return ( megabytes / 1024 ) ;
}
2018-07-25 02:44:00 +00:00
2017-10-13 18:35:11 +00:00
// Wipes DB for the specified world
public void clearDatabase ( World world )
{
clearDatabase ( world , false ) ;
}
// Wipes DB for the specified world
public void clearDatabase ( World world , Boolean shutdown )
{
2019-02-22 23:16:08 +00:00
if ( ! ConfigEntry . COREPROTECT_MYSQL_ENABLED . getBoolean ( ) )
{
return ;
}
2017-10-13 18:35:11 +00:00
final CoreProtect coreProtect = getCoreProtect ( ) ;
if ( coreProtect = = null )
{
return ;
}
2018-07-28 07:11:48 +00:00
/ * As CoreProtect doesn ' t have an API method for deleting all of the data for a specific world
we have to do this manually via SQL * /
2020-12-25 19:46:43 +00:00
Connection connection ;
2017-10-13 18:35:11 +00:00
try
{
2019-02-22 23:16:08 +00:00
String host = ConfigEntry . COREPROTECT_MYSQL_HOST . getString ( ) ;
String port = ConfigEntry . COREPROTECT_MYSQL_PORT . getString ( ) ;
String username = ConfigEntry . COREPROTECT_MYSQL_USERNAME . getString ( ) ;
String password = ConfigEntry . COREPROTECT_MYSQL_PASSWORD . getString ( ) ;
String database = ConfigEntry . COREPROTECT_MYSQL_DATABASE . getString ( ) ;
String url = host + " : " + port + " / " + database + " ?user= " + username + " &password= " + password + " &useSSL=false " ;
2020-05-29 10:14:21 +00:00
connection = DriverManager . getConnection ( " jdbc:sql:// " + url ) ;
2021-03-19 15:39:58 +00:00
final PreparedStatement statement = connection . prepareStatement ( " SELECT id FROM co_world WHERE world = ? " ) ;
2017-10-13 18:35:11 +00:00
statement . setQueryTimeout ( 30 ) ;
2018-07-25 02:44:00 +00:00
2017-10-13 18:35:11 +00:00
// Obtain world ID from CoreProtect database
2021-03-19 15:39:58 +00:00
statement . setString ( 1 , world . getName ( ) ) ;
ResultSet resultSet = statement . executeQuery ( ) ;
2017-10-13 18:35:11 +00:00
String worldID = null ;
while ( resultSet . next ( ) )
{
worldID = String . valueOf ( resultSet . getInt ( " id " ) ) ;
}
// Ensure the world ID is not null
2020-05-29 10:14:21 +00:00
2017-10-13 18:35:11 +00:00
if ( worldID = = null )
{
FLog . warning ( " Failed to obtain the world ID for the " + world . getName ( ) ) ;
return ;
}
// Iterate through each table and delete their data if the world ID matches
for ( String table : tables )
{
2021-03-19 15:39:58 +00:00
final PreparedStatement statement1 = connection . prepareStatement ( " DELETE FROM ? WHERE wid = ? " ) ;
statement1 . setString ( 1 , table ) ;
statement1 . setString ( 2 , worldID ) ;
statement1 . executeQuery ( ) ;
2017-10-13 18:35:11 +00:00
}
connection . close ( ) ;
}
catch ( SQLException e )
{
FLog . warning ( " Failed to delete the CoreProtect data for the " + world . getName ( ) ) ;
}
// This exits for flatlands wipes
if ( shutdown )
{
2020-04-23 11:18:03 +00:00
server . shutdown ( ) ;
2017-10-13 18:35:11 +00:00
}
}
2020-11-30 23:17:27 +00:00
@EventHandler ( priority = EventPriority . MONITOR )
public void onPlayerInteract ( PlayerInteractEvent event )
{
Player player = event . getPlayer ( ) ;
PlayerData data = plugin . pl . getData ( player ) ;
Block block = event . getClickedBlock ( ) ;
final CoreProtectAPI coreProtect = getCoreProtectAPI ( ) ;
if ( data . hasInspection ( ) )
{
if ( event . getAction ( ) = = Action . LEFT_CLICK_BLOCK )
{
if ( block ! = null )
{
event . setCancelled ( true ) ;
int cooldownTime = 3 ;
if ( cooldown . containsKey ( player . getName ( ) ) )
{
long secondsLeft = getSecondsLeft ( cooldown . get ( player . getName ( ) ) , cooldownTime ) ;
if ( secondsLeft > 0L )
{
event . setCancelled ( true ) ;
player . sendMessage ( ChatColor . RED + String . valueOf ( secondsLeft ) + " seconds left before next query. " ) ;
return ;
}
}
2021-10-08 22:54:21 +00:00
List < String [ ] > lookup = coreProtect . blockLookup ( block , - 1 ) ;
2020-12-04 00:28:53 +00:00
if ( ! plugin . al . isAdmin ( player ) )
2020-11-30 23:17:27 +00:00
{
cooldown . put ( player . getName ( ) , System . currentTimeMillis ( ) ) ;
}
if ( lookup ! = null )
{
if ( lookup . isEmpty ( ) )
{
player . sendMessage ( net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " ) + " Block Inspector " + ChatColor . WHITE + " - " + " No block data found for this location " ) ;
return ;
}
HISTORY_MAP . remove ( event . getPlayer ( ) ) ;
HISTORY_MAP . put ( event . getPlayer ( ) , new FUtil . PaginationList < > ( 10 ) ) ;
FUtil . PaginationList < String > paged = HISTORY_MAP . get ( event . getPlayer ( ) ) ;
2021-10-01 08:26:50 +00:00
player . sendMessage ( " ---- " + net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " ) + " Block Inspector " + ChatColor . WHITE + " ---- "
+ ChatColor . GRAY + " (x " + block . getX ( ) + " / " + " y " + block . getY ( ) + " / " + " z " + block . getZ ( ) + " ) " ) ;
2020-11-30 23:17:27 +00:00
for ( String [ ] value : lookup )
{
CoreProtectAPI . ParseResult result = coreProtect . parseResult ( value ) ;
BlockData bl = result . getBlockData ( ) ;
String s ;
String st = " " ;
if ( result . getActionString ( ) . equals ( " Placement " ) )
{
s = " placed " ;
}
2020-12-01 23:53:28 +00:00
else if ( result . getActionString ( ) . equals ( " Removal " ) )
2020-11-30 23:17:27 +00:00
{
s = " broke " ;
}
2020-12-01 23:53:28 +00:00
else
{
s = " interacted with " ;
}
2020-11-30 23:17:27 +00:00
if ( result . isRolledBack ( ) )
{
st + = " §m " ;
}
int time = ( int ) ( System . currentTimeMillis ( ) / 1000L ) ;
2021-10-01 08:26:50 +00:00
paged . add ( ChatColor . GRAY + getTimeAgo ( result . getTime ( ) , time ) + ChatColor . WHITE + " - " + net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " )
+ st + result . getPlayer ( ) + ChatColor . WHITE + st + s + net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " ) + st + bl . getMaterial ( ) . toString ( ) . toLowerCase ( ) ) ;
2020-11-30 23:17:27 +00:00
}
List < String > page = paged . getPage ( 1 ) ;
for ( String entries : page )
{
player . sendMessage ( entries ) ;
}
player . sendMessage ( " Page 1/ " + paged . getPageCount ( ) + " | To index through the pages, type " + net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " ) + " /ins history <page> " ) ;
}
}
}
else if ( event . getAction ( ) = = Action . RIGHT_CLICK_BLOCK )
{
if ( block ! = null )
{
if ( data . hasInspection ( ) )
{
2020-12-01 23:53:28 +00:00
BlockState blockState = block . getRelative ( event . getBlockFace ( ) ) . getState ( ) ;
Block placedBlock = blockState . getBlock ( ) ;
2020-11-30 23:17:27 +00:00
event . setCancelled ( true ) ;
2020-12-01 23:53:28 +00:00
List < String [ ] > lookup = coreProtect . blockLookup ( placedBlock , - 1 ) ;
if ( lookup . isEmpty ( ) )
{
lookup = coreProtect . blockLookup ( block , - 1 ) ;
}
2020-11-30 23:17:27 +00:00
int cooldownTime = 3 ;
if ( cooldown . containsKey ( player . getName ( ) ) )
{
long secondsLeft = getSecondsLeft ( cooldown . get ( player . getName ( ) ) , cooldownTime ) ;
if ( secondsLeft > 0L )
{
event . setCancelled ( true ) ;
player . sendMessage ( ChatColor . RED + String . valueOf ( secondsLeft ) + " seconds left before next query. " ) ;
return ;
}
}
2020-12-04 00:28:53 +00:00
if ( ! plugin . al . isAdmin ( player ) )
2020-11-30 23:17:27 +00:00
{
cooldown . put ( player . getName ( ) , System . currentTimeMillis ( ) ) ;
}
if ( lookup ! = null )
{
if ( lookup . isEmpty ( ) )
{
player . sendMessage ( net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " ) + " Block Inspector " + ChatColor . WHITE + " - " + " No block data found for this location " ) ;
return ;
}
HISTORY_MAP . remove ( event . getPlayer ( ) ) ;
HISTORY_MAP . put ( event . getPlayer ( ) , new FUtil . PaginationList < > ( 10 ) ) ;
FUtil . PaginationList < String > paged = HISTORY_MAP . get ( event . getPlayer ( ) ) ;
2021-10-01 08:26:50 +00:00
player . sendMessage ( " ---- " + net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " ) + " Block Inspector " + ChatColor . WHITE + " ---- "
+ ChatColor . GRAY + " (x " + block . getX ( ) + " / " + " y " + block . getY ( ) + " / " + " z " + block . getZ ( ) + " ) " ) ;
2020-11-30 23:17:27 +00:00
for ( String [ ] value : lookup )
{
CoreProtectAPI . ParseResult result = coreProtect . parseResult ( value ) ;
BlockData bl = result . getBlockData ( ) ;
String s ;
String st = " " ;
if ( result . getActionString ( ) . equals ( " Placement " ) )
{
s = " placed " ;
}
2020-12-01 23:53:28 +00:00
else if ( result . getActionString ( ) . equals ( " Removal " ) )
2020-11-30 23:17:27 +00:00
{
s = " broke " ;
}
2020-12-01 23:53:28 +00:00
else
{
s = " interacted with " ;
}
2020-11-30 23:17:27 +00:00
if ( result . isRolledBack ( ) )
{
st + = " §m " ;
}
int time = ( int ) ( System . currentTimeMillis ( ) / 1000L ) ;
2021-10-01 08:26:50 +00:00
paged . add ( ChatColor . GRAY + getTimeAgo ( result . getTime ( ) , time ) + ChatColor . WHITE + " - " + net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " )
+ st + result . getPlayer ( ) + ChatColor . WHITE + st + s + net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " ) + st + bl . getMaterial ( ) . toString ( ) . toLowerCase ( ) ) ;
2020-11-30 23:17:27 +00:00
}
List < String > page = paged . getPage ( 1 ) ;
for ( String entries : page )
{
player . sendMessage ( entries ) ;
}
player . sendMessage ( " Page 1/ " + paged . getPageCount ( ) + " | To index through the pages, type " + net . md_5 . bungee . api . ChatColor . of ( " #30ade4 " ) + " /ins history <page> " ) ;
}
}
}
}
}
}
2017-10-13 18:35:11 +00:00
}