Skip to content

Commit

Permalink
Better console log message
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Dec 16, 2024
1 parent 05122bd commit b564a46
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void onJoin(PlayerJoinEvent event) {

Inventory inv = event.getPlayer().getInventory();

ItemUtil.cleanIllegals(inv);
ItemUtil.cleanIllegals(inv, event.getPlayer().getName());
}

@EventHandler(ignoreCancelled = true)
Expand All @@ -40,7 +40,7 @@ public void onInventoryMove(InventoryMoveItemEvent event) {

if (!inv.getType().equals(InventoryType.CRAFTING)) return;

ItemUtil.cleanIllegals(inv);
ItemUtil.cleanIllegals(inv, inv.getType().name());
}

@EventHandler
Expand All @@ -52,7 +52,7 @@ public void onInventoryClose(InventoryCloseEvent event) {

if (!inv.getType().equals(InventoryType.PLAYER)) return;

ItemUtil.cleanIllegals(inv);
ItemUtil.cleanIllegals(inv, event.getPlayer().getName());
}

@EventHandler
Expand All @@ -64,7 +64,7 @@ public void onInventoryOpen(InventoryOpenEvent event) {

if (!inv.getType().equals(InventoryType.PLAYER)) return;

ItemUtil.cleanIllegals(inv);
ItemUtil.cleanIllegals(inv, event.getPlayer().getName());
}

