Skip to content

Commit

Permalink
database wip
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeTheTech committed Sep 25, 2023
1 parent fc799f9 commit 5634108
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/main/java/lee/code/pets/Pets.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.mojang.brigadier.tree.LiteralCommandNode;
import lee.code.pets.commands.CommandManager;
import lee.code.pets.commands.TabCompletion;
import lee.code.pets.database.CacheManager;
import lee.code.pets.database.DatabaseManager;
import lee.code.pets.listeners.PetListener;
import lee.code.pets.pets.PetManager;
import lombok.Getter;
Expand All @@ -18,20 +20,26 @@ public class Pets extends JavaPlugin {
@Getter private CommandManager commandManager;
@Getter private PetManager petManager;
@Getter private ProtocolManager protocolManager;
@Getter private CacheManager cacheManager;
private DatabaseManager databaseManager;

@Override
public void onEnable() {
this.databaseManager = new DatabaseManager(this);
this.cacheManager = new CacheManager(this, databaseManager);
this.commandManager = new CommandManager(this);
this.protocolManager = ProtocolLibrary.getProtocolManager();
this.petManager = new PetManager(this);
databaseManager.initialize(false);
registerCommands();
registerPacketListeners();
registerListeners();

}

@Override
public void onDisable() {

databaseManager.closeConnection();
}

private void registerPacketListeners() {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/lee/code/pets/database/CacheManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lee.code.pets.database;

import lee.code.pets.Pets;
import lee.code.pets.database.cache.CachePets;
import lombok.Getter;

public class CacheManager {
private final Pets pets;
@Getter private final CachePets cachePets;

public CacheManager(Pets pets, DatabaseManager databaseManager) {
this.pets = pets;
this.cachePets = new CachePets(databaseManager);
}
}
100 changes: 100 additions & 0 deletions src/main/java/lee/code/pets/database/DatabaseManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package lee.code.pets.database;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.jdbc.db.DatabaseTypeUtils;
import com.j256.ormlite.logger.LogBackendType;
import com.j256.ormlite.logger.LoggerFactory;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import lee.code.pets.Pets;
import lee.code.pets.database.tables.PetTable;
import org.bukkit.Bukkit;

import java.io.File;
import java.sql.SQLException;

public class DatabaseManager {
private final Pets pets;
private Dao<PetTable, Integer> petsDao;
private ConnectionSource connectionSource;

public DatabaseManager(Pets pets) {
this.pets = pets;
}

private String getDatabaseURL() {
//Setup MongoDB
if (!pets.getDataFolder().exists()) pets.getDataFolder().mkdir();
return "jdbc:sqlite:" + new File(pets.getDataFolder(), "database.db");
}

public void initialize(boolean debug) {
if (!debug) LoggerFactory.setLogBackendFactory(LogBackendType.NULL);
try {
final String databaseURL = getDatabaseURL();
connectionSource = new JdbcConnectionSource(
databaseURL,
"test",
"test",
DatabaseTypeUtils.createDatabaseType(databaseURL));
createOrCacheTables();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void closeConnection() {
try {
connectionSource.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private void createOrCacheTables() throws SQLException {
final CacheManager cacheManager = pets.getCacheManager();

//Pet data
TableUtils.createTableIfNotExists(connectionSource, PetTable.class);
petsDao = DaoManager.createDao(connectionSource, PetTable.class);

int highestID = 0;
for (PetTable petTable : petsDao.queryForAll()) {
cacheManager.getCachePets().setPetTable(petTable);
if (petTable.getId() > highestID) highestID = petTable.getId();
}
cacheManager.getCachePets().getNextID().set(highestID);
}

public synchronized void createPetTable(PetTable petTable) {
Bukkit.getAsyncScheduler().runNow(pets, scheduledTask -> {
try {
petsDao.createIfNotExists(petTable);
} catch (SQLException e) {
e.printStackTrace();
}
});
}

public synchronized void updatePetTable(PetTable petTable) {
Bukkit.getAsyncScheduler().runNow(pets, scheduledTask -> {
try {
petsDao.update(petTable);
} catch (SQLException e) {
e.printStackTrace();
}
});
}

public synchronized void deletePetTable(PetTable petTable) {
Bukkit.getAsyncScheduler().runNow(pets, scheduledTask -> {
try {
petsDao.delete(petTable);
} catch (SQLException e) {
e.printStackTrace();
}
});
}
}
39 changes: 39 additions & 0 deletions src/main/java/lee/code/pets/database/cache/CachePets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lee.code.pets.database.cache;

import lee.code.pets.database.DatabaseManager;
import lee.code.pets.database.cache.data.PlayerPetData;
import lee.code.pets.database.handlers.DatabaseHandler;
import lee.code.pets.database.tables.PetTable;
import lombok.Getter;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class CachePets extends DatabaseHandler {
@Getter private final AtomicInteger nextID = new AtomicInteger(0);
@Getter private final PlayerPetData playerPetData;
private final HashMap<Integer, PetTable> petsCache = new HashMap<>();

public CachePets(DatabaseManager databaseManager) {
super(databaseManager);
this.playerPetData = new PlayerPetData(this);
}

public PetTable getPetTable(int id) {
return petsCache.get(id);
}

public void setPetTable(PetTable petTable) {
petsCache.put(petTable.getId(), petTable);
playerPetData.cachePlayerPetTable(petTable);
}

public void createNewPet(UUID uuid, String data) {
nextID.addAndGet(1);
final PetTable petTable = new PetTable(nextID.get(), uuid, data);
setPetTable(petTable);
createPetDatabase(petTable);
}

}
33 changes: 33 additions & 0 deletions src/main/java/lee/code/pets/database/cache/data/PlayerPetData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lee.code.pets.database.cache.data;

import lee.code.pets.database.cache.CachePets;
import lee.code.pets.database.tables.PetTable;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class PlayerPetData {
private final CachePets cachePets;
private final HashMap<UUID, Set<Integer>> playerPetCache = new HashMap<>();

public PlayerPetData(CachePets cachePets) {
this.cachePets = cachePets;
}

public void cachePlayerPetTable(PetTable petTable) {
if (playerPetCache.containsKey(petTable.getOwner())) {
playerPetCache.get(petTable.getOwner()).add(petTable.getId());
} else {
final Set<Integer> pets = new HashSet<>();
pets.add(petTable.getId());
playerPetCache.put(petTable.getOwner(), pets);
}
}

public Set<Integer> getAllPets(UUID uuid) {
if (!playerPetCache.containsKey(uuid)) return new HashSet<>();
else return playerPetCache.get(uuid);
}
}
24 changes: 24 additions & 0 deletions src/main/java/lee/code/pets/database/handlers/DatabaseHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lee.code.pets.database.handlers;

import lee.code.pets.database.DatabaseManager;
import lee.code.pets.database.tables.PetTable;

public class DatabaseHandler {
private final DatabaseManager databaseManager;

public DatabaseHandler(DatabaseManager databaseManager) {
this.databaseManager = databaseManager;
}

public void createPetDatabase(PetTable petTable) {
databaseManager.createPetTable(petTable);
}

public void updatePetDatabase(PetTable petTable) {
databaseManager.updatePetTable(petTable);
}

public void deletePetDatabase(PetTable petTable) {
databaseManager.deletePetTable(petTable);
}
}
30 changes: 30 additions & 0 deletions src/main/java/lee/code/pets/database/tables/PetTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lee.code.pets.database.tables;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.UUID;

@Setter
@Getter
@NoArgsConstructor
@DatabaseTable(tableName = "pets")
public class PetTable {
@DatabaseField(id = true, canBeNull = false)
private int id;

@DatabaseField(columnName = "owner", canBeNull = false)
private UUID owner;

@DatabaseField(columnName = "data", canBeNull = false)
private String data;

public PetTable(int id, UUID owner, String data) {
this.id = id;
this.owner = owner;
this.data = data;
}
}
2 changes: 1 addition & 1 deletion src/main/java/lee/code/pets/pets/PetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public int getPetID(org.bukkit.entity.Entity entity) {
}

public boolean isPet(org.bukkit.entity.Entity entity) {
return getPetID(entity) != 0;
return petTracker.containsValue(entity.getUniqueId());
}

public void removePet(Player player, int id) {
Expand Down

0 comments on commit 5634108

Please sign in to comment.