|
27 | 27 | import org.bstats.bukkit.Metrics;
|
28 | 28 | import org.bukkit.NamespacedKey;
|
29 | 29 | import org.bukkit.OfflinePlayer;
|
| 30 | +import org.bukkit.attribute.Attribute; |
30 | 31 | import org.bukkit.configuration.file.YamlConfiguration;
|
| 32 | +import org.bukkit.enchantments.Enchantment; |
| 33 | +import org.bukkit.entity.FishHook; |
| 34 | +import org.bukkit.entity.Item; |
| 35 | +import org.bukkit.entity.Player; |
31 | 36 | import org.bukkit.event.EventHandler;
|
32 | 37 | import org.bukkit.event.EventPriority;
|
33 | 38 | import org.bukkit.event.HandlerList;
|
34 | 39 | import org.bukkit.event.Listener;
|
| 40 | +import org.bukkit.event.player.PlayerFishEvent; |
35 | 41 | import org.bukkit.event.world.LootGenerateEvent;
|
| 42 | +import org.bukkit.inventory.ItemStack; |
36 | 43 | import org.bukkit.loot.LootTables;
|
37 | 44 | import org.bukkit.permissions.Permission;
|
38 | 45 | import org.bukkit.permissions.PermissionAttachment;
|
@@ -515,4 +522,45 @@ public void on_module_loot_generate(final LootGenerateEvent event) {
|
515 | 522 | (loc.getBlockZ() & (0xffff << 48)));
|
516 | 523 | additional_loot_table.generate_loot(event.getLoot(), local_random);
|
517 | 524 | }
|
| 525 | + |
| 526 | + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) |
| 527 | + public void on_module_player_caught_fish(final PlayerFishEvent event) { |
| 528 | + // This is a dirty non-commutative way to apply fishing loot tables |
| 529 | + // that skews subtable probabilities, |
| 530 | + // consider somehow programmatically generating datapacks or |
| 531 | + // modifying loot tables directly instead. |
| 532 | + if (event.getState() != PlayerFishEvent.State.CAUGHT_FISH) { |
| 533 | + return; |
| 534 | + } |
| 535 | + if (event.getCaught() instanceof Item item_entity) { |
| 536 | + final Player player = event.getPlayer(); |
| 537 | + final FishHook hook_entity = event.getHook(); |
| 538 | + final double player_luck = player.getAttribute(Attribute.LUCK).getValue(); |
| 539 | + final ItemStack rod_stack = player.getInventory().getItem(event.getHand()); |
| 540 | + final double rod_luck = rod_stack.getEnchantmentLevel(Enchantment.LUCK_OF_THE_SEA); // Can bukkit provide access to fishing_luck_bonus of 1.24 item component system? |
| 541 | + final double total_luck = player_luck + rod_luck; |
| 542 | + final double weight_fish = Math.max(0, 85 + total_luck * -1); |
| 543 | + final double weight_junk = Math.max(0, 10 + total_luck * -2); |
| 544 | + final double weight_treasure = hook_entity.isInOpenWater() ? Math.max(0, 5 + total_luck * 2) : 0; |
| 545 | + final double roll = random.nextDouble() * (weight_fish + weight_junk + weight_treasure); |
| 546 | + NamespacedKey key; |
| 547 | + if (roll < weight_fish) { |
| 548 | + key = LootTables.FISHING_FISH.getKey(); |
| 549 | + } else if (roll < weight_fish + weight_junk) { |
| 550 | + key = LootTables.FISHING_JUNK.getKey(); |
| 551 | + } else { |
| 552 | + key = LootTables.FISHING_TREASURE.getKey(); |
| 553 | + } |
| 554 | + final var additional_loot_table = additional_loot_tables.get(key); |
| 555 | + if (additional_loot_table == null) { |
| 556 | + // Do not modify the caught item |
| 557 | + return; |
| 558 | + } |
| 559 | + final var new_item = additional_loot_table.generate_override(new Random(random.nextInt())); |
| 560 | + if (new_item == null) { |
| 561 | + return; |
| 562 | + } |
| 563 | + item_entity.setItemStack(new_item); |
| 564 | + } |
| 565 | + } |
518 | 566 | }
|
0 commit comments