Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
Fixed runLater HashMap being null.
Fixed runLater loading.
  • Loading branch information
ChromMob authored Feb 18, 2022
1 parent 4b80021 commit b179658
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/main/java/me/chrommob/minestore/MineStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.Getter;
import me.chrommob.minestore.commandexecution.Command;
import me.chrommob.minestore.commands.Buy;
import me.chrommob.minestore.commands.Reload;
import me.chrommob.minestore.commands.Store;
Expand Down Expand Up @@ -140,6 +141,6 @@ public void loadConfig() {
Config.setItemName(getConfig().getString("format.item-name"));
Config.setItemDescription(getConfig().getString("format.item-description"));
Config.setItemPrice(getConfig().getString("format.item-price"));
Config.setApiKey(getConfig().getInt("api-key"));
Config.setApiKey(Integer.parseInt(getConfig().getString("api-key")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.HashMap;

public class Command {
public static HashMap<String, ArrayList<String>> runLater = new HashMap<>();
public static HashMap<String, ArrayList<String>> runLater;

public static void online(String command) {
Bukkit.getScheduler().runTask(Bukkit.getPluginManager().getPlugin("MineStore"), () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public void onPlayerJoin(final PlayerJoinEvent event) {
Bukkit.getScheduler().runTask(Bukkit.getPluginManager().getPlugin("MineStore"), () -> {
runLater.get(name).forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
runLater.remove(name);
System.out.print(runLater);
PunishmentManager.update();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package me.chrommob.minestore.commandexecution;

import me.chrommob.minestore.commands.PunishmentManager;
import org.apache.commons.lang.ObjectUtils;

import java.util.ArrayList;

import static me.chrommob.minestore.commandexecution.Command.runLater;

public class Manager {
public static void add(String player, String command) {
ArrayList commands;

ArrayList<String> commands;
try {
commands = runLater.get(player);
commands.add(command);
runLater.put(player, commands);
PunishmentManager.update();
} catch (Exception e) {
commands = new ArrayList();
commands = new ArrayList<String>();
commands.add(command);
runLater.put(player, commands);
PunishmentManager.update();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package me.chrommob.minestore.commands;

import me.chrommob.minestore.MineStore;
import me.chrommob.minestore.commandexecution.Command;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

import static me.chrommob.minestore.commandexecution.Command.runLater;

public class PunishmentManager extends JavaPlugin {
static File later = new File(MineStore.instance.getDataFolder()+"/later.yml");
static Yaml yaml = new Yaml();
Expand All @@ -23,16 +23,25 @@ public static void create() {
public static void update(){
try {
PrintWriter writer = new PrintWriter(later);
yaml.dump(runLater, writer);

yaml.dump(Command.runLater, writer);
} catch (Exception e){
e.printStackTrace();
}
}
public static void get(){
try {
InputStream inputStream = new FileInputStream(later);
runLater = (HashMap<String, ArrayList<String>>) yaml.load(inputStream);
try {
Command.runLater = (HashMap<String, ArrayList<String>>) yaml.load(inputStream);
} catch (Exception e){
e.printStackTrace();
}
if (Command.runLater == null){
Command.runLater = new HashMap<>();
Bukkit.getLogger().info("[MineStore] Command file empty.");
} else {
Bukkit.getLogger().info("[MineStore] Loaded commands for " + Command.runLater.size() + " players.");
}
} catch (Exception e){
e.printStackTrace();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/me/chrommob/minestore/data/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ public class Config {
@Getter
private static boolean vaultPresent;

@Setter
@Getter
private static boolean empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class DonationGoalListener {
private static HttpsURLConnection urlConnection;
@SneakyThrows
//Trying to get the donation goal from the server with the secret key
public static void run() {
if (Config.getApiKey() != 0) {
try {
Expand All @@ -35,6 +36,7 @@ public static void run() {
e.printStackTrace();
}
} else {
//If the api key is not set, we will try to get the donation goal from the server withouth the secret key
try {
String link = Config.getApiUrl() + "donation_goal";
URL url = new URL(link);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.chrommob.minestore.data.Config;
import me.chrommob.minestore.data.PlaceHolderData;
import me.chrommob.minestore.placeholders.objects.LastDonator;
import org.bukkit.Bukkit;

import javax.net.ssl.HttpsURLConnection;
import java.io.InputStream;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/chrommob/minestore/util/Mode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Getter;
import lombok.Setter;

// Enum for the different modes
@Getter
public enum Mode {
WEBSOCKET,
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/me/chrommob/minestore/util/Runnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

//Runnable to execute plugin functions
public class Runnable {
private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
public static void runListener(String load) {
executor.scheduleAtFixedRate(() -> {
if (load.equalsIgnoreCase("web")) {
if (load.equalsIgnoreCase("web") && Config.isEmpty()) {
Listener.run();
}
if (Config.isGuiEnabled()) {
Expand All @@ -39,5 +40,10 @@ public static void runListener(String load) {
ConnectionPool.updateTable();
}
} , 0, 30, TimeUnit.SECONDS);
executor.scheduleAtFixedRate(() -> {
if (load.equalsIgnoreCase("web") && !Config.isEmpty()) {
Listener.run();
}
} , 0, 1, TimeUnit.SECONDS);
}
}
14 changes: 11 additions & 3 deletions src/main/java/me/chrommob/minestore/weblistener/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ public class Listener {

@SneakyThrows
public static void run() {
String link;
if (Config.getSecretKey().equalsIgnoreCase("") || Config.getSecretKey().equalsIgnoreCase("hard_secret_key_here")) {
link = Config.getApiUrl() + "servers/commands/queue";
} else {
link = Config.getApiUrl() + "servers/" + Config.getSecretKey() + "/commands/queue";
}
WebListenerObjects data = new WebListenerObjects();
String link = "https://pro.minestorecms.com/api/servers/" + Config.getSecretKey() + "/commands/queue";
URL url = new URL(link);
//Listening for commands
try {
urlConnection = (HttpsURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
Expand All @@ -44,17 +50,19 @@ public static void run() {
Command.online(commandWithoutPrefix);
}
post(data.getId());

} catch (Exception e) {
e.printStackTrace();
Config.setEmpty(true);
}
finally {
urlConnection.disconnect();
}
}


//Posting to the server that the command has been executed
@SneakyThrows
private static void post(int id) {
Config.setEmpty(false);
String link = "https://pro.minestorecms.com/api/servers/" + Config.getSecretKey() + "/commands/executed/" + id;
URL url = new URL(link);
urlConnection = (HttpsURLConnection) url.openConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ public class SocketHandler extends SimpleChannelInboundHandler<String> {

private final Gson gson = new Gson();


@Override
protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, String msg) {
final SocketObjects data = gson.fromJson(msg, SocketObjects.class);
//Removing "/" from the command
String commandWithoutPrefix = data.getCommand().replaceFirst("/", "");
commandWithoutPrefix = commandWithoutPrefix.replaceFirst(" ", " ");
//Checking if the password is correct
if (!data.getPassword().equalsIgnoreCase(Config.getPassword())) {
Bukkit.getLogger().info("[MineStore] Wrong password!");
return;
}
//Executing the command
if (Bukkit.getPlayer(data.getUsername()) == null && data.isPlayerOnlineNeeded()) {
Command.offline(data.getUsername(), commandWithoutPrefix);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# Official MineStore Plugin by ChromMob -------#
# Version: 1.0 ---------#
#--------------------------------------------------------#
# Modes: websocker or weblistener. #
# Modes: websocket or web-listener. #
# Websocket requires opened port. #
# Weblistener listens to your URL. #
# Web-listener listens to your URL. #
#------------------------------------------------#
mode: websocket
#------------------------------------------------#
Expand Down

0 comments on commit b179658

Please sign in to comment.