Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TFAGaming committed Dec 3, 2024
1 parent d06a048 commit d83e609
Show file tree
Hide file tree
Showing 39 changed files with 779 additions and 553 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

**SimpleShopGUI** is a simple and user-friendly Minecraft shop plugin based on GUIs. Players can browse a wide range of items, each organized into specific categories and types, making finding exactly what they're looking for easier.

The current database is **SQLite**, other providers are still unsupported.
Supported database providers: **SQLite**, **PostgreSQL**, **MySQL**

YouTube video: https://www.youtube.com/watch?v=2oxVJxw8HZE

Expand All @@ -21,19 +21,24 @@ YouTube video: https://www.youtube.com/watch?v=2oxVJxw8HZE
\*: **EssentialsX** or any other economy plugins that **Vault** currently supports.

## How to install
### From GitHub
Go to the [releases section](https://github.com/TFAGaming/SimpleShopGUI/releases), scroll down to find the version you want to install, click on **Assets** and then click on the **.jar** file.

Go to the [releases section](https://github.com/TFAGaming/SimpleShopGUI/releases), scroll down to find the version you want to install, click on **Assets** and then click on the **.jar** file. After the download completes, copy the **.jar** file, navigate to the plugins folder within your Minecraft server directory, and paste the file there. If your Minecraft server is currently running, you can use the command `/reload` to activate the plugin. To avoid any future problems, we advise stopping the server and then restarting it for a clean startup.
### From Spigot
Click on the **Download Now** button to download the latest version of the plugin. If you want to use older versions, [click here](https://www.spigotmc.org/resources/simpleshopgui.119478/updates).

After the download completes, copy the **.jar** file, navigate to the plugins folder within your Minecraft server directory, and paste the file there. If your Minecraft server is currently running, you can use the command `/reload` to activate the plugin. To avoid any future problems, we advise stopping the server and then restarting it for a clean startup.

## How it works

The plugin allows players to sell their items to any player at any price they want. Each item in the shop has its maximum duration; by default, each item expires after 7 days. You can change the duration in the **config.yml** file if you want.
The plugin allows players to sell their items with a fixed price, and other players chooses to buy the items or not. Each item in the shop has its maximum duration; by default, each item expires after 7 days. You can change the duration in the **config.yml** file if you want.

Expired items are not gone forever, they will stay as an item in the shop, but nobody can buy it. The expired items' sellers can get their items back.
Expired items are not gone forever, they will stay as an item in the shop, but nobody can buy them. The sellers of the expired items can get their items back using the command `/listed`.

## Commands

- `/shop (category)`: Opens a GUI with all available categories in the shop.
- `/listed`: View all items that you are currently selling and the expired ones.
- `/listed`: View all items that you are currently selling, including the expired items.
- `/sell [price]`: Sell an item that you are currently holding with your hand.

## Contributing
Expand Down
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>simpleshopgui</groupId>
<artifactId>simpleshopgui</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -25,6 +25,10 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>PostgreSQL</id>
<url> https://mvnrepository.com/artifact/postgresql/postgresq</url>
</repository>
</repositories>

<dependencies>
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/simpleshopgui/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import simpleshopgui.commands.SellCommand;
import simpleshopgui.commands.ShopCommand;
import simpleshopgui.database.Database;
import simpleshopgui.events.gui.NormalGUIListener;
import simpleshopgui.events.gui.GUIListener;
import simpleshopgui.events.gui.PaginationGUIListener;
import simpleshopgui.integrations.Vault;
import simpleshopgui.utils.console.Console;
Expand Down Expand Up @@ -47,29 +47,24 @@ public void onEnable() {
getDataFolder().mkdirs();
}

config = Plugin.getPlugin(Plugin.class).getConfig();
config = getConfig();

String provider = config.getString("database.provider");
String jdbcUrl = "";

if (provider.equalsIgnoreCase("sqlite")) {
jdbcUrl = getDataFolder().getAbsolutePath() + "/" + config.getString("database.path");
}

database = new Database(config.getString("database.provider"), jdbcUrl);
database = new Database(config.getString("database.provider"), this);

try {
database.getConnection();
database.prepareTables();

Console.info("Successfully connected to the database.");
Console.info("Successfully connected to the database (provider: " + provider + ").");
} catch (SQLException e) {
e.printStackTrace();
Console.error("Failed to connect to the database, disabling...");
Console.error("Failed to connect to the database (provider: " + provider + "), disabling...");
disablePlugin();
}

getServer().getPluginManager().registerEvents(new NormalGUIListener(), this);
getServer().getPluginManager().registerEvents(new GUIListener(), this);
getServer().getPluginManager().registerEvents(new PaginationGUIListener(), this);

getCommand("shop").setExecutor(new ShopCommand());
Expand All @@ -85,10 +80,16 @@ public void onDisable() {
}

public static String getVersion() {
return "1.1.0";
return "1.2.0";
}

private void disablePlugin() {
public void disablePlugin() {
try {
database.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
}

getServer().getPluginManager().disablePlugin(this);
}
}
13 changes: 11 additions & 2 deletions src/main/java/simpleshopgui/commands/ListedCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

import simpleshopgui.Plugin;
import simpleshopgui.gui.ListedItemsGUI;
import simpleshopgui.utils.colors.ChatColorTranslator;
import simpleshopgui.utils.players.PlayerUtils;
import simpleshopgui.managers.ShopDatabaseManager;
import simpleshopgui.utils.chat.ChatColorTranslator;
import simpleshopgui.utils.player.PlayerUtils;

public class ListedCommand implements TabExecutor {
@Override
Expand All @@ -26,6 +27,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return true;
}

List<List<Object>> listedItems = ShopDatabaseManager.getListedItemsByPlayer(player);

if (listedItems.size() == 0) {
player.sendMessage(ChatColorTranslator
.translate(Plugin.config.getString("messages.commands.listed.no_items_listed")));
return true;
}

ListedItemsGUI.create(player);

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/simpleshopgui/commands/SellCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

import simpleshopgui.Plugin;
import simpleshopgui.managers.ShopDatabaseManager;
import simpleshopgui.utils.colors.ChatColorTranslator;
import simpleshopgui.utils.players.PlayerUtils;
import simpleshopgui.utils.chat.ChatColorTranslator;
import simpleshopgui.utils.player.PlayerUtils;
import simpleshopgui.utils.shop.ShopUtils;

public class SellCommand implements TabExecutor {
Expand Down
42 changes: 21 additions & 21 deletions src/main/java/simpleshopgui/commands/ShopCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simpleshopgui.commands;

import java.util.List;
import java.util.Locale.Category;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
Expand All @@ -18,14 +19,11 @@
import simpleshopgui.gui.ShopGUINatural;
import simpleshopgui.gui.ShopGUIRedstone;
import simpleshopgui.gui.ShopGUITools;
import simpleshopgui.utils.colors.ChatColorTranslator;
import simpleshopgui.utils.players.PlayerUtils;
import simpleshopgui.utils.shop.ShopUtils;
import simpleshopgui.managers.PlayerGUIManager;
import simpleshopgui.utils.chat.ChatColorTranslator;
import simpleshopgui.utils.player.PlayerUtils;

public class ShopCommand implements TabExecutor {
private List<String> availableCategories = Lists.newArrayList("Blocks", "Tools", "Food", "Minerals", "Natural",
"Redstone", "Miscellaneous");

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Expand All @@ -38,39 +36,39 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}

if (args.length == 1) {
if (!availableCategories.contains(args[0])) {
if (!simpleshopgui.utils.shop.Category.getAllCategories(true).contains(args[0].toLowerCase())) {
player.sendMessage(ChatColorTranslator
.translate(Plugin.config.getString("messages.commands.shop.invalid_category")));
return true;
}

switch (args[0]) {
case "Blocks":
switch (args[0].toLowerCase()) {
case "blocks":
ShopGUIBuildingBlocks.create(player);
break;
case "Tools":
case "tools":
ShopGUITools.create(player);
break;
case "Food":
case "food":
ShopGUIFood.create(player);
break;
case "Minerals":
case "minerals":
ShopGUIMinerals.create(player);
break;
case "Natural":
case "natural":
ShopGUINatural.create(player);
break;
case "Redstone":
case "redstone":
ShopGUIRedstone.create(player);
break;
case "Miscellaneous":
case "miscellaneous":
ShopGUIMiscellaneous.create(player);
break;
default:
break;
}

ShopUtils.playerTriggerEvent.put(player.getUniqueId(), true);
PlayerGUIManager.playerTriggerEvent.put(player.getUniqueId(), true);
} else {
ShopGUI gui = new ShopGUI(player);

Expand All @@ -85,20 +83,22 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
List<String> categories = simpleshopgui.utils.shop.Category.getAllCategories(false);

if (args.length == 1) {
if (args[0].length() <= 0) {
return availableCategories;
return categories;
}

List<String> finalList = Lists.newArrayList();
List<String> returnList = Lists.newArrayList();

for (String category : availableCategories) {
for (String category : categories) {
if (category.toLowerCase().startsWith(args[0].toLowerCase())) {
finalList.add(category);
returnList.add(category);
}
}

return finalList;
return returnList;
}

return Lists.newArrayList();
Expand Down
97 changes: 70 additions & 27 deletions src/main/java/simpleshopgui/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,58 @@
import java.sql.SQLException;
import java.sql.Statement;

import simpleshopgui.Plugin;
import simpleshopgui.managers.ShopDatabaseManager;

public class Database {
public Connection connection = null;
private final String provider;
private final String jdbcUrl;
private final Plugin plugin;

public Database(String provider, String jdbcUrl) {
public Database(String provider, Plugin plugin) {
this.provider = provider;
this.jdbcUrl = jdbcUrl;
this.plugin = plugin;
}

public Connection getConnection() throws SQLException {
if (this.connection == null) {
if (provider.equalsIgnoreCase("sqlite")) {
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + jdbcUrl);
String sqlitePath = "";

if (provider.equalsIgnoreCase("sqlite")) {
sqlitePath = this.plugin.getDataFolder().getAbsolutePath() + "/"
+ this.plugin.getConfig().getString("database.sqlite.path");
}

String url = "jdbc:sqlite:" + sqlitePath;

Connection connection = DriverManager.getConnection(url);
this.connection = connection;
} else if (provider.equalsIgnoreCase("postgresql")) {
String host = Plugin.config.getString("database.postgresql.host");
int port = Plugin.config.getInt("database.postgresql.port");
String database = Plugin.config.getString("database.postgresql.database");
String username = Plugin.config.getString("database.postgresql.username");
String password = Plugin.config.getString("database.postgresql.password");

String url = "jdbc:postgresql://" + host + ":" + port + "/" + database;

Connection connection = DriverManager.getConnection(url, username, password);
this.connection = connection;
} /*
* else if (provider.equalsIgnoreCase("postgresql")) {
* String host = Plugin.config.getString("database.host");
* int port = Plugin.config.getInt("database.port");
* String database = Plugin.config.getString("database.name");
* String username = Plugin.config.getString("database.username");
* String password = Plugin.config.getString("database.password");
*
* Connection connection = DriverManager.getConnection("jdbc:postgresql://" +
* host + ":" + port + "/" + database, username, password);
* this.connection = connection;
* }
*/ else {
throw new SQLException("Invalid provider \"" + provider + "\"");
} else if (provider.equalsIgnoreCase("mysql")) {
String host = Plugin.config.getString("database.mysql.host");
int port = Plugin.config.getInt("database.mysql.port");
String database = Plugin.config.getString("database.mysql.database");
String username = Plugin.config.getString("database.mysql.username");
String password = Plugin.config.getString("database.mysql.password");

String url = "jdbc:mysql://" + host + ":" + port + "/" + database;

Connection connection = DriverManager.getConnection(url, username, password);
this.connection = connection;
} else {
throw new SQLException(
"Invalid provider \"" + provider + "\", valid providers: sqlite, postgresql, mysql");
}
}

Expand All @@ -50,15 +71,37 @@ public void closeConnection() throws SQLException {
}

public void prepareTables() throws SQLException {
executeStatement("CREATE TABLE IF NOT EXISTS sold_items (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"player_uuid VARCHAR(36) NOT NULL," +
"item_data TEXT NOT NULL," +
"price BIGINT NOT NULL," +
"created_at BIGINT NOT NULL," +
"expires BIGINT NOT NULL," +
"category TEXT NOT NULL" +
");");
if (provider.equalsIgnoreCase("sqlite")) {
executeStatement("CREATE TABLE IF NOT EXISTS shop_items (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"player_uuid VARCHAR(36) NOT NULL," +
"item_data TEXT NOT NULL," +
"price BIGINT NOT NULL," +
"created_at BIGINT NOT NULL," +
"expires_at BIGINT NOT NULL," +
"category INTEGER NOT NULL" +
");");
} else if (provider.equalsIgnoreCase("postgresql")) {
executeStatement("CREATE TABLE IF NOT EXISTS shop_items (" +
"id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY," +
"player_uuid VARCHAR(36) NOT NULL," +
"item_data TEXT NOT NULL," +
"price BIGINT NOT NULL," +
"created_at BIGINT NOT NULL," +
"expires_at BIGINT NOT NULL," +
"category INTEGER NOT NULL" +
");");
} else if (provider.equalsIgnoreCase("mysql")) {
executeStatement("CREATE TABLE IF NOT EXISTS shop_items (" +
"id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY," +
"player_uuid VARCHAR(36) NOT NULL," +
"item_data TEXT NOT NULL," +
"price BIGINT NOT NULL," +
"created_at BIGINT NOT NULL," +
"expires_at BIGINT NOT NULL," +
"category TINYINT UNSIGNED NOT NULL" +
");");
}

ShopDatabaseManager.updateCache();
}
Expand Down
Loading

0 comments on commit d83e609

Please sign in to comment.