1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

[CI skip] Reduced technical debt

This commit is contained in:
TheBusyBiscuit 2020-09-21 12:51:13 +02:00
parent 749bb972a3
commit 0227e0cc30
4 changed files with 53 additions and 28 deletions

View File

@ -8,6 +8,7 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;
@ -41,7 +42,7 @@ import me.mrCookieSlime.Slimefun.api.Slimefun;
public class ErrorReport<T extends Throwable> {
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm", Locale.ROOT);
private static int count;
private static final AtomicInteger count = new AtomicInteger(0);
private SlimefunAddon addon;
private T throwable;
@ -124,12 +125,12 @@ public class ErrorReport<T extends Throwable> {
* @return The amount of {@link ErrorReport ErrorReports} created.
*/
public static int count() {
return count;
return count.get();
}
private void print(@Nonnull Consumer<PrintStream> printer) {
this.file = getNewFile();
count++;
count.incrementAndGet();
try (PrintStream stream = new PrintStream(file, StandardCharsets.UTF_8.name())) {
stream.println();

View File

@ -404,6 +404,8 @@ public final class PlayerProfile {
* @return Whether the {@link PlayerProfile} was already loaded
*/
public static boolean request(@Nonnull OfflinePlayer p) {
Validate.notNull(p, "Cannot request a Profile for null");
if (!SlimefunPlugin.getRegistry().getPlayerProfiles().containsKey(p.getUniqueId())) {
// Should probably prevent multiple requests for the same profile in the future
Bukkit.getScheduler().runTaskAsynchronously(SlimefunPlugin.instance(), () -> {

View File

@ -5,6 +5,7 @@ import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
@ -24,16 +25,19 @@ class PlaceholderAPIHook extends PlaceholderExpansion {
this.author = plugin.getDescription().getAuthors().toString();
}
@Nonnull
@Override
public String getIdentifier() {
return "slimefun";
}
@Nonnull
@Override
public String getVersion() {
return version;
}
@Nonnull
@Override
public String getAuthor() {
return author;
@ -49,9 +53,18 @@ class PlaceholderAPIHook extends PlaceholderExpansion {
return true;
}
private boolean isPlaceholder(@Nullable OfflinePlayer p, boolean requiresProfile, @Nonnull String params, @Nonnull String placeholder) {
if (requiresProfile) {
return p != null && placeholder.equals(params) && PlayerProfile.request(p);
}
else {
return placeholder.equals(params);
}
}
@Override
public String onRequest(OfflinePlayer p, String params) {
if (params.equals("researches_total_xp_levels_spent") && PlayerProfile.request(p)) {
public String onRequest(@Nullable OfflinePlayer p, @Nonnull String params) {
if (isPlaceholder(p, true, params, "researches_total_xp_levels_spent")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) {
@ -60,7 +73,7 @@ class PlaceholderAPIHook extends PlaceholderExpansion {
}
}
if (params.equals("researches_total_researches_unlocked") && PlayerProfile.request(p)) {
if (isPlaceholder(p, true, params, "researches_total_researches_unlocked")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) {
@ -69,11 +82,11 @@ class PlaceholderAPIHook extends PlaceholderExpansion {
}
}
if (params.equals("researches_total_researches")) {
if (isPlaceholder(p, false, params, "researches_total_researches")) {
return String.valueOf(SlimefunPlugin.getRegistry().getResearches().size());
}
if (params.equals("researches_percentage_researches_unlocked") && PlayerProfile.request(p)) {
if (isPlaceholder(p, true, params, "researches_percentage_researches_unlocked")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) {
@ -82,7 +95,7 @@ class PlaceholderAPIHook extends PlaceholderExpansion {
}
}
if (params.equals("researches_title") && PlayerProfile.request(p)) {
if (isPlaceholder(p, true, params, "researches_title")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) {
@ -90,20 +103,17 @@ class PlaceholderAPIHook extends PlaceholderExpansion {
}
}
if (params.equals("gps_complexity")) {
if (isPlaceholder(p, false, params, "gps_complexity") && p != null) {
return String.valueOf(SlimefunPlugin.getGPSNetwork().getNetworkComplexity(p.getUniqueId()));
}
if (params.equals("timings_lag")) {
if (isPlaceholder(p, false, params, "timings_lag")) {
return SlimefunPlugin.getProfiler().getTime();
}
if (params.equals("language")) {
if (!(p instanceof Player)) {
return "Unknown";
}
return SlimefunPlugin.getLocalization().getLanguage((Player) p).getName((Player) p);
if (isPlaceholder(p, false, params, "language") && p instanceof Player) {
Player player = (Player) p;
return SlimefunPlugin.getLocalization().getLanguage(player).getName(player);
}
return null;

View File

@ -33,7 +33,7 @@ import io.github.thebusybiscuit.slimefun4.utils.HeadTexture;
import io.github.thebusybiscuit.slimefun4.utils.PatternUtils;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
public class SlimefunItemStack extends CustomItem {
public class SlimefunItemStack extends CustomItem implements Cloneable {
private String id;
private ImmutableItemMeta immutableMeta;
@ -243,16 +243,6 @@ public class SlimefunItemStack extends CustomItem {
locked = true;
}
@Override
public ItemStack clone() {
return new SlimefunItemStack(id, this);
}
@Override
public String toString() {
return "SlimefunItemStack (" + id + (getAmount() > 1 ? (" x " + getAmount()) : "") + ')';
}
@Nonnull
public Optional<String> getSkullTexture() {
return Optional.ofNullable(texture);
@ -292,4 +282,26 @@ public class SlimefunItemStack extends CustomItem {
throw new IllegalArgumentException("The provided texture for Item \"" + id + "\" does not seem to be a valid texture String!");
}
}
@Override
public ItemStack clone() {
return new SlimefunItemStack(id, this);
}
@Override
public String toString() {
return "SlimefunItemStack (" + id + (getAmount() > 1 ? (" x " + getAmount()) : "") + ')';
}
@Override
public final boolean equals(Object obj) {
// We don't want people to override this, it should use the super method
return super.equals(obj);
}
@Override
public final int hashCode() {
// We don't want people to override this, it should use the super method
return super.hashCode();
}
}