-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from SpaceServerUniverse/dev/yurisi-receive-box
Dev/yurisi receive box
- Loading branch information
Showing
17 changed files
with
616 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
src/main/java/space/yurisi/universecorev2/database/models/ReceiveBox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/main/java/space/yurisi/universecorev2/database/repositories/ReceiveBoxRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/space/yurisi/universecorev2/exception/ReceiveBoxNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.