Skip to content

Commit

Permalink
Fixed MySQL issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Smudgge committed Jun 6, 2024
1 parent 52b1979 commit 3185c84
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 15 deletions.
12 changes: 11 additions & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.smuddgge</groupId>
<artifactId>Leaf</artifactId>
<name>Leaf</name>
<version>5.1.1</version>
<version>5.2.0</version>
<description>A velocity utility plugin</description>
<build>
<resources>
Expand All @@ -27,6 +27,16 @@
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<artifactSet>
<excludes>
<exclude>jupiter:*</exclude>
<exclude>spark-api:*</exclude>
<exclude>dev.simplix:*</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.smuddgge</groupId>
<artifactId>Leaf</artifactId>
<version>5.1.1</version>
<version>5.2.0</version>
<packaging>jar</packaging>

<name>Leaf</name>
Expand Down Expand Up @@ -97,7 +97,7 @@
<dependency>
<groupId>com.github.smuddgge</groupId>
<artifactId>SquishyDatabase</artifactId>
<version>3.5.0</version>
<version>4.0.0</version>
</dependency>

<!-- Configuration API -->
Expand Down Expand Up @@ -239,6 +239,16 @@
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<artifactSet>
<excludes>
<exclude>jupiter:*</exclude>
<exclude>spark-api:*</exclude>
<exclude>dev.simplix:*</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/github/smuddgge/leaf/Leaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.github.smuddgge.squishyconfiguration.implementation.yaml.YamlConfiguration;
import com.github.smuddgge.squishyconfiguration.interfaces.Configuration;
import com.github.smuddgge.squishydatabase.DatabaseBuilder;
import com.github.smuddgge.squishydatabase.DatabaseCredentials;
import com.github.smuddgge.squishydatabase.DatabaseFactory;
import com.github.smuddgge.squishydatabase.Query;
import com.github.smuddgge.squishydatabase.console.Console;
import com.github.smuddgge.squishydatabase.interfaces.Database;
Expand All @@ -50,13 +48,12 @@
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;

