Skip to content

Commit 2760c75

Browse files
committed
feat: enhance spawner event logging with entity type information
1 parent ca1274d commit 2760c75

12 files changed

Lines changed: 122 additions & 20 deletions

File tree

api/src/main/java/github/nighter/smartspawner/api/events/SpawnerBreakEvent.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import lombok.Getter;
44
import org.bukkit.Location;
55
import org.bukkit.entity.Entity;
6+
import org.bukkit.entity.EntityType;
67
import org.bukkit.event.HandlerList;
78
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
810

911
/**
1012
* Called when a spawner is broken by a player or explosion.
@@ -15,6 +17,7 @@ public class SpawnerBreakEvent extends SpawnerEvent {
1517
private static final HandlerList handlers = new HandlerList();
1618

1719
private final Entity entity;
20+
private final EntityType entityType;
1821

1922
/**
2023
* Creates a new spawner break event.
@@ -24,8 +27,21 @@ public class SpawnerBreakEvent extends SpawnerEvent {
2427
* @param quantity the quantity of the broken spawner
2528
*/
2629
public SpawnerBreakEvent(Entity entity, Location location, int quantity) {
30+
this(entity, location, quantity, null);
31+
}
32+
33+
/**
34+
* Creates a new spawner break event.
35+
*
36+
* @param entity the entity that broke the spawner
37+
* @param location the location of the broken spawner
38+
* @param quantity the quantity of the broken spawner
39+
* @param entityType the spawned entity type of the broken spawner (nullable for item spawners)
40+
*/
41+
public SpawnerBreakEvent(Entity entity, Location location, int quantity, @Nullable EntityType entityType) {
2742
super(location, quantity);
2843
this.entity = entity;
44+
this.entityType = entityType;
2945
}
3046

3147

api/src/main/java/github/nighter/smartspawner/api/events/SpawnerExplodeEvent.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import lombok.Getter;
44
import org.bukkit.Location;
55
import org.bukkit.entity.Entity;
6+
import org.bukkit.entity.EntityType;
67
import org.bukkit.event.HandlerList;
78
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
810

911
/**
1012
* Called when a spawner is destroyed by an explosion.
@@ -25,7 +27,21 @@ public class SpawnerExplodeEvent extends SpawnerBreakEvent {
2527
* @param exploded whether the spawner was successfully exploded
2628
*/
2729
public SpawnerExplodeEvent(Entity entity, Location location, int quantity, boolean exploded) {
28-
super(entity, location, quantity);
30+
this(entity, location, quantity, exploded, null);
31+
}
32+
33+
/**
34+
* Creates a new spawner explode event.
35+
*
36+
* @param entity the entity that caused the explosion
37+
* @param location the location where the spawner was destroyed
38+
* @param quantity the quantity of the spawner
39+
* @param exploded whether the spawner was successfully exploded
40+
* @param entityType the spawned entity type of the exploded spawner
41+
*/
42+
public SpawnerExplodeEvent(Entity entity, Location location, int quantity, boolean exploded,
43+
@Nullable EntityType entityType) {
44+
super(entity, location, quantity, entityType);
2945
this.exploded = exploded;
3046
}
3147

api/src/main/java/github/nighter/smartspawner/api/events/SpawnerPlayerBreakEvent.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import org.bukkit.Location;
6+
import org.bukkit.entity.EntityType;
67
import org.bukkit.entity.Player;
78
import org.bukkit.event.Cancellable;
89
import org.bukkit.event.HandlerList;
910
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
1012

1113
/**
1214
* Called when a spawner is broken by a player.
@@ -30,7 +32,19 @@ public class SpawnerPlayerBreakEvent extends SpawnerBreakEvent implements Cancel
3032
* @param quantity the quantity of the spawner
3133
*/
3234
public SpawnerPlayerBreakEvent(Player player, Location location, int quantity) {
33-
super(player, location, quantity);
35+
this(player, location, quantity, null);
36+
}
37+
38+
/**
39+
* Creates a new spawner player break event.
40+
*
41+
* @param player the player who broke the spawner
42+
* @param location the location of the broken spawner
43+
* @param quantity the quantity of the spawner
44+
* @param entityType the spawned entity type of the broken spawner
45+
*/
46+
public SpawnerPlayerBreakEvent(Player player, Location location, int quantity, @Nullable EntityType entityType) {
47+
super(player, location, quantity, entityType);
3448
this.player = player;
3549
}
3650

api/src/main/java/github/nighter/smartspawner/api/events/SpawnerSellEvent.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import org.bukkit.Location;
6+
import org.bukkit.entity.EntityType;
67
import org.bukkit.entity.Player;
78
import org.bukkit.event.Cancellable;
89
import org.bukkit.event.Event;
910
import org.bukkit.event.HandlerList;
1011
import org.bukkit.inventory.ItemStack;
1112
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
1214

