9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-29 03:49:07 +00:00

岩浆钓鱼

This commit is contained in:
Xiao-MoMi
2022-10-19 14:07:10 +08:00
parent bf265617d6
commit 976bce4dbf
13 changed files with 316 additions and 82 deletions

View File

@@ -84,7 +84,7 @@ public final class CustomFishing extends JavaPlugin {
this.lootManager = new LootManager();
this.layoutManager = new LayoutManager();
this.dataManager = new DataManager();
this.totemManager = new TotemManager(integrationManager.getBlockInterface());
this.totemManager = new TotemManager();
ConfigUtil.reload();
PluginCommand pluginCommand = new PluginCommand();

View File

@@ -153,6 +153,7 @@ public class BonusManager extends Function {
case "difficulty" -> bonus.setDifficulty(config.getInt(key + ".modifier.difficulty"));
case "double-loot" -> bonus.setDoubleLoot(config.getDouble(key + ".modifier.double-loot"));
case "score" -> bonus.setScore(config.getDouble(key + ".modifier.score"));
case "lava-fishing" -> bonus.setCanLavaFishing(config.getBoolean(key + ".modifier.lava-fishing", false));
}
});
return bonus;

View File

@@ -45,6 +45,8 @@ public class ConfigManager {
public static int failureFadeStay;
public static int failureFadeOut;
public static boolean useRedis;
public static int lavaMaxTime;
public static int lavaMinTime;
public static void load() {
ConfigUtil.update("config.yml");
@@ -97,6 +99,9 @@ public class ConfigManager {
failureFadeStay = config.getInt("titles.failure.fade.stay", 30) * 50;
failureFadeOut = config.getInt("titles.failure.fade.out", 10) * 50;
lavaMinTime = config.getInt("mechanics.lava-fishing.min-time", 100);
lavaMaxTime = config.getInt("mechanics.lava-fishing.max-time", 600) - lavaMinTime;
useRedis = false;
if (enableCompetition && config.getBoolean("mechanics.fishing-competition.redis", false)) {
YamlConfiguration configuration = ConfigUtil.getConfig("database.yml");

View File

@@ -18,6 +18,7 @@ import net.momirealms.customfishing.object.Function;
import net.momirealms.customfishing.object.action.ActionInterface;
import net.momirealms.customfishing.object.fishing.*;
import net.momirealms.customfishing.object.loot.DroppedItem;
import net.momirealms.customfishing.object.fishing.BobberCheckTask;
import net.momirealms.customfishing.object.loot.Loot;
import net.momirealms.customfishing.object.loot.Mob;
import net.momirealms.customfishing.object.requirements.RequirementInterface;
@@ -65,6 +66,7 @@ public class FishingManager extends Function {
private final HashMap<Player, VanillaLoot> vanillaLoot;
private final ConcurrentHashMap<Player, FishingPlayer> fishingPlayerCache;
private final ConcurrentHashMap<Location, ActivatedTotem> totemCache;
private final ConcurrentHashMap<Player, BobberCheckTask> lavaFishing;
public FishingManager() {
this.playerFishListener = new PlayerFishListener(this);
@@ -76,6 +78,7 @@ public class FishingManager extends Function {
this.vanillaLoot = new HashMap<>();
this.fishingPlayerCache = new ConcurrentHashMap<>();
this.totemCache = new ConcurrentHashMap<>();
this.lavaFishing = new ConcurrentHashMap<>();
load();
}
@@ -112,7 +115,7 @@ public class FishingManager extends Function {
final FishHook fishHook = event.getHook();
hooksCache.put(player, fishHook);
if (isCoolDown(player, 2000)) return;
if (isCoolDown(player, 500)) return;
Bukkit.getScheduler().runTaskAsynchronously(CustomFishing.plugin, () -> {
@@ -121,6 +124,7 @@ public class FishingManager extends Function {
boolean noSpecialRod = true;
boolean noRod = true;
boolean noBait = true;
int lureLevel = 0;
Bonus initialBonus = new Bonus();
initialBonus.setDifficulty(0);
@@ -136,6 +140,7 @@ public class FishingManager extends Function {
if (mainHandItemType == Material.FISHING_ROD) {
noRod = false;
enchantBonus(initialBonus, mainHandItem);
lureLevel = mainHandItem.getEnchantmentLevel(Enchantment.LURE);
}
NBTItem mainHandNBTItem = new NBTItem(mainHandItem);
NBTCompound nbtCompound = mainHandNBTItem.getCompound("CustomFishing");
@@ -163,6 +168,7 @@ public class FishingManager extends Function {
if (offHandItemType != Material.AIR){
if (noRod && offHandItemType == Material.FISHING_ROD) {
enchantBonus(initialBonus, offHandItem);
lureLevel = offHandItem.getEnchantmentLevel(Enchantment.LURE);
}
NBTItem offHandNBTItem = new NBTItem(offHandItem);
NBTCompound nbtCompound = offHandNBTItem.getCompound("CustomFishing");
@@ -202,14 +208,11 @@ public class FishingManager extends Function {
return;
}
fishHook.setMaxWaitTime((int) (fishHook.getMaxWaitTime() * initialBonus.getTime()));
fishHook.setMinWaitTime((int) (fishHook.getMinWaitTime() * initialBonus.getTime()));
nextBonus.put(player, initialBonus);
List<Loot> possibleLoots = getPossibleLootList(new FishingCondition(fishHook.getLocation(), player), false);
List<Loot> availableLoots = new ArrayList<>();
if (possibleLoots.size() == 0){
nextLoot.put(player, null);
return;
}
if (ConfigManager.needRodForLoots && noSpecialRod){
if (!ConfigManager.enableVanillaLoot) AdventureUtil.playerMessage(player, MessageManager.prefix + MessageManager.noRod);
nextLoot.put(player, null);
@@ -219,54 +222,65 @@ public class FishingManager extends Function {
}
if ((ConfigManager.needRodForLoots || ConfigManager.needRodToFish) && noSpecialRod) return;
HashMap<String, Integer> as = initialBonus.getWeightAS();
HashMap<String, Double> md = initialBonus.getWeightMD();
double[] weights = new double[possibleLoots.size()];
int index = 0;
for (Loot loot : possibleLoots){
double weight = loot.getWeight();
String group = loot.getGroup();
if (group != null){
if (as.get(group) != null){
weight += as.get(group);
}
if (md.get(group) != null){
weight *= md.get(group);
}
}
if (weight <= 0) continue;
availableLoots.add(loot);
weights[index++] = weight;
}
double total = Arrays.stream(weights).sum();
double[] weightRatios = new double[index];
for (int i = 0; i < index; i++){
weightRatios[i] = weights[i]/total;
}
double[] weightRange = new double[index];
double startPos = 0;
for (int i = 0; i < index; i++) {
weightRange[i] = startPos + weightRatios[i];
startPos += weightRatios[i];
}
double random = Math.random();
int pos = Arrays.binarySearch(weightRange, random);
if (pos < 0) {
pos = -pos - 1;
}
if (pos < weightRange.length && random < weightRange[pos]) {
nextLoot.put(player, availableLoots.get(pos));
return;
}
nextLoot.put(player, null);
BobberCheckTask bobberCheckTask = new BobberCheckTask(player, initialBonus, fishHook, this, lureLevel);
bobberCheckTask.runTaskTimer(CustomFishing.plugin, 1, 1);
});
}
public void getNextLoot(Player player, Bonus initialBonus, List<Loot> possibleLoots) {
List<Loot> availableLoots = new ArrayList<>();
if (possibleLoots.size() == 0){
nextLoot.put(player, null);
return;
}
HashMap<String, Integer> as = initialBonus.getWeightAS();
HashMap<String, Double> md = initialBonus.getWeightMD();
double[] weights = new double[possibleLoots.size()];
int index = 0;
for (Loot loot : possibleLoots){
double weight = loot.getWeight();
String group = loot.getGroup();
if (group != null){
if (as.get(group) != null){
weight += as.get(group);
}
if (md.get(group) != null){
weight *= md.get(group);
}
}
if (weight <= 0) continue;
availableLoots.add(loot);
weights[index++] = weight;
}
double total = Arrays.stream(weights).sum();
double[] weightRatios = new double[index];
for (int i = 0; i < index; i++){
weightRatios[i] = weights[i]/total;
}
double[] weightRange = new double[index];
double startPos = 0;
for (int i = 0; i < index; i++) {
weightRange[i] = startPos + weightRatios[i];
startPos += weightRatios[i];
}
double random = Math.random();
int pos = Arrays.binarySearch(weightRange, random);
if (pos < 0) {
pos = -pos - 1;
}
if (pos < weightRange.length && random < weightRange[pos]) {
nextLoot.put(player, availableLoots.get(pos));
return;
}
nextLoot.put(player, null);
}
public void onCaughtFish(PlayerFishEvent event) {
final Player player = event.getPlayer();
if (!(event.getCaught() instanceof Item item)) return;
@@ -356,8 +370,15 @@ public class FishingManager extends Function {
public void onReelIn(PlayerFishEvent event) {
final Player player = event.getPlayer();
FishingPlayer fishingPlayer = fishingPlayerCache.remove(player);
if (fishingPlayer == null) return;
proceedReelIn(event, player, fishingPlayer);
if (fishingPlayer != null) {
proceedReelIn(event, player, fishingPlayer);
lavaFishing.remove(player);
return;
}
if (lavaFishing.containsKey(player)) {
showPlayerBar(player, nextLoot.get(player));
event.setCancelled(true);
}
}
private void dropCustomFishingLoot(Player player, Location location, DroppedItem droppedItem, boolean isDouble, double scoreMultiplier) {
@@ -594,16 +615,7 @@ public class FishingManager extends Function {
}
public void onInGround(PlayerFishEvent event) {
FishHook fishHook = event.getHook();
Block belowBlock = fishHook.getLocation().clone().subtract(0,1,0).getBlock();
Block inBlock = fishHook.getLocation().getBlock();
if (inBlock.getType() == Material.AIR && belowBlock.getType() == Material.ICE) {
}
else if (inBlock.getType() == Material.LAVA) {
}
//Empty
}
public void onMMOItemsRodCast(PlayerFishEvent event) {
@@ -629,8 +641,8 @@ public class FishingManager extends Function {
return false;
}
private void enchantBonus(Bonus initialBonus, ItemStack mainHandItem) {
Map<Enchantment, Integer> enchantments = mainHandItem.getEnchantments();
private void enchantBonus(Bonus initialBonus, ItemStack itemStack) {
Map<Enchantment, Integer> enchantments = itemStack.getEnchantments();
for (Map.Entry<Enchantment, Integer> en : enchantments.entrySet()) {
String key = en.getKey().getKey() + ":" + en.getValue();
Bonus enchantBonus = BonusManager.ENCHANTS.get(key);
@@ -640,7 +652,7 @@ public class FishingManager extends Function {
}
}
private List<Loot> getPossibleLootList(FishingCondition fishingCondition, boolean finder) {
public List<Loot> getPossibleWaterLootList(FishingCondition fishingCondition, boolean finder) {
List<Loot> available = new ArrayList<>();
outer:
for (Loot loot : LootManager.WATERLOOTS.values()) {
@@ -661,6 +673,27 @@ public class FishingManager extends Function {
return available;
}
public List<Loot> getPossibleLavaLootList(FishingCondition fishingCondition, boolean finder) {
List<Loot> available = new ArrayList<>();
outer:
for (Loot loot : LootManager.LAVALOOTS.values()) {
if (finder && !loot.isShowInFinder()) continue;
RequirementInterface[] requirements = loot.getRequirements();
if (requirements == null){
available.add(loot);
}
else {
for (RequirementInterface requirement : requirements){
if (!requirement.isConditionMet(fishingCondition)){
continue outer;
}
}
available.add(loot);
}
}
return available;
}
@Override
public void onInteract(PlayerInteractEvent event) {
ItemStack itemStack = event.getItem();
@@ -721,7 +754,7 @@ public class FishingManager extends Function {
private void useFinder(Player player) {
if (isCoolDown(player, ConfigManager.fishFinderCoolDown)) return;
List<Loot> possibleLoots = getPossibleLootList(new FishingCondition(player.getLocation(), player), true);
List<Loot> possibleLoots = getPossibleWaterLootList(new FishingCondition(player.getLocation(), player), true);
FishFinderEvent fishFinderEvent = new FishFinderEvent(player, possibleLoots);
Bukkit.getPluginManager().callEvent(fishFinderEvent);
@@ -739,6 +772,9 @@ public class FishingManager extends Function {
}
private void showPlayerBar(Player player, @Nullable Loot loot){
if (loot == Loot.EMPTY) return;
Layout layout;
if (loot != null && loot.getLayout() != null){
layout = loot.getLayout()[new Random().nextInt(loot.getLayout().length)];
@@ -802,6 +838,8 @@ public class FishingManager extends Function {
nextLoot.remove(player);
nextBonus.remove(player);
vanillaLoot.remove(player);
BobberCheckTask task = lavaFishing.remove(player);
if (task != null) task.stop();
// prevent bar duplication
FishHook fishHook = hooksCache.remove(player);
if (fishHook != null) fishHook.remove();
@@ -833,4 +871,12 @@ public class FishingManager extends Function {
public void removeTotem(ActivatedTotem activatedTotem) {
totemCache.remove(activatedTotem);
}
public void addPlayerToLavaFishing(Player player, BobberCheckTask task) {
this.lavaFishing.put(player, task);
}
public void removePlayerFromLavaFishing(Player player) {
this.lavaFishing.remove(player);
}
}

View File

@@ -36,10 +36,6 @@ public class IntegrationManager extends Function {
private PlaceholderManager placeholderManager;
private AntiGriefInterface[] antiGriefs;
public IntegrationManager() {
load();
}
@Override
public void load() {
@@ -67,7 +63,9 @@ public class IntegrationManager extends Function {
this.blockInterface = new ItemsAdderBlockImpl();
} else if (pluginManager.getPlugin("Oraxen") != null) {
this.blockInterface = new OraxenBlockImpl();
} else this.blockInterface = new VanillaBlockImpl();
} else {
this.blockInterface = new VanillaBlockImpl();
}
if (pluginManager.getPlugin("eco") != null) {
EcoItemRegister.registerItems();

View File

@@ -1,5 +1,6 @@
package net.momirealms.customfishing.manager;
import net.momirealms.customfishing.CustomFishing;
import net.momirealms.customfishing.integration.BlockInterface;
import net.momirealms.customfishing.object.Function;
import net.momirealms.customfishing.object.action.ActionInterface;
@@ -30,11 +31,6 @@ public class TotemManager extends Function {
public static HashMap<String, List<Totem>> CORES;
public static HashMap<String, String> BLOCKS;
public static HashMap<String, String> INVERTED;
private final BlockInterface blockInterface;
public TotemManager(BlockInterface blockInterface) {
this.blockInterface = blockInterface;
}
@Override
public void unload() {
@@ -213,6 +209,8 @@ public class TotemManager extends Function {
public int checkLocationModel(OriginalModel model, Location location){
BlockInterface blockInterface = CustomFishing.plugin.getIntegrationManager().getBlockInterface();
CorePos corePos = model.getCorePos();
int xOffset = corePos.getX();
int yOffset = corePos.getY();
@@ -388,6 +386,8 @@ public class TotemManager extends Function {
public void removeModel(FinalModel model, Location location, int id) {
BlockInterface blockInterface = CustomFishing.plugin.getIntegrationManager().getBlockInterface();
CorePos corePos = model.getCorePos();
int xOffset = corePos.getX();
int yOffset = corePos.getY();

View File

@@ -0,0 +1,127 @@
package net.momirealms.customfishing.object.fishing;
import com.plotsquared.core.plot.PlotId;
import net.momirealms.customfishing.CustomFishing;
import net.momirealms.customfishing.manager.ConfigManager;
import net.momirealms.customfishing.manager.FishingManager;
import net.momirealms.customfishing.object.loot.Loot;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
import java.util.List;
import java.util.Random;
public class BobberCheckTask extends BukkitRunnable {
private final FishHook fishHook;
private int timer;
private final Player player;
private final Bonus bonus;
private final FishingManager fishingManager;
private boolean hooked;
private boolean first_time;
private int jump_timer;
private final int lureLevel;
private BukkitTask cache_1;
private BukkitTask cache_2;
private BukkitTask cache_3;
public BobberCheckTask(Player player, Bonus bonus, FishHook fishHook, FishingManager fishingManager, int lureLevel) {
this.fishHook = fishHook;
this.fishingManager = fishingManager;
this.player = player;
this.timer = 0;
this.bonus = bonus;
this.first_time = true;
this.jump_timer = 0;
this.lureLevel = lureLevel;
}
@Override
public void run() {
timer ++;
if (!fishHook.isValid()) {
stop();
return;
}
if (fishHook.getLocation().getBlock().getType() == Material.LAVA) {
if (!bonus.canLavaFishing()) {
stop();
return;
}
if (hooked) {
jump_timer++;
if (jump_timer < 5) {
return;
}
jump_timer = 0;
fishHook.setVelocity(new Vector(0,0.24,0));
return;
}
if (first_time) {
first_time = false;
randomTime();
}
fishHook.setVelocity(new Vector(0, 0.12,0));
return;
}
if (fishHook.isInWater()) {
List<Loot> possibleLoots = fishingManager.getPossibleWaterLootList(new FishingCondition(fishHook.getLocation(), player), false);
fishingManager.getNextLoot(player, bonus, possibleLoots);
stop();
return;
}
if (fishHook.isOnGround()) {
stop();
return;
}
if (timer > 2400) {
stop();
}
}
public void stop() {
cancel();
cancelTask();
}
public void cancelTask() {
if (cache_1 != null) {
cache_1.cancel();
cache_1 = null;
}
if (cache_2 != null) {
cache_2.cancel();
cache_2 = null;
}
if (cache_3 != null) {
cache_3.cancel();
cache_3 = null;
}
}
private void randomTime() {
List<Loot> possibleLoots = fishingManager.getPossibleLavaLootList(new FishingCondition(fishHook.getLocation(), player), false);
fishingManager.getNextLoot(player, bonus, possibleLoots);
cancelTask();
int random = new Random().nextInt(ConfigManager.lavaMaxTime) + ConfigManager.lavaMinTime;
random -= lureLevel * 100;
random *= bonus.getTime();
if (random < ConfigManager.lavaMinTime) random = ConfigManager.lavaMinTime;
cache_1 = Bukkit.getScheduler().runTaskLater(CustomFishing.plugin, () -> {
hooked = true;
fishingManager.addPlayerToLavaFishing(player, this);
}, random);
cache_2 = Bukkit.getScheduler().runTaskLater(CustomFishing.plugin, () -> {
hooked = false;
first_time = true;
fishingManager.removePlayerFromLavaFishing(player);
}, random + 40);
cache_3 = new LavaEffect(fishHook.getLocation()).runTaskTimerAsynchronously(CustomFishing.plugin,random - 60,1);
}
}

View File

@@ -12,6 +12,7 @@ public class Bonus {
private double score;
private int difficulty;
private double doubleLoot;
private boolean canLavaFishing;
public HashMap<String, Double> getWeightMD() {
return weightMD;
@@ -61,9 +62,17 @@ public class Bonus {
this.doubleLoot = doubleLoot;
}
public boolean canLavaFishing() {
return canLavaFishing;
}
public void setCanLavaFishing(boolean canLavaFishing) {
this.canLavaFishing = canLavaFishing;
}
public void addBonus(Bonus anotherBonus) {
HashMap<String, Integer> weightAS = anotherBonus.getWeightAS();
if (weightAS != null){
if (weightAS != null) {
for (Map.Entry<String, Integer> en : weightAS.entrySet()) {
String group = en.getKey();
this.weightAS.put(group, Optional.ofNullable(this.weightAS.get(group)).orElse(0) + en.getValue());
@@ -80,5 +89,6 @@ public class Bonus {
if (anotherBonus.getDoubleLoot() != 0) this.doubleLoot += anotherBonus.getDoubleLoot();
if (anotherBonus.getDifficulty() != 0) this.difficulty += anotherBonus.getDifficulty();
if (anotherBonus.getScore() != 0) this.score += (anotherBonus.getScore() - 1);
if (anotherBonus.canLavaFishing()) this.canLavaFishing = true;
}
}

View File

@@ -0,0 +1,34 @@
package net.momirealms.customfishing.object.fishing;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.scheduler.BukkitRunnable;
public class LavaEffect extends BukkitRunnable {
private final Location startLoc;
private final Location endLoc;
private final Location controlLoc;
private int timer;
public LavaEffect(Location loc) {
this.startLoc = loc.clone().add(0,0.3,0);
this.endLoc = this.startLoc.clone().add((Math.random() * 16 - 8), startLoc.getY(), (Math.random() * 16 - 8));
this.controlLoc = new Location(startLoc.getWorld(), (startLoc.getX() + endLoc.getX())/2 + Math.random() * 12 - 6, startLoc.getY(), (startLoc.getZ() + endLoc.getZ())/2 + Math.random() * 12 - 6);
}
@Override
public void run() {
timer++;
if (timer > 60) {
cancel();
}
else {
double t = (double) timer / 60;
Location particleLoc = endLoc.clone().multiply(Math.pow((1 - t), 2)).add(controlLoc.clone().multiply(2 * t * (1 - t))).add(startLoc.clone().multiply(Math.pow(t, 2)));
particleLoc.setY(startLoc.getY());
startLoc.getWorld().spawnParticle(Particle.FLAME, particleLoc,1,0,0,0,0);
}
}
}

View File

@@ -38,6 +38,8 @@ public class ConfigUtil {
CustomFishing.plugin.getCompetitionManager().load();
CustomFishing.plugin.getTotemManager().unload();
CustomFishing.plugin.getTotemManager().load();
CustomFishing.plugin.getIntegrationManager().unload();
CustomFishing.plugin.getIntegrationManager().load();
try {
Reflection.load();
}