Skip to content

Commit 27427c9

Browse files
committed
RS multikill support
still experimental
1 parent 7cd5e8f commit 27427c9

File tree

4 files changed

+104
-34
lines changed

4 files changed

+104
-34
lines changed

src/main/java/lol/hyper/toolstats/ToolStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void onEnable() {
132132
playerDrop = new PlayerDrop(this);
133133
if (Bukkit.getPluginManager().isPluginEnabled("RoseStacker")) {
134134
logger.info("RoseStacker has been detected, adding support!");
135-
roseStacker = new RoseStacker();
135+
roseStacker = new RoseStacker(this);
136136
}
137137

138138
Bukkit.getServer().getPluginManager().registerEvents(blockBreak, this);

src/main/java/lol/hyper/toolstats/events/ChunkPopulate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void onPopulate(ChunkPopulateEvent event) {
5656
// this is delayed because entities are not loaded instantly
5757
// we just check 1 second later
5858
Chunk chunk = event.getChunk();
59-
Bukkit.getRegionScheduler().runDelayed(toolStats, world, chunk.getX(), chunk.getZ(), scheduledTask -> {
59+
Bukkit.getRegionScheduler().runDelayed(toolStats, world, chunk.getX(), chunk.getZ(), _ -> {
6060
for (Entity entity : chunk.getEntities()) {
6161
// if there is a new item frame
6262
if (!(entity instanceof ItemFrame itemFrame)) {

src/main/java/lol/hyper/toolstats/events/EntityDamage.java

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,22 @@ private void updateBowKills(PlayerInventory playerInventory, String type, Living
259259

260260
if (type.equalsIgnoreCase("mob")) {
261261
// player is shooting a mob
262-
ItemMeta newBow;
263-
int count = 1;
264262
if (toolStats.roseStacker != null) {
265-
count = toolStats.roseStacker.countMobs(entity);
263+
toolStats.roseStacker.countMobs(entity, count -> {
264+
ItemMeta newBow = toolStats.itemLore.updateMobKills(heldBow, count);
265+
if (newBow != null) {
266+
if (isMain && isOffHand) {
267+
playerInventory.getItemInMainHand().setItemMeta(newBow);
268+
} else if (isMain) {
269+
playerInventory.getItemInMainHand().setItemMeta(newBow);
270+
} else if (isOffHand) {
271+
playerInventory.getItemInOffHand().setItemMeta(newBow);
272+
}
273+
}
274+
});
275+
return;
266276
}
267-
newBow = toolStats.itemLore.updateMobKills(heldBow, count);
277+
ItemMeta newBow = toolStats.itemLore.updateMobKills(heldBow, 1);
268278
if (newBow != null) {
269279
if (isMain && isOffHand) {
270280
playerInventory.getItemInMainHand().setItemMeta(newBow);
@@ -292,19 +302,30 @@ private void updateBowKills(PlayerInventory playerInventory, String type, Living
292302

293303
private void updateTridentKills(Trident trident, String type, LivingEntity entity) {
294304
ItemStack newTrident = trident.getItemStack();
295-
ItemMeta newTridentMeta;
296305
if (type.equalsIgnoreCase("player")) {
297-
newTridentMeta = toolStats.itemLore.updatePlayerKills(trident.getItemStack(), 1);
298-
} else {
299-
int count = 1;
300-
if (toolStats.roseStacker != null) {
301-
count = toolStats.roseStacker.countMobs(entity);
306+
ItemMeta newTridentMeta = toolStats.itemLore.updatePlayerKills(trident.getItemStack(), 1);
307+
if (newTridentMeta != null) {
308+
newTrident.setItemMeta(newTridentMeta);
309+
trident.setItemStack(newTrident);
302310
}
303-
newTridentMeta = toolStats.itemLore.updateMobKills(newTrident, count);
311+
return;
304312
}
305-
if (newTridentMeta != null) {
306-
newTrident.setItemMeta(newTridentMeta);
307-
trident.setItemStack(newTrident);
313+
if (type.equalsIgnoreCase("mob")) {
314+
if (toolStats.roseStacker != null) {
315+
toolStats.roseStacker.countMobs(entity, count -> {
316+
ItemMeta newTridentMeta = toolStats.itemLore.updateMobKills(newTrident, count);
317+
if (newTridentMeta != null) {
318+
newTrident.setItemMeta(newTridentMeta);
319+
trident.setItemStack(newTrident);
320+
}
321+
});
322+
return;
323+
}
324+
ItemMeta newTridentMeta = toolStats.itemLore.updateMobKills(trident.getItemStack(), 1);
325+
if (newTridentMeta != null) {
326+
newTrident.setItemMeta(newTridentMeta);
327+
trident.setItemStack(newTrident);
328+
}
308329
}
309330
}
310331

@@ -327,27 +348,36 @@ private void updateWeaponDamage(PlayerInventory playerInventory, double damage)
327348

328349
private void updateWeaponKills(PlayerInventory playerInventory, String type, LivingEntity entity) {
329350
ItemStack heldWeapon = playerInventory.getItemInMainHand();
330-
ItemMeta newHeldWeaponMeta = null;
331351
if (type.equalsIgnoreCase("player")) {
332-
newHeldWeaponMeta = toolStats.itemLore.updatePlayerKills(heldWeapon, 1);
352+
ItemMeta newHeldWeaponMeta = toolStats.itemLore.updatePlayerKills(heldWeapon, 1);
353+
if (newHeldWeaponMeta != null) {
354+
playerInventory.getItemInMainHand().setItemMeta(newHeldWeaponMeta);
355+
}
356+
return;
333357
}
334358
if (type.equalsIgnoreCase("mob")) {
335-
int count = 1;
336359
if (toolStats.roseStacker != null) {
337-
count = toolStats.roseStacker.countMobs(entity);
360+
toolStats.roseStacker.countMobs(entity, count -> {
361+
ItemStack currentHeldWeapon = playerInventory.getItemInMainHand();
362+
ItemMeta newHeldWeaponMeta = toolStats.itemLore.updateMobKills(currentHeldWeapon, count);
363+
if (newHeldWeaponMeta != null) {
364+
currentHeldWeapon.setItemMeta(newHeldWeaponMeta);
365+
}
366+
});
367+
} else {
368+
ItemMeta newHeldWeaponMeta = toolStats.itemLore.updateMobKills(heldWeapon, 1);
369+
if (newHeldWeaponMeta != null) {
370+
playerInventory.getItemInMainHand().setItemMeta(newHeldWeaponMeta);
371+
}
338372
}
339-
newHeldWeaponMeta = toolStats.itemLore.updateMobKills(heldWeapon, count);
340-
}
341-
if (newHeldWeaponMeta != null) {
342-
playerInventory.getItemInMainHand().setItemMeta(newHeldWeaponMeta);
343373
}
344374
}
345375

346376
private void updateBossesKilled(PlayerInventory playerInventory, String boss, LivingEntity entity) {
347377
ItemStack heldWeapon = playerInventory.getItemInMainHand();
348378
int count = 1;
349379
if (toolStats.roseStacker != null) {
350-
count = toolStats.roseStacker.countMobs(entity);
380+
//count = toolStats.roseStacker.countMobs(entity);
351381
}
352382
ItemMeta newHeldWeaponMeta = toolStats.itemLore.updateBossesKilled(heldWeapon, count, boss);
353383
if (newHeldWeaponMeta != null) {
@@ -364,12 +394,23 @@ private void updateBossesKilledByBow(PlayerInventory playerInventory, String bos
364394
boolean isMain = playerInventory.getItemInMainHand().getType() == Material.BOW || playerInventory.getItemInMainHand().getType() == Material.CROSSBOW;
365395
boolean isOffHand = playerInventory.getItemInOffHand().getType() == Material.BOW || playerInventory.getItemInOffHand().getType() == Material.CROSSBOW;
366396

367-
int count = 1;
368397
if (toolStats.roseStacker != null) {
369-
count = toolStats.roseStacker.countMobs(entity);
398+
toolStats.roseStacker.countMobs(entity, count -> {
399+
ItemMeta newHeldWeaponMeta = toolStats.itemLore.updateBossesKilled(heldBow, count, boss);
400+
if (newHeldWeaponMeta != null) {
401+
if (isMain && isOffHand) {
402+
playerInventory.getItemInMainHand().setItemMeta(newHeldWeaponMeta);
403+
} else if (isMain) {
404+
playerInventory.getItemInMainHand().setItemMeta(newHeldWeaponMeta);
405+
} else if (isOffHand) {
406+
playerInventory.getItemInOffHand().setItemMeta(newHeldWeaponMeta);
407+
}
408+
}
409+
});
410+
return;
370411
}
371412

372-
ItemMeta newHeldWeaponMeta = toolStats.itemLore.updateBossesKilled(heldBow, count, boss);
413+
ItemMeta newHeldWeaponMeta = toolStats.itemLore.updateBossesKilled(heldBow, 1, boss);
373414
if (newHeldWeaponMeta != null) {
374415
if (isMain && isOffHand) {
375416
playerInventory.getItemInMainHand().setItemMeta(newHeldWeaponMeta);

src/main/java/lol/hyper/toolstats/support/rosestacker/RoseStacker.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,61 @@
3636

3737
import dev.rosewood.rosestacker.api.RoseStackerAPI;
3838
import dev.rosewood.rosestacker.stack.StackedEntity;
39+
import lol.hyper.toolstats.ToolStats;
40+
import org.bukkit.Bukkit;
41+
import org.bukkit.Chunk;
42+
import org.bukkit.Location;
3943
import org.bukkit.entity.LivingEntity;
4044

45+
import java.util.function.Consumer;
46+
4147
public class RoseStacker {
4248

49+
private final ToolStats toolStats;
4350
private final RoseStackerAPI rsAPI;
4451

45-
public RoseStacker() {
52+
public RoseStacker(ToolStats toolStats) {
53+
this.toolStats = toolStats;
4654
this.rsAPI = RoseStackerAPI.getInstance();
4755
}
4856

49-
public int countMobs(LivingEntity entity) {
57+
public void countMobs(LivingEntity entity, Consumer<Integer> callback) {
5058
if (!rsAPI.isEntityStacked(entity)) {
5159
// if the entity is not stacked, ignore
52-
return 1;
60+
callback.accept(1);
61+
return;
5362
}
5463
StackedEntity stackedEntity = rsAPI.getStackedEntity(entity);
5564
if (stackedEntity == null) {
56-
return 1;
65+
callback.accept(1);
66+
return;
5767
}
5868

69+
int before = stackedEntity.getStackSize();
5970
boolean killAll = stackedEntity.getStackSettings().shouldKillEntireStackOnDeath();
6071
// if we kill the entire stack, add the entire stack to the count
6172
if (killAll) {
62-
return stackedEntity.getStackSize();
73+
callback.accept(before);
74+
return;
6375
}
6476

65-
return 1;
77+
Location stackedLocation = stackedEntity.getLocation();
78+
Chunk stackedChunk = stackedEntity.getLocation().getChunk();
79+
// check the stack size after a tick to see the difference
80+
Bukkit.getRegionScheduler().runDelayed(toolStats, stackedLocation.getWorld(), stackedChunk.getX(), stackedChunk.getZ(), _ -> {
81+
int after = stackedEntity.getStackSize();
82+
int difference = before - after;
83+
// if the diff goes negative, we killed more than the stack
84+
// we killed the entire stack, so return the size
85+
if (difference <= 0) {
86+
difference = before;
87+
}
88+
89+
toolStats.logger.info("before: {}", before);
90+
toolStats.logger.info("after: {}", after);
91+
toolStats.logger.info("difference: {}", difference);
92+
93+
callback.accept(difference);
94+
}, 1);
6695
}
6796
}

0 commit comments

Comments
 (0)