1315
import java.util.List;
1416

@@ -24,6 +26,7 @@ public class SpawnerSellEvent extends Event implements Cancellable {
2426
private final Player player;
2527
private final Location location;
2628
private final List<ItemStack> items;
29+
private final EntityType entityType;
2730
private double moneyAmount;
2831
private boolean cancelled = false;
2932

@@ -36,10 +39,25 @@ public class SpawnerSellEvent extends Event implements Cancellable {
3639
* @param moneyAmount the amount of money to be given
3740
*/
3841
public SpawnerSellEvent(Player player, Location location, List<ItemStack> items, double moneyAmount) {
42+
this(player, location, items, moneyAmount, null);
43+
}
44+
45+
/**
46+
* Creates a new spawner sell event.
47+
*
48+
* @param player the player selling the items
49+
* @param location the location of the spawner
50+
* @param items the items being sold
51+
* @param moneyAmount the amount of money to be given
52+
* @param entityType the spawned entity type of the selling spawner
53+
*/
54+
public SpawnerSellEvent(Player player, Location location, List<ItemStack> items, double moneyAmount,
55+
@Nullable EntityType entityType) {
3956
this.player = player;
4057
this.location = location;
4158
this.items = items;
4259
this.moneyAmount = moneyAmount;
60+
this.entityType = entityType;
4361
}
4462

4563
@Override

api/src/main/java/github/nighter/smartspawner/api/events/SpawnerStackEvent.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import org.bukkit.Location;
6+
import org.bukkit.entity.EntityType;
67
import org.bukkit.entity.Player;
78
import org.bukkit.event.Cancellable;
89
import org.bukkit.event.Event;
910
import org.bukkit.event.HandlerList;
1011
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
1113

1214
/**
1315
* Called when spawners are stacked together.
@@ -22,6 +24,7 @@ public class SpawnerStackEvent extends Event implements Cancellable {
2224
private final int oldStackSize;
2325
private final int newStackSize;
2426
private final StackSource source;
27+
private final EntityType entityType;
2528

2629
@Setter
2730
private boolean cancelled = false;
@@ -36,11 +39,27 @@ public class SpawnerStackEvent extends Event implements Cancellable {
3639
* @param source the source of the stack operation
3740
*/
3841
public SpawnerStackEvent(Player player, Location location, int oldStackSize, int newStackSize, StackSource source) {
42+
this(player, location, oldStackSize, newStackSize, source, null);
43+
}
44+
45+
/**
46+
* Creates a new spawner stack event.
47+
*
48+
* @param player the player who stacked the spawner
49+
* @param location the location of the spawner
50+
* @param oldStackSize the old stack size
51+
* @param newStackSize the new stack size
52+
* @param source the source of the stack operation
53+
* @param entityType the spawned entity type of the spawner
54+
*/
55+
public SpawnerStackEvent(Player player, Location location, int oldStackSize, int newStackSize,
56+
StackSource source, @Nullable EntityType entityType) {
3957
this.player = player;
4058
this.location = location;
4159
this.oldStackSize = oldStackSize;
4260
this.newStackSize = newStackSize;
4361
this.source = source;
62+
this.entityType = entityType;
4463
}
4564

4665
/**
@@ -52,7 +71,7 @@ public SpawnerStackEvent(Player player, Location location, int oldStackSize, int
5271
* @param newStackSize the new stack size
5372
*/
5473
public SpawnerStackEvent(Player player, Location location, int oldStackSize, int newStackSize) {
55-
this(player, location, oldStackSize, newStackSize, StackSource.PLACE);
74+
this(player, location, oldStackSize, newStackSize, StackSource.PLACE, null);
5675
}
5776

5877
@Override

core/src/main/java/github/nighter/smartspawner/logging/SpawnerAuditListener.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void onSpawnerPlace(SpawnerPlaceEvent event) {
3131
public void onSpawnerBreak(SpawnerBreakEvent event) {
3232
logger.log(new SpawnerLogEntry.Builder(SpawnerEventType.SPAWNER_BREAK)
3333
.location(event.getLocation())
34+
.entityType(event.getEntityType())
3435
.metadata("quantity", event.getQuantity())
3536
.build());
3637
}
@@ -40,6 +41,7 @@ public void onSpawnerPlayerBreak(SpawnerPlayerBreakEvent event) {
4041
logger.log(new SpawnerLogEntry.Builder(SpawnerEventType.SPAWNER_BREAK)
4142
.player(event.getPlayer().getName(), event.getPlayer().getUniqueId())
4243
.location(event.getLocation())
44+
.entityType(event.getEntityType())
4345
.metadata("quantity", event.getQuantity())
4446
.build());
4547
}
@@ -48,6 +50,7 @@ public void onSpawnerPlayerBreak(SpawnerPlayerBreakEvent event) {
4850
public void onSpawnerExplode(SpawnerExplodeEvent event) {
4951
logger.log(new SpawnerLogEntry.Builder(SpawnerEventType.SPAWNER_EXPLODE)
5052
.location(event.getLocation())
53+
.entityType(event.getEntityType())
5154
.metadata("quantity", event.getQuantity())
5255
.metadata("cause", event.getClass().getSimpleName())
5356
.build());
@@ -64,6 +67,7 @@ public void onSpawnerStack(SpawnerStackEvent event) {
6467
logger.log(new SpawnerLogEntry.Builder(eventType)
6568
.player(event.getPlayer().getName(), event.getPlayer().getUniqueId())
6669
.location(event.getLocation())
70+
.entityType(event.getEntityType())
6771
.metadata("amount_added", amountAdded)
6872
.metadata("old_stack_size", event.getOldStackSize())
6973
.metadata("new_stack_size", event.getNewStackSize())
@@ -97,7 +101,8 @@ public void onSpawnerSell(SpawnerSellEvent event) {
97101
logger.log(new SpawnerLogEntry.Builder(SpawnerEventType.SPAWNER_SELL_ALL)
98102
.player(event.getPlayer().getName(), event.getPlayer().getUniqueId())
99103
.location(event.getLocation())
100-
.metadata("total_value", event.getMoneyAmount())
104+
.entityType(event.getEntityType())
105+
.metadata("total_price", event.getMoneyAmount())
101106
.metadata("items_sold", itemsSold)
102107
.build());
103108
}

core/src/main/java/github/nighter/smartspawner/logging/discord/DiscordEmbedBuilder.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,13 @@ private static Map<String, String> buildPlaceholders(SpawnerLogEntry entry,
153153
loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
154154
}
155155

156-
if (entry.getEntityType() != null) {
157-
p.put("entity", formatEntityName(entry.getEntityType().name()));
158-
}
159-
160156
for (Map.Entry<String, Object> meta : entry.getMetadata().entrySet()) {
161157
p.put(meta.getKey(), String.valueOf(meta.getValue()));
162158
}
163159

160+
// Keep a single placeholder for all spawner types: mob entity or item material fallback.
161+
p.put("entity", resolveEntityPlaceholder(entry, p));
162+
164163
return p;
165164
}
166165

@@ -211,6 +210,19 @@ private static String formatEntityName(String name) {
211210
return sb.toString().trim();
212211
}
213212

213+
private static String resolveEntityPlaceholder(SpawnerLogEntry entry, Map<String, String> placeholders) {
214+
String itemType = placeholders.get("item_type");
215+
if (itemType != null && !itemType.isBlank()) {
216+
return formatEntityName(itemType);
217+
}
218+
219+
if (entry.getEntityType() != null) {
220+
return formatEntityName(entry.getEntityType().name());
221+
}
222+
223+
return "N/A";
224+
}
225+
214226
private static String getFieldIcon(String key) {
215227
String l = key.toLowerCase();
216228
if (l.contains("command")) return "⚙️";

core/src/main/java/github/nighter/smartspawner/spawner/gui/stacker/SpawnerStackerHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ private void handleStackIncrease(Player player, SpawnerData spawner, int changeA
429429

430430
// Remove from inventory and update stack
431431
if(SpawnerStackEvent.getHandlerList().getRegisteredListeners().length != 0) {
432-
SpawnerStackEvent e = new SpawnerStackEvent(player, spawner.getSpawnerLocation(), spawner.getStackSize(), spawner.getStackSize() + actualChange, SpawnerStackEvent.StackSource.GUI);
432+
SpawnerStackEvent e = new SpawnerStackEvent(player, spawner.getSpawnerLocation(), spawner.getStackSize(),
433+
spawner.getStackSize() + actualChange, SpawnerStackEvent.StackSource.GUI, spawner.getEntityType());
433434
Bukkit.getPluginManager().callEvent(e);
434435
if (e.isCancelled()) return;
435436
}
@@ -484,7 +485,7 @@ private void handleAddAll(Player player, SpawnerData spawner) {
484485

485486
if (SpawnerStackEvent.getHandlerList().getRegisteredListeners().length != 0) {
486487
SpawnerStackEvent e = new SpawnerStackEvent(player, spawner.getSpawnerLocation(), spawner.getStackSize(),
487-
spawner.getStackSize() + actualChange, SpawnerStackEvent.StackSource.GUI);
488+
spawner.getStackSize() + actualChange, SpawnerStackEvent.StackSource.GUI, spawner.getEntityType());
488489
Bukkit.getPluginManager().callEvent(e);
489490
if (e.isCancelled())
490491
return;

core/src/main/java/github/nighter/smartspawner/spawner/interactions/destroy/SpawnerBreakListener.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void onSpawnerBreak(BlockBreakEvent event) {
8888
handleSmartSpawnerBreak(block, spawner, player);
8989
} else {
9090
CreatureSpawner creatureSpawner = (CreatureSpawner) block.getState(false);
91-
if(callAPIEvent(player, block.getLocation(), 1)) {
91+
if(callAPIEvent(player, block.getLocation(), 1, creatureSpawner.getSpawnedType())) {
9292
event.setCancelled(true);
9393
return;
9494
}
@@ -244,15 +244,15 @@ private SpawnerBreakResult processDrops(Player player, Location location, Spawne
244244
if (isCrouching) {
245245
if (currentStackSize <= MAX_STACK_SIZE) {
246246
dropAmount = currentStackSize;
247-
if(callAPIEvent(player, location, dropAmount)) return new SpawnerBreakResult(false, dropAmount, 0);
247+
if(callAPIEvent(player, location, dropAmount, spawner.getEntityType())) return new SpawnerBreakResult(false, dropAmount, 0);
248248
} else {
249249
dropAmount = MAX_STACK_SIZE;
250-
if(callAPIEvent(player, location, dropAmount)) return new SpawnerBreakResult(false, dropAmount, 0);
250+
if(callAPIEvent(player, location, dropAmount, spawner.getEntityType())) return new SpawnerBreakResult(false, dropAmount, 0);
251251
spawner.setStackSize(currentStackSize - MAX_STACK_SIZE);
252252
}
253253
} else {
254254
dropAmount = 1;
255-
if(callAPIEvent(player, location, dropAmount)) return new SpawnerBreakResult(false, dropAmount, 0);
255+
if(callAPIEvent(player, location, dropAmount, spawner.getEntityType())) return new SpawnerBreakResult(false, dropAmount, 0);
256256
if (currentStackSize <= 1) {
257257
shouldDeleteSpawner = true;
258258
} else {
@@ -279,9 +279,9 @@ private SpawnerBreakResult processDrops(Player player, Location location, Spawne
279279
return new SpawnerBreakResult(true, dropAmount, durabilityLoss);
280280
}
281281

282-
private boolean callAPIEvent(Player player, Location location, int dropAmount) {
282+
private boolean callAPIEvent(Player player, Location location, int dropAmount, EntityType entityType) {
283283
if(SpawnerPlayerBreakEvent.getHandlerList().getRegisteredListeners().length != 0) {
284-
SpawnerPlayerBreakEvent e = new SpawnerPlayerBreakEvent(player, location, dropAmount);
284+
SpawnerPlayerBreakEvent e = new SpawnerPlayerBreakEvent(player, location, dropAmount, entityType);
285285
Bukkit.getPluginManager().callEvent(e);
286286
return e.isCancelled();
287287
}

core/src/main/java/github/nighter/smartspawner/spawner/interactions/destroy/SpawnerExplosionListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ private void handleExplosion(List<Block> blockList, ExplosionResult explosionRes
7676
plugin.getSpawnerGuiViewManager().closeAllViewersInventory(spawnerData);
7777
cleanupAssociatedHopper(block);
7878
if (hasApiListeners) {
79-
Bukkit.getPluginManager().callEvent(new SpawnerExplodeEvent(null, spawnerData.getSpawnerLocation(), 1, false));
79+
Bukkit.getPluginManager().callEvent(new SpawnerExplodeEvent(null, spawnerData.getSpawnerLocation(), 1, false, spawnerData.getEntityType()));
8080
}
8181
} else {
8282
spawnerData.getSpawnerStop().set(true);
8383
String spawnerId = spawnerData.getSpawnerId();
8484
cleanupAssociatedHopper(block);
8585
if (hasApiListeners) {
86-
Bukkit.getPluginManager().callEvent(new SpawnerExplodeEvent(null, spawnerData.getSpawnerLocation(), 1, true));
86+
Bukkit.getPluginManager().callEvent(new SpawnerExplodeEvent(null, spawnerData.getSpawnerLocation(), 1, true, spawnerData.getEntityType()));
8787
}
8888
spawnerManager.removeSpawner(spawnerId);
8989
spawnerManager.markSpawnerDeleted(spawnerId);

0 commit comments

Comments
 (0)