mirror of
https://github.com/JamesPeters98/ChestsPlusPlus.git
synced 2024-11-22 10:36:11 +01:00
Incomplete Database migrations.
Probably going to stick with in memory method for now.
This commit is contained in:
parent
816a1eb9cc
commit
23d9fae584
@ -56,25 +56,25 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-jpamodelgen</artifactId>
|
||||
<version>5.5.4.Final</version>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.20</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
|
||||
<!-- <version>3.8.1</version>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <annotationProcessorPaths>-->
|
||||
<!-- <path>-->
|
||||
<!-- <groupId>org.hibernate</groupId>-->
|
||||
<!-- <artifactId>hibernate-jpamodelgen</artifactId>-->
|
||||
<!-- <version>5.5.4.Final</version>-->
|
||||
<!-- </path>-->
|
||||
<!-- <path>-->
|
||||
<!-- <groupId>org.projectlombok</groupId>-->
|
||||
<!-- <artifactId>lombok</artifactId>-->
|
||||
<!-- <version>1.18.20</version>-->
|
||||
<!-- </path>-->
|
||||
<!-- </annotationProcessorPaths>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </plugin>-->
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
|
@ -23,7 +23,7 @@ public class HibernateUtil {
|
||||
private static Properties hibernateProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.put(Environment.DRIVER, "org.h2.Driver");
|
||||
properties.put(Environment.URL, "jdbc:h2:file:"+databaseDirectory.getAbsolutePath()+"/db;AUTO_SERVER=TRUE");
|
||||
properties.put(Environment.URL, "jdbc:h2:file:"+databaseDirectory.getAbsolutePath()+"/data/database");
|
||||
properties.put(Environment.USER, "sa");
|
||||
properties.put(Environment.PASS, "sa");
|
||||
properties.put(Environment.FORMAT_SQL, "true");
|
||||
|
@ -40,6 +40,7 @@
|
||||
<include>net.wesjd:anvilgui*</include>
|
||||
<include>org.bstats*:*</include>
|
||||
<include>com.jamesdpeters.minecraft.chests</include>
|
||||
<include>com.jamesdpeters*:*</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
<relocations>
|
||||
|
@ -184,7 +184,6 @@
|
||||
<classifier>jar-with-dependencies</classifier>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<!--Tell maven how to prepare and build our jar file from our source and dependancies-->
|
||||
|
@ -16,8 +16,9 @@ import com.jamesdpeters.minecraft.chests.misc.Permissions;
|
||||
import com.jamesdpeters.minecraft.chests.misc.ServerType;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Stats;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Utils;
|
||||
import com.jamesdpeters.minecraft.chests.party.PlayerParty;
|
||||
//import com.jamesdpeters.minecraft.chests.party.PlayerParty;
|
||||
import com.jamesdpeters.minecraft.chests.party.PlayerPartyStorage;
|
||||
import com.jamesdpeters.minecraft.chests.party.PlayerParty_OLD;
|
||||
import com.jamesdpeters.minecraft.chests.serialize.Config;
|
||||
import com.jamesdpeters.minecraft.chests.serialize.ConfigStorage;
|
||||
import com.jamesdpeters.minecraft.chests.serialize.LocationInfo;
|
||||
@ -83,7 +84,7 @@ public class ChestsPlusPlus extends JavaPlugin {
|
||||
ConfigurationSerialization.registerClass(RecipeSerializable.class, "Recipe");
|
||||
ConfigurationSerialization.registerClass(LocationInfo.class, "LocationInfo");
|
||||
ConfigurationSerialization.registerClass(PlayerPartyStorage.class, "PlayerPartyStorage");
|
||||
ConfigurationSerialization.registerClass(PlayerParty.class, "PlayerParty");
|
||||
ConfigurationSerialization.registerClass(PlayerParty_OLD.class, "PlayerParty");
|
||||
|
||||
}
|
||||
|
||||
@ -137,18 +138,6 @@ public class ChestsPlusPlus extends JavaPlugin {
|
||||
Utils.fixEntities();
|
||||
|
||||
DBUtil.init();
|
||||
var playerDb = DBUtil.PLAYER;
|
||||
var partyDb = DBUtil.PARTIES;
|
||||
|
||||
var test = playerDb.findPlayer(UUID.fromString("e0e93eb6-2ca4-4ac2-803f-684ce0b69b2c"));
|
||||
getLogger().info("Original parties: "+test.getOwnedParties());
|
||||
|
||||
var party = partyDb.findParty(test, "Test Party 2");
|
||||
party = party == null ? partyDb.createParty(test, "Test Party 2") : party;
|
||||
|
||||
getLogger().info("Found party: "+party);
|
||||
getLogger().info("Players parties: "+test.getOwnedParties());
|
||||
|
||||
|
||||
//Load storages after load.
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
|
||||
@ -163,14 +152,32 @@ public class ChestsPlusPlus extends JavaPlugin {
|
||||
getServer().getPluginManager().registerEvents(new WorldListener(), this);
|
||||
Config.getStorageTypes().forEach(storageType -> getServer().getPluginManager().registerEvents(storageType, this));
|
||||
getLogger().info("Chests++ enabled!");
|
||||
|
||||
// Test DB
|
||||
var playerDb = DBUtil.PLAYER;
|
||||
var partyDb = DBUtil.PARTIES;
|
||||
|
||||
var playerFuture = playerDb.findPlayer(UUID.fromString("e0e93eb6-2ca4-4ac2-803f-684ce0b69b2c"));
|
||||
|
||||
playerFuture.thenAccept(player -> {
|
||||
getLogger().info("Found player: "+player.getOfflinePlayer().getName());
|
||||
getLogger().info("Original Parties: "+player.getOwnedParties());
|
||||
|
||||
var partyName = "Test Party Name 2";
|
||||
var partyOpt = partyDb.findParty(player, partyName).join();
|
||||
var party = partyOpt.orElseGet(() -> partyDb.createParty(player, partyName).join());
|
||||
|
||||
getLogger().info("Found party: "+party);
|
||||
getLogger().info("Players parties: "+player.getOwnedParties());
|
||||
});
|
||||
}, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
super.onDisable();
|
||||
Config.save();
|
||||
HibernateUtil.close();
|
||||
super.onDisable();
|
||||
// //Remove entities that could have been left behind from bad save files/crashes etc.
|
||||
// Utils.fixEntities();
|
||||
}
|
||||
|
@ -2,10 +2,10 @@ package com.jamesdpeters.minecraft.chests.commands;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
|
||||
import com.jamesdpeters.minecraft.chests.api.ApiSpecific;
|
||||
import com.jamesdpeters.minecraft.chests.database.DBUtil;
|
||||
import com.jamesdpeters.minecraft.chests.maventemplates.BuildConstants;
|
||||
import com.jamesdpeters.minecraft.chests.menus.PartyMenu;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Utils;
|
||||
import com.jamesdpeters.minecraft.chests.party.PartyUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
@ -68,6 +68,11 @@ public class ChestsPlusPlusCommand extends ServerCommand {
|
||||
return true;
|
||||
|
||||
case PARTY:
|
||||
DBUtil.PLAYER.findPlayer(player).thenAccept(p -> {
|
||||
sender.sendMessage("Found player: "+p.getOfflinePlayer().getName());
|
||||
DBUtil.PARTIES.createParty(p, "Test Party");
|
||||
sender.sendMessage(String.valueOf(p.getOwnedParties()));
|
||||
});
|
||||
PartyMenu.getMenu(player).getMenu().open(player);
|
||||
return true;
|
||||
|
||||
@ -93,7 +98,7 @@ public class ChestsPlusPlusCommand extends ServerCommand {
|
||||
try {
|
||||
switch (OPTIONS.valueOf(args[0].toUpperCase())) {
|
||||
case PARTY:
|
||||
return Stream.of("menu", "create", "invite", "delete", "remove-member", "view-invites", "list").filter(s -> s.contains(args[1])).collect(Collectors.toList());
|
||||
return Stream.of("menu", "create", "invite", "remove", "remove-member", "view-invites", "list").filter(s -> s.contains(args[1])).collect(Collectors.toList());
|
||||
}
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
@ -103,8 +108,8 @@ public class ChestsPlusPlusCommand extends ServerCommand {
|
||||
switch (OPTIONS.valueOf(args[0].toUpperCase())) {
|
||||
case PARTY: {
|
||||
String arg = args[1];
|
||||
if (arg.equals("delete") || arg.equals("invite") || arg.equals("remove-member")) {
|
||||
List<String> strings = PartyUtils.getPlayerPartyStorage(player).getOwnedPartiesAsStrings();
|
||||
if (arg.equals("remove") || arg.equals("invite") || arg.equals("remove-member")) {
|
||||
List<String> strings = DBUtil.PLAYER.findPlayer(player).join().getOwnedPartyStrings();
|
||||
return Utils.filterList(strings, args[2]);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package com.jamesdpeters.minecraft.chests.database;
|
||||
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
|
||||
import com.jamesdpeters.minecraft.chests.database.dao.PlayerDatabase;
|
||||
import com.jamesdpeters.minecraft.chests.database.dao.PlayerPartyDatabase;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.Player;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.CppPlayer;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.PlayerParty;
|
||||
import com.jamesdpeters.minecraft.database.hibernate.HibernateUtil;
|
||||
|
||||
@ -13,7 +13,7 @@ public class DBUtil {
|
||||
* ALL HIBERNATE ENTITIES
|
||||
*/
|
||||
private final static Class<?>[] entities = {
|
||||
Player.class,
|
||||
CppPlayer.class,
|
||||
PlayerParty.class
|
||||
};
|
||||
|
||||
|
@ -1,9 +1,13 @@
|
||||
package com.jamesdpeters.minecraft.database.hibernate;
|
||||
package com.jamesdpeters.minecraft.chests.database.dao;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.misc.BukkitFuture;
|
||||
import com.jamesdpeters.minecraft.database.hibernate.HibernateUtil;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Database<T> {
|
||||
@ -19,6 +23,10 @@ public class Database<T> {
|
||||
runTransaction(entityManager -> entityManager.persist(object));
|
||||
}
|
||||
|
||||
public Future<Optional<T>> findEntityAsync(Object id) {
|
||||
return BukkitFuture.supplyAsync(() -> findEntity(id));
|
||||
}
|
||||
|
||||
public Optional<T> findEntity(Object id) {
|
||||
return Optional.ofNullable(HibernateUtil.getEntityManager().find(clazz, id));
|
||||
}
|
||||
@ -55,6 +63,10 @@ public class Database<T> {
|
||||
HibernateUtil.getEntityManager().refresh(entity);
|
||||
}
|
||||
|
||||
public void remove(T entity) {
|
||||
HibernateUtil.getEntityManager().remove(entity);
|
||||
}
|
||||
|
||||
public Class<T> clazz() {
|
||||
return clazz;
|
||||
}
|
@ -1,22 +1,23 @@
|
||||
package com.jamesdpeters.minecraft.chests.database.dao;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.Player;
|
||||
import com.jamesdpeters.minecraft.database.hibernate.Database;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.CppPlayer;
|
||||
import com.jamesdpeters.minecraft.chests.misc.BukkitFuture;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class PlayerDatabase extends Database<Player> {
|
||||
public class PlayerDatabase extends Database<CppPlayer> {
|
||||
|
||||
public PlayerDatabase() {
|
||||
super(Player.class);
|
||||
super(CppPlayer.class);
|
||||
}
|
||||
|
||||
public Player findPlayer(UUID uuid) {
|
||||
return findEntity(uuid).orElseGet(() -> new Player(uuid));
|
||||
public CompletableFuture<CppPlayer> findPlayer(UUID uuid) {
|
||||
return BukkitFuture.supplyAsync(() -> findEntity(uuid).orElseGet(() -> new CppPlayer(uuid)));
|
||||
}
|
||||
|
||||
public Player findPlayer(OfflinePlayer player) {
|
||||
public CompletableFuture<CppPlayer> findPlayer(OfflinePlayer player) {
|
||||
return findPlayer(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,13 @@
|
||||
package com.jamesdpeters.minecraft.chests.database.dao;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.database.DBUtil;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.Player;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.CppPlayer;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.PlayerParty;
|
||||
import com.jamesdpeters.minecraft.database.hibernate.Database;
|
||||
import com.jamesdpeters.minecraft.chests.misc.BukkitFuture;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class PlayerPartyDatabase extends Database<PlayerParty> {
|
||||
|
||||
@ -11,26 +15,96 @@ public class PlayerPartyDatabase extends Database<PlayerParty> {
|
||||
super(PlayerParty.class);
|
||||
}
|
||||
|
||||
public PlayerParty createParty(Player owner, String name) {
|
||||
PlayerParty playerParty = findParty(owner, name);
|
||||
public enum PartyResponse {
|
||||
NO_PARTY_EXISTS,
|
||||
MEMBER_NOT_PRESENT,
|
||||
COMPLETE
|
||||
}
|
||||
|
||||
if (playerParty != null)
|
||||
return playerParty;
|
||||
public CompletableFuture<PlayerParty> createParty(CppPlayer owner, String name) {
|
||||
return BukkitFuture.supplyAsync(() -> createPartyOnThread(owner, name));
|
||||
}
|
||||
|
||||
playerParty = new PlayerParty();
|
||||
playerParty.setOwner(owner);
|
||||
playerParty.setName(name);
|
||||
public CompletableFuture<PlayerParty> createParty(OfflinePlayer owner, String name) {
|
||||
return BukkitFuture.supplyAsync(() -> {
|
||||
var ownerPlayer = DBUtil.PLAYER.findPlayer(owner).join();
|
||||
return createPartyOnThread(ownerPlayer, name);
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Boolean> deleteParty(CppPlayer owner, String name) {
|
||||
return BukkitFuture.supplyAsync(() -> deletePartyOnThread(owner, name));
|
||||
}
|
||||
|
||||
public CompletableFuture<Optional<PlayerParty>> findParty(CppPlayer owner, String name) {
|
||||
return BukkitFuture.supplyAsync(() -> findPartyOnThread(owner, name));
|
||||
}
|
||||
|
||||
public CompletableFuture<Optional<PlayerParty>> findParty(OfflinePlayer owner, String name) {
|
||||
return BukkitFuture.supplyAsync(() -> {
|
||||
var ownerPlayer = DBUtil.PLAYER.findPlayer(owner).join();
|
||||
return findPartyOnThread(ownerPlayer, name);
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<PartyResponse> removePlayerFromParty(OfflinePlayer owner, String partyName, OfflinePlayer playerToRemove) {
|
||||
return BukkitFuture.supplyAsync(() -> {
|
||||
var ownerPlayer = DBUtil.PLAYER.findPlayer(owner).join();
|
||||
var party = ownerPlayer.getParty(partyName);
|
||||
if (party.isPresent()) {
|
||||
var p = party.get();
|
||||
var didRemove = p.removeMember(playerToRemove);
|
||||
if (didRemove) {
|
||||
DBUtil.PLAYER.saveEntity(ownerPlayer);
|
||||
}
|
||||
return didRemove ? PartyResponse.COMPLETE : PartyResponse.MEMBER_NOT_PRESENT;
|
||||
}
|
||||
return PartyResponse.NO_PARTY_EXISTS;
|
||||
});
|
||||
}
|
||||
|
||||
/* *******************
|
||||
* MAIN THREAD METHODS
|
||||
* *******************/
|
||||
|
||||
private Optional<PlayerParty> findPartyOnThread(CppPlayer owner, String name) {
|
||||
var query = getQuery("from PlayerParty where owner.playerUUID = :ownerId and name = :name");
|
||||
query.setParameter("ownerId", owner.getPlayerUUID());
|
||||
query.setParameter("name", name);
|
||||
query.setMaxResults(1);
|
||||
var results = query.getResultList();
|
||||
if (results.size() == 1)
|
||||
return Optional.of(results.get(0));
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private PlayerParty createPartyOnThread(CppPlayer owner, String name) {
|
||||
var playerPartyOptional = findPartyOnThread(owner, name);
|
||||
|
||||
var playerParty = playerPartyOptional.orElseGet(() -> {
|
||||
var party = new PlayerParty();
|
||||
party.setOwner(owner);
|
||||
party.setName(name);
|
||||
return party;
|
||||
});
|
||||
|
||||
saveEntity(playerParty);
|
||||
DBUtil.PLAYER.refresh(owner);
|
||||
return playerParty;
|
||||
}
|
||||
|
||||
public PlayerParty findParty(Player owner, String name) {
|
||||
var query = getQuery("from PlayerParty where owner.playerUUID = :ownerId and name = :name");
|
||||
query.setParameter("ownerId", owner.getPlayerUUID());
|
||||
query.setParameter("name", name);
|
||||
return query.getSingleResult();
|
||||
private boolean deletePartyOnThread(CppPlayer owner, String name) {
|
||||
var playerPartyOptional = findPartyOnThread(owner, name);
|
||||
if (playerPartyOptional.isPresent()) {
|
||||
var playerParty = playerPartyOptional.get();
|
||||
DBUtil.PARTIES.remove(playerParty);
|
||||
DBUtil.PLAYER.refresh(owner);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void deleteParty(PlayerParty party) {
|
||||
DBUtil.PARTIES.remove(party);
|
||||
DBUtil.PLAYER.refresh(party.getOwner());
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,74 @@
|
||||
package com.jamesdpeters.minecraft.chests.database.entities;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Objects.nonNull;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@Table(name = "Player")
|
||||
public class CppPlayer {
|
||||
|
||||
public CppPlayer(UUID uuid) {
|
||||
playerUUID = uuid;
|
||||
}
|
||||
|
||||
@Id
|
||||
private UUID playerUUID;
|
||||
|
||||
@JoinColumn(name = "cpp_player_player_uuid")
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@ToString.Exclude
|
||||
private Set<PlayerParty> ownedParties = new HashSet<>();
|
||||
|
||||
@ManyToMany(mappedBy = "members", cascade = CascadeType.ALL)
|
||||
@ToString.Exclude
|
||||
private Set<PlayerParty> playerParties = new HashSet<>();
|
||||
|
||||
public OfflinePlayer getOfflinePlayer() {
|
||||
return Bukkit.getOfflinePlayer(playerUUID);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return getOfflinePlayer().getName();
|
||||
}
|
||||
|
||||
public Optional<PlayerParty> getParty(String partyName) {
|
||||
return nonNull(playerParties)
|
||||
? playerParties.stream().filter(playerParty -> playerParty.getName().equals(partyName)).findFirst()
|
||||
: Optional.empty();
|
||||
}
|
||||
|
||||
public Set<PlayerParty> getAllParties() {
|
||||
return Stream.concat(ownedParties.stream(), playerParties.stream()).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public boolean isMemberOfParty(OfflinePlayer player) {
|
||||
return playerParties.stream().anyMatch(playerParty -> playerParty.isMember(player));
|
||||
}
|
||||
|
||||
public List<String> getOwnedPartyStrings() {
|
||||
return ownedParties.stream().map(PlayerParty::getName).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package com.jamesdpeters.minecraft.chests.database.entities;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class Player {
|
||||
|
||||
public Player(UUID uuid) {
|
||||
playerUUID = uuid;
|
||||
}
|
||||
|
||||
@Id
|
||||
private UUID playerUUID;
|
||||
|
||||
@OneToMany(mappedBy = "owner")
|
||||
@ToString.Exclude
|
||||
private List<PlayerParty> ownedParties;
|
||||
|
||||
@ManyToMany(mappedBy = "members")
|
||||
@ToString.Exclude
|
||||
private List<PlayerParty> playerParties;
|
||||
|
||||
public OfflinePlayer getOfflinePlayer() {
|
||||
return Bukkit.getOfflinePlayer(playerUUID);
|
||||
}
|
||||
}
|
@ -4,17 +4,22 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@ -34,10 +39,29 @@ public class PlayerParty {
|
||||
private String name;
|
||||
|
||||
@JoinColumn(nullable = false)
|
||||
@ManyToOne(optional = false)
|
||||
private Player owner;
|
||||
@ManyToOne(cascade = CascadeType.ALL, optional = false)
|
||||
private CppPlayer owner;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(name = "PLAYER_PARTY_CPP_PLAYER",
|
||||
joinColumns = @JoinColumn(name = "PLAYER_PARTY_ID"),
|
||||
inverseJoinColumns = @JoinColumn(name = "CPP_PLAYER_ID"))
|
||||
@ManyToMany(cascade = {CascadeType.ALL})
|
||||
@ToString.Exclude
|
||||
private List<Player> members;
|
||||
private Set<CppPlayer> members = new HashSet<>();
|
||||
|
||||
public boolean removeMember(OfflinePlayer player) {
|
||||
return members.removeIf(p -> p.getPlayerUUID().equals(player.getUniqueId()));
|
||||
}
|
||||
|
||||
public boolean addMember(CppPlayer player) {
|
||||
return members.add(player);
|
||||
}
|
||||
|
||||
public Set<OfflinePlayer> getBukkitMembers() {
|
||||
return members.stream().map(CppPlayer::getOfflinePlayer).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public boolean isMember(OfflinePlayer player) {
|
||||
return owner.getOfflinePlayer() == player || members.stream().anyMatch(cppPlayer -> cppPlayer.getOfflinePlayer() == player);
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ public class InvitesMenu implements InventoryProvider {
|
||||
|
||||
for (PartyInvite invite : PartyUtils.getPartyInvites(player)){
|
||||
// Pass through click to the menus onPlayerSelect function.
|
||||
ItemStack inviteBook = ItemBuilder.getInstance(Material.ENCHANTED_BOOK).setName(invite.getParty().getPartyName()).get();
|
||||
ItemStack inviteBook = ItemBuilder.getInstance(Material.ENCHANTED_BOOK).setName(invite.getParty().getName()).get();
|
||||
ClickableItem clickableItem = ClickableItem.from(inviteBook, itemClickData -> onPlayerSelect.accept(invite, menu));
|
||||
itemList.add(clickableItem);
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class PartyMenu implements InventoryProvider {
|
||||
|
||||
@ -137,7 +138,7 @@ public class PartyMenu implements InventoryProvider {
|
||||
return;
|
||||
}
|
||||
TextInputUI.getInput(player, Message.PARTY_ENTER_NAME.getString(), (p, partyName) -> {
|
||||
boolean result = PartyUtils.createParty(player, partyName);
|
||||
var result = PartyUtils.createParty(player, partyName).join();
|
||||
if (result){
|
||||
player.sendMessage(ChatColor.GREEN+ Message.PARTY_CREATED.getString(ChatColor.WHITE+partyName+ChatColor.GREEN));
|
||||
getMenu().open(player);
|
||||
@ -155,8 +156,8 @@ public class PartyMenu implements InventoryProvider {
|
||||
return;
|
||||
}
|
||||
PartySelectorMenu.open(player, getMenu(), PartySelectorMenu.Type.OWNED, (party, menu) -> {
|
||||
List<OfflinePlayer> inviteablePlayers = Utils.getOnlinePlayersNotInList(party.getMembers());
|
||||
inviteablePlayers.remove(party.getOwner());
|
||||
Set<OfflinePlayer> inviteablePlayers = Utils.getOnlinePlayersNotInList(party.getBukkitMembers());
|
||||
inviteablePlayers.remove(party.getOwner().getOfflinePlayer());
|
||||
PlayerSelectorMenu.open(player, Message.PARTY_INVITE_PLAYER.getString(), menu, inviteablePlayers, (player1, itemStack) -> itemStack, (playerToInvite, menu2) -> {
|
||||
PartyUtils.invitePlayer(party, playerToInvite);
|
||||
getMenu().open(player);
|
||||
@ -166,7 +167,7 @@ public class PartyMenu implements InventoryProvider {
|
||||
|
||||
public void removePlayer(Player player) {
|
||||
PartySelectorMenu.open(player, getMenu(), PartySelectorMenu.Type.OWNED, (party, menu) -> {
|
||||
PlayerSelectorMenu.open(player, Message.PARTY_REMOVE_PLAYER.getString(), menu, party.getMembers(), (player1, itemStack) -> itemStack, (selectedPlayer, menu2) -> {
|
||||
PlayerSelectorMenu.open(player, Message.PARTY_REMOVE_PLAYER.getString(), menu, party.getBukkitMembers(), (player1, itemStack) -> itemStack, (selectedPlayer, menu2) -> {
|
||||
AcceptDialogMenu.open(player, Message.PARTY_REMOVE_PLAYER_DIALOG.getString(selectedPlayer.getName()), Message.YES.getString(), Message.NO.getString(), aBoolean -> {
|
||||
if (aBoolean) {
|
||||
party.removeMember(selectedPlayer);
|
||||
@ -179,10 +180,10 @@ public class PartyMenu implements InventoryProvider {
|
||||
|
||||
public void listParties(Player player) {
|
||||
PartySelectorMenu.open(player, getMenu(), PartySelectorMenu.Type.ALL, (party, smartInventory) -> {
|
||||
PlayerSelectorMenu.open(player, Message.PARTY_MEMBERS.getString(party.getPartyName()), smartInventory, party.getAllPlayers(),
|
||||
PlayerSelectorMenu.open(player, Message.PARTY_MEMBERS.getString(party.getName()), smartInventory, party.getBukkitMembers(),
|
||||
// Change player head to enchanted if owner.
|
||||
(offlinePlayer, itemStack) -> {
|
||||
if (party.getOwner().getUniqueId().equals(offlinePlayer.getUniqueId())) return ItemBuilder.fromInstance(itemStack).addLore(Message.PARTY_OWNER.getString()).get();
|
||||
if (party.getOwner().getPlayerUUID().equals(offlinePlayer.getUniqueId())) return ItemBuilder.fromInstance(itemStack).addLore(Message.PARTY_OWNER.getString()).get();
|
||||
return itemStack;
|
||||
} ,
|
||||
(playerSelected, smartInventory1) -> {
|
||||
@ -193,14 +194,14 @@ public class PartyMenu implements InventoryProvider {
|
||||
|
||||
public void deleteParty(Player player) {
|
||||
PartySelectorMenu.open(player, getMenu(), PartySelectorMenu.Type.OWNED, (party, smartInventory) -> {
|
||||
AcceptDialogMenu.open(player, Message.PARTY_DELETE.getString(party.getPartyName()), Message.YES.getString(), Message.NO.getString(), aBoolean -> {
|
||||
AcceptDialogMenu.open(player, Message.PARTY_DELETE.getString(party.getName()), Message.YES.getString(), Message.NO.getString(), aBoolean -> {
|
||||
// If user accepts
|
||||
if (aBoolean) {
|
||||
boolean result = PartyUtils.deleteParty(party);
|
||||
if (result) {
|
||||
player.sendMessage(ChatColor.GREEN + Message.PARTY_DELETED.getString(ChatColor.WHITE + party.getPartyName() + ChatColor.GREEN));
|
||||
player.sendMessage(ChatColor.GREEN + Message.PARTY_DELETED.getString(ChatColor.WHITE + party.getName() + ChatColor.GREEN));
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED+Message.PARTY_DOESNT_EXIST.getString(ChatColor.WHITE+party.getPartyName()+ChatColor.RED));
|
||||
player.sendMessage(ChatColor.RED+Message.PARTY_DOESNT_EXIST.getString(ChatColor.WHITE+party.getName()+ChatColor.RED));
|
||||
}
|
||||
// Call deleteParty() to refresh party list.
|
||||
deleteParty(player);
|
||||
@ -218,7 +219,7 @@ public class PartyMenu implements InventoryProvider {
|
||||
return;
|
||||
}
|
||||
InvitesMenu.open(player, getMenu(), (invite, smartInventory) -> {
|
||||
AcceptDialogMenu.open(player, Message.PARTY_JOIN.getString(invite.getParty().getOwner().getName(), invite.getParty().getPartyName()), Message.YES.getString(), Message.NO.getString(), aBoolean -> {
|
||||
AcceptDialogMenu.open(player, Message.PARTY_JOIN.getString(invite.getParty().getOwner().getName(), invite.getParty().getName()), Message.YES.getString(), Message.NO.getString(), aBoolean -> {
|
||||
if (aBoolean) {
|
||||
PartyUtils.acceptInvite(player, invite);
|
||||
getMenu().open(player);
|
||||
@ -232,7 +233,7 @@ public class PartyMenu implements InventoryProvider {
|
||||
|
||||
public void leaveParty(Player player) {
|
||||
PartySelectorMenu.open(player, getMenu(), PartySelectorMenu.Type.MEMBER_OF, (party, smartInventory) -> {
|
||||
AcceptDialogMenu.open(player, Message.PARTY_LEAVE.getString(party.getPartyName()), Message.YES.getString(), Message.NO.getString(), aBoolean -> {
|
||||
AcceptDialogMenu.open(player, Message.PARTY_LEAVE.getString(party.getName()), Message.YES.getString(), Message.NO.getString(), aBoolean -> {
|
||||
if (aBoolean) {
|
||||
party.removeMember(player);
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.jamesdpeters.minecraft.chests.menus;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
|
||||
import com.jamesdpeters.minecraft.chests.database.DBUtil;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.PlayerParty;
|
||||
import com.jamesdpeters.minecraft.chests.misc.ItemBuilder;
|
||||
import com.jamesdpeters.minecraft.chests.party.PartyUtils;
|
||||
import com.jamesdpeters.minecraft.chests.party.PlayerParty;
|
||||
import fr.minuskube.inv.ClickableItem;
|
||||
import fr.minuskube.inv.SmartInventory;
|
||||
import fr.minuskube.inv.content.InventoryContents;
|
||||
@ -16,6 +16,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class PartySelectorMenu implements InventoryProvider {
|
||||
@ -62,7 +63,7 @@ public class PartySelectorMenu implements InventoryProvider {
|
||||
// Pass through click to the menus onPlayerSelect function.
|
||||
ItemStack partyBook = ItemBuilder
|
||||
.getInstance(Material.ENCHANTED_BOOK)
|
||||
.setName(playerParty.getPartyName())
|
||||
.setName(playerParty.getName())
|
||||
.addLore("Owner: "+playerParty.getOwner().getName())
|
||||
.get();
|
||||
ClickableItem clickableItem = ClickableItem.from(partyBook, itemClickData -> onPlayerSelect.accept(playerParty, menu));
|
||||
@ -109,16 +110,12 @@ public class PartySelectorMenu implements InventoryProvider {
|
||||
return menu;
|
||||
}
|
||||
|
||||
private List<PlayerParty> getParties(OfflinePlayer player) {
|
||||
switch (type) {
|
||||
case ALL:
|
||||
return PartyUtils.getPlayerPartyStorage(player).getAllParties();
|
||||
case OWNED:
|
||||
return PartyUtils.getPlayerPartyStorage(player).getOwnedPartiesList();
|
||||
case MEMBER_OF:
|
||||
return PartyUtils.getPlayerPartyStorage(player).getPartiesMemberOf();
|
||||
default:
|
||||
return new ArrayList<>();
|
||||
}
|
||||
private Set<PlayerParty> getParties(OfflinePlayer player) {
|
||||
var cppPlayer = DBUtil.PLAYER.findPlayer(player).join();
|
||||
return switch (type) {
|
||||
case ALL -> cppPlayer.getAllParties();
|
||||
case OWNED -> cppPlayer.getOwnedParties();
|
||||
case MEMBER_OF -> cppPlayer.getPlayerParties();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
@ -22,7 +23,7 @@ public class PlayerSelectorMenu implements InventoryProvider {
|
||||
private final SmartInventory menu;
|
||||
BiConsumer<OfflinePlayer, SmartInventory> onPlayerSelect;
|
||||
BiFunction<OfflinePlayer, ItemStack, ItemStack> onPlayerHeadCreation;
|
||||
private List<OfflinePlayer> players;
|
||||
private Set<OfflinePlayer> players;
|
||||
private int lastPage; // Store the last page the player was on.
|
||||
private SmartInventory previousInv;
|
||||
|
||||
@ -37,7 +38,7 @@ public class PlayerSelectorMenu implements InventoryProvider {
|
||||
//menu.setInsertable(true);
|
||||
}
|
||||
|
||||
public static void open(Player player, String title, SmartInventory previousInv, List<OfflinePlayer> players, BiFunction<OfflinePlayer, ItemStack, ItemStack> onPlayerHeadCreation, BiConsumer<OfflinePlayer, SmartInventory> onPlayerSelect) {
|
||||
public static void open(Player player, String title, SmartInventory previousInv, Set<OfflinePlayer> players, BiFunction<OfflinePlayer, ItemStack, ItemStack> onPlayerHeadCreation, BiConsumer<OfflinePlayer, SmartInventory> onPlayerSelect) {
|
||||
PlayerSelectorMenu playerSelectorMenu = new PlayerSelectorMenu(title);
|
||||
playerSelectorMenu.players = players;
|
||||
playerSelectorMenu.onPlayerHeadCreation = onPlayerHeadCreation;
|
||||
|
@ -0,0 +1,19 @@
|
||||
package com.jamesdpeters.minecraft.chests.misc;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* CompleteableFuture that runs the {@link #thenAccept(Consumer)} method on the Bukkit main thread.
|
||||
* @param <T>
|
||||
*/
|
||||
public class BukkitFuture<T> extends CompletableFuture<T> {
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> thenAccept(Consumer<? super T> action) {
|
||||
return super.thenAccept(t -> Bukkit.getScheduler().runTask(ChestsPlusPlus.PLUGIN, () -> action.accept(t)));
|
||||
}
|
||||
}
|
@ -37,6 +37,7 @@ import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.stream.Collectors;
|
||||
@ -338,13 +339,11 @@ public class Utils {
|
||||
}
|
||||
}
|
||||
|
||||
public static List<OfflinePlayer> getOnlinePlayersNotInList(List<OfflinePlayer> players) {
|
||||
List<OfflinePlayer> list = new ArrayList<>();
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (!players.contains(onlinePlayer)){
|
||||
list.add(onlinePlayer);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
public static Set<OfflinePlayer> getOnlinePlayersNotInList(Set<OfflinePlayer> players) {
|
||||
return Bukkit.getOnlinePlayers()
|
||||
.stream()
|
||||
.filter(player -> !players.contains(player))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.jamesdpeters.minecraft.chests.party;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.CppPlayer;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.PlayerParty;
|
||||
import com.jamesdpeters.minecraft.chests.lang.Message;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@ -7,26 +9,27 @@ import org.bukkit.entity.Player;
|
||||
|
||||
public class PartyInvite {
|
||||
|
||||
OfflinePlayer owner, player;
|
||||
CppPlayer owner, player;
|
||||
PlayerParty party;
|
||||
boolean pending;
|
||||
|
||||
public PartyInvite(OfflinePlayer owner, OfflinePlayer player, PlayerParty party) {
|
||||
public PartyInvite(CppPlayer owner, CppPlayer player, PlayerParty party) {
|
||||
this.owner = owner;
|
||||
this.player = player;
|
||||
this.party = party;
|
||||
}
|
||||
|
||||
public void sendInvite() {
|
||||
Player onlinePlayer = player.getPlayer();
|
||||
OfflinePlayer offlinePlayer = player.getOfflinePlayer();
|
||||
Player onlinePlayer = player.getOfflinePlayer().getPlayer();
|
||||
if(onlinePlayer != null) {
|
||||
onlinePlayer.sendMessage(ChatColor.GREEN+Message.PARTY_INVITE.getString(ChatColor.WHITE+player.getName()+ChatColor.GREEN, ChatColor.WHITE+party.getPartyName()+ChatColor.GREEN));
|
||||
onlinePlayer.sendMessage(ChatColor.GREEN+Message.PARTY_INVITE.getString(ChatColor.WHITE+ player.getName()+ChatColor.GREEN, ChatColor.WHITE+party.getName()+ChatColor.GREEN));
|
||||
String tellraw = "tellraw @p {\"text\":\""+Message.PARTY_ACCEPT_INVITE.getString()+"\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/c++ party view-invites\"}}";
|
||||
onlinePlayer.performCommand(tellraw);
|
||||
}
|
||||
Player onlineOwner = owner.getPlayer();
|
||||
Player onlineOwner = owner.getOfflinePlayer().getPlayer();
|
||||
if (onlineOwner != null){
|
||||
onlineOwner.sendMessage(ChatColor.GREEN+Message.PARTY_INVITE_OWNER.getString(ChatColor.WHITE+player.getName()+ChatColor.GREEN, ChatColor.WHITE+party.getPartyName()+ChatColor.GREEN));
|
||||
onlineOwner.sendMessage(ChatColor.GREEN+Message.PARTY_INVITE_OWNER.getString(ChatColor.WHITE+ player.getName()+ChatColor.GREEN, ChatColor.WHITE+party.getName()+ChatColor.GREEN));
|
||||
}
|
||||
pending = true;
|
||||
}
|
||||
@ -34,9 +37,9 @@ public class PartyInvite {
|
||||
public void acceptInvite(){
|
||||
if (pending){
|
||||
party.addMember(player);
|
||||
Player onlinePlayer = player.getPlayer();
|
||||
Player onlinePlayer = player.getOfflinePlayer().getPlayer();
|
||||
if (onlinePlayer != null){
|
||||
onlinePlayer.sendMessage(ChatColor.GREEN+Message.PARTY_JOINED.getString(ChatColor.WHITE+owner.getName()+ChatColor.GREEN, ChatColor.WHITE+party.getPartyName()+ChatColor.GREEN));
|
||||
onlinePlayer.sendMessage(ChatColor.GREEN+Message.PARTY_JOINED.getString(ChatColor.WHITE+owner.getName()+ChatColor.GREEN, ChatColor.WHITE+party.getName()+ChatColor.GREEN));
|
||||
}
|
||||
pending = false;
|
||||
}
|
||||
|
@ -1,15 +1,18 @@
|
||||
package com.jamesdpeters.minecraft.chests.party;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.database.DBUtil;
|
||||
import com.jamesdpeters.minecraft.chests.database.dao.PlayerDatabase;
|
||||
import com.jamesdpeters.minecraft.chests.database.dao.PlayerPartyDatabase;
|
||||
import com.jamesdpeters.minecraft.chests.database.entities.PlayerParty;
|
||||
import com.jamesdpeters.minecraft.chests.lang.Message;
|
||||
import com.jamesdpeters.minecraft.chests.serialize.Config;
|
||||
import org.bukkit.ChatColor;
|
||||
import com.jamesdpeters.minecraft.chests.misc.BukkitFuture;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class PartyUtils {
|
||||
|
||||
@ -18,7 +21,10 @@ public class PartyUtils {
|
||||
PARTY_CREATED
|
||||
}
|
||||
|
||||
// Stores party invites sent to this player.
|
||||
private static final PlayerPartyDatabase partyDb = DBUtil.PARTIES;
|
||||
private static final PlayerDatabase playerDb = DBUtil.PLAYER;
|
||||
|
||||
// Stores party invites sent to this cppPlayer.
|
||||
// Only stored in memory
|
||||
private static final HashMap<UUID, List<PartyInvite>> partyInvites = new HashMap<>();
|
||||
|
||||
@ -33,24 +39,23 @@ public class PartyUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Invites a player to the owners given party. The last pending invite is overwritten.
|
||||
* Invites a cppPlayer to the owners given party. The last pending invite is overwritten.
|
||||
* @param owner
|
||||
* @param playerToInvite
|
||||
* @param partyName
|
||||
*/
|
||||
public static void invitePlayer(OfflinePlayer owner, OfflinePlayer playerToInvite, String partyName){
|
||||
PlayerPartyStorage storage = getPlayerPartyStorage(owner);
|
||||
|
||||
PlayerParty party = storage.getOwnedParties().get(partyName);
|
||||
if (party == null){
|
||||
Player onlineOwner = owner.getPlayer();
|
||||
if(onlineOwner != null) {
|
||||
onlineOwner.sendMessage(Message.PARTY_DOESNT_EXIST.getString(partyName));
|
||||
playerDb.findPlayer(owner).thenAccept(player -> {
|
||||
var party = player.getParty(partyName);
|
||||
if (party.isPresent()) {
|
||||
invitePlayer(party.get(), playerToInvite);
|
||||
} else {
|
||||
var onlinePlayer = owner.getPlayer();
|
||||
if (onlinePlayer != null) {
|
||||
onlinePlayer.sendMessage(Message.PARTY_DOESNT_EXIST.getString(partyName));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
invitePlayer(party, playerToInvite);
|
||||
});
|
||||
}
|
||||
|
||||
private static void addPlayerInvite(OfflinePlayer player, PartyInvite invite) {
|
||||
@ -59,28 +64,28 @@ public class PartyUtils {
|
||||
}
|
||||
|
||||
public static void invitePlayer(PlayerParty party, OfflinePlayer playerToInvite) {
|
||||
PartyInvite invite = new PartyInvite(party.getOwner(), playerToInvite, party);
|
||||
playerDb.findPlayer(playerToInvite).thenAccept(player -> {
|
||||
PartyInvite invite = new PartyInvite(party.getOwner(), player, party);
|
||||
addPlayerInvite(playerToInvite, invite);
|
||||
invite.sendInvite();
|
||||
});
|
||||
}
|
||||
|
||||
public static void removePlayer(OfflinePlayer owner, OfflinePlayer playerToRemove, String partyName){
|
||||
PlayerPartyStorage storage = getPlayerPartyStorage(owner);
|
||||
|
||||
PlayerParty party = storage.getOwnedParties().get(partyName);
|
||||
if (party == null){
|
||||
Player onlineOwner = owner.getPlayer();
|
||||
if(onlineOwner != null) {
|
||||
onlineOwner.sendMessage(Message.PARTY_DOESNT_EXIST.getString(partyName));
|
||||
partyDb
|
||||
.removePlayerFromParty(owner, partyName, playerToRemove)
|
||||
.thenAccept(partyResponse -> {
|
||||
var onlinePlayer = owner.getPlayer();
|
||||
if (onlinePlayer != null) {
|
||||
if (partyResponse == PlayerPartyDatabase.PartyResponse.NO_PARTY_EXISTS) {
|
||||
onlinePlayer.sendMessage(Message.PARTY_DOESNT_EXIST.getString(partyName));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
party.removeMember(playerToRemove);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts the current pending invite for this player.
|
||||
* Accepts the current pending invite for this cppPlayer.
|
||||
* @param player
|
||||
*/
|
||||
public static void acceptInvite(OfflinePlayer player, PartyInvite invite){
|
||||
@ -105,49 +110,19 @@ public class PartyUtils {
|
||||
* @param partyName
|
||||
* @return false if party already exists.
|
||||
*/
|
||||
public static boolean createParty(OfflinePlayer owner, String partyName){
|
||||
PlayerPartyStorage storage = getPlayerPartyStorage(owner);
|
||||
|
||||
// Check if party already exists.
|
||||
if (storage.getOwnedParties().containsKey(partyName)) return false;
|
||||
|
||||
storage.getOwnedParties().put(partyName, new PlayerParty(owner, partyName));
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean deleteParty(OfflinePlayer owner, String partyName){
|
||||
HashMap<String, PlayerPartyStorage> map = Config.getStore().parties;
|
||||
PlayerPartyStorage storage = map.get(owner.getUniqueId().toString());
|
||||
if (storage == null) {
|
||||
storage = new PlayerPartyStorage(owner);
|
||||
map.put(owner.getUniqueId().toString(), storage);
|
||||
}
|
||||
|
||||
// Check if party already exists.
|
||||
if (!storage.getOwnedParties().containsKey(partyName)) {
|
||||
Player onlineOwner = owner.getPlayer();
|
||||
if (onlineOwner != null) {
|
||||
onlineOwner.sendMessage(ChatColor.RED + Message.PARTY_DOESNT_EXIST.getString(partyName));
|
||||
}
|
||||
public static CompletableFuture<Boolean> createParty(OfflinePlayer owner, String partyName){
|
||||
return BukkitFuture.supplyAsync(() -> {
|
||||
var party = DBUtil.PARTIES.findParty(owner, partyName).join();
|
||||
if (party.isPresent())
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove party
|
||||
storage.getOwnedParties().remove(partyName);
|
||||
DBUtil.PARTIES.createParty(owner, partyName);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean deleteParty(PlayerParty party){
|
||||
return deleteParty(party.getOwner(), party.getPartyName());
|
||||
}
|
||||
|
||||
public static PlayerPartyStorage getPlayerPartyStorage(OfflinePlayer owner) {
|
||||
HashMap<String, PlayerPartyStorage> map = Config.getStore().parties;
|
||||
PlayerPartyStorage storage = map.get(owner.getUniqueId().toString());
|
||||
if (storage == null){
|
||||
storage = new PlayerPartyStorage(owner);
|
||||
map.put(owner.getUniqueId().toString(), storage);
|
||||
}
|
||||
return storage;
|
||||
DBUtil.PARTIES.remove(party);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,12 @@
|
||||
package com.jamesdpeters.minecraft.chests.party;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.serialize.Config;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -18,7 +14,7 @@ import java.util.UUID;
|
||||
public class PlayerPartyStorage implements ConfigurationSerializable {
|
||||
|
||||
private OfflinePlayer owner;
|
||||
private HashMap<String, PlayerParty> ownedParties;
|
||||
private HashMap<String, PlayerParty_OLD> ownedParties;
|
||||
|
||||
public PlayerPartyStorage(OfflinePlayer owner){
|
||||
this.owner = owner;
|
||||
@ -30,7 +26,7 @@ public class PlayerPartyStorage implements ConfigurationSerializable {
|
||||
UUID ownerUUID = UUID.fromString((String) map.get("owner"));
|
||||
owner = Bukkit.getOfflinePlayer(ownerUUID);
|
||||
|
||||
ownedParties = (HashMap<String, PlayerParty>) map.get("ownedParties");
|
||||
ownedParties = (HashMap<String, PlayerParty_OLD>) map.get("ownedParties");
|
||||
if (ownedParties == null) ownedParties = new HashMap<>();
|
||||
}
|
||||
|
||||
@ -41,68 +37,68 @@ public class PlayerPartyStorage implements ConfigurationSerializable {
|
||||
map.put("ownedParties", ownedParties);
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the owners parties to see if the given player is a member.
|
||||
*/
|
||||
public static boolean doPlayersShareParty(OfflinePlayer owner, OfflinePlayer player){
|
||||
PlayerPartyStorage storage = Config.getStore().parties.get(owner.getUniqueId().toString());
|
||||
if (storage != null){
|
||||
for (PlayerParty ownedParty : storage.ownedParties.values()) {
|
||||
if (ownedParty.getMembers().contains(player)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public OfflinePlayer getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public HashMap<String, PlayerParty> getOwnedParties() {
|
||||
return ownedParties;
|
||||
}
|
||||
|
||||
public List<PlayerParty> getOwnedPartiesList() {
|
||||
return new ArrayList<>(ownedParties.values());
|
||||
}
|
||||
|
||||
public Collection<PlayerParty> getOwnedPartiesCollection() {
|
||||
return ownedParties.values();
|
||||
}
|
||||
|
||||
public List<String> getOwnedPartiesAsStrings(){
|
||||
List<String> strings = new ArrayList<>();
|
||||
ownedParties.values().forEach(party -> strings.add(party.getPartyName()));
|
||||
return strings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List of Parties this player is a member of, not including their owned parties.
|
||||
* @return
|
||||
*/
|
||||
public List<PlayerParty> getPartiesMemberOf() {
|
||||
// Create list containing all owned parties
|
||||
List<PlayerParty> parties = new ArrayList<>();
|
||||
|
||||
Config.getStore().parties.values().forEach(playerPartyStorage -> {
|
||||
playerPartyStorage.getOwnedPartiesCollection().forEach(party -> {
|
||||
if (party.isMember(getOwner())) parties.add(party);
|
||||
});
|
||||
});
|
||||
|
||||
return parties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns ALL parties this player is a member of;.
|
||||
* @return
|
||||
*/
|
||||
public List<PlayerParty> getAllParties() {
|
||||
// Create list containing all owned parties
|
||||
List<PlayerParty> parties = getPartiesMemberOf();
|
||||
parties.addAll(getOwnedPartiesCollection());
|
||||
|
||||
return parties;
|
||||
}
|
||||
//
|
||||
// /**
|
||||
// * Checks the owners parties to see if the given cppPlayer is a member.
|
||||
// */
|
||||
// public static boolean doPlayersShareParty(OfflinePlayer owner, OfflinePlayer player){
|
||||
// PlayerPartyStorage storage = Config.getStore().parties.get(owner.getUniqueId().toString());
|
||||
// if (storage != null){
|
||||
// for (PlayerParty_OLD ownedParty : storage.ownedParties.values()) {
|
||||
// if (ownedParty.getMembers().contains(player)) return true;
|
||||
// }
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// public OfflinePlayer getOwner() {
|
||||
// return owner;
|
||||
// }
|
||||
//
|
||||
// public HashMap<String, PlayerParty_OLD> getOwnedParties() {
|
||||
// return ownedParties;
|
||||
// }
|
||||
//
|
||||
// public List<PlayerParty_OLD> getOwnedPartiesList() {
|
||||
// return new ArrayList<>(ownedParties.values());
|
||||
// }
|
||||
//
|
||||
// public Collection<PlayerParty_OLD> getOwnedPartiesCollection() {
|
||||
// return ownedParties.values();
|
||||
// }
|
||||
//
|
||||
// public List<String> getOwnedPartiesAsStrings(){
|
||||
// List<String> strings = new ArrayList<>();
|
||||
// ownedParties.values().forEach(party -> strings.add(party.getPartyName()));
|
||||
// return strings;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Returns a List of Parties this cppPlayer is a member of, not including their owned parties.
|
||||
// * @return
|
||||
// */
|
||||
// public List<PlayerParty_OLD> getPartiesMemberOf() {
|
||||
// // Create list containing all owned parties
|
||||
// List<PlayerParty_OLD> parties = new ArrayList<>();
|
||||
//
|
||||
// Config.getStore().parties.values().forEach(playerPartyStorage -> {
|
||||
// playerPartyStorage.getOwnedPartiesCollection().forEach(party -> {
|
||||
// if (party.isMember(getOwner())) parties.add(party);
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// return parties;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Returns ALL parties this cppPlayer is a member of;.
|
||||
// * @return
|
||||
// */
|
||||
// public List<PlayerParty_OLD> getAllParties() {
|
||||
// // Create list containing all owned parties
|
||||
// List<PlayerParty_OLD> parties = getPartiesMemberOf();
|
||||
// parties.addAll(getOwnedPartiesCollection());
|
||||
//
|
||||
// return parties;
|
||||
// }
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@SerializableAs("PlayerParty")
|
||||
public class PlayerParty implements ConfigurationSerializable {
|
||||
public class PlayerParty_OLD implements ConfigurationSerializable {
|
||||
|
||||
private OfflinePlayer owner;
|
||||
private UUID ownerUUID;
|
||||
@ -21,7 +21,7 @@ public class PlayerParty implements ConfigurationSerializable {
|
||||
private List<OfflinePlayer> members;
|
||||
private ArrayList<String> memberUUIDs;
|
||||
|
||||
public PlayerParty(OfflinePlayer owner, String partyName) {
|
||||
public PlayerParty_OLD(OfflinePlayer owner, String partyName) {
|
||||
this.owner = owner;
|
||||
this.ownerUUID = owner.getUniqueId();
|
||||
this.partyName = partyName;
|
||||
@ -29,7 +29,7 @@ public class PlayerParty implements ConfigurationSerializable {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public PlayerParty(Map<String, Object> map){
|
||||
public PlayerParty_OLD(Map<String, Object> map){
|
||||
ownerUUID = UUID.fromString((String) map.get("owner"));
|
||||
owner = Bukkit.getOfflinePlayer(ownerUUID);
|
||||
|
@ -115,7 +115,7 @@ public class Config {
|
||||
File legacyFile = getLegacyFile();
|
||||
if (!legacyFile.exists()) return;
|
||||
ChestsPlusPlus.PLUGIN.getLogger().info("Found a Legacy config! Converting to new data-format and moving to: /plugins/ChestsPlusPlus/data/storage.yml");
|
||||
ChestsPlusPlus.PLUGIN.getLogger().info("If you are having issues with data-loss the plugin may not have permissions to delete the legacy file 'chests.yml'");
|
||||
ChestsPlusPlus.PLUGIN.getLogger().info("If you are having issues with data-loss the plugin may not have permissions to remove the legacy file 'chests.yml'");
|
||||
|
||||
try {
|
||||
Path path = Paths.get(legacyFile.toURI());
|
||||
|
@ -2,10 +2,10 @@ package com.jamesdpeters.minecraft.chests.storage.abstracts;
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
|
||||
import com.jamesdpeters.minecraft.chests.api.ApiSpecific;
|
||||
import com.jamesdpeters.minecraft.chests.database.DBUtil;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Permissions;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Utils;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Values;
|
||||
import com.jamesdpeters.minecraft.chests.party.PlayerPartyStorage;
|
||||
import com.jamesdpeters.minecraft.chests.serialize.LocationInfo;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@ -352,7 +352,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable {
|
||||
public boolean hasPermission(OfflinePlayer player) {
|
||||
if (isPublic) return true;
|
||||
if (player.getUniqueId().equals(playerUUID)) return true;
|
||||
if (PlayerPartyStorage.doPlayersShareParty(getOwner(), player)) return true;
|
||||
if (DBUtil.PLAYER.findPlayer(player).join().isMemberOfParty(player)) return true;
|
||||
if (members != null) {
|
||||
for (String uuid : members) {
|
||||
if (player.getUniqueId().toString().equals(uuid)) return true;
|
||||
|
@ -2,10 +2,10 @@ package com.jamesdpeters.minecraft.chests.storage.abstracts;
|
||||
|
||||
|
||||
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
|
||||
import com.jamesdpeters.minecraft.chests.database.DBUtil;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Messages;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Utils;
|
||||
import com.jamesdpeters.minecraft.chests.misc.Values;
|
||||
import com.jamesdpeters.minecraft.chests.party.PartyUtils;
|
||||
import com.jamesdpeters.minecraft.chests.serialize.Config;
|
||||
import com.jamesdpeters.minecraft.chests.serialize.ConfigStorage;
|
||||
import com.jamesdpeters.minecraft.chests.serialize.LocationInfo;
|
||||
@ -145,7 +145,7 @@ public abstract class StorageType<T extends AbstractStorage> implements Listener
|
||||
|
||||
public List<T> getStorageMemberOf(Player player) {
|
||||
return getMap().entrySet().stream().flatMap(map -> map.getValue().values().stream().filter(storage -> {
|
||||
if (PartyUtils.getPlayerPartyStorage(storage.getOwner()).getOwnedPartiesCollection().stream().anyMatch(party -> party.isMember(player))) return true; // Uses party to match.
|
||||
if (DBUtil.PLAYER.findPlayer(player).join().getAllParties().stream().anyMatch(party -> party.isMember(player))) return true; // Uses party to match.
|
||||
|
||||
if (storage.isPublic()) return false;
|
||||
if (storage.getOwner().getUniqueId().equals(player.getUniqueId())) return false;
|
||||
|
Loading…
Reference in New Issue
Block a user