@Plugin(
id = "leaf",
name = "Leaf",
version = "5.0.1",
version = "5.2.0",
description = "A velocity utility plugin",
authors = {"Smudge"}
)
Expand Down Expand Up @@ -356,6 +353,12 @@ public static boolean isDatabaseDisabled() {
*/
public static void setupDatabase(File folder) {

try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}

// Set up the database.
if (!ConfigDatabase.get().getBoolean("enabled", true)) {
Leaf.database = null;
Expand Down Expand Up @@ -396,6 +399,9 @@ public static void setupDatabase(File folder) {

// Version 4.3.0
Leaf.database.createTable(new CommandLimitTable());

// Version 5.2.0
Leaf.database.createTable(new CommandCooldownTable());
}

/**
Expand Down
62 changes: 58 additions & 4 deletions src/main/java/com/github/smuddgge/leaf/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.smuddgge.leaf.MessageManager;
import com.github.smuddgge.leaf.configuration.ConfigMessages;
import com.github.smuddgge.leaf.configuration.ConfigurationManager;
import com.github.smuddgge.leaf.database.tables.CommandCooldownTable;
import com.github.smuddgge.leaf.database.tables.CommandLimitTable;
import com.github.smuddgge.leaf.datatype.User;
import com.github.smuddgge.leaf.dependencys.ProtocolizeDependency;
Expand All @@ -14,6 +15,7 @@
import com.velocitypowered.api.proxy.Player;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -119,6 +121,9 @@ public CommandStatus onPlayerRun(String[] arguments, User user) {
// Check the command limit.
if (this.isLimited(user)) return new CommandStatus().isLimited();

// Check the command cooldown.
if (this.isOnCooldown(user)) return new CommandStatus().isOnCooldown();

// Play sound.
if (this.getSound() != null || Objects.equals(this.getSound(), "")) {
try {
Expand All @@ -132,7 +137,9 @@ public CommandStatus onPlayerRun(String[] arguments, User user) {
// Check if there are no sub command types.
if (this.commandType.getSubCommandTypes().isEmpty()
|| arguments.length <= 0)
return this.commandType.onPlayerRun(this.getSection(), arguments, user).increaseExecutions(user, this);
return this.commandType.onPlayerRun(this.getSection(), arguments, user)
.increaseExecutions(user, this)
.updateCooldownTimeStamp(user, this);

// Otherwise, check if it is a sub command.
for (CommandType commandType : this.commandType.getSubCommandTypes()) {
Expand All @@ -143,10 +150,14 @@ public CommandStatus onPlayerRun(String[] arguments, User user) {
subCommandNames.addAll(this.getSection().getSection(commandType.getName()).getListString("aliases", new ArrayList<>()));

if (subCommandNames.contains(name))
return commandType.onPlayerRun(this.getSection(), arguments, user).increaseExecutions(user, this);
return commandType.onPlayerRun(this.getSection(), arguments, user)
.increaseExecutions(user, this)
.updateCooldownTimeStamp(user, this);
}

return this.commandType.onPlayerRun(this.getSection(), arguments, user).increaseExecutions(user, this);
return this.commandType.onPlayerRun(this.getSection(), arguments, user)
.increaseExecutions(user, this)
.updateCooldownTimeStamp(user, this);
}

/**
Expand Down Expand Up @@ -259,7 +270,8 @@ public boolean isLimited(@NotNull User user) {
// Check if the database is disabled.
// We return true because the database may have disabled its self
// and the admin may still want commands to be limited.
if (Leaf.isDatabaseDisabled()) return true;
// -> If the admin is not using a database, they will set the limit to -1.
if (Leaf.isDatabaseDisabled()) return false;

int amountExecuted = Leaf.getDatabase()
.getTable(CommandLimitTable.class)
Expand All @@ -270,6 +282,25 @@ public boolean isLimited(@NotNull User user) {
return amountExecuted >= limit;
}

private boolean isOnCooldown(@NotNull User user) {

long cooldown = ConfigurationManager.getCommands().getCommandCooldown(this.identifier);

if (cooldown == -1) return false;

// Check if the database is disabled.
// We return true because the database may have disabled its self
// and the admin may still want commands to be limited.
// -> If the admin is not using a database, they will set the cooldown to -1.
if (Leaf.isDatabaseDisabled()) return true;

long lastExecutedTimeStamp = Leaf.getDatabase()
.getTable(CommandCooldownTable.class)
.getExecutedTimeStamp(user.getUniqueId(), this.identifier);

return (lastExecutedTimeStamp + cooldown) > System.currentTimeMillis();
}

/**
* Used to check if the command has a limit.
*
Expand All @@ -284,6 +315,23 @@ public boolean hasLimit() {
return limit != -1;
}

public boolean hasCooldown() {
long cooldown = ConfigurationManager.getCommands().getCommandCooldown(this.identifier);
return cooldown != -1;
}

public @NotNull String getCooldown(@NotNull User user) {
long cooldown = ConfigurationManager.getCommands().getCommandCooldown(this.identifier);

if (cooldown == -1) return "0";

long lastCooldownTimeStamp = Leaf.getDatabase()
.getTable(CommandCooldownTable.class)
.getExecutedTimeStamp(user.getUniqueId(), this.getIdentifier());

return String.valueOf(Duration.ofMillis((lastCooldownTimeStamp + cooldown) - System.currentTimeMillis()).toSeconds());
}

@Override
public void execute(final Invocation invocation) {
CommandSource source = invocation.source();
Expand All @@ -299,6 +347,12 @@ public void execute(final Invocation invocation) {
.replace("[name]", this.getName()));
}

if (status.hasOnCooldown()) {
user.sendMessage(ConfigMessages.getOnCooldown()
.replace("%cooldown%", this.getCooldown(user))
);
}

String message = status.getMessage();
if (message == null) return;
user.sendMessage(message);
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/github/smuddgge/leaf/commands/CommandStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.smuddgge.leaf.Leaf;
import com.github.smuddgge.leaf.configuration.ConfigMessages;
import com.github.smuddgge.leaf.database.tables.CommandCooldownTable;
import com.github.smuddgge.leaf.database.tables.CommandLimitTable;
import com.github.smuddgge.leaf.datatype.User;
import net.dv8tion.jda.api.entities.Member;
Expand All @@ -20,6 +21,7 @@ public class CommandStatus {
private boolean hasPlayerCommand = false;
private boolean hasNoPermission = false;
private boolean hasIsLimited = false;
private boolean hasOnCooldown = false;

private boolean hasStopIncreaseLimit = false;

Expand Down Expand Up @@ -93,6 +95,11 @@ public CommandStatus isLimited() {
return this;
}

public CommandStatus isOnCooldown() {
this.hasOnCooldown = true;
return this;
}

/**
* Used to stop the limit from increasing.
*
Expand Down Expand Up @@ -168,6 +175,10 @@ public boolean hasIsLimited() {
return this.hasIsLimited;
}

public boolean hasOnCooldown() {
return this.hasOnCooldown;
}

/**
* Used to check if the increase of the command limit
* should be stopped.
Expand Down Expand Up @@ -242,4 +253,23 @@ public String getMessage() {

return this;
}

public @NotNull CommandStatus updateCooldownTimeStamp(@NotNull User user, @NotNull Command command) {
if (this.hasOnCooldown()) return this;
if (!command.hasCooldown()) return this;
user.updateCooldownTimeStamp(command.getIdentifier());
return this;
}

public @NotNull CommandStatus updateCooldownTimeStamp(@NotNull Member member, @NotNull Command command) {
if (this.hasOnCooldown()) return this;
if (command.getSection().getSection("discord_bot").getLong("cooldown", -1) == -1) return this;
if (Leaf.isDatabaseDisabled()) return this;

Leaf.getDatabase()
.getTable(CommandCooldownTable.class)
.updateExecutedTimeStamp(member, command.getIdentifier());

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ public static String getIsLimited() {
return ConfigMessages.config.getSection("messages")
.getString("is_limited", "{error_colour}You cannot execute this command anymore as you have reached the limit.");
}

public static String getOnCooldown() {
return ConfigMessages.config.getSection("messages")
.getString("on_cooldown", "{error_colour}Please wait %cooldown% before executing this command again.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public int getCommandLimit(String identifier) {
return this.getCommand(identifier).getInteger("limit", -1);
}

public long getCommandCooldown(String identifier) {
return this.getCommand(identifier).getLong("cooldown", -1);
}

/**
* Used to get the first command with a certain
* command type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.smuddgge.leaf.database.records;

import com.github.smuddgge.squishydatabase.record.Field;
import com.github.smuddgge.squishydatabase.record.Record;
import com.github.smuddgge.squishydatabase.record.RecordFieldType;
import org.jetbrains.annotations.NotNull;

public class CommandCooldownRecord extends Record {

@Field(type = RecordFieldType.PRIMARY)
public String primaryKey;
public String lastExecutedTimeStamp;

public long getLastExecutedTimeStamp() {
return Long.parseLong(this.lastExecutedTimeStamp);
}

public @NotNull CommandCooldownRecord setLastExecutedTimeStamp(final long lastExecutedTimeStamp) {
this.lastExecutedTimeStamp = Long.toString(lastExecutedTimeStamp);
return this;
}
}
Loading

0 comments on commit 3185c84

Please sign in to comment.