Prevent lava fishing and the start of lava fishing from having multiple active instances per player
This commit is contained in:
parent
dc6a233c1b
commit
173324c68f
|
|
@ -13,9 +13,7 @@ import com.alttd.fishingevent.util.Logger;
|
|||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Item;
|
||||
|
|
@ -23,6 +21,7 @@ import org.bukkit.entity.Player;
|
|||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerFishEvent;
|
||||
import org.bukkit.event.player.PlayerItemHeldEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.Damageable;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
|
@ -37,6 +36,7 @@ public class CatchFish implements Listener {
|
|||
private final EventManager eventManager = EventManager.getInstance();
|
||||
private final FishingEvent fishingEvent;
|
||||
private final HashMap<UUID, LavaFishing> activeLavaFishers = new HashMap<>();
|
||||
private final HashMap<UUID, BukkitRunnable> activeFishers = new HashMap<>();
|
||||
|
||||
public CatchFish(FishingEvent fishingEvent, Logger logger, FishGenerator waterFishGenerator, PointsManagement pointsManagement) {
|
||||
this.fishingEvent = fishingEvent;
|
||||
|
|
@ -46,6 +46,21 @@ public class CatchFish implements Listener {
|
|||
logger.debug("Registered PlayerFishEvent listener");
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onItemSwitch(PlayerItemHeldEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (player.getInventory().getItem(event.getNewSlot()) != null) {
|
||||
return;
|
||||
}
|
||||
UUID uuid = player.getUniqueId();
|
||||
stopLavaFishStart(uuid);
|
||||
LavaFishing lavaFishing = activeLavaFishers.get(uuid);
|
||||
if (lavaFishing == null)
|
||||
return;
|
||||
lavaFishing.cancel();
|
||||
activeLavaFishers.remove(uuid);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerFish(PlayerFishEvent event) {
|
||||
if (!eventManager.isRunning()) {
|
||||
|
|
@ -58,11 +73,14 @@ public class CatchFish implements Listener {
|
|||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
UUID uuid = player.getUniqueId();
|
||||
if (event.getState().equals(PlayerFishEvent.State.FISHING)) {
|
||||
//if fishing rod in lava continue else return
|
||||
new BukkitRunnable() {
|
||||
BukkitRunnable bukkitRunnable = new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (activeFishers.containsKey(uuid)) {
|
||||
activeFishers.get(uuid).cancel();
|
||||
}
|
||||
if (event.getHook().getLocation().getBlock().getType().equals(Material.LAVA)) {
|
||||
player.sendActionBar(MiniMessage.miniMessage().deserialize("<gold>You are now fishing in lava...</gold>")); //TODO move to config
|
||||
Optional<ItemStack> optionalFishingRod = getFishingRod(player);
|
||||
|
|
@ -74,17 +92,18 @@ public class CatchFish implements Listener {
|
|||
int lure = itemStack.getEnchantmentLevel(Enchantment.LURE);
|
||||
int fireProtection = itemStack.getEnchantmentLevel(Enchantment.PROTECTION_FIRE);
|
||||
LavaFishing lavaFishing = new LavaFishing(lure, fireProtection, player, logger, event.getHook().getLocation());
|
||||
activeLavaFishers.put(player.getUniqueId(), lavaFishing);
|
||||
activeLavaFishers.put(uuid, lavaFishing);
|
||||
lavaFishing.runTaskTimerAsynchronously(fishingEvent, 1, 1);
|
||||
logger.debug("in lava above");
|
||||
} else
|
||||
logger.debug("Not in lava");
|
||||
// logger.debug("% might have caught a lava fish", event.getPlayer().getName());
|
||||
// handleLavaFishCaught(event);
|
||||
}
|
||||
}.runTaskLater(fishingEvent, 50);
|
||||
};
|
||||
activeFishers.put(uuid, bukkitRunnable);
|
||||
bukkitRunnable.runTaskLater(fishingEvent, 50);
|
||||
|
||||
} else if (event.getState().equals(PlayerFishEvent.State.CAUGHT_FISH)) {
|
||||
stopLavaFishStart(uuid);
|
||||
if (event.getCaught() == null) {
|
||||
logger.debug("% caught a water fish but it was null", player.getName());
|
||||
return;
|
||||
|
|
@ -92,14 +111,24 @@ public class CatchFish implements Listener {
|
|||
logger.debug("% caught a water fish", player.getName());
|
||||
handleFishCaught(event);
|
||||
} else if (event.getState().equals(PlayerFishEvent.State.IN_GROUND)) {
|
||||
stopLavaFishStart(uuid);
|
||||
logger.debug("% reeled in fishing rod that was in the ground", player.getName());
|
||||
|
||||
handleLavaFishCaught(event);
|
||||
} else {
|
||||
stopLavaFishStart(uuid);
|
||||
logger.debug("Registered different fish state [%]", event.getState().toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void stopLavaFishStart(UUID uuid) {
|
||||
if (!activeFishers.containsKey(uuid)) {
|
||||
activeFishers.remove(uuid);
|
||||
return;
|
||||
}
|
||||
activeFishers.get(uuid).cancel();
|
||||
}
|
||||
|
||||
private boolean fishingRodBroken(Player player) {
|
||||
Optional<ItemStack> optionalFishingROd = getFishingRod(player);
|
||||
if (optionalFishingROd.isEmpty()) {
|
||||
|
|
@ -121,14 +150,6 @@ public class CatchFish implements Listener {
|
|||
.findFirst();
|
||||
}
|
||||
|
||||
private void handleLavaFish(PlayerFishEvent event) {
|
||||
Location location = event.getHook().getLocation();
|
||||
Block block = location.getWorld().getBlockAt(location.getBlockX(), location.getBlockY() + 1, location.getBlockZ());
|
||||
if (!block.isLiquid())
|
||||
return;
|
||||
//TODO get fish and start timer for it
|
||||
}
|
||||
|
||||
private void handleLavaFishCaught(PlayerFishEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
LavaFishing lavaFishing = activeLavaFishers.get(player.getUniqueId());
|
||||
|
|
|
|||
|
|
@ -11,16 +11,19 @@ import org.bukkit.inventory.PlayerInventory;
|
|||
import org.bukkit.inventory.meta.Damageable;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
public class LavaFishing extends BukkitRunnable {
|
||||
private static HashMap<UUID, LavaFishing> activeFishers = new HashMap<>();
|
||||
|
||||
private int timeUntilHooked;
|
||||
private int timeUntilLured;
|
||||
private final int lure;
|
||||
private final int fireProtection;
|
||||
private final int minWaitTime = 100;
|
||||
private final int maxWaitTime = 600;
|
||||
private final int minWaitTime = 60;
|
||||
private final int maxWaitTime = 500;
|
||||
private boolean isBiting;
|
||||
private int timeUntilHookDamage;
|
||||
private boolean shouldDamageHook;
|
||||
|
|
@ -30,12 +33,17 @@ public class LavaFishing extends BukkitRunnable {
|
|||
private final Location hookLocation;
|
||||
|
||||
public LavaFishing(int lure, int fireProtection, Player player, Logger logger, Location hookLocation) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
if (activeFishers.containsKey(uuid)) {
|
||||
activeFishers.get(uuid).cancel();
|
||||
}
|
||||
activeFishers.put(uuid, this);
|
||||
this.lure = lure;
|
||||
this.fireProtection = Math.min(5, fireProtection);
|
||||
this.player = player;
|
||||
this.logger = logger;
|
||||
this.hookLocation = hookLocation;
|
||||
this.shouldDamageHook = fireProtection >= 5;
|
||||
this.shouldDamageHook = fireProtection < 5;
|
||||
resetTimeUntilHookDamage();
|
||||
resetTimeUntilLured();
|
||||
}
|
||||
|
|
@ -43,16 +51,19 @@ public class LavaFishing extends BukkitRunnable {
|
|||
private void resetTimeUntilLured() {
|
||||
isBiting = false;
|
||||
this.timeUntilLured = random.nextInt(minWaitTime, maxWaitTime);
|
||||
this.timeUntilLured = Math.max(timeUntilLured - this.lure * 100, minWaitTime);
|
||||
this.timeUntilLured = Math.max(timeUntilLured - this.lure * 50, minWaitTime);
|
||||
}
|
||||
|
||||
private void damageHook() {
|
||||
if (!shouldDamageHook)
|
||||
if (!shouldDamageHook) {
|
||||
logger.debug("Rod should not be damaged for this player");
|
||||
return;
|
||||
}
|
||||
if (timeUntilHookDamage > 0) {
|
||||
timeUntilHookDamage--;
|
||||
return;
|
||||
}
|
||||
logger.debug("Damaging hook...");
|
||||
resetTimeUntilHookDamage();
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
int fishingRodSlot = findFishingRodSlot(inventory);
|
||||
|
|
@ -67,6 +78,7 @@ public class LavaFishing extends BukkitRunnable {
|
|||
}
|
||||
if (item.getItemMeta() instanceof Damageable damageable) {
|
||||
int damage = damageable.getDamage();
|
||||
logger.debug("Setting rod damage to %", String.valueOf(damage));
|
||||
damageable.setDamage(Math.min(++damage, 63));
|
||||
item.setItemMeta(damageable);
|
||||
} else {
|
||||
|
|
@ -88,7 +100,7 @@ public class LavaFishing extends BukkitRunnable {
|
|||
}
|
||||
|
||||
private void resetTimeUntilHookDamage() {
|
||||
timeUntilHookDamage = random.nextInt(fireProtection, 5 + fireProtection);
|
||||
timeUntilHookDamage = random.nextInt(fireProtection * 5, 20 + fireProtection * 5);
|
||||
}
|
||||
|
||||
private void spawnLavaParticles() {
|
||||
|
|
@ -123,7 +135,7 @@ public class LavaFishing extends BukkitRunnable {
|
|||
if (this.timeUntilHooked > 0) {
|
||||
this.timeUntilHooked--;
|
||||
if (timeUntilHooked % 5 == 0)
|
||||
logger.debug("Time until hooked: %", String.valueOf(timeUntilLured));
|
||||
logger.debug("Time until hooked: %", String.valueOf(timeUntilHooked));
|
||||
if (this.timeUntilHooked % 5 == 0)
|
||||
spawnLavaParticles();
|
||||
return;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user