Upload progress (changes will be in next commit)

This commit is contained in:
ceze88 2023-06-05 16:25:37 +02:00
parent 5a9964c45e
commit ff8cc1ddb2
27 changed files with 223 additions and 360 deletions

View File

@ -6,12 +6,13 @@
<parent>
<groupId>com.craftaro</groupId>
<artifactId>UltimateStackerParent</artifactId>
<version>${revision}</version>
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>Bootstrap</artifactId>
<build>
<finalName>UltimateStacker-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -57,7 +58,7 @@
<dependency>
<groupId>com.craftaro</groupId>
<artifactId>UltimateStacker</artifactId>
<version>${revision}</version>
<version>3.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.craftaro</groupId>
<artifactId>UltimateStackerParent</artifactId>
<version>${revision}</version>
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>UltimateStacker</artifactId>
@ -114,7 +114,7 @@
<dependency>
<groupId>com.songoda</groupId>
<artifactId>SongodaCore</artifactId>
<version>2.6.21</version>
<version>2.6.23-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

View File

@ -16,7 +16,6 @@ import com.craftaro.ultimatestacker.commands.CommandReload;
import com.craftaro.ultimatestacker.commands.CommandRemoveAll;
import com.craftaro.ultimatestacker.commands.CommandSettings;
import com.craftaro.ultimatestacker.commands.CommandSpawn;
import com.craftaro.ultimatestacker.database.DataManager;
import com.craftaro.ultimatestacker.database.migrations._1_InitialMigration;
import com.craftaro.ultimatestacker.database.migrations._2_EntityStacks;
import com.craftaro.ultimatestacker.database.migrations._3_BlockStacks;
@ -54,10 +53,7 @@ import com.songoda.core.commands.CommandManager;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.configuration.Config;
import com.songoda.core.database.DataMigrationManager;
import com.songoda.core.database.DatabaseConnector;
import com.songoda.core.database.MySQLConnector;
import com.songoda.core.database.SQLiteConnector;
import com.songoda.core.database.DataManager;
import com.songoda.core.gui.GuiManager;
import com.songoda.core.hooks.EntityStackerManager;
import com.songoda.core.hooks.HologramManager;
@ -98,10 +94,6 @@ public class UltimateStacker extends SongodaPlugin {
private CommandManager commandManager;
private CustomEntityManager customEntityManager;
private StackingTask stackingTask;
private DatabaseConnector databaseConnector;
private DataMigrationManager dataMigrationManager;
private DataManager dataManager;
private UltimateStackerAPI API;
public static UltimateStacker getInstance() {
@ -120,8 +112,8 @@ public class UltimateStacker extends SongodaPlugin {
public void onPluginDisable() {
this.stackingTask.stop();
this.stackingTask = null;
this.dataManager.bulkUpdateSpawners(this.spawnerStackManager.getStacks());
this.dataManager.bulkUpdateBlocks(this.blockStackManager.getStacks());
this.dataManager.saveBatch(this.spawnerStackManager.getStacksData());
this.dataManager.saveBatch(this.blockStackManager.getStacksData());
HologramManager.removeAllHolograms();
Async.shutdown();
}
@ -229,37 +221,9 @@ public class UltimateStacker extends SongodaPlugin {
HologramManager.load(this);
EntityStackerManager.load();
// Database stuff, go!
try {
if (Settings.MYSQL_ENABLED.getBoolean()) {
String hostname = Settings.MYSQL_HOSTNAME.getString();
int port = Settings.MYSQL_PORT.getInt();
String database = Settings.MYSQL_DATABASE.getString();
String username = Settings.MYSQL_USERNAME.getString();
String password = Settings.MYSQL_PASSWORD.getString();
boolean useSSL = Settings.MYSQL_USE_SSL.getBoolean();
int poolSize = Settings.MYSQL_POOL_SIZE.getInt();
initDatabase(Arrays.asList(new _1_InitialMigration(), new _2_EntityStacks(), new _3_BlockStacks(), new _6_RemoveStackedEntityTable()));
this.databaseConnector = new MySQLConnector(this, hostname, port, database, username, password, useSSL, poolSize);
this.getLogger().info("Data handler connected using MySQL.");
} else {
this.databaseConnector = new SQLiteConnector(this);
this.getLogger().info("Data handler connected using SQLite.");
}
} catch (Exception ex) {
this.getLogger().severe("Fatal error trying to connect to database. Please make sure all your connection settings are correct and try again. Plugin has been disabled.");
Bukkit.getPluginManager().disablePlugin(this);
}
this.dataManager = new DataManager(this.databaseConnector, this);
this.dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager,
new _1_InitialMigration(),
new _2_EntityStacks(),
new _3_BlockStacks(),
new _6_RemoveStackedEntityTable());
this.dataMigrationManager.runMigrations();
API = new UltimateStackerAPI(entityStackManager, stackedItemManager, spawnerStackManager, blockStackManager);
API = new UltimateStackerAPI(this, entityStackManager, stackedItemManager, spawnerStackManager, blockStackManager, new Settings());
}
@Override
@ -270,30 +234,27 @@ public class UltimateStacker extends SongodaPlugin {
// Load current data.
final boolean useSpawnerHolo = Settings.SPAWNER_HOLOGRAMS.getBoolean();
this.dataManager.getSpawners((spawners) -> {
this.spawnerStackManager.addSpawners(spawners);
this.dataManager.loadBatch(SpawnerStackImpl.class, "spawners").forEach((data) -> {
SpawnerStack spawner = (SpawnerStack) data;
this.spawnerStackManager.addSpawner(spawner);
if (useSpawnerHolo) {
if (spawners.isEmpty()) return;
for (SpawnerStack spawner : spawners.values()) {
if (spawner == null) return;
if (spawner.getLocation() == null) return;
if (spawner.getLocation().getWorld() != null) {
updateHologram(spawner);
}
}
}
});
this.stackingTask = new StackingTask(this);
final boolean useBlockHolo = Settings.BLOCK_HOLOGRAMS.getBoolean();
this.dataManager.getBlocks((blocks) -> {
this.blockStackManager.addBlocks(blocks);
this.dataManager.loadBatch(BlockStackImpl.class, "blocks").forEach((data) -> {
BlockStack blockStack = (BlockStack) data;
this.blockStackManager.addBlock(blockStack);
if (useBlockHolo) {
if (blocks.isEmpty()) return;
for (BlockStack stack : blocks.values()) {
if (stack.getLocation().getWorld() != null) {
updateHologram(stack);
}
if (blockStack == null) return;
if (blockStack.getLocation().getWorld() != null) {
updateHologram(blockStack);
}
}
});
@ -362,11 +323,7 @@ public class UltimateStacker extends SongodaPlugin {
return spawnerFile;
}
public DatabaseConnector getDatabaseConnector() {
return databaseConnector;
}
public DataManager getDataManager() {
public DataManager getPluginDataManager() {
return dataManager;
}

View File

@ -1,243 +0,0 @@
package com.craftaro.ultimatestacker.database;
import com.craftaro.ultimatestacker.api.stack.block.BlockStack;
import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStack;
import com.craftaro.ultimatestacker.stackable.block.BlockStackImpl;
import com.craftaro.ultimatestacker.stackable.spawner.SpawnerStackImpl;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.database.DataManagerAbstract;
import com.songoda.core.database.DatabaseConnector;
import com.songoda.core.database.DatabaseType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
public class DataManager extends DataManagerAbstract {
public DataManager(DatabaseConnector databaseConnector, Plugin plugin) {
super(databaseConnector, plugin);
}
public void bulkUpdateSpawners(Collection<SpawnerStack> spawnerStacks) {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updateSpawner);
for (SpawnerStack spawnerStack : spawnerStacks) {
statement.setInt(1, spawnerStack.getAmount());
statement.setInt(2, spawnerStack.getId());
statement.addBatch();
}
statement.executeBatch();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void updateSpawner(SpawnerStack spawnerStack) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updateSpawner);
statement.setInt(1, spawnerStack.getAmount());
statement.setInt(2, spawnerStack.getId());
statement.executeUpdate();
} catch (SQLException exception) {
exception.printStackTrace();
}
});
}
public void createSpawner(SpawnerStack spawnerStack) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String createSpawner = "INSERT " + getSyntax("INTO ", DatabaseType.MYSQL) + getSyntax("OR REPLACE INTO ", DatabaseType.SQLITE) + this.getTablePrefix() + "spawners (amount, world, x, y, z) VALUES (?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(createSpawner);
statement.setInt(1, spawnerStack.getAmount());
statement.setString(2, spawnerStack.getWorld().getName());
statement.setInt(3, spawnerStack.getX());
statement.setInt(4, spawnerStack.getY());
statement.setInt(5, spawnerStack.getZ());
statement.executeUpdate();
int spawnerId = this.lastInsertedId(connection, "spawners");
this.sync(() -> spawnerStack.setId(spawnerId));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void updateBlock(BlockStack blockStack) {
if (blockStack.getAmount() == 0) return;
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateBlock = "UPDATE " + this.getTablePrefix() + "blocks SET amount = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updateBlock);
if (blockStack.getAmount() == 0) return;
statement.setInt(1, blockStack.getAmount());
statement.setInt(2, blockStack.getId());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void createBlock(BlockStack blockStack) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String createSpawner = "INSERT " + getSyntax("INTO ", DatabaseType.MYSQL) + getSyntax("OR REPLACE INTO ", DatabaseType.SQLITE) + this.getTablePrefix() + "blocks (amount, material, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(createSpawner);
statement.setInt(1, blockStack.getAmount());
statement.setString(2, blockStack.getMaterial().name());
statement.setString(3, blockStack.getLocation().getWorld().getName());
statement.setInt(4, (int) blockStack.getLocation().getX());
statement.setInt(5, (int) blockStack.getLocation().getY());
statement.setInt(6, (int) blockStack.getLocation().getZ());
statement.executeUpdate();
int blockId = this.lastInsertedId(connection, "blocks");
this.sync(() -> ((BlockStackImpl)blockStack).setId(blockId));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void deleteSpawner(SpawnerStack spawnerStack) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String deleteSpawner = "DELETE FROM " + this.getTablePrefix() + "spawners WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(deleteSpawner);
statement.setInt(1, spawnerStack.getId());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void deleteBlock(BlockStackImpl blockStack) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String deleteBlock = "DELETE FROM " + this.getTablePrefix() + "blocks WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(deleteBlock);
statement.setInt(1, blockStack.getId());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void getSpawners(Consumer<Map<Location, SpawnerStack>> callback) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String selectSpawners = "SELECT * FROM " + this.getTablePrefix() + "spawners";
Map<Location, SpawnerStack> spawners = new HashMap<>();
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery(selectSpawners);
while (result.next()) {
World world = Bukkit.getWorld(result.getString("world"));
if (world == null)
continue;
int spawnerId = result.getInt("id");
int amount = result.getInt("amount");
int x = result.getInt("x");
int y = result.getInt("y");
int z = result.getInt("z");
Location location = new Location(world, x, y, z);
SpawnerStack spawnerStack = new SpawnerStackImpl(location, amount);
spawnerStack.setId(spawnerId);
spawners.put(location, spawnerStack);
}
} catch (Exception e) {
e.printStackTrace();
}
this.sync(() -> callback.accept(spawners));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void getBlocks(Consumer<Map<Location, BlockStack>> callback) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String selectBlocks = "SELECT * FROM " + this.getTablePrefix() + "blocks";
Map<Location, BlockStack> blocks = new HashMap<>();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(selectBlocks);
while (result.next()) {
World world = Bukkit.getWorld(result.getString("world"));
if (world == null)
continue;
int blockId = result.getInt("id");
CompatibleMaterial material = CompatibleMaterial.getMaterial(result.getString("material"));
int amount = result.getInt("amount");
int x = result.getInt("x");
int y = result.getInt("y");
int z = result.getInt("z");
Location location = new Location(world, x, y, z);
BlockStackImpl blockStack = new BlockStackImpl(material, location, amount);
blockStack.setId(blockId);
if (amount == 0) {
//remove from database
this.deleteBlock(blockStack);
continue;
}
blocks.put(location, blockStack);
this.sync(() -> callback.accept(blocks));
}
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void bulkUpdateBlocks(Collection<BlockStack> stacks) {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateSpawner = "UPDATE " + this.getTablePrefix() + "blocks SET amount = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updateSpawner);
for (BlockStack spawnerStack : stacks) {
statement.setInt(1, spawnerStack.getAmount());
statement.setInt(2, spawnerStack.getId());
statement.addBatch();
}
statement.executeBatch();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

View File

@ -2,6 +2,7 @@ package com.craftaro.ultimatestacker.database.migrations;
import com.craftaro.ultimatestacker.UltimateStacker;
import com.songoda.core.database.DataMigration;
import com.songoda.core.database.DatabaseConnector;
import com.songoda.core.database.MySQLConnector;
import java.sql.Connection;
@ -15,11 +16,11 @@ public class _1_InitialMigration extends DataMigration {
}
@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
String autoIncrement = UltimateStacker.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
public void migrate(DatabaseConnector connector, String tablePrefix) throws SQLException {
String autoIncrement = connector instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
// Create spawners table
try (Statement statement = connection.createStatement()) {
try (Statement statement = connector.getConnection().createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS " + tablePrefix + "spawners (" +
"id INTEGER PRIMARY KEY" + autoIncrement + ", " +
"amount INTEGER NOT NULL," +

View File

@ -2,6 +2,7 @@ package com.craftaro.ultimatestacker.database.migrations;
import com.craftaro.ultimatestacker.UltimateStacker;
import com.songoda.core.database.DataMigration;
import com.songoda.core.database.DatabaseConnector;
import com.songoda.core.database.MySQLConnector;
import java.sql.Connection;
@ -15,11 +16,11 @@ public class _2_EntityStacks extends DataMigration {
}
@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
String autoIncrement = UltimateStacker.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
public void migrate(DatabaseConnector connector, String tablePrefix) throws SQLException {
String autoIncrement = connector instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
// Create host entities table
try (Statement statement = connection.createStatement()) {
try (Statement statement = connector.getConnection().createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS " + tablePrefix + "host_entities (" +
"id INTEGER PRIMARY KEY" + autoIncrement + ", " +
"uuid VARCHAR(36) NOT NULL," +

View File

@ -2,6 +2,7 @@ package com.craftaro.ultimatestacker.database.migrations;
import com.craftaro.ultimatestacker.UltimateStacker;
import com.songoda.core.database.DataMigration;
import com.songoda.core.database.DatabaseConnector;
import com.songoda.core.database.MySQLConnector;
import java.sql.Connection;
@ -15,11 +16,11 @@ public class _3_BlockStacks extends DataMigration {
}
@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
String autoIncrement = UltimateStacker.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
public void migrate(DatabaseConnector connector, String tablePrefix) throws SQLException {
String autoIncrement = connector instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
// Create blocks table
try (Statement statement = connection.createStatement()) {
try (Statement statement = connector.getConnection().createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS " + tablePrefix + "blocks (" +
"id INTEGER PRIMARY KEY" + autoIncrement + ", " +
"amount INTEGER NOT NULL," +

View File

@ -1,6 +1,7 @@
package com.craftaro.ultimatestacker.database.migrations;
import com.songoda.core.database.DataMigration;
import com.songoda.core.database.DatabaseConnector;
import java.sql.Connection;
import java.sql.SQLException;
@ -13,9 +14,9 @@ public class _6_RemoveStackedEntityTable extends DataMigration {
}
@Override
public void migrate(Connection connection, String tablePrefix) {
try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE " + tablePrefix + "stacked_entities");
public void migrate(DatabaseConnector connector, String tablePrefix) {
try (Statement statement = connector.getConnection().createStatement()) {
statement.execute("DROP TABLE IF EXISTS " + tablePrefix + "stacked_entities");
} catch (SQLException e) {
e.printStackTrace();
}

View File

@ -129,7 +129,7 @@ public class BlockListeners implements Listener {
}
//update hologram
plugin.updateHologram(stack);
plugin.getDataManager().updateBlock(stack);
plugin.getPluginDataManager().save(stack);
return;
} else {
if (isSneaking) return;
@ -143,7 +143,7 @@ public class BlockListeners implements Listener {
hand.takeItem(player, 1);
}
BlockStack newStack = blockStackManager.createBlock(block);
plugin.getDataManager().createBlock(newStack);
plugin.getPluginDataManager().save(newStack);
plugin.updateHologram(newStack);
}
return;
@ -232,7 +232,7 @@ public class BlockListeners implements Listener {
}
SpawnerStack stack = UltimateStackerAPI.getSpawnerStackManager().addSpawner(new SpawnerStackImpl(block.getLocation(), amount));
plugin.getDataManager().createSpawner(stack);
plugin.getPluginDataManager().save(stack);
cs.setSpawnedType(cs2.getSpawnedType());
cs.update();
@ -279,7 +279,7 @@ public class BlockListeners implements Listener {
event.setCancelled(false);
plugin.removeHologram(stack);
SpawnerStack spawnerStack = UltimateStackerAPI.getSpawnerStackManager().removeSpawner(block.getLocation());
plugin.getDataManager().deleteSpawner(spawnerStack);
plugin.getPluginDataManager().delete(spawnerStack);
} else {
stack.setAmount(stack.getAmount() - 1);
plugin.updateHologram(stack);

View File

@ -138,7 +138,7 @@ public class EntityListeners implements Listener {
spawnLocation.getWorld().dropItemNaturally(spawnLocation.clone().add(.5, 0, .5), item);
SpawnerStack spawnerStack = UltimateStackerAPI.getSpawnerStackManager().removeSpawner(spawnLocation);
plugin.getDataManager().deleteSpawner(spawnerStack);
plugin.getPluginDataManager().delete(spawnerStack);
plugin.removeHologram(spawnerStack);
}
}

View File

@ -11,7 +11,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class Settings {
public class Settings implements com.craftaro.ultimatestacker.api.Settings {
static final Config config = UltimateStacker.getInstance().getCoreConfig();
@ -275,16 +275,6 @@ public class Settings {
"The enabled language file.",
"More language files (if available) can be found in the plugins data folder.");
public static final ConfigSetting MYSQL_ENABLED = new ConfigSetting(config, "MySQL.Enabled", false,
"Set to 'true' to use MySQL instead of SQLite for data storage.");
public static final ConfigSetting MYSQL_HOSTNAME = new ConfigSetting(config, "MySQL.Hostname", "localhost");
public static final ConfigSetting MYSQL_PORT = new ConfigSetting(config, "MySQL.Port", 3306);
public static final ConfigSetting MYSQL_DATABASE = new ConfigSetting(config, "MySQL.Database", "your-database");
public static final ConfigSetting MYSQL_USERNAME = new ConfigSetting(config, "MySQL.Username", "user");
public static final ConfigSetting MYSQL_PASSWORD = new ConfigSetting(config, "MySQL.Password", "pass");
public static final ConfigSetting MYSQL_USE_SSL = new ConfigSetting(config, "MySQL.Use SSL", false);
public static final ConfigSetting MYSQL_POOL_SIZE = new ConfigSetting(config, "MySQL.Pool Size", 3, "Determines the number of connections the pool is using. Increase this value if you are getting timeout errors when more players online.");
public static void setupConfig() {
config.load();
config.setAutoremove(true).setAutosave(true);
@ -302,4 +292,9 @@ public class Settings {
config.saveChanges();
}
@Override
public int getMaxItemStackSize() {
return MAX_STACK_ITEMS.getInt();
}
}

View File

@ -4,9 +4,13 @@ import com.craftaro.ultimatestacker.UltimateStacker;
import com.craftaro.ultimatestacker.api.stack.block.BlockStack;
import com.craftaro.ultimatestacker.settings.Settings;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.database.Data;
import com.songoda.core.database.SerializedLocation;
import com.songoda.core.utils.TextUtils;
import org.bukkit.Location;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class BlockStackImpl implements BlockStack {
@ -20,8 +24,8 @@ public class BlockStackImpl implements BlockStack {
private int id;
private int amount = 1;
private final CompatibleMaterial material;
private final Location location;
private CompatibleMaterial material;
private Location location;
public BlockStackImpl(CompatibleMaterial material, Location location) {
this.material = material;
@ -39,6 +43,30 @@ public class BlockStackImpl implements BlockStack {
return id;
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
map.put("id", id);
map.put("amount", amount);
map.put("material", material.name());
map.putAll(SerializedLocation.of(location));
return map;
}
@Override
public Data deserialize(Map<String, Object> map) {
id = (int) map.get("id");
amount = (int) map.get("amount");
material = CompatibleMaterial.valueOf((String) map.get("material"));
location = SerializedLocation.of(map);
return this;
}
@Override
public String getTableName() {
return "blocks";
}
public void setId(int id) {
this.id = id;
}
@ -78,7 +106,7 @@ public class BlockStackImpl implements BlockStack {
UltimateStacker plugin = UltimateStacker.getInstance();
plugin.getBlockStackManager().removeBlock(location);
plugin.removeHologram(this);
plugin.getDataManager().deleteBlock(this);
plugin.getPluginDataManager().delete(this);
}
@Override

View File

@ -4,6 +4,7 @@ import com.craftaro.ultimatestacker.UltimateStacker;
import com.craftaro.ultimatestacker.api.stack.block.BlockStack;
import com.craftaro.ultimatestacker.api.stack.block.BlockStackManager;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.database.Data;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -64,6 +65,11 @@ public class BlockStackManagerImpl implements BlockStackManager {
return Collections.unmodifiableCollection(this.registeredBlocks.values());
}
@Override
public Collection<Data> getStacksData() {
return Collections.unmodifiableCollection(this.registeredBlocks.values());
}
@Override
public boolean isMaterialBlacklisted(ItemStack item) {
return UltimateStacker.isMaterialBlacklisted(item);

View File

@ -6,6 +6,7 @@ import com.craftaro.ultimatestacker.api.stack.item.StackedItem;
import com.craftaro.ultimatestacker.api.stack.item.StackedItemManager;
import com.craftaro.ultimatestacker.settings.Settings;
import com.songoda.core.compatibility.ServerVersion;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -15,7 +16,11 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class StackedItemManagerImpl implements StackedItemManager {
@ -56,6 +61,16 @@ public class StackedItemManagerImpl implements StackedItemManager {
return null;
}
@Override
public @Nullable Future<StackedItem> createStackSync(ItemStack item, Location location, int amount) {
return Bukkit.getScheduler().callSyncMethod(UltimateStacker.getInstance(), () -> createStack(item, location, amount));
}
@Override
public @Nullable Future<StackedItem> createStackSync(Item item, int amount) {
return Bukkit.getScheduler().callSyncMethod(UltimateStacker.getInstance(), () -> createStack(item, amount));
}
@Override
public int getActualItemAmount(Item item) {
if (isStackedItem(item)) {

View File

@ -6,6 +6,8 @@ import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStack;
import com.craftaro.ultimatestacker.settings.Settings;
import com.craftaro.ultimatestacker.utils.Methods;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.database.Data;
import com.songoda.core.database.SerializedLocation;
import com.songoda.core.nms.world.SpawnedEntity;
import com.songoda.core.world.SSpawner;
import org.bukkit.Location;
@ -13,6 +15,8 @@ import org.bukkit.World;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.EntityType;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
@ -48,7 +52,7 @@ public class SpawnerStackImpl extends SSpawner implements SpawnerStack {
@Override
public void setAmount(int amount) {
this.amount = amount;
plugin.getDataManager().updateSpawner(this);
plugin.getPluginDataManager().save(this);
}
@Override
@ -86,6 +90,28 @@ public class SpawnerStackImpl extends SSpawner implements SpawnerStack {
return this.id;
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
map.put("id", id);
map.put("amount", amount);
map.putAll(new SerializedLocation(location).asMap());
return map;
}
@Override
public Data deserialize(Map<String, Object> map) {
this.id = (int) map.get("id");
this.amount = (int) map.get("amount");
this.initFromData(map);
return this;
}
@Override
public String getTableName() {
return "spawners";
}
public void setId(int id) {
this.id = id;
}

View File

@ -3,12 +3,14 @@ package com.craftaro.ultimatestacker.stackable.spawner;
import com.craftaro.ultimatestacker.UltimateStacker;
import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStack;
import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStackManager;
import com.songoda.core.database.Data;
import org.bukkit.Location;
import org.bukkit.block.Block;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SpawnerStackManagerImpl implements SpawnerStackManager {
@ -16,8 +18,10 @@ public class SpawnerStackManagerImpl implements SpawnerStackManager {
private final Map<Location, SpawnerStack> registeredSpawners = new HashMap<>();
@Override
public void addSpawners(Map<Location, SpawnerStack> spawners) {
this.registeredSpawners.putAll(spawners);
public void addSpawners(List<SpawnerStack> spawners) {
for (SpawnerStack spawner : spawners) {
this.registeredSpawners.put(roundLocation(spawner.getLocation()), spawner);
}
}
@Override
@ -35,7 +39,7 @@ public class SpawnerStackManagerImpl implements SpawnerStackManager {
public SpawnerStack getSpawner(Location location) {
if (!this.registeredSpawners.containsKey(roundLocation(location))) {
SpawnerStack spawnerStack = addSpawner(new SpawnerStackImpl(roundLocation(location), 1));
UltimateStacker.getInstance().getDataManager().createSpawner(spawnerStack);
UltimateStacker.getInstance().getPluginDataManager().save(spawnerStack);
return spawnerStack;
}
return this.registeredSpawners.get(location);
@ -56,6 +60,11 @@ public class SpawnerStackManagerImpl implements SpawnerStackManager {
return Collections.unmodifiableCollection(this.registeredSpawners.values());
}
@Override
public Collection<Data> getStacksData() {
return Collections.unmodifiableCollection(this.registeredSpawners.values());
}
private Location roundLocation(Location location) {
location = location.clone();
location.setX(location.getBlockX());

View File

@ -3,8 +3,8 @@ description: UltimateStacker
version: maven-version-number
softdepend: [MythicMobs, HolographicDisplays, Holograms, DecentHolograms, CMI, WorldGuard, EpicSpawners, mcMMO, WildStacker, StackMob, BentoBox, ViaVersion]
loadbefore: [WorldGuard]
main: com.songoda.ultimatestacker.UltimateStacker
author: songoda
main: com.craftaro.ultimatestacker.UltimateStacker
author: craftaro
api-version: 1.13
commands:
us:

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.craftaro</groupId>
<artifactId>UltimateStackerParent</artifactId>
<version>${revision}</version>
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>UltimateStackerAPI</artifactId>
@ -37,6 +37,15 @@
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>com.songoda:SongodaCore</artifact>
<excludeDefaults>false</excludeDefaults>
<includes>
<include>**/nms/v*/**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
@ -72,7 +81,7 @@
<dependency>
<groupId>com.songoda</groupId>
<artifactId>SongodaCore</artifactId>
<version>2.6.22</version>
<version>2.6.23-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

View File

@ -0,0 +1,11 @@
package com.craftaro.ultimatestacker.api;
public interface Settings {
/**
* @return The maximum size of a StackedItem
*/
int getMaxItemStackSize();
}

View File

@ -4,23 +4,29 @@ import com.craftaro.ultimatestacker.api.stack.block.BlockStackManager;
import com.craftaro.ultimatestacker.api.stack.entity.EntityStackManager;
import com.craftaro.ultimatestacker.api.stack.item.StackedItemManager;
import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStackManager;
import com.songoda.core.hooks.stackers.UltimateStacker;
import org.bukkit.plugin.Plugin;
public final class UltimateStackerAPI {
private static Plugin plugin;
private static EntityStackManager entityStackManager;
private static StackedItemManager stackedItemManager;
private static SpawnerStackManager spawnerStackManager;
private static BlockStackManager blockStackManager;
private static Settings settings;
private static UltimateStackerAPI instance;
public UltimateStackerAPI(EntityStackManager entityStackManager, StackedItemManager itemStackManager, SpawnerStackManager spawnerStackManager, BlockStackManager blockStackManager) {
if (UltimateStackerAPI.entityStackManager != null || UltimateStackerAPI.stackedItemManager != null || UltimateStackerAPI.spawnerStackManager != null || UltimateStackerAPI.blockStackManager != null) {
public UltimateStackerAPI(Plugin plugin, EntityStackManager entityStackManager, StackedItemManager itemStackManager, SpawnerStackManager spawnerStackManager, BlockStackManager blockStackManager, Settings settings) {
if (UltimateStackerAPI.plugin != null || UltimateStackerAPI.entityStackManager != null || UltimateStackerAPI.stackedItemManager != null || UltimateStackerAPI.spawnerStackManager != null || UltimateStackerAPI.blockStackManager != null || UltimateStackerAPI.settings != null) {
throw new IllegalStateException("UltimateStackerAPI has already been initialized!");
}
UltimateStackerAPI.plugin = plugin;
UltimateStackerAPI.entityStackManager = entityStackManager;
UltimateStackerAPI.stackedItemManager = itemStackManager;
UltimateStackerAPI.spawnerStackManager = spawnerStackManager;
UltimateStackerAPI.blockStackManager = blockStackManager;
UltimateStackerAPI.settings = settings;
instance = this;
}
@ -28,6 +34,14 @@ public final class UltimateStackerAPI {
return instance;
}
/**
* Used to interact with the plugin
* @return The plugin
*/
public static Plugin getPlugin() {
return plugin;
}
/**
* Used to interact with EntityStacks
* @return The EntityStackManager
@ -60,4 +74,12 @@ public final class UltimateStackerAPI {
return blockStackManager;
}
/**
* Used to interact with the plugin's settings
* @return The Settings
*/
public static Settings getSettings() {
return settings;
}
}

View File

@ -3,8 +3,9 @@ package com.craftaro.ultimatestacker.api.stack.block;
import com.craftaro.ultimatestacker.api.utils.Hologramable;
import com.craftaro.ultimatestacker.api.utils.Stackable;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.database.Data;
public interface BlockStack extends Stackable, Hologramable {
public interface BlockStack extends Stackable, Hologramable, Data {
int getId();

View File

@ -1,6 +1,7 @@
package com.craftaro.ultimatestacker.api.stack.block;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.database.Data;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -29,6 +30,8 @@ public interface BlockStackManager {
Collection<BlockStack> getStacks();
Collection<Data> getStacksData();
/**
* Check to see if this material is not permitted to stack
*

View File

@ -7,6 +7,8 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.Future;
public interface StackedItemManager {
@ -42,6 +44,23 @@ public interface StackedItemManager {
*/
@Nullable StackedItem createStack(Item item, int amount);
/**
* Create a new StackedItem for the given item in the main thread
* @param item The ItemStack to create the stack for
* @param amount The amount of items in the stack
* @param location The location to spawn the stack
* @return The StackedItem for the given Item or null if it could not be created
*/
@Nullable Future<StackedItem> createStackSync(ItemStack item, Location location, int amount);
/**
* Create a new StackedItem for the given item in the main thread
* @param item The item to create the stack for
* @param amount The amount of items in the stack
* @return The StackedItem for the given Item or null if it could not be created
*/
@Nullable Future<StackedItem> createStackSync(Item item, int amount);
/**
* Get the actual amount of the given item
* @param item item to check

View File

@ -3,6 +3,7 @@ package com.craftaro.ultimatestacker.api.stack.spawner;
import com.craftaro.ultimatestacker.api.utils.Hologramable;
import com.craftaro.ultimatestacker.api.utils.Stackable;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.database.Data;
import com.songoda.core.nms.world.SpawnedEntity;
import org.bukkit.Location;
import org.bukkit.World;
@ -10,7 +11,7 @@ import org.bukkit.entity.EntityType;
import java.util.Set;
public interface SpawnerStack extends Stackable, Hologramable {
public interface SpawnerStack extends Stackable, Hologramable, Data {
int getAmount();

View File

@ -1,13 +1,15 @@
package com.craftaro.ultimatestacker.api.stack.spawner;
import com.songoda.core.database.Data;
import org.bukkit.Location;
import org.bukkit.block.Block;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public interface SpawnerStackManager {
void addSpawners(Map<Location, SpawnerStack> spawners);
void addSpawners(List<SpawnerStack> spawners);
SpawnerStack addSpawner(SpawnerStack spawnerStack);
@ -20,4 +22,6 @@ public interface SpawnerStackManager {
SpawnerStack removeSpawner(Location location);
Collection<SpawnerStack> getStacks();
Collection<Data> getStacksData();
}

View File

@ -1,4 +0,0 @@
name: UltimateStackerAPI
version: '${project.version}'
main: com.craftaro.ultimatestacker.api.UltimateStackerAPI
api-version: 1.18

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.craftaro</groupId>
<artifactId>UltimateStackerParent</artifactId>
<version>${revision}</version>
<version>3.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
@ -13,10 +13,9 @@
</modules>
<properties>
<revision>3.0.0-SNAPSHOT</revision>
<java.version>1.8</java.version>
<java.release>8</java.release>
<core.version>2.6.23-SNAPSHOT</core.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>