-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move common code from PlaceholderValueProvider to NumberStringValuePr…
…ovider
- Loading branch information
Showing
2 changed files
with
77 additions
and
45 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
.../main/java/me/hsgamer/topper/spigot/plugin/holder/provider/NumberStringValueProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package me.hsgamer.topper.spigot.plugin.holder.provider; | ||
|
||
import io.github.projectunified.minelib.scheduler.async.AsyncScheduler; | ||
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler; | ||
import me.hsgamer.topper.spigot.plugin.TopperPlugin; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.logging.Level; | ||
|
||
public abstract class NumberStringValueProvider implements ValueProvider { | ||
protected final TopperPlugin plugin; | ||
private final boolean isAsync; | ||
private final boolean showErrors; | ||
|
||
public NumberStringValueProvider(TopperPlugin plugin, Map<String, Object> map) { | ||
this.plugin = plugin; | ||
isAsync = Optional.ofNullable(map.get("async")) | ||
.map(Object::toString) | ||
.map(String::toLowerCase) | ||
.map(Boolean::parseBoolean) | ||
.orElse(false); | ||
showErrors = Optional.ofNullable(map.get("show-errors")) | ||
.map(Object::toString) | ||
.map(String::toLowerCase) | ||
.map(Boolean::parseBoolean) | ||
.orElse(false); | ||
} | ||
|
||
protected abstract String getDisplayName(); | ||
|
||
protected abstract Optional<String> getString(UUID uuid); | ||
|
||
@Override | ||
public CompletableFuture<Optional<Double>> getValue(UUID uuid) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
Optional<String> value = getString(uuid); | ||
if (!value.isPresent()) { | ||
if (showErrors) { | ||
plugin.getLogger().warning("The value of " + getDisplayName() + " is empty"); | ||
} | ||
return Optional.empty(); | ||
} | ||
return Optional.of(Double.parseDouble(value.get())); | ||
} catch (Exception e) { | ||
if (showErrors) { | ||
plugin.getLogger().log(Level.WARNING, "There is an error while parsing the value of " + getDisplayName(), e); | ||
} | ||
return Optional.empty(); | ||
} | ||
}, (isAsync ? AsyncScheduler.get(plugin) : GlobalScheduler.get(plugin)).getExecutor()); | ||
} | ||
} |
66 changes: 21 additions & 45 deletions
66
...gin/src/main/java/me/hsgamer/topper/spigot/plugin/hook/papi/PlaceholderValueProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,49 @@ | ||
package me.hsgamer.topper.spigot.plugin.hook.papi; | ||
|
||
import io.github.projectunified.minelib.scheduler.async.AsyncScheduler; | ||
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler; | ||
import me.clip.placeholderapi.PlaceholderAPI; | ||
import me.hsgamer.topper.spigot.plugin.TopperPlugin; | ||
import me.hsgamer.topper.spigot.plugin.holder.provider.ValueProvider; | ||
import me.hsgamer.topper.spigot.plugin.holder.provider.NumberStringValueProvider; | ||
import org.bukkit.OfflinePlayer; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.logging.Level; | ||
|
||
public class PlaceholderValueProvider implements ValueProvider { | ||
private final TopperPlugin plugin; | ||
public class PlaceholderValueProvider extends NumberStringValueProvider { | ||
private final String placeholder; | ||
private final boolean isOnlineOnly; | ||
private final boolean isAsync; | ||
private final boolean showErrors; | ||
|
||
public PlaceholderValueProvider(TopperPlugin plugin, Map<String, Object> map) { | ||
this.plugin = plugin; | ||
super(plugin, map); | ||
placeholder = Optional.ofNullable(map.get("placeholder")).map(Object::toString).orElse(""); | ||
isOnlineOnly = Optional.ofNullable(map.get("online")) | ||
.map(Object::toString) | ||
.map(String::toLowerCase) | ||
.map(Boolean::parseBoolean) | ||
.orElse(false); | ||
isAsync = Optional.ofNullable(map.get("async")) | ||
.map(Object::toString) | ||
.map(String::toLowerCase) | ||
.map(Boolean::parseBoolean) | ||
.orElse(false); | ||
showErrors = Optional.ofNullable(map.get("show-errors")) | ||
.map(Object::toString) | ||
.map(String::toLowerCase) | ||
.map(Boolean::parseBoolean) | ||
.orElse(false); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Optional<Double>> getValue(UUID uuid) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
OfflinePlayer player; | ||
if (isOnlineOnly) { | ||
player = plugin.getServer().getPlayer(uuid); | ||
if (player == null) { | ||
return Optional.empty(); | ||
} | ||
} else { | ||
player = plugin.getServer().getOfflinePlayer(uuid); | ||
} | ||
protected String getDisplayName() { | ||
return placeholder; | ||
} | ||
|
||
try { | ||
String parsed = PlaceholderAPI.setPlaceholders(player, placeholder).trim(); | ||
if (parsed.isEmpty()) { | ||
if (showErrors) { | ||
plugin.getLogger().warning("The placeholder " + placeholder + " returns empty"); | ||
} | ||
return Optional.empty(); | ||
} | ||
return Optional.of(Double.parseDouble(parsed)); | ||
} catch (Exception e) { | ||
if (showErrors) { | ||
plugin.getLogger().log(Level.WARNING, "There is an error while parsing the placeholder: " + placeholder, e); | ||
} | ||
@Override | ||
protected Optional<String> getString(UUID uuid) { | ||
OfflinePlayer player; | ||
if (isOnlineOnly) { | ||
player = plugin.getServer().getPlayer(uuid); | ||
if (player == null) { | ||
return Optional.empty(); | ||
} | ||
}, (isAsync ? AsyncScheduler.get(plugin) : GlobalScheduler.get(plugin)).getExecutor()); | ||
} else { | ||
player = plugin.getServer().getOfflinePlayer(uuid); | ||
} | ||
|
||
String parsed = PlaceholderAPI.setPlaceholders(player, placeholder).trim(); | ||
if (parsed.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(parsed); | ||
} | ||
} |