2015-11-15 23:32:04 +00:00
package me.totalfreedom.totalfreedommod ;
2021-09-06 10:44:39 +00:00
import com.google.common.collect.Multimap ;
2020-08-31 00:13:46 +00:00
import io.papermc.lib.PaperLib ;
2021-09-06 10:44:39 +00:00
import java.util.Collection ;
import java.util.Map ;
2018-07-31 07:01:29 +00:00
import java.util.Objects ;
2018-07-21 16:34:34 +00:00
import org.bukkit.ChatColor ;
2020-01-04 23:35:41 +00:00
import org.bukkit.Location ;
2018-07-21 16:34:34 +00:00
import org.bukkit.Material ;
2021-09-06 10:44:39 +00:00
import org.bukkit.attribute.Attribute ;
import org.bukkit.attribute.AttributeModifier ;
2015-11-15 23:32:04 +00:00
import org.bukkit.entity.Player ;
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
2018-07-21 16:34:34 +00:00
import org.bukkit.event.player.PlayerItemHeldEvent ;
2015-11-15 23:32:04 +00:00
import org.bukkit.event.player.PlayerLoginEvent ;
2017-10-21 17:28:20 +00:00
import org.bukkit.event.player.PlayerMoveEvent ;
2015-11-15 23:32:04 +00:00
import org.bukkit.event.player.PlayerTeleportEvent ;
2018-07-21 16:34:34 +00:00
import org.bukkit.inventory.ItemStack ;
2021-09-06 10:44:39 +00:00
import org.bukkit.inventory.meta.ItemMeta ;
2018-07-21 16:34:34 +00:00
2016-03-01 16:47:01 +00:00
public class MovementValidator extends FreedomService
2015-11-15 23:32:04 +00:00
{
2017-10-21 17:28:20 +00:00
public static final int MAX_XYZ_COORD = 29999998 ;
2020-01-04 23:35:41 +00:00
public static final int MAX_DISTANCE_TRAVELED = 100 ;
2015-11-15 23:32:04 +00:00
@Override
2020-07-01 01:51:06 +00:00
public void onStart ( )
2015-11-15 23:32:04 +00:00
{
}
@Override
2020-07-01 01:51:06 +00:00
public void onStop ( )
2015-11-15 23:32:04 +00:00
{
}
@EventHandler ( priority = EventPriority . HIGH )
public void onPlayerTeleport ( PlayerTeleportEvent event )
{
// Check absolute value to account for negatives
2020-12-25 19:46:43 +00:00
if ( Math . abs ( Objects . requireNonNull ( event . getTo ( ) ) . getX ( ) ) > = MAX_XYZ_COORD | | Math . abs ( event . getTo ( ) . getZ ( ) ) > = MAX_XYZ_COORD | | Math . abs ( event . getTo ( ) . getY ( ) ) > = MAX_XYZ_COORD )
2015-11-15 23:32:04 +00:00
{
event . setCancelled ( true ) ; // illegal position, cancel it
}
}
2017-10-21 17:28:20 +00:00
@EventHandler ( priority = EventPriority . HIGH )
public void onPlayerMove ( PlayerMoveEvent event )
{
final Player player = event . getPlayer ( ) ;
2020-01-04 23:35:41 +00:00
Location from = event . getFrom ( ) ;
Location to = event . getTo ( ) ;
2020-12-25 19:46:43 +00:00
assert to ! = null ;
2020-01-04 23:35:41 +00:00
if ( to . getX ( ) > = from . getX ( ) + MAX_DISTANCE_TRAVELED | | to . getY ( ) > = from . getY ( ) + MAX_DISTANCE_TRAVELED | | to . getZ ( ) > = from . getZ ( ) + MAX_DISTANCE_TRAVELED )
{
event . setCancelled ( true ) ;
player . kickPlayer ( ChatColor . RED + " You were moving too quickly! " ) ;
}
2017-10-21 17:28:20 +00:00
// Check absolute value to account for negatives
if ( Math . abs ( event . getTo ( ) . getX ( ) ) > = MAX_XYZ_COORD | | Math . abs ( event . getTo ( ) . getZ ( ) ) > = MAX_XYZ_COORD | | Math . abs ( event . getTo ( ) . getY ( ) ) > = MAX_XYZ_COORD )
{
event . setCancelled ( true ) ;
2020-08-31 00:13:46 +00:00
PaperLib . teleportAsync ( player , player . getWorld ( ) . getSpawnLocation ( ) ) ;
2017-10-21 17:28:20 +00:00
}
2018-07-21 16:34:34 +00:00
if ( exploitItem ( event . getPlayer ( ) . getInventory ( ) . getHelmet ( ) ) )
{
event . getPlayer ( ) . getInventory ( ) . setHelmet ( new ItemStack ( Material . AIR ) ) ;
event . getPlayer ( ) . sendMessage ( ChatColor . RED + " An item with both negative infinity and positive infinity attributes was cleared from your helmet slot. " ) ;
event . setCancelled ( true ) ;
}
if ( exploitItem ( event . getPlayer ( ) . getInventory ( ) . getBoots ( ) ) )
{
event . getPlayer ( ) . getInventory ( ) . setBoots ( new ItemStack ( Material . AIR ) ) ;
event . getPlayer ( ) . sendMessage ( ChatColor . RED + " An item with both negative infinity and positive infinity attributes was cleared from your boots slot. " ) ;
event . setCancelled ( true ) ;
}
if ( exploitItem ( event . getPlayer ( ) . getInventory ( ) . getLeggings ( ) ) )
{
event . getPlayer ( ) . getInventory ( ) . setLeggings ( new ItemStack ( Material . AIR ) ) ;
event . getPlayer ( ) . sendMessage ( ChatColor . RED + " An item with both negative infinity and positive infinity attributes was cleared from your leggings slot. " ) ;
event . setCancelled ( true ) ;
}
if ( exploitItem ( event . getPlayer ( ) . getInventory ( ) . getChestplate ( ) ) )
{
event . getPlayer ( ) . getInventory ( ) . setChestplate ( new ItemStack ( Material . AIR ) ) ;
event . getPlayer ( ) . sendMessage ( ChatColor . RED + " An item with both negative infinity and positive infinity attributes was cleared from your chestplate slot. " ) ;
event . setCancelled ( true ) ;
}
if ( exploitItem ( event . getPlayer ( ) . getInventory ( ) . getItemInMainHand ( ) ) )
{
event . getPlayer ( ) . getInventory ( ) . setItemInMainHand ( new ItemStack ( Material . AIR ) ) ;
event . getPlayer ( ) . sendMessage ( ChatColor . RED + " An item with both negative infinity and positive infinity attributes was cleared from your hand. " ) ;
event . setCancelled ( true ) ;
}
if ( exploitItem ( event . getPlayer ( ) . getInventory ( ) . getItemInOffHand ( ) ) )
{
event . getPlayer ( ) . getInventory ( ) . setItemInOffHand ( new ItemStack ( Material . AIR ) ) ;
event . getPlayer ( ) . sendMessage ( ChatColor . RED + " An item with both negative infinity and positive infinity attributes was cleared from your offhand. " ) ;
event . setCancelled ( true ) ;
}
2017-10-21 17:28:20 +00:00
}
2015-11-15 23:32:04 +00:00
@EventHandler ( priority = EventPriority . HIGH )
public void onPlayerLogin ( PlayerLoginEvent event )
{
final Player player = event . getPlayer ( ) ;
// Validate position
2017-10-21 17:28:20 +00:00
if ( Math . abs ( player . getLocation ( ) . getX ( ) ) > = MAX_XYZ_COORD | | Math . abs ( player . getLocation ( ) . getZ ( ) ) > = MAX_XYZ_COORD | | Math . abs ( player . getLocation ( ) . getY ( ) ) > = MAX_XYZ_COORD )
2015-11-15 23:32:04 +00:00
{
2020-08-31 00:13:46 +00:00
PaperLib . teleportAsync ( player , player . getWorld ( ) . getSpawnLocation ( ) ) ; // Illegal position, teleport to spawn
2015-11-15 23:32:04 +00:00
}
}
2018-07-21 16:34:34 +00:00
@EventHandler
public void onPlayerHoldItem ( PlayerItemHeldEvent event )
{
if ( exploitItem ( event . getPlayer ( ) . getInventory ( ) . getItemInMainHand ( ) ) )
{
event . getPlayer ( ) . getInventory ( ) . setItemInMainHand ( new ItemStack ( Material . AIR ) ) ;
event . getPlayer ( ) . sendMessage ( ChatColor . RED + " An item with both negative infinity and positive infinity attributes was cleared from your hand. " ) ;
}
if ( exploitItem ( event . getPlayer ( ) . getInventory ( ) . getItemInOffHand ( ) ) )
{
event . getPlayer ( ) . getInventory ( ) . setItemInOffHand ( new ItemStack ( Material . AIR ) ) ;
event . getPlayer ( ) . sendMessage ( ChatColor . RED + " An item with both negative infinity and positive infinity attributes was cleared from your offhand. " ) ;
}
}
private Boolean exploitItem ( ItemStack item )
{
2021-09-06 10:44:39 +00:00
if ( item = = null )
2018-07-21 16:34:34 +00:00
{
2021-09-06 10:44:39 +00:00
return false ;
}
ItemMeta meta = item . getItemMeta ( ) ;
if ( meta ! = null )
{
Multimap < Attribute , AttributeModifier > attributes = meta . getAttributeModifiers ( ) ;
if ( attributes ! = null )
2018-07-21 16:34:34 +00:00
{
2021-09-06 10:44:39 +00:00
Map < Attribute , Collection < AttributeModifier > > attrMap = attributes . asMap ( ) ;
// For every attribute...
for ( Attribute attr : attributes . keySet ( ) )
2018-07-21 16:34:34 +00:00
{
2021-09-06 10:44:39 +00:00
// Default values
boolean posInf = false ;
boolean negInf = false ;
// For every AttributeModifier...
for ( AttributeModifier modifier : attrMap . get ( attr ) )
2018-07-21 16:34:34 +00:00
{
2021-09-06 10:44:39 +00:00
// Are they ∞ or -∞?
if ( modifier . getAmount ( ) = = Double . POSITIVE_INFINITY )
2018-07-21 16:34:34 +00:00
{
2021-09-06 10:44:39 +00:00
posInf = true ;
2018-07-21 16:34:34 +00:00
}
2021-09-06 10:44:39 +00:00
else if ( modifier . getAmount ( ) = = Double . NEGATIVE_INFINITY )
2018-07-21 16:34:34 +00:00
{
2021-09-06 10:44:39 +00:00
negInf = true ;
2018-07-21 16:34:34 +00:00
}
}
2021-09-06 10:44:39 +00:00
// Are both values set as true?
if ( posInf & & negInf )
{
return true ;
}
2018-07-21 16:34:34 +00:00
}
}
}
2021-09-06 10:44:39 +00:00
return false ;
2018-07-21 16:34:34 +00:00
}
2020-12-04 00:28:53 +00:00
}