Skip to content

Commit

Permalink
Fix #1692
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakllp committed Nov 23, 2024
1 parent ef6d144 commit 641b68f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public boolean hasActiveMyPet(String name) {
public abstract Optional<MyPet> activateMyPet(StoredMyPet storedMyPet);

public boolean deactivateMyPet(MyPetPlayer owner, boolean update) {
return deactivateMyPet(owner, update, false);
}
public boolean deactivateMyPet(MyPetPlayer owner, boolean update, boolean save) {
if (mActivePlayerPets.containsKey(owner)) {
final MyPet myPet = owner.getMyPet();

Expand All @@ -94,6 +97,9 @@ public boolean deactivateMyPet(MyPetPlayer owner, boolean update) {
if (update) {
MyPetApi.getRepository().updateMyPet(myPet, null);
}
if (save) {
MyPetApi.getRepository().savePet(myPet);
}
mActivePetsPlayer.remove(myPet);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public interface Repository {

void addMyPet(final StoredMyPet storedMyPet, final RepositoryCallback<Boolean> callback);

boolean savePet(final StoredMyPet storedMyPet);

void updateMyPet(final StoredMyPet storedMyPet, final RepositoryCallback<Boolean> callback);

void isMyPetPlayer(final Player player, final RepositoryCallback<Boolean> callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ public void on(PlayerQuitEvent event) {
if (myPet.getStatus() == MyPet.PetState.Here) {
myPet.removePet(true);
}
MyPetApi.getMyPetManager().deactivateMyPet(player, true);

MyPetApi.getMyPetManager().deactivateMyPet(player, false, true);
}

MyPetApi.getPlayerManager().setOffline(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
public class MongoDbRepository implements Repository {

private MongoClient mongo;
private ArrayList<StoredMyPet> petsToBeSaved = new ArrayList<>();
private ArrayList<MyPetPlayer> playersToBeSaved = new ArrayList<>();
private HashMap<UUID, StoredMyPet> petsToBeSaved = new HashMap<>();
private HashMap<UUID, MyPetPlayer> playersToBeSaved = new HashMap<>();
private MongoDatabase db;
private int version = 4;
// https://search.maven.org/remotecontent?filepath=org/mongodb/mongo-java-driver/3.2.1/mongo-java-driver-3.2.1.jar
Expand Down Expand Up @@ -243,10 +243,10 @@ private void updateInfoDocument(Document info) {

private void savePets() {
for (StoredMyPet storedMyPet : MyPetApi.getMyPetManager().getAllActiveMyPets()) {
updateMyPet(storedMyPet);
savePet(storedMyPet);
}
for (StoredMyPet myPet : petsToBeSaved) {
updateMyPet(myPet);
for (StoredMyPet myPet : petsToBeSaved.values()) {
savePet(myPet);
}
}

Expand All @@ -255,7 +255,7 @@ private void savePlayers() {
for (MyPetPlayer player : MyPetApi.getPlayerManager().getMyPetPlayers()) {
updatePlayer(player);
}
for (MyPetPlayer player : playersToBeSaved) {
for (MyPetPlayer player : playersToBeSaved.values()) {
updatePlayer(player);
}
}
Expand Down Expand Up @@ -380,6 +380,10 @@ public void getMyPet(final UUID uuid, final RepositoryCallback<StoredMyPet> call
new BukkitRunnable() {
@Override
public void run() {
if(petsToBeSaved.containsKey(uuid)) {
return;
}

MongoCollection petCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
Document petDocument = (Document) petCollection.find(new Document("uuid", uuid.toString())).first();
if (petDocument != null) {
Expand All @@ -390,8 +394,10 @@ public void run() {
callback.runTask(pet);
}
}

this.cancel();
}
}.runTaskAsynchronously(MyPetApi.getPlugin());
}.runTaskTimerAsynchronously(MyPetApi.getPlugin(), 0, 5);
}
}

Expand Down Expand Up @@ -458,14 +464,14 @@ public void addMyPet(StoredMyPet storedMyPet) {

@Override
public void updateMyPet(final StoredMyPet storedMyPet, final RepositoryCallback<Boolean> callback) {
petsToBeSaved.add(storedMyPet);
petsToBeSaved.put(storedMyPet.getUUID(), storedMyPet);
new BukkitRunnable() {
@Override
public void run() {
boolean result = updateMyPet(storedMyPet);
boolean result = savePet(storedMyPet);

if (result) {
petsToBeSaved.remove(storedMyPet);
petsToBeSaved.remove(storedMyPet.getUUID());
}

if (callback != null) {
Expand All @@ -476,7 +482,7 @@ public void run() {
}

@SuppressWarnings("unchecked")
private boolean updateMyPet(StoredMyPet storedMyPet) {
public boolean savePet(StoredMyPet storedMyPet) {
MongoCollection petCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
Document filter = new Document("uuid", storedMyPet.getUUID().toString());
Document petDocument = (Document) petCollection.find(filter).first();
Expand Down Expand Up @@ -637,7 +643,7 @@ public void run() {

@Override
public void updateMyPetPlayer(final MyPetPlayer player, final RepositoryCallback<Boolean> callback) {
playersToBeSaved.add(player);
playersToBeSaved.put(player.getPlayerUUID(), player);
new BukkitRunnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
public class MySqlRepository implements Repository {

protected Gson gson = new Gson();
private ArrayList<StoredMyPet> petsToBeSaved = new ArrayList<>();
private ArrayList<MyPetPlayer> playersToBeSaved = new ArrayList<>();
private HashMap<UUID, StoredMyPet> petsToBeSaved = new HashMap<>();
private HashMap<UUID, MyPetPlayer> playersToBeSaved = new HashMap<>();
private HikariDataSource dataSource;
private int version = 10;

Expand Down Expand Up @@ -355,12 +355,12 @@ private void savePets() {
for (MyPet myPet : MyPetApi.getMyPetManager().getAllActiveMyPets()) {
savePet(myPet);
}
for (StoredMyPet myPet : petsToBeSaved) {
for (StoredMyPet myPet : petsToBeSaved.values()) {
savePet(myPet);
}
}

private void savePet(StoredMyPet myPet) {
public boolean savePet(StoredMyPet myPet) {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("UPDATE " + Configuration.Repository.MySQL.PREFIX + "pets SET " +
"owner_uuid=?, " +
Expand Down Expand Up @@ -396,15 +396,17 @@ private void savePet(StoredMyPet myPet) {
statement.executeUpdate();
} catch (SQLException | IOException e) {
e.printStackTrace();
return false;
}
return true;
}

@SuppressWarnings("unchecked")
private void savePlayers() {
for (MyPetPlayer player : MyPetApi.getPlayerManager().getMyPetPlayers()) {
savePlayer(player);
}
for (MyPetPlayer player : playersToBeSaved) {
for (MyPetPlayer player : playersToBeSaved.values()) {
savePlayer(player);
}
}
Expand Down Expand Up @@ -596,6 +598,10 @@ public void getMyPet(final UUID uuid, final RepositoryCallback<StoredMyPet> call
new BukkitRunnable() {
@Override
public void run() {
if(petsToBeSaved.containsKey(uuid)) {
return;
}

try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + Configuration.Repository.MySQL.PREFIX + "pets WHERE uuid=?;",
ResultSet.TYPE_SCROLL_SENSITIVE,
Expand All @@ -618,8 +624,10 @@ public void run() {
} catch (SQLException e) {
e.printStackTrace();
}

this.cancel();
}
}.runTaskAsynchronously(MyPetApi.getPlugin());
}.runTaskTimerAsynchronously(MyPetApi.getPlugin(), 0, 5);
}
}

Expand Down Expand Up @@ -769,7 +777,7 @@ public boolean addMyPets(List<StoredMyPet> pets) {

@Override
public void updateMyPet(final StoredMyPet storedMyPet, final RepositoryCallback<Boolean> callback) {
petsToBeSaved.add(storedMyPet);
petsToBeSaved.put(storedMyPet.getUUID(), storedMyPet);
new BukkitRunnable() {
@Override
public void run() {
Expand Down Expand Up @@ -808,7 +816,7 @@ public void run() {
int result = statement.executeUpdate();

if (result > 0) {
petsToBeSaved.remove(storedMyPet);
petsToBeSaved.remove(storedMyPet.getUUID());
}

if (callback != null) {
Expand Down Expand Up @@ -981,7 +989,7 @@ public void run() {

@Override
public void updateMyPetPlayer(final MyPetPlayer player, final RepositoryCallback<Boolean> callback) {
playersToBeSaved.add(player);
playersToBeSaved.put(player.getPlayerUUID(), player);
new BukkitRunnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ public void updateMyPet(final StoredMyPet storedMyPet, final RepositoryCallback<
}
}

@Override
public boolean savePet(StoredMyPet storedMyPet) {
return false;
}

private int loadPets(TagList petList) {
int petCount = 0;
boolean oldPets = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import de.Keyle.MyPet.entity.InactiveMyPet;
import de.Keyle.MyPet.util.player.MyPetPlayerImpl;
import de.keyle.knbt.TagStream;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

Expand All @@ -50,8 +49,8 @@
public class SqLiteRepository implements Repository {

protected Gson gson = new Gson();
private ArrayList<StoredMyPet> petsToBeSaved = new ArrayList<>();
private ArrayList<MyPetPlayer> playersToBeSaved = new ArrayList<>();
private HashMap<UUID, StoredMyPet> petsToBeSaved = new HashMap<>();
private HashMap<UUID, MyPetPlayer> playersToBeSaved = new HashMap<>();
private Connection connection;
private int version = 1;

Expand Down Expand Up @@ -272,12 +271,12 @@ private void savePets() {
for (MyPet myPet : MyPetApi.getMyPetManager().getAllActiveMyPets()) {
savePet(myPet);
}
for (StoredMyPet myPet : petsToBeSaved) {
for (StoredMyPet myPet : petsToBeSaved.values()) {
savePet(myPet);
}
}

private void savePet(StoredMyPet myPet) {
public boolean savePet(StoredMyPet myPet) {
try {
PreparedStatement statement = connection.prepareStatement("UPDATE pets SET " +
"owner_uuid=?, " +
Expand Down Expand Up @@ -315,15 +314,17 @@ private void savePet(StoredMyPet myPet) {
//MyPetLogger.write("UPDATE pet: " + result);
} catch (SQLException | IOException e) {
e.printStackTrace();
return false;
}
return true;
}

@SuppressWarnings("unchecked")
private void savePlayers() {
for (MyPetPlayer player : MyPetApi.getPlayerManager().getMyPetPlayers()) {
savePlayer(player);
}
for (MyPetPlayer player : playersToBeSaved) {
for (MyPetPlayer player : playersToBeSaved.values()) {
savePlayer(player);
}
}
Expand Down Expand Up @@ -510,6 +511,10 @@ public void getMyPet(final UUID uuid, final RepositoryCallback<StoredMyPet> call
new BukkitRunnable() {
@Override
public void run() {
if(petsToBeSaved.containsKey(uuid)) {
return;
}

try {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM pets WHERE uuid=?;");
statement.setString(1, uuid.toString());
Expand All @@ -529,8 +534,10 @@ public void run() {
} catch (SQLException e) {
e.printStackTrace();
}

cancel();
}
}.runTaskAsynchronously(MyPetApi.getPlugin());
}.runTaskTimerAsynchronously(MyPetApi.getPlugin(), 0, 5);
}
}

Expand Down Expand Up @@ -682,7 +689,7 @@ public boolean addMyPets(List<StoredMyPet> pets) {

@Override
public void updateMyPet(final StoredMyPet storedMyPet, final RepositoryCallback<Boolean> callback) {
petsToBeSaved.add(storedMyPet);
petsToBeSaved.put(storedMyPet.getUUID(), storedMyPet);
new BukkitRunnable() {
@Override
public void run() {
Expand Down Expand Up @@ -721,7 +728,7 @@ public void run() {
int result = statement.executeUpdate();

if (result > 0) {
petsToBeSaved.remove(storedMyPet);
petsToBeSaved.remove(storedMyPet.getUUID());
}

//MyPetLogger.write("UPDATE pet: " + result);
Expand Down Expand Up @@ -878,7 +885,7 @@ public void run() {

@Override
public void updateMyPetPlayer(final MyPetPlayer player, final RepositoryCallback<Boolean> callback) {
playersToBeSaved.add(player);
playersToBeSaved.put(player.getPlayerUUID(), player);
new BukkitRunnable() {
@Override
public void run() {
Expand Down

0 comments on commit 641b68f

Please sign in to comment.