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