Refactor code for clarity: standardize optional checks, improve list handling, and adjust PhantomPreSpawnEvent import.
This commit is contained in:
@@ -43,21 +43,23 @@ public class BoosterCache {
|
||||
LinkedList<Booster> list = boosters.get(boosterType);
|
||||
Booster oldActive = null;
|
||||
if (list.size() > 1) {
|
||||
oldActive = list.get(0);
|
||||
oldActive = list.getFirst();
|
||||
}
|
||||
list.sort(Booster::compareTo);
|
||||
if (oldActive != null && !list.get(0).getBoosterUUID().equals(oldActive.getBoosterUUID())) {
|
||||
if (oldActive != null && !list.getFirst().getBoosterUUID().equals(oldActive.getBoosterUUID())) {
|
||||
oldActive.updateDuration();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Optional<Booster> getActiveBooster(BoosterType boosterType) {
|
||||
if (!boosters.containsKey(boosterType))
|
||||
if (!boosters.containsKey(boosterType)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
LinkedList<Booster> list = boosters.get(boosterType);
|
||||
if (list.isEmpty())
|
||||
if (list.isEmpty()) {
|
||||
return Optional.empty();
|
||||
return Optional.of(list.get(0));
|
||||
}
|
||||
return Optional.of(list.getFirst());
|
||||
}
|
||||
|
||||
public synchronized List<Booster> getAllActiveBoosters() {
|
||||
|
||||
@@ -25,8 +25,9 @@ public class MyPetListener implements Listener {
|
||||
}
|
||||
|
||||
Optional<Booster> myPetBooster = boosterCache.getActiveBooster(BoosterType.MYPET);
|
||||
if (myPetBooster.isEmpty())
|
||||
if (myPetBooster.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Booster booster = myPetBooster.get();
|
||||
event.setExp(booster.useMultiplier(event.getExp()));
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.alttd.boosters.listeners;
|
||||
|
||||
import com.alttd.boosterapi.data.BoosterCache;
|
||||
import com.alttd.boosterapi.data.BoosterType;
|
||||
import com.destroystokyo.paper.event.entity.PhantomPreSpawnEvent;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -16,7 +17,7 @@ public class PhantomSpawnListener implements Listener {
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPhantomPreSpawn(com.destroystokyo.paper.event.entity.PhantomPreSpawnEvent event) {
|
||||
public void onPhantomPreSpawn(PhantomPreSpawnEvent event) {
|
||||
Entity spawningEntity = event.getSpawningEntity();
|
||||
if (spawningEntity instanceof Player
|
||||
&& boosterCache.getActiveBooster(BoosterType.PHANTOM).isPresent()) {
|
||||
|
||||
@@ -23,8 +23,9 @@ public class mcMMOListener implements Listener {
|
||||
String skillName = event.getSkill().name();
|
||||
BoosterType type = BoosterType.getByName(skillName);
|
||||
Optional<Booster> optionalBooster = boosterCache.getActiveBooster(type);
|
||||
if (optionalBooster.isEmpty())
|
||||
if (optionalBooster.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Booster booster = optionalBooster.get();
|
||||
event.setRawXpGained(Math.round(booster.useMultiplier(event.getRawXpGained())));
|
||||
|
||||
Reference in New Issue
Block a user