Fixed fishing tugs

This commit is contained in:
Indyuce 2022-03-05 12:17:17 +01:00
parent c3f51a3520
commit 848b402393
2 changed files with 68 additions and 54 deletions

View File

@ -36,7 +36,7 @@ import java.util.UUID;
public class FishingListener implements Listener { public class FishingListener implements Listener {
private final Set<UUID> fishing = new HashSet<>(); private final Set<UUID> fishing = new HashSet<>();
private static final Random random = new Random(); private static final Random RANDOM = new Random();
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void a(PlayerFishEvent event) { public void a(PlayerFishEvent event) {
@ -65,9 +65,9 @@ public class FishingListener implements Listener {
private final Player player; private final Player player;
private final FishHook hook; private final FishHook hook;
private final int total, exp; private final int fishStrength, experienceDropped;
private int pulls; private int currentPulls;
/** /**
* Used to track the last time the player swung the fishing rod. * Used to track the last time the player swung the fishing rod.
@ -84,16 +84,20 @@ public class FishingListener implements Listener {
this.playerData = PlayerData.get(this.player = player); this.playerData = PlayerData.get(this.player = player);
this.hook = hook; this.hook = hook;
this.total = (int) (caught.rollTugs() * (1 - PlayerData.get(player).getStats().getStat(StatType.FISHING_STRENGTH) / 100)); this.fishStrength = (int) Math.floor(caught.rollTugs() * (1 - PlayerData.get(player).getStats().getStat(StatType.FISHING_STRENGTH) / 100));
this.exp = caught.rollExperience(); this.experienceDropped = caught.rollExperience();
fishing.add(player.getUniqueId()); fishing.add(player.getUniqueId());
runTaskTimer(MMOCore.plugin, 0, 2); runTaskTimer(MMOCore.plugin, 0, 2);
Bukkit.getPluginManager().registerEvents(this, MMOCore.plugin); Bukkit.getPluginManager().registerEvents(this, MMOCore.plugin);
// Check for instant loot
if (fishStrength == 0)
lootFish();
} }
public void criticalFish() { public void criticalFish() {
pulls = total + 2; currentPulls = fishStrength + 2;
} }
public boolean isTimedOut() { public boolean isTimedOut() {
@ -105,14 +109,15 @@ public class FishingListener implements Listener {
*/ */
public boolean pull() { public boolean pull() {
last = System.currentTimeMillis(); last = System.currentTimeMillis();
return pulls++ > total; currentPulls++;
return currentPulls >= fishStrength;
} }
/** /**
* Critical fish's means you catch the fish on the very first try * Critical fish's means you catch the fish on the very first try
*/ */
public boolean isCrit() { public boolean isCrit() {
return pulls > total + 1; return currentPulls > fishStrength + 1;
} }
private void close() { private void close() {
@ -128,7 +133,7 @@ public class FishingListener implements Listener {
if (isTimedOut()) if (isTimedOut())
close(); close();
location.getWorld().spawnParticle(Particle.CRIT, location, 0, 2 * (random.nextDouble() - .5), 3, 2 * (random.nextDouble() - .5), .6); location.getWorld().spawnParticle(Particle.CRIT, location, 0, 2 * (RANDOM.nextDouble() - .5), 3, 2 * (RANDOM.nextDouble() - .5), .6);
} }
@EventHandler @EventHandler
@ -143,32 +148,39 @@ public class FishingListener implements Listener {
return; return;
} }
if (pulls == 0 && random.nextDouble() < PlayerData.get(player).getStats().getStat(StatType.CRITICAL_FISHING_CHANCE) / 100) System.out.println("Pulls: " + currentPulls + " / " + fishStrength);
if (currentPulls == 0 && RANDOM.nextDouble() < PlayerData.get(player).getStats().getStat(StatType.CRITICAL_FISHING_CHANCE) / 100)
criticalFish(); criticalFish();
// Check if enough pulls; if not, wait till the next fish event // Check if enough pulls; if not, wait till the next fish event
if (!pull()) if (pull())
return; lootFish();
}
}
// The fish is successfully looted from here public void lootFish() {
close(); close();
ItemStack mainhand = player.getInventory().getItem(EquipmentSlot.HAND); ItemStack mainhand = player.getInventory().getItem(EquipmentSlot.HAND);
MMOCoreUtils.decreaseDurability(player, MMOCoreUtils.decreaseDurability(player,
(mainhand != null && mainhand.getType() == Material.FISHING_ROD) ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND, 1); (mainhand != null && mainhand.getType() == Material.FISHING_ROD) ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND, 1);
if (!isCrit() && random.nextDouble() < PlayerData.get(player).getStats().getStat(StatType.CRITICAL_FISHING_FAILURE_CHANCE) / 100) { // Critical fishing failure
if (!isCrit() && RANDOM.nextDouble() < PlayerData.get(player).getStats().getStat(StatType.CRITICAL_FISHING_FAILURE_CHANCE) / 100) {
player.setVelocity(hook.getLocation().subtract(player.getLocation()).toVector().setY(0).multiply(3).setY(.5)); player.setVelocity(hook.getLocation().subtract(player.getLocation()).toVector().setY(0).multiply(3).setY(.5));
hook.getWorld().spawnParticle(Particle.SMOKE_NORMAL, location, 24, 0, 0, 0, .08); hook.getWorld().spawnParticle(Particle.SMOKE_NORMAL, location, 24, 0, 0, 0, .08);
return; return;
} }
// Find looted item
ItemStack collect = caught.collect(new LootBuilder(playerData, 0)); ItemStack collect = caught.collect(new LootBuilder(playerData, 0));
if (collect == null) { if (collect == null) {
hook.getWorld().spawnParticle(Particle.SMOKE_NORMAL, location, 24, 0, 0, 0, .08); hook.getWorld().spawnParticle(Particle.SMOKE_NORMAL, location, 24, 0, 0, 0, .08);
return; return;
} }
// Call Bukkit event
CustomPlayerFishEvent called = new CustomPlayerFishEvent(playerData, collect); CustomPlayerFishEvent called = new CustomPlayerFishEvent(playerData, collect);
Bukkit.getPluginManager().callEvent(called); Bukkit.getPluginManager().callEvent(called);
if (called.isCancelled()) if (called.isCancelled())
@ -184,13 +196,11 @@ public class FishingListener implements Listener {
vec.setZ(vec.getZ() * .08); vec.setZ(vec.getZ() * .08);
item.setVelocity(vec); item.setVelocity(vec);
player.getWorld().playSound(player.getLocation(), VersionSound.BLOCK_NOTE_BLOCK_HAT.toSound(), 1, 0); player.getWorld().playSound(player.getLocation(), VersionSound.BLOCK_NOTE_BLOCK_HAT.toSound(), 1, 0);
for (int j = 0; j < 16; j++) for (int j = 0; j < 8; j++)
location.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, location, 0, 4 * (random.nextDouble() - .5), 2, location.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, location, 0, 4 * (RANDOM.nextDouble() - .5), RANDOM.nextDouble() + 1, 4 * (RANDOM.nextDouble() - .5), .08);
4 * (random.nextDouble() - .5), .05);
if (MMOCore.plugin.fishingManager.hasLinkedProfession()) if (MMOCore.plugin.fishingManager.hasLinkedProfession())
playerData.getCollectionSkills().giveExperience(MMOCore.plugin.fishingManager.getLinkedProfession(), exp, EXPSource.FISHING, location); playerData.getCollectionSkills().giveExperience(MMOCore.plugin.fishingManager.getLinkedProfession(), experienceDropped, EXPSource.FISHING, location);
}
} }
} }
} }

View File

@ -3,6 +3,9 @@ package net.Indyuce.mmocore.manager.profession;
import net.Indyuce.mmocore.experience.Profession; import net.Indyuce.mmocore.experience.Profession;
import net.Indyuce.mmocore.manager.MMOCoreManager; import net.Indyuce.mmocore.manager.MMOCoreManager;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public abstract class SpecificProfessionManager implements MMOCoreManager { public abstract class SpecificProfessionManager implements MMOCoreManager {
@ -25,8 +28,9 @@ public abstract class SpecificProfessionManager implements MMOCoreManager {
this.linkedProfession = linkedProfession; this.linkedProfession = linkedProfession;
} }
@NotNull
public Profession getLinkedProfession() { public Profession getLinkedProfession() {
return linkedProfession; return Objects.requireNonNull(linkedProfession, "No linked profession found");
} }
public boolean hasLinkedProfession() { public boolean hasLinkedProfession() {