1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.logging.Level; import java.util.logging.Level;
@ -41,7 +42,7 @@ import me.mrCookieSlime.Slimefun.api.Slimefun;
public class ErrorReport<T extends Throwable> { public class ErrorReport<T extends Throwable> {
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm", Locale.ROOT); 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 SlimefunAddon addon;
private T throwable; private T throwable;
@ -124,12 +125,12 @@ public class ErrorReport<T extends Throwable> {
* @return The amount of {@link ErrorReport ErrorReports} created. * @return The amount of {@link ErrorReport ErrorReports} created.
*/ */
public static int count() { public static int count() {
return count; return count.get();
} }
private void print(@Nonnull Consumer<PrintStream> printer) { private void print(@Nonnull Consumer<PrintStream> printer) {
this.file = getNewFile(); this.file = getNewFile();
count++; count.incrementAndGet();
try (PrintStream stream = new PrintStream(file, StandardCharsets.UTF_8.name())) { try (PrintStream stream = new PrintStream(file, StandardCharsets.UTF_8.name())) {
stream.println(); stream.println();

View File

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

View File

@ -5,6 +5,7 @@ import java.util.Set;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -24,16 +25,19 @@ class PlaceholderAPIHook extends PlaceholderExpansion {
this.author = plugin.getDescription().getAuthors().toString(); this.author = plugin.getDescription().getAuthors().toString();
} }
@Nonnull
@Override @Override
public String getIdentifier() { public String getIdentifier() {
return "slimefun"; return "slimefun";
} }
@Nonnull
@Override @Override
public String getVersion() { public String getVersion() {
return version; return version;
} }
@Nonnull
@Override @Override
public String getAuthor() { public String getAuthor() {
return author; return author;
@ -49,9 +53,18 @@ class PlaceholderAPIHook extends PlaceholderExpansion {
return true; 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 @Override
public String onRequest(OfflinePlayer p, String params) { public String onRequest(@Nullable OfflinePlayer p, @Nonnull String params) {
if (params.equals("researches_total_xp_levels_spent") && PlayerProfile.request(p)) { if (isPlaceholder(p, true, params, "researches_total_xp_levels_spent")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p); Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) { 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); Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) { 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()); 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); Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) { 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); Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) { 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())); return String.valueOf(SlimefunPlugin.getGPSNetwork().getNetworkComplexity(p.getUniqueId()));
} }
if (params.equals("timings_lag")) { if (isPlaceholder(p, false, params, "timings_lag")) {
return SlimefunPlugin.getProfiler().getTime(); return SlimefunPlugin.getProfiler().getTime();
} }
if (params.equals("language")) { if (isPlaceholder(p, false, params, "language") && p instanceof Player) {
if (!(p instanceof Player)) { Player player = (Player) p;
return "Unknown"; return SlimefunPlugin.getLocalization().getLanguage(player).getName(player);
}
return SlimefunPlugin.getLocalization().getLanguage((Player) p).getName((Player) p);
} }
return null; return null;

View File

@ -33,7 +33,7 @@ import io.github.thebusybiscuit.slimefun4.utils.HeadTexture;
import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import io.github.thebusybiscuit.slimefun4.utils.PatternUtils;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
public class SlimefunItemStack extends CustomItem { public class SlimefunItemStack extends CustomItem implements Cloneable {
private String id; private String id;
private ImmutableItemMeta immutableMeta; private ImmutableItemMeta immutableMeta;
@ -243,16 +243,6 @@ public class SlimefunItemStack extends CustomItem {
locked = true; locked = true;
} }
@Override
public ItemStack clone() {
return new SlimefunItemStack(id, this);
}
@Override
public String toString() {
return "SlimefunItemStack (" + id + (getAmount() > 1 ? (" x " + getAmount()) : "") + ')';
}
@Nonnull @Nonnull
public Optional<String> getSkullTexture() { public Optional<String> getSkullTexture() {
return Optional.ofNullable(texture); 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!"); 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();
}
} }