Skip to content

Commit

Permalink
5.1.2.4 - hotfixes (#402)
Browse files Browse the repository at this point in the history
* Update Metrics code from bstats and fix typo

* Fix StackOverflow when appending components

* Bump version
  • Loading branch information
sandtechnology authored Sep 19, 2023
1 parent d5b729a commit dc77f81
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<artifactId>QuickShop</artifactId>

<properties>
<pluginver>5.1.2.3</pluginver>
<pluginver>5.1.2.4</pluginver>
<package>org.maxgamer.quickshop</package>
<developer>Ghost-chu</developer>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
66 changes: 40 additions & 26 deletions src/main/java/org/maxgamer/quickshop/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,28 @@
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import org.maxgamer.quickshop.util.JsonUtil;
import org.maxgamer.quickshop.util.Util;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;

Expand Down Expand Up @@ -248,33 +260,35 @@ private static byte[] compress(final String str) throws IOException {
return outputStream.toByteArray();
}

// This ThreadFactory enforces the naming convention for our Threads
private final ThreadFactory threadFactory = task -> new Thread(task, "bStats-Metrics");

// Executor service for requests
// We use an executor service because the Bukkit scheduler is affected by server lags
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, threadFactory);

/**
* Starts the Scheduler which submits our data every 30 minutes.
*/
private void startSubmitting() {
final Timer timer =
new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
timer.scheduleAtFixedRate(
new TimerTask() {
@Override
public void run() {
if (!plugin.isEnabled()) { // Plugin was disabled
timer.cancel();
return;
}
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the
// Bukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats
// collection is sync ;)
Util.mainThreadRun(() -> submitData());
}
},
1000 * 60 * 5,
1000 * 60 * 30);
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough
// time to start
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
// WARNING: Just don't do it!
final Runnable submitTask = () -> {
if (!plugin.isEnabled()) { // Plugin was disabled
scheduler.shutdown();
return;
}
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
plugin.getServer().getScheduler().runTask(plugin, this::submitData);
};

// Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution of requests on the
// bStats backend. To circumvent this problem, we introduce some randomness into the initial and second delay.
// WARNING: You must not modify and part of this Metrics class, including the submit delay or frequency!
// WARNING: Modifying this code will get your plugin banned on bStats. Just don't do it!
long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3));
long secondDelay = (long) (1000 * 60 * (Math.random() * 30));
scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS);
scheduler.scheduleAtFixedRate(submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/maxgamer/quickshop/QuickShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ public void run() {
@Override
public void run() {
getLogger().info("Registering bStats metrics...");
submitMeritcs();
submitMetrics();
}
}.runTask(this);
if (loaded) {
Expand Down Expand Up @@ -1237,7 +1237,7 @@ private boolean setupDatabase() {
return true;
}

private void submitMeritcs() {
private void submitMetrics() {
if (!getConfig().getBoolean("disabled-metrics")) {
String vaultVer;
Plugin vault = Bukkit.getPluginManager().getPlugin("Vault");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public BungeeComponentBuilder append(BaseComponent component) {
if (builder.getCursor() == -1) {
append(component, ComponentBuilder.FormatRetention.EVENTS);
} else {
append(component);
append(component, ComponentBuilder.FormatRetention.ALL);
}
return this;
}
Expand Down

0 comments on commit dc77f81

Please sign in to comment.