Skip to content

Commit

Permalink
Merge pull request #152 from SpaceServerUniverse/dev/yurisi-receive-box
Browse files Browse the repository at this point in the history
Dev/yurisi receive box
  • Loading branch information
m1sk9 authored Oct 15, 2024
2 parents 4f4357d + 045f803 commit 6d90878
Show file tree
Hide file tree
Showing 17 changed files with 616 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ private Configuration registerAnnotatedClasses(Configuration configuration) {
.addAnnotatedClass(CustomName.class)
.addAnnotatedClass(Market.class)
.addAnnotatedClass(ChestShop.class)
.addAnnotatedClass(Ammo.class);
.addAnnotatedClass(Ammo.class)
.addAnnotatedClass(ReceiveBox.class);
}

private Configuration registerProperties(Configuration configuration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class DatabaseManager {
private final PlayerCountRepository playerCountRepository;
private final CustomNameRepository customNameRepository;
private final MarketRepository marketRepository;
private final ReceiveBoxRepository receiveBoxRepository;

public DatabaseManager(SessionFactory sessionFactory) {
this.userRepository = new UserRepository(sessionFactory);
Expand All @@ -51,6 +52,7 @@ public DatabaseManager(SessionFactory sessionFactory) {
this.marketRepository = new MarketRepository(sessionFactory);
this.chestShopRepository = new ChestShopRepository(sessionFactory);
this.ammoRepository = new AmmoRepository(sessionFactory);
this.receiveBoxRepository = new ReceiveBoxRepository(sessionFactory);
}

/**
Expand Down Expand Up @@ -239,4 +241,12 @@ public ChestShopRepository getChestShopRepository() {
public AmmoRepository getAmmoRepository() {
return ammoRepository;
}

/**
* 受取ボックスリポジトリーを取得
* @return ReceiveBoxRepository 受取ボックスのリポジトリ
*/
public ReceiveBoxRepository getReceiveBoxRepository(){
return receiveBoxRepository;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package space.yurisi.universecorev2.database.models;

import jakarta.persistence.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.util.Date;

@Entity
@Table(name = "receive_boxes")
public class ReceiveBox {
@Id
@Column(name = "id", columnDefinition = "BIGINT UNSIGNED")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "uuid", columnDefinition = "VARCHAR(255) NOT NULL")
private String uuid;

@Column(name = "item_name", nullable = false)
private String itemName;

@Column(name = "display_name")
private String displayName;

@Column(name = "description")
private String description;

@Column(name = "serialized_item", nullable = false)
private byte[] serializedItem;

@Column(name = "serialized_item_stack_json", nullable = false)
private String serializedItemStackJson;

@Column(name = "serialized_item_meta_json", nullable = false)
private String serializedItemStackMetaJson;

@Column(name = "is_received")
private boolean is_received;

@Column(name = "expired_at", nullable = false)
private Date expired_at;

@CreationTimestamp
@Column(name = "created_at", nullable = false)
private Date created_at;

@UpdateTimestamp
@Column(name = "updated_at", nullable = false)
private Date updated_at;

public ReceiveBox(
Long id,
String uuid,
String itemName,
String displayName,
String description,
byte[] serializedItem,
String serializedItemStackJson,
String serializedItemStackMetaJson,
int is_received,
Date expired_at
) {
this.id = id;
this.uuid = uuid;
this.itemName = itemName;
this.displayName = displayName;
this.description = description;
this.serializedItem = serializedItem;
this.serializedItemStackJson = serializedItemStackJson;
this.serializedItemStackMetaJson = serializedItemStackMetaJson;
this.is_received = (is_received != 0);
this.expired_at = expired_at;
}

public ReceiveBox() {

}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}

public String getItemName() {
return itemName;
}

public void setItemName(String itemName) {
this.itemName = itemName;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getDescription(){
return description;
}

public void setDescription(String description){
this.description = description;
}

public byte[] getSerializedItem() {
return serializedItem;
}

public void setSerializedItem(byte[] serializedItem) {
this.serializedItem = serializedItem;
}

public String getSerializedItemStackJson() {
return serializedItemStackJson;
}

public void setSerializedItemStackJson(String serializedItemStackJson) {
this.serializedItemStackJson = serializedItemStackJson;
}

public String getSerializedItemStackMetaJson() {
return serializedItemStackMetaJson;
}

public void setSerializedItemStackMetaJson(String serializedItemStackMetaJson) {
this.serializedItemStackMetaJson = serializedItemStackMetaJson;
}

public boolean isReceived() {
return is_received;
}

public void setReceived(boolean is_received) {
this.is_received = is_received;
}

public Date getExpired_at() {
return expired_at;
}

public void setExpired_at(Date expired_at) {
this.expired_at = expired_at;
}

public Date getCreated_at() {
return created_at;
}

public void setCreated_at(Date created_at) {
this.created_at = created_at;
}

public Date getUpdated_at() {
return updated_at;
}

public void setUpdated_at(Date updated_at) {
this.updated_at = updated_at;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package space.yurisi.universecorev2.database.repositories;

import org.bukkit.entity.Player;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import space.yurisi.universecorev2.database.models.Market;
import space.yurisi.universecorev2.database.models.Mywarp;
import space.yurisi.universecorev2.database.models.ReceiveBox;
import space.yurisi.universecorev2.exception.MywarpNotFoundException;
import space.yurisi.universecorev2.exception.ReceiveBoxNotFoundException;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class ReceiveBoxRepository {

private final SessionFactory sessionFactory;

public ReceiveBoxRepository(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

/**
* 受取アイテムを作ります
*
* @return ReceiveBox
*/
public ReceiveBox createReceiveBox(ReceiveBox receiveBox) {
Session session = this.sessionFactory.getCurrentSession();
try {
session.beginTransaction();
session.persist(receiveBox);
session.getTransaction().commit();

} finally {
session.close();
}
return receiveBox;
}

/**
* 受取アイテムをプライマリーキーから検索します
* @param id
* @return ReceiveBox
* @throws ReceiveBoxNotFoundException
*/
public ReceiveBox getItemFromId(Long id) throws ReceiveBoxNotFoundException {
Session session = this.sessionFactory.getCurrentSession();
try {
session.beginTransaction();
ReceiveBox data = session.createSelectionQuery("from ReceiveBox where is_received = 0 and id = :id", ReceiveBox.class)
.setParameter("id", id)
.getSingleResult();
session.getTransaction().commit();
if (data == null) {
throw new ReceiveBoxNotFoundException("受取ボックスにアイテムが存在しませんでした。id: " + id);
}
return data;
} finally {
session.close();
}
}


/**
* 受取アイテムを全て取得します
*
* @return Market
*/
public List<ReceiveBox> getReceiveBoxes() {
Session session = this.sessionFactory.getCurrentSession();
try {
session.beginTransaction();
List<ReceiveBox> receiveBoxes = session.createSelectionQuery("from ReceiveBox where is_received != :is_received", ReceiveBox.class)
.setParameter("is_received", true)
.getResultList();
session.getTransaction().commit();
return receiveBoxes;
} finally {
session.close();
}
}

/**
* 受取アイテムをプレイヤーから検索します
* @param player
* @return List<ReceiveBox>
*/
public List<ReceiveBox> getReceiveBoxesFromPlayer(Player player) {
Session session = this.sessionFactory.getCurrentSession();
UUID uuid = player.getUniqueId();
Date now = new Date();
try {
session.beginTransaction();
List<ReceiveBox> receiveBoxes = session.createQuery(
"from ReceiveBox where uuid = :uuid and is_received = 0 and expired_at > :now", ReceiveBox.class)
.setParameter("uuid", uuid.toString())
.setParameter("now", now)
.getResultList();
session.getTransaction().commit();
return receiveBoxes;
} finally {
session.close();
}
}


/**
* 受取アイテムを更新します
*
* @return ReceiveBox
*/
public ReceiveBox updateReceiveBox(ReceiveBox receiveBox){
Session session = this.sessionFactory.getCurrentSession();
try {
session.beginTransaction();
session.merge(receiveBox);
session.getTransaction().commit();
return receiveBox;
} finally {
session.close();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package space.yurisi.universecorev2.exception;

public class ReceiveBoxNotFoundException extends Exception{
public ReceiveBoxNotFoundException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import space.yurisi.universecorev2.subplugins.playerhead.PlayerHead;
import space.yurisi.universecorev2.subplugins.playerinfoscoreboard.PlayerInfoScoreBoard;
import space.yurisi.universecorev2.subplugins.rankcounter.RankCounter;
import space.yurisi.universecorev2.subplugins.receivebox.ReceiveBox;
import space.yurisi.universecorev2.subplugins.repaircream.RepairCream;
import space.yurisi.universecorev2.subplugins.snowsafeframe.SnowSafeFrame;
import space.yurisi.universecorev2.subplugins.tickfreezer.TickFreezer;
Expand Down Expand Up @@ -79,6 +80,7 @@ private void registerPlugin() {
this.subPlugins.add(new TickFreezer());
this.subPlugins.add(new UniverseGuns());
this.subPlugins.add(new SnowSafeFrame());
this.subPlugins.add(new ReceiveBox());
this.subPlugins.add(new Elevator());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import space.yurisi.universecorev2.UniverseCoreV2API;
import space.yurisi.universecorev2.database.models.Market;
import space.yurisi.universecorev2.database.models.Money;
import space.yurisi.universecorev2.exception.MarketItemNotFoundException;
import space.yurisi.universecorev2.exception.MoneyNotFoundException;
import space.yurisi.universecorev2.exception.UserNotFoundException;
import space.yurisi.universecorev2.subplugins.freemarket.data.Config;
import space.yurisi.universecorev2.subplugins.freemarket.data.JsonConverter;
import space.yurisi.universecorev2.utils.JsonConverter;
import space.yurisi.universecorev2.subplugins.freemarket.menu.MarketMenu;
import space.yurisi.universecorev2.subplugins.freemarket.menu.PurchasedItemMenu;
import space.yurisi.universecorev2.subplugins.freemarket.menu.RemoveMenu;
import space.yurisi.universecorev2.subplugins.universeeconomy.UniverseEconomyAPI;
import space.yurisi.universecorev2.subplugins.universeeconomy.exception.CanNotAddMoneyException;
import space.yurisi.universecorev2.subplugins.universeeconomy.exception.CanNotReduceMoneyException;
import space.yurisi.universecorev2.subplugins.universeeconomy.exception.ParameterException;
Expand Down
Loading

0 comments on commit 6d90878

Please sign in to comment.