@EventHandler(ignoreCancelled = true)
Expand Down Expand Up @@ -95,7 +95,11 @@ public void onPickup(EntityPickupItemEvent event) {
Player player = (Player) event.getEntity();
MessageUtil.sendMessage(player, "&6You can not pick up this illegal item.");
} else {
MessageUtil.println(event.getEntity().getName() + " try to pick up an illegal item at " + event.getItem().getLocation());
MessageUtil.println(String.format(
"%s try to pick up an illegal item at %s",
event.getEntity().getName().toLowerCase(),
MessageUtil.locToString(event.getItem().getLocation())
));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public void onPickup(ItemPickupEvent event) {
Player player = (Player) event.getEntity();
MessageUtil.sendMessage(player, "&6You can not pick up this illegal item.");
} else {
MessageUtil.println(event.getEntity().getName() + " try to pick up an illegal item at " + event.getStackedItem().getLocation());
MessageUtil.println(String.format(
"%s try to pick up an illegal item at %s",
event.getEntity().getName().toLowerCase(),
MessageUtil.locToString(event.getStackedItem().getLocation())
));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,25 @@ public void onDamage(EntityDamageByEntityEvent event) {
LivingEntity damager = (LivingEntity) entity;
// Only check entities using illegal items
if (damager.getEquipment() != null && damager.getEquipment().getItemInMainHand().hasItemMeta()) {
if (event.getDamage() > 30) {
double damage = event.getDamage();

if (damage > 30) {
String itemName = ItemUtil.getItemDisplayName(damager.getEquipment().getItemInMainHand());

event.setCancelled(true);
damager.getEquipment().setItemInMainHand(null); // Seems only can use item on main hand to attack
MessageUtil.println(damager.getName() + " | " + Config.checkIllegalDamageMessage + " | " + damager.getLocation());

MessageUtil.println(String.format(
"%s try to use illegal item %s with damage %s at %s",
damager.getName(),
itemName,
damage,
MessageUtil.locToString(damager.getLocation())
));
}
}
}

// Player: Projectile -> Entity
// Dreeam TODO: this is temp fix, need to rewrite.
if (entity instanceof Projectile) {
Expand Down Expand Up @@ -186,12 +198,18 @@ public void onDispense(BlockDispenseEvent event) {
if (ItemUtil.isIllegalEffect(effect)) {
event.setCancelled(true);
disp.getInventory().remove(event.getItem());
MessageUtil.println(Config.checkIllegalPotionMessage + " | " + event.getBlock().getLocation());

// One illegal potion effect appear, remove whole item
// then break the for loop.
break;
}
}

MessageUtil.println(String.format(
"Detected item %s with illegal effects at %s",
ItemUtil.getItemDisplayName(event.getItem()),
MessageUtil.locToString(event.getBlock().getLocation())
));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cn/dreeam/surf/modules/antilag/MinecartLag.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public void onSpawn(VehicleCreateEvent event) {

if (amount >= max) {
event.setCancelled(true);

for (Entity ent : event.getVehicle().getChunk().getEntities()) {
if (ent instanceof Vehicle) {
ent.remove();
}
}

MessageUtil.sendMessage(player, Util.getPrefix() + "&6Please limit " + formattedName + " to &r&c" + max + "&r&6 per chunk");
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cn/dreeam/surf/modules/patch/BucketEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public void onDispense(BlockDispenseEvent event) {

if (checkEndPortal(event.getBlock())) {
event.setCancelled(true);
MessageUtil.println(Config.preventBuketPortalMessage + " | " + event.getBlock().getLocation());

MessageUtil.println(String.format(
"Prevent a bucket on portal crash at %s.",
MessageUtil.locToString(event.getBlock().getLocation())
));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public void dispense(BlockDispenseEvent event) {
boolean isYMaxFacingUp = block.getY() == block.getWorld().getMaxHeight() - 1 && face == BlockFace.UP;
if (isY0FacingDown || isYMaxFacingUp) {
event.setCancelled(true);
MessageUtil.println("Prevent a dispenser crash at " + block.getLocation() +".");

MessageUtil.println(String.format(
"Prevent a dispenser crash at %s.",
MessageUtil.locToString(block.getLocation())
));
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/cn/dreeam/surf/modules/patch/GateWay.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public void onEntityPortal(EntityPortalEvent event) {
if (entity instanceof ChestedHorse) {
entity.eject();
event.setCancelled(true);
MessageUtil.println("&1Prevented a entity enter portal crash at" + entity.getLocation());

MessageUtil.println(String.format(
"&1Prevented a entity enter portal crash at %s",
MessageUtil.locToString(entity.getLocation())
));
}
}

Expand All @@ -77,7 +81,12 @@ public void EndGatewayTeleportProtection(VehicleMoveEvent event) {
vehicle.eject();
vehicle.remove();
Util.kickPlayer(player, "Sorry this exploit got patched :(");
MessageUtil.println("&1Prevented&r&e " + player.getName() + "&r&1 at &r&e" + vehicle.getLocation() + " &r&1from crashing the server");

MessageUtil.println(String.format(
"&1Prevented&r&e %s &r&1 at &r&e %s &r&1from crashing the server",
player.getName(),
MessageUtil.locToString(vehicle.getLocation())
));
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/cn/dreeam/surf/util/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import cn.dreeam.surf.config.Config;
import com.cryptomorin.xseries.XMaterial;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Item;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SpawnEggMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

Expand Down Expand Up @@ -172,7 +175,7 @@ public static boolean isIllegal(ItemStack i) {
|| isUnbreakable(i) || hasIllegalEnchants(i) || hasIllegalItemFlag(i) || hasIllegalAttributes(i);
}

public static void cleanIllegals(Inventory inventory) {
public static void cleanIllegals(Inventory inventory, String name) {
ItemStack[] contents = inventory.getContents();

// if inventory is empty, skip
Expand All @@ -190,7 +193,11 @@ public static void cleanIllegals(Inventory inventory) {
item.setAmount(0);
}

MessageUtil.println("&6Detected illegals " + original.getI18NDisplayName());
MessageUtil.println(String.format(
"&6Detected illegals %s on %s",
ItemUtil.getItemDisplayName(original),
name
) );
}
}
}
Expand Down Expand Up @@ -297,7 +304,11 @@ private static List<String> initIllegalBlocks() {
"REINFORCED_DEEPSLATE"
));

// <= 1.12.2
if (Util.isNewerAndEqual(12, 0)) {
list.addAll(Arrays.asList(

));
}

return list;
Expand Down Expand Up @@ -381,4 +392,5 @@ private static Map<String, Integer> initIllegalEnchantsMap() {

return map;
}
public static String getItemDisplayName(ItemStack itemStack) {
}
8 changes: 7 additions & 1 deletion src/main/java/cn/dreeam/surf/util/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand All @@ -21,7 +22,7 @@ public static void sendMessage(Player player, String message) {
player.sendMessage(message);
}

println(player + " | " + message);
println("[" + player + "] " + message);
}

@SuppressWarnings("deprecation")
Expand All @@ -43,4 +44,9 @@ public static void println(String message) {

Bukkit.getServer().getConsoleSender().sendMessage(message);
}

// World<x: 1, y: 2, z:3>
public static String locToString(Location loc) {
return loc.getWorld().getName() + "<x: " + loc.getBlockX() + ", y: " + loc.getBlockY() + ", z: " + loc.getBlockZ() + ">";
}
}

0 comments on commit b564a46

Please sign in to comment.