Merge branch 'development' into development

This commit is contained in:
allinkdev 2023-07-25 17:45:19 +01:00 committed by GitHub
commit e2bfab49db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 114 deletions

View File

@ -1,25 +1,17 @@
package me.totalfreedom.totalfreedommod; package me.totalfreedom.totalfreedommod;
import com.google.common.collect.Multimap;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import java.util.Collection; import net.kyori.adventure.text.Component;
import java.util.Map; import net.kyori.adventure.text.format.NamedTextColor;
import java.util.Objects;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack; import org.spigotmc.event.player.PlayerSpawnLocationEvent;
import org.bukkit.inventory.meta.ItemMeta;
public class MovementValidator extends FreedomService public class MovementValidator extends FreedomService
{ {
@ -41,67 +33,36 @@ public class MovementValidator extends FreedomService
public void onPlayerTeleport(PlayerTeleportEvent event) public void onPlayerTeleport(PlayerTeleportEvent event)
{ {
// Check absolute value to account for negatives // Check absolute value to account for negatives
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) if (isOutOfBounds(event.getTo()))
{ {
event.setCancelled(true); // illegal position, cancel it event.setCancelled(true); // illegal position, cancel it
} }
} }
private boolean isOutOfBounds(final Location position)
{
return Math.abs(position.getX()) >= MAX_XYZ_COORD || Math.abs(position.getY()) >= MAX_XYZ_COORD || Math.abs(position.getZ()) >= MAX_XYZ_COORD;
}
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
public void onPlayerMove(PlayerMoveEvent event) public void onPlayerMove(PlayerMoveEvent event)
{ {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
Location from = event.getFrom(); Location from = event.getFrom();
Location to = event.getTo(); Location to = event.getTo();
assert to != null; double distance = from.distanceSquared(to);
if (to.getX() >= from.getX() + MAX_DISTANCE_TRAVELED || to.getY() >= from.getY() + MAX_DISTANCE_TRAVELED || to.getZ() >= from.getZ() + MAX_DISTANCE_TRAVELED)
if (distance >= MAX_DISTANCE_TRAVELED)
{ {
event.setCancelled(true); event.setCancelled(true);
player.kickPlayer(ChatColor.RED + "You were moving too quickly!"); player.kick(Component.text("You were moving too quickly!", NamedTextColor.RED));
} }
// Check absolute value to account for negatives // 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) if (isOutOfBounds(event.getTo()))
{ {
event.setCancelled(true); event.setCancelled(true);
PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation());
} }
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);
}
} }
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
@ -110,72 +71,28 @@ public class MovementValidator extends FreedomService
final Player player = event.getPlayer(); final Player player = event.getPlayer();
// Validate position // Validate position
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) if (isOutOfBounds(player.getLocation()))
{ {
PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn
} }
} }
@EventHandler @EventHandler(priority = EventPriority.HIGH)
public void onPlayerHoldItem(PlayerItemHeldEvent event) public void onPlayerSpawn(PlayerSpawnLocationEvent event)
{ {
if (exploitItem(event.getPlayer().getInventory().getItemInMainHand())) final Location playerSpawn = event.getSpawnLocation();
{ final Location worldSpawn = event.getPlayer().getWorld().getSpawnLocation();
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) // If the player's spawn is equal to the world's spawn, there is no need to check.
{ // This will also prevent any possible feedback loops pertaining to setting an out of bounds world spawn to the same world spawn.
if (item == null) if (playerSpawn == worldSpawn)
{ {
return false; return;
} }
ItemMeta meta = item.getItemMeta(); if (isOutOfBounds(worldSpawn))
if (meta != null)
{ {
Multimap<Attribute, AttributeModifier> attributes = meta.getAttributeModifiers(); event.setSpawnLocation(worldSpawn);
if (attributes != null)
{
Map<Attribute, Collection<AttributeModifier>> attrMap = attributes.asMap();
// For every attribute...
for (Attribute attr : attributes.keySet())
{
// Default values
boolean posInf = false;
boolean negInf = false;
// For every AttributeModifier...
for (AttributeModifier modifier : attrMap.get(attr))
{
// Are they or -?
if (modifier.getAmount() == Double.POSITIVE_INFINITY)
{
posInf = true;
}
else if (modifier.getAmount() == Double.NEGATIVE_INFINITY)
{
negInf = true;
}
}
// Are both values set as true?
if (posInf && negInf)
{
return true;
}
}
}
} }
return false;
} }
} }

View File

@ -172,7 +172,7 @@ public class EventBlocker extends FreedomService
@EventHandler(priority = EventPriority.NORMAL) @EventHandler(priority = EventPriority.NORMAL)
public void onPlayerDropItem(PlayerDropItemEvent event) public void onPlayerDropItem(PlayerDropItemEvent event)
{ {
if (!plugin.al.isAdmin(event.getPlayer())) if (!plugin.al.isAdmin(event.getPlayer()) && !ConfigEntry.ALLOW_ITEM_DROPS.getBoolean())
{ {
event.setCancelled(true); event.setCancelled(true);
} }

View File

@ -19,9 +19,9 @@ public class Command_toggle extends FreedomCommand
private final List<String> toggles = Arrays.asList( private final List<String> toggles = Arrays.asList(
"waterplace", "fireplace", "lavaplace", "fluidspread", "lavadmg", "firespread", "frostwalk", "waterplace", "fireplace", "lavaplace", "fluidspread", "lavadmg", "firespread", "frostwalk",
"firework", "prelog", "lockdown", "petprotect", "entitywipe", "nonuke [range] [count]", "firework", "prelog", "lockdown", "petprotect", "entitywipe", "nonuke [range] [count]",
"explosives [radius]", "unsafeenchs", "bells", "armorstands", "masterblocks", "books", "grindstones", "explosives [radius]", "unsafeenchs", "bells", "armorstands", "masterblocks", "item_drops",
"jukeboxes", "spawners", "4chan", "beehives", "respawnanchors", "autotp", "autoclear", "minecarts", "mp44", "grindstones", "jukeboxes", "spawners", "4chan", "beehives", "respawnanchors", "autotp", "autoclear", "minecarts",
"landmines", "tossmob", "gravity"); "mp44", "landmines", "tossmob", "gravity");
@Override @Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
@ -205,6 +205,12 @@ public class Command_toggle extends FreedomCommand
break; break;
} }
case "item_drops":
{
toggle("Item drops are", ConfigEntry.ALLOW_ITEM_DROPS);
break;
}
case "grindstones": case "grindstones":
{ {
toggle("Grindstones are", ConfigEntry.ALLOW_GRINDSTONES); toggle("Grindstones are", ConfigEntry.ALLOW_GRINDSTONES);

View File

@ -35,6 +35,7 @@ public enum ConfigEntry
ALLOW_GRAVITY(Boolean.class, "allow.gravity"), ALLOW_GRAVITY(Boolean.class, "allow.gravity"),
ALLOW_MASTERBLOCKS(Boolean.class, "allow.masterblocks"), ALLOW_MASTERBLOCKS(Boolean.class, "allow.masterblocks"),
ALLOW_BOOKS(Boolean.class, "allow.books"), ALLOW_BOOKS(Boolean.class, "allow.books"),
ALLOW_ITEM_DROPS(Boolean.class, "allow.item_drops"),
// //
BLOCKED_CHATCODES(String.class, "blocked_chatcodes"), BLOCKED_CHATCODES(String.class, "blocked_chatcodes"),
// //

View File

@ -221,6 +221,7 @@ allow:
gravity: false gravity: false
masterblocks: false masterblocks: false
books: true books: true
item_drops: false
blocked_commands: blocked_commands:
# #