Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions docker/playerkits-db/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# docker compose up -d
services:
postgres:
image: postgres
container_name: 'player_kits_postgresql'
mariadb:
image: mariadb
container_name: 'player_kits_mariadb'
ports:
- 5432:5432
- "3306:3306"
shm_size: 128mb
volumes:
- ./postgresql/data/:/var/lib/postgresql
- ./mariadb/data/:/var/lib/mysql
environment:
POSTGRES_PASSWORD: playerkits
POSTGRES_USER: playerkits
POSTGRES_DB: playerkits
MARIADB_ROOT_PASSWORD: playerkits
MARIADB_USER: playerkits
MARIADB_PASSWORD: playerkits
MARIADB_DATABASE: playerkits
networks:
- player_kits_postgresql_go_net
- player_kits_mariadb_net

networks:
player_kits_postgresql_go_net:
player_kits_mariadb_net:
driver: bridge
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public void onEnable() {
this.itemRegistry = new ItemRegistry(this);

this.claimedKitService = new ClaimedKitService(this);

this.playerKitService = new PlayerKitService(this);
this.playerKitService.init();

this.kitSetupService = new PlayerKitSetupService(this);

this.paperCommandService = new PaperCommandService(this);
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/net/onelitefeather/playerkits/kit/ClaimedKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import jakarta.persistence.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.UUID;

@Entity
@Table
@Table(name = "claimed_kits")
public class ClaimedKit {

@Id
Expand All @@ -18,7 +19,7 @@ public class ClaimedKit {
private String claimedBy;

@Column
private String kitName;
private Long kitId;

@Column
private Boolean firstJoin;
Expand All @@ -35,16 +36,16 @@ public class ClaimedKit {
public ClaimedKit() {
}

public ClaimedKit(@NotNull Long id,
public ClaimedKit(@Nullable Long id,
@NotNull String claimedBy,
@NotNull String kitName,
@NotNull Long kitId,
@NotNull Boolean firstJoin,
@NotNull Boolean oneTime,
@NotNull Long claimedAt,
@NotNull Long cooldown) {
this.id = id;
this.claimedBy = claimedBy;
this.kitName = kitName;
this.kitId = kitId;
this.firstJoin = firstJoin;
this.oneTime = oneTime;
this.claimedAt = claimedAt;
Expand Down Expand Up @@ -79,12 +80,12 @@ public void setClaimedBy(@NotNull String claimedBy) {
}

@NotNull
public String getKitName() {
return kitName;
public Long getKitId() {
return kitId;
}

public void setKitName(@NotNull String kitName) {
this.kitName = kitName;
public void setKitId(@NotNull Long kitId) {
this.kitId = kitId;
}

@NotNull
Expand Down Expand Up @@ -129,7 +130,7 @@ public String toString() {
return "ClaimedKit{" +
"id=" + id +
", claimedBy='" + claimedBy + '\'' +
", kitName='" + kitName + '\'' +
", kitName='" + kitId + '\'' +
'}';
}

Expand All @@ -140,7 +141,7 @@ public boolean equals(Object o) {

if (!Objects.equals(id, that.id)) return false;
if (!Objects.equals(claimedBy, that.claimedBy)) return false;
if (!Objects.equals(kitName, that.kitName)) return false;
if (!Objects.equals(kitId, that.kitId)) return false;
if (!Objects.equals(firstJoin, that.firstJoin)) return false;
if (!Objects.equals(oneTime, that.oneTime)) return false;
if (!Objects.equals(claimedAt, that.claimedAt)) return false;
Expand All @@ -151,7 +152,7 @@ public boolean equals(Object o) {
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (claimedBy != null ? claimedBy.hashCode() : 0);
result = 31 * result + (kitName != null ? kitName.hashCode() : 0);
result = 31 * result + (kitId != null ? kitId.hashCode() : 0);
result = 31 * result + (firstJoin != null ? firstJoin.hashCode() : 0);
result = 31 * result + (oneTime != null ? oneTime.hashCode() : 0);
result = 31 * result + (claimedAt != null ? claimedAt.hashCode() : 0);
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/net/onelitefeather/playerkits/kit/PlayerKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.onelitefeather.playerkits.kit.property.PlayerKitProperties;
import net.onelitefeather.playerkits.util.InventoryUtil;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

@Entity
Expand All @@ -14,8 +16,9 @@ public final class PlayerKit {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(columnDefinition = "TEXT")
private String items;
@Lob
@Column(name = "kit_content", columnDefinition = "longblob")
private byte[] contents;

@Column
private String name;
Expand All @@ -35,13 +38,12 @@ public void setId(@NotNull Long id) {
this.id = id;
}

@NotNull
public String getItems() {
return items;
public void setContents(byte[] contents) {
this.contents = contents;
}

public void setItems(@NotNull String items) {
this.items = items;
public byte[] getContents() {
return contents;
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public PlayerKit createKit(@Nullable ItemStack @NotNull[] items) {
var displayItemStep = KitSetupStep.DISPLAY_ITEM;

kit.setDisplayName((String) this.values.getOrDefault(displayNameStep.getId(), kitName));
kit.setItems(InventoryUtil.serializeInventoryToString(items));

kit.setContents(ItemStack.serializeItemsAsBytes(items));
var properties = new PlayerKitProperties();
properties.setOneTime((Boolean) this.values.getOrDefault(oneTimeStep.getId(), oneTimeStep.getDefaultValue()));
properties.setFirstJoin((Boolean) this.values.getOrDefault(firstJoinStep.getId(), firstJoinStep.getDefaultValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public final class ClaimedKitService {

public static final Long IGNORE_COOLDOWN = -1L;
private static final String KIT_NAME_PARAMETER = "kitName";
private static final String KIT_NAME_PARAMETER = "kitId";
private static final String CLAIMED_BY_PARAMETER = "claimedBy";
private final PlayerKitsPlugin plugin;

Expand All @@ -35,20 +35,20 @@ public List<ClaimedKit> getClaimedKits(@NotNull UUID claimedBy) {
}).orElseGet(ArrayList::new);
}

public Optional<ClaimedKit> getClaimedKit(@NotNull String kitName, @NotNull UUID claimedBy) {
public Optional<ClaimedKit> getClaimedKit(@NotNull Long kitId, @NotNull UUID claimedBy) {
return this.plugin.getDatabaseService().getSessionFactory().map(SessionFactory::openSession).map(session -> {
var query = session.createQuery("SELECT ck FROM ClaimedKit ck WHERE ck.kitName = :kitName AND ck.claimedBy = :claimedBy", ClaimedKit.class);
query.setParameter(KIT_NAME_PARAMETER, kitName);
var query = session.createQuery("SELECT ck FROM ClaimedKit ck WHERE ck.kitId = :kitId AND ck.claimedBy = :claimedBy", ClaimedKit.class);
query.setParameter(KIT_NAME_PARAMETER, kitId);
query.setParameter(CLAIMED_BY_PARAMETER, claimedBy.toString());
return query.uniqueResult();
});
}

@NotNull
public KitClaimResult canClaim(@NotNull UUID claimedBy, @NotNull String kitName) {
public KitClaimResult canClaim(@NotNull UUID claimedBy, @NotNull Long kitId) {

var claimedKit = getClaimedKit(kitName, claimedBy);
var playerKit = plugin.getPlayerKitService().getPlayerKit(kitName);
var claimedKit = getClaimedKit(kitId, claimedBy);
var playerKit = plugin.getPlayerKitService().getPlayerKit(kitId);
if (playerKit == null) return KitClaimResult.UNKNOWN_KIT;

var offlinePlayer = plugin.getServer().getOfflinePlayer(claimedBy);
Expand All @@ -61,44 +61,59 @@ public KitClaimResult canClaim(@NotNull UUID claimedBy, @NotNull String kitName)
return System.currentTimeMillis() > claimedKit.get().getCooldown() ? KitClaimResult.SUCCESS : KitClaimResult.COOLDOWN_NOT_EXPIRED;
}

public boolean claimKit(@NotNull String kitName,
@NotNull UUID claimedBy,
@NotNull Boolean firstJoin,
@NotNull Boolean oneTime,
@NotNull Long claimedAt,
@NotNull Long cooldown) {

var claimedKit = getClaimedKit(kitName, claimedBy);
if (claimedKit.isPresent()) return !firstJoin || !oneTime || cooldown.equals(IGNORE_COOLDOWN);

public boolean updateClaimedKit(ClaimedKit claimedKit) {
Transaction transaction = null;

var sessionFactory = this.plugin.getDatabaseService().getSessionFactory();
if (sessionFactory.isEmpty()) return false;

try (Session session = sessionFactory.get().openSession()) {
transaction = session.beginTransaction();
session.merge(claimedKit);
transaction.commit();
return true;
} catch (HibernateException e) {
if (transaction != null) transaction.rollback();
this.plugin.getLogger().log(Level.SEVERE, "Cannot update claimed kit called %s".formatted(claimedKit.getKitId()), e);
return false;
}
}

ClaimedKit kit = new ClaimedKit();
kit.setKitName(kitName);
kit.setClaimedByUniqueId(claimedBy);
kit.setClaimedAt(claimedAt);
kit.setFirstJoin(firstJoin);
kit.setCooldown(cooldown);
kit.setOneTime(oneTime);
public boolean saveClaimedKit(ClaimedKit claimedKit) {
Transaction transaction = null;

session.persist(kit);
transaction.commit();
var sessionFactory = this.plugin.getDatabaseService().getSessionFactory();
if (sessionFactory.isEmpty()) return false;

try (Session session = sessionFactory.get().openSession()) {
transaction = session.beginTransaction();
session.persist(claimedKit);
transaction.commit();
return true;
} catch (HibernateException e) {
if (transaction != null) transaction.rollback();
this.plugin.getLogger().log(Level.SEVERE, "Cannot save claimed kit called %s".formatted(claimedKit.getKitId()), e);
return false;
}
}

if (transaction != null) {
transaction.rollback();
}
public boolean claimKit(@NotNull Long kitId,
@NotNull UUID claimedBy,
@NotNull Boolean firstJoin,
@NotNull Boolean oneTime,
@NotNull Long claimedAt,
@NotNull Long cooldown) {

this.plugin.getLogger().log(Level.SEVERE, "Cannot save claimed kit called %s".formatted(kitName), e);
return false;
var claimedKit = getClaimedKit(kitId, claimedBy);
if (claimedKit.isPresent()) {
var kit = claimedKit.get();
kit.setCooldown(cooldown);
kit.setClaimedAt(claimedAt);
return updateClaimedKit(kit);
}

var kit = new ClaimedKit(null, claimedBy.toString(), kitId, firstJoin, oneTime, claimedAt, cooldown);
return saveClaimedKit(kit);
}
}
Loading
Loading