From 8295f06691c4d088e3fb3f39549bd96ca4ab4ee5 Mon Sep 17 00:00:00 2001 From: poopki Date: Mon, 9 Jan 2023 04:10:38 +0900 Subject: [PATCH] =?UTF-8?q?Group=20Event=20Handler=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AccountArray.java | 1 + AccountEventHandler.java | 1 - DB/DBUpdate.java | 180 ++++++++++++++++++++++++++++-- Farm.java | 47 ++++++++ FarmArray.java | 162 +++++++++++++++++++++++++++ FarmEventHandler.java | 188 ++++++++++++++++++++++++++++--- Group.java | 17 ++- GroupArray.java | 51 ++++++++- Nation.java | 47 ++++++++ NationArray.java | 162 +++++++++++++++++++++++++++ NationEventHandler.java | 232 ++++++++++++++++++++++++++++++++++++--- User_DB.java | 14 +-- 12 files changed, 1048 insertions(+), 54 deletions(-) create mode 100644 Farm.java create mode 100644 FarmArray.java create mode 100644 Nation.java create mode 100644 NationArray.java diff --git a/AccountArray.java b/AccountArray.java index 1898e7b..8b5d951 100644 --- a/AccountArray.java +++ b/AccountArray.java @@ -26,6 +26,7 @@ public class AccountArray { } } + public int getAccountInfo(UUID uuid) { // DB backup시 account 정보 접근 method Account m_Account = m_AccountArray.get(uuid); return m_Account.getBalance(); diff --git a/AccountEventHandler.java b/AccountEventHandler.java index 4f926b4..281d44d 100644 --- a/AccountEventHandler.java +++ b/AccountEventHandler.java @@ -38,7 +38,6 @@ public class AccountEventHandler implements CommandExecutor { } else{ p.sendMessage("최대 금액 이상으로 설정할 수 없습니다."); } - } else { p.sendMessage("설정 금액은 숫자만 가능합니다."); } diff --git a/DB/DBUpdate.java b/DB/DBUpdate.java index fb37225..fadd0c0 100644 --- a/DB/DBUpdate.java +++ b/DB/DBUpdate.java @@ -1,9 +1,11 @@ package org.poopki.duckdns.user_db.DB; -import org.poopki.duckdns.user_db.AccountArray; -import org.poopki.duckdns.user_db.UserInfoArray; +import org.poopki.duckdns.user_db.*; import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; import java.util.UUID; public class DBUpdate { @@ -14,9 +16,15 @@ public class DBUpdate { private static UserInfoArray m_InfoArrayInstance; private static AccountArray m_AccountArray; - public DBUpdate(UserInfoArray UIA, AccountArray AA){ + private static FarmArray m_FarmArray; + private static NationArray m_NationArray; + UUID ZERO_UUID = new UUID(0,0); + static int MAX_FARM_NUM = 13; + public DBUpdate(UserInfoArray UIA, AccountArray AA, FarmArray FA, NationArray NA){ m_InfoArrayInstance = UIA; m_AccountArray = AA; + m_FarmArray = FA; + m_NationArray = NA; } public static void InitDB() throws SQLException { // 플러그인 초기 사용 시 테이블 생성 @@ -33,9 +41,9 @@ public class DBUpdate { String sql = sb.append("create table if not exists UserInfo(") .append("UUID varchar(36) PRIMARY KEY,") .append("Name varchar(15), ") - .append("Nation int(8),") //public - .append("Squad int(8),") //public - .append("Farm int(8),") //public + .append("Nation varchar(36),") //public + .append("Squad varchar(36),") //public + .append("Farm varchar(36),") //public .append("Account int(10)") //public /* .append("Occupation int(8),") //private @@ -92,13 +100,83 @@ public class DBUpdate { conn.close(); } } + + + try { + Class.forName("org.mariadb.jdbc.Driver"); + + conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); + System.out.println("Connection success"); + stmt = conn.createStatement(); + String sql = "SELECT * FROM Farm"; + ResultSet rs = stmt.executeQuery(sql); + + while(rs.next()){ + List m_MemberList = new ArrayList<>(); + + for(int index = 4; index m_MemberList = new ArrayList<>(); + + for(int index = 4; index m_MemberList = new ArrayList<>(); + UUID ZERO_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); + int MAX_FARM_NUM = 13; + + public Farm(String Name, List uuid_list){ + int index = 0; + for(UUID key: uuid_list){ + m_MemberList.add(index,key); + index++; + } + m_Name = Name; + } + + public void SignUp(UUID uuid){ + m_MemberList.add((UUID)uuid); + } + + public void Expulsion(UUID uuid){ + m_MemberList.remove(uuid); + m_MemberList.add(ZERO_UUID); + } + + public boolean isMember(UUID uuid){ + return m_MemberList.contains(uuid); + } + + public List getGroupMembers(){ return m_MemberList;} + + + public String getGroupName(){ return m_Name; } + public int isEmpty(){return m_MemberList.size();} + public boolean isOwner(UUID uuid){ + if(m_MemberList.get(0).equals(uuid)){ + return true; + }else{ + return false; + } + } +} diff --git a/FarmArray.java b/FarmArray.java new file mode 100644 index 0000000..4409f9d --- /dev/null +++ b/FarmArray.java @@ -0,0 +1,162 @@ +package org.poopki.duckdns.user_db; + +import org.bukkit.Bukkit; + +import java.util.*; + +public class FarmArray { + + + public static Map m_GroupArray; + public static Map m_GroupNameList; + UserInfoArray m_UserInfoArray; + UUID ZERO_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); + int MAX_GROUP_NUM = 13; + + public FarmArray(UserInfoArray m_UIA){ + m_UserInfoArray = m_UIA; + m_GroupArray = new HashMap(); + m_GroupNameList = new HashMap(); + } + + public boolean CreateGroup(UUID GroupUUID, String Name, UUID PlayerUUID){ + if(m_GroupNameList.containsKey(Name)){ + return false; // Conflict + } else{ + List m_MemberList = new ArrayList<>(); + m_MemberList.add(PlayerUUID); + for(int index=1; index PlayerUUID){ + List m_MemberList = new ArrayList<>(); + + Iterator it = PlayerUUID.iterator(); + + while(it.hasNext()){ + m_MemberList.add(it.next()); + } + + for(int index=MAX_GROUP_NUM-PlayerUUID.size();index K getKey(Map map, V value) { + + for (K key : map.keySet()) { + if (value.equals(map.get(key))) { + return key; + } + } + return null; + } + public boolean DeleteGroup(UUID GroupUUID, String GroupType){ + if(m_GroupArray.containsKey(GroupUUID)){ + for(UUID key: GetGroupMembersUUID(GroupUUID)){ + m_UserInfoArray.setUserGroupUUID(GroupType,key,new UUID(0,0)); + } + + m_GroupNameList.remove(GetGroupName(GroupUUID)); + m_GroupArray.remove(GroupUUID); + return true; + } + else{ + return false; // Group is not exists. + } + + } + + public Integer SignUpGroup(String GroupType, UUID GroupUUID, UUID PlayerUUID){ + if(m_GroupArray.containsKey(GroupUUID)){ + if(isOffline(PlayerUUID)){ + Farm m_Group = m_GroupArray.get(GroupUUID); + m_Group.SignUp(PlayerUUID); + m_UserInfoArray.setUserGroupUUID(GroupType,PlayerUUID,GroupUUID); + return 1; // Success + }else{ + return 2; // Offline Player + } + } else{ + return 3; // Wrong Group + } + } + + public boolean ExpulsionGroupPlayer(String GroupType, UUID GroupUUID, UUID PlayerUUID){ + if(m_GroupArray.get(GroupUUID).isMember(PlayerUUID)) { + Farm m_Group = m_GroupArray.get(GroupUUID); + m_Group.Expulsion(PlayerUUID); + m_UserInfoArray.setUserGroupUUID(GroupType,PlayerUUID,ZERO_UUID); + List temp = m_Group.getGroupMembers(); + if(temp.get(0).equals(ZERO_UUID)){ + m_GroupArray.remove(GroupUUID); + m_GroupNameList.remove(getKey(m_GroupNameList,GroupUUID)); + } + return true; + } else{ + return false; // Wrong Player Name. FIXME{Need Checking function for a validate user name's } + /* Player op = Bukkit.getPlayer(args[1]); + if (op != null) { + */ + } + } + + public List GetGroupMembersName(UUID GroupUUID){ + List MembersName = new ArrayList<>(); + for (UUID key : m_GroupArray.get(GroupUUID).getGroupMembers()){ + try { + MembersName.add(Bukkit.getPlayer(key).getDisplayName()); + } + catch(NullPointerException e){ + + } + } + return MembersName; + } + public List GetGroupMembersUUID(UUID GroupUUID){ + return m_GroupArray.get(GroupUUID).getGroupMembers(); + } + public boolean isGroupMember(UUID GroupUUID, UUID PlayerUUID){ + Farm m_Group = m_GroupArray.get(GroupUUID); + return m_Group.isMember(PlayerUUID); + } + + public Set GetGroupNameList(){ + return m_GroupNameList.keySet(); + } + + public Set getKeySet(){ // 유저 목록 return + return m_GroupArray.keySet(); + } + + public Set getNameSet() { return m_GroupNameList.keySet(); } + public String GetGroupName(UUID GroupUUID){ + Farm m_Group = m_GroupArray.get(GroupUUID); + return m_Group.getGroupName(); + } + public static boolean isOffline(UUID uuid){ + return Bukkit.getPlayer(uuid).equals(null) && !Bukkit.getOfflinePlayer(uuid).equals(null); + } + + public static boolean isOwner(UUID GroupUUID, UUID PlayerUUID){ + return m_GroupArray.get(GroupUUID).isOwner(PlayerUUID); + } + private boolean isAvaliableGroup(UUID GroupUUID){ + return m_GroupArray.containsKey(GroupUUID); + } + +} diff --git a/FarmEventHandler.java b/FarmEventHandler.java index b8af48b..00aced6 100644 --- a/FarmEventHandler.java +++ b/FarmEventHandler.java @@ -1,5 +1,6 @@ package org.poopki.duckdns.user_db; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -9,12 +10,22 @@ import java.util.UUID; public class FarmEventHandler implements CommandExecutor { - GroupArray m_GroupArray; + FarmArray m_FarmArray; UserInfoArray m_UserInfoArray; + AccountArray m_AccountArray; UUID ZERO_UUID = new UUID(0,0); - public FarmEventHandler(GroupArray GA, UserInfoArray UIA){ - m_GroupArray = GA; + public FarmEventHandler(FarmArray GA, UserInfoArray UIA, AccountArray AA){ + m_FarmArray = GA; m_UserInfoArray = UIA; + m_AccountArray = AA; + } + public static boolean isStringDouble(String s) { + try { + Double.parseDouble(s); + return true; + } catch (NumberFormatException e) { + return false; + } } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -25,15 +36,16 @@ public class FarmEventHandler implements CommandExecutor { case "생성":{ UUID tempUUID = UUID.randomUUID(); if(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm").equals(ZERO_UUID)){ - if(m_GroupArray.CreateGroup(tempUUID, args[1], p.getUniqueId())){ + if(m_FarmArray.CreateGroup(tempUUID, args[1], p.getUniqueId())){ m_UserInfoArray.setUserGroupUUID("Farm",p.getUniqueId(), tempUUID); + m_AccountArray.putAccountInfo(tempUUID,0); p.sendMessage(args[1]+"농장이 생성되었습니다."); p.sendMessage("농장 멤버 목록: "); - for (String Name: m_GroupArray.GetGroupMembersName(tempUUID)){ + for (String Name: m_FarmArray.GetGroupMembersName(tempUUID)){ p.sendMessage(Name); } p.sendMessage("농장 목록:"); - for (String Name: m_GroupArray.GetGroupNameList()){ + for (String Name: m_FarmArray.GetGroupNameList()){ p.sendMessage(Name); } } @@ -54,13 +66,13 @@ public class FarmEventHandler implements CommandExecutor { } else{ p.sendMessage("가입 완료"); - m_GroupArray.SignUpGroup("Farm",m_GroupArray.m_GroupNameList.get(args[1]),p.getUniqueId()); + m_FarmArray.SignUpGroup("Farm",m_FarmArray.m_GroupNameList.get(args[1]),p.getUniqueId()); p.sendMessage("농장 멤버 목록: "); - for (String Name: m_GroupArray.GetGroupMembersName(m_GroupArray.m_GroupNameList.get(args[1]))){ + for (String Name: m_FarmArray.GetGroupMembersName(m_FarmArray.m_GroupNameList.get(args[1]))){ p.sendMessage(Name); } p.sendMessage("농장 목록:"); - for (String Name: m_GroupArray.GetGroupNameList()){ + for (String Name: m_FarmArray.GetGroupNameList()){ p.sendMessage(Name); } } @@ -72,31 +84,177 @@ public class FarmEventHandler implements CommandExecutor { } case "강퇴":{ p.sendMessage("강퇴 테스트"); + if(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm").equals(ZERO_UUID)){ + p.sendMessage("가입한 농장이 없습니다."+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm")); + } + else{ + if(m_FarmArray.isOwner(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm"), p.getUniqueId())){ + if (args.length > 1) { + Player op = Bukkit.getPlayer(args[1]); + if (op != null) { + if(m_FarmArray.isGroupMember(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm"),op.getUniqueId())) { + m_FarmArray.ExpulsionGroupPlayer("Farm", m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm"), p.getUniqueId()); + } + else{ + p.sendMessage("농장에 가입되지 않은 유저"); + } + } + else{ + p.sendMessage("잘못된 대상"); + } + } + else{ + p.sendMessage("강퇴할 대상을 지정"); + } + } + else{ + p.sendMessage("농장주만 사용 가능한 명령어"+ p.getUniqueId()); + } + } break; } case "탈퇴": { - if(!m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm").equals(ZERO_UUID)){ - m_GroupArray.ExpulsionGroupPlayer("Farm",m_UserInfoArray.getUserGroupUUID(p.getUniqueId(),"Farm"),p.getUniqueId()); + if(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm").equals(ZERO_UUID)){ + p.sendMessage("가입한 농장이 없습니다."+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm")); } else{ - p.sendMessage("가입한 농장이 없습니다."+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm")); + p.sendMessage("탈퇴 플레이어 UUID:"+p.getUniqueId()+" 농장 UUID:"+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Farm")); + m_FarmArray.ExpulsionGroupPlayer("Farm",m_UserInfoArray.getUserGroupUUID(p.getUniqueId(),"Farm"),p.getUniqueId()); + } break; } case "이름":{ - p.sendMessage("현재 파티 이름"+m_GroupArray.GetGroupName(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(),"Party"))); + p.sendMessage("현재 파티 이름"+m_FarmArray.GetGroupName(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(),"Party"))); break; } case "정보": { - for (String Name: m_GroupArray.GetGroupNameList()){ + for (String Name: m_FarmArray.GetGroupNameList()){ p.sendMessage("농장 이름:"); p.sendMessage(Name); p.sendMessage("인원 목록"); - for (String Name2: m_GroupArray.GetGroupMembersName(m_GroupArray.m_GroupNameList.get(Name))) { + for (String Name2: m_FarmArray.GetGroupMembersName(m_FarmArray.m_GroupNameList.get(Name))) { p.sendMessage(Name2); } + p.sendMessage("계좌 잔액:"+m_AccountArray.getAccountInfo(m_FarmArray.m_GroupNameList.get(Name))); } } + case "계좌설정":{ + if (p.isOp()) { + if (args.length > 1) { + if (args[1] != null) { + UUID op = m_FarmArray.m_GroupNameList.get(args[1]); + if (op != null) { + if (args.length > 2) { + if (isStringDouble(args[2])) { + if(m_AccountArray.setAccountInfo(op, Long.parseLong(args[2]))){ + p.sendMessage(args[1] + "님의 잔액을 " + Integer.parseInt(args[2]) + "원으로 설정하였습니다."); + } else{ + p.sendMessage("최대 금액 이상으로 설정할 수 없습니다."); + } + } else { + p.sendMessage("설정 금액은 숫자만 가능합니다."); + } + } else { + p.sendMessage("설정 금액을 입력해주세요."); + } + } else { + p.sendMessage("잘못된 계좌입니다."); + } + } + }else{ + p.sendMessage("설정 대상이 지정되지 않았습니다."); + } + } else { + p.sendMessage("관리자만 실행 가능한 명령어입니다."); + } + break; + } + case "입금": { + if (args.length > 1) { + if(args.length > 2) { + UUID op = m_FarmArray.m_GroupNameList.get(args[1]); + if (op != null) { + if (isStringDouble(args[2])) { + if(p.getUniqueId() != op) { + switch(m_AccountArray.transfer(p.getUniqueId(), op, Long.parseLong(args[2]))) + { + case 1: { + p.sendMessage(args[1] + "농장에 " + Integer.parseInt(args[2]) + "원을 이체하였습니다."); + break; + } + case 2: { + p.sendMessage("잔액은 최대 금액을 초과할 수 없습니다."); + break; + } + case 3:{ + p.sendMessage("잔액이 부족합니다."); + break; + } + } + } else{ + p.sendMessage("자기 자신에게는 이체할 수 없습니다."); + } + } else { + p.sendMessage("이체 금액은 숫자만 가능합니다."); + } + } else { + p.sendMessage("잘못된 계좌입니다."); + } + } else { + p.sendMessage("이체 금액을 입력해주세요."); + } + } else { + p.sendMessage("이체 대상이 지정되지 않았습니다."); + } + break; + } + case "출금": { + if (args.length > 1) { + if(args.length > 2) { + UUID op = m_FarmArray.m_GroupNameList.get(args[1]); + if (op != null) { + if (isStringDouble(args[2])) { + if(p.getUniqueId() != op) { + switch(m_AccountArray.transfer(op,p.getUniqueId(), Long.parseLong(args[2]))) + { + case 1: { + p.sendMessage(args[1] + "농장에서 " + Integer.parseInt(args[2]) + "원을 출금하였습니다."); + break; + } + case 2: { + p.sendMessage("잔액은 최대 금액을 초과할 수 없습니다."); + break; + } + case 3:{ + p.sendMessage("잔액이 부족합니다."); + break; + } + } + } else{ + p.sendMessage("자기 자신에게는 이체할 수 없습니다."); + } + } else { + p.sendMessage("이체 금액은 숫자만 가능합니다."); + } + } else { + p.sendMessage("잘못된 계좌입니다."); + } + } else { + p.sendMessage("이체 금액을 입력해주세요."); + } + } else { + p.sendMessage("이체 대상이 지정되지 않았습니다."); + } + break; + } + case "잔액": { + p.sendMessage("==잔액: " + m_AccountArray.getAccountInfo(m_FarmArray.GetUUID(args[1]))+ "원=="); + break; + } + default: { + p.sendMessage("잘못된 명령어"); + } } } return true; diff --git a/Group.java b/Group.java index f56be02..e2b1b88 100644 --- a/Group.java +++ b/Group.java @@ -7,18 +7,25 @@ import java.util.UUID; public class Group { private String m_Name; private List m_MemberList = new ArrayList<>(); + UUID ZERO_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); + int MAX_FARM_NUM = 13; - public Group(String Name, UUID uuid){ - m_MemberList.add(0, uuid); + public Group(String Name, List uuid_list){ + int index = 0; + for(UUID key: uuid_list){ + m_MemberList.add(index,key); + index++; + } m_Name = Name; } public void SignUp(UUID uuid){ - m_MemberList.add(uuid); + m_MemberList.add((UUID)uuid); } public void Expulsion(UUID uuid){ m_MemberList.remove(uuid); + m_MemberList.add(ZERO_UUID); } public boolean isMember(UUID uuid){ @@ -26,10 +33,12 @@ public class Group { } public List getGroupMembers(){ return m_MemberList;} + + public String getGroupName(){ return m_Name; } public int isEmpty(){return m_MemberList.size();} public boolean isOwner(UUID uuid){ - if(m_MemberList.get(0)==uuid){ + if(m_MemberList.get(0).equals(uuid)){ return true; }else{ return false; diff --git a/GroupArray.java b/GroupArray.java index bd8086d..0b26766 100644 --- a/GroupArray.java +++ b/GroupArray.java @@ -5,24 +5,52 @@ import org.bukkit.Bukkit; import java.util.*; public class GroupArray { - public static Map m_GroupArray = new HashMap(); - public static Map m_GroupNameList = new HashMap(); + public static Map m_GroupArray; + public static Map m_GroupNameList; UserInfoArray m_UserInfoArray; UUID ZERO_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); + int MAX_GROUP_NUM = 13; + public GroupArray(UserInfoArray m_UIA){ m_UserInfoArray = m_UIA; + m_GroupArray = new HashMap(); + m_GroupNameList = new HashMap(); } + public boolean CreateGroup(UUID GroupUUID, String Name, UUID PlayerUUID){ if(m_GroupNameList.containsKey(Name)){ return false; // Conflict } else{ - m_GroupArray.put(GroupUUID, new Group(Name, PlayerUUID)); + List m_MemberList = new ArrayList<>(); + m_MemberList.add(PlayerUUID); + for(int index=1; index PlayerUUID){ + List m_MemberList = new ArrayList<>(); + + Iterator it = PlayerUUID.iterator(); + + while(it.hasNext()){ + m_MemberList.add(it.next()); + } + + for(int index=MAX_GROUP_NUM-PlayerUUID.size();index temp = m_Group.getGroupMembers(); + if(temp.get(0).equals(ZERO_UUID)){ m_GroupArray.remove(GroupUUID); m_GroupNameList.remove(getKey(m_GroupNameList,GroupUUID)); } @@ -87,7 +116,12 @@ public class GroupArray { public List GetGroupMembersName(UUID GroupUUID){ List MembersName = new ArrayList<>(); for (UUID key : m_GroupArray.get(GroupUUID).getGroupMembers()){ - MembersName.add(Bukkit.getPlayer(key).getDisplayName()); + try { + MembersName.add(Bukkit.getPlayer(key).getDisplayName()); + } + catch(NullPointerException e){ + + } } return MembersName; } @@ -103,7 +137,11 @@ public class GroupArray { return m_GroupNameList.keySet(); } + public Set getKeySet(){ // 유저 목록 return + return m_GroupArray.keySet(); + } + public Set getNameSet() { return m_GroupNameList.keySet(); } public String GetGroupName(UUID GroupUUID){ Group m_Group = m_GroupArray.get(GroupUUID); return m_Group.getGroupName(); @@ -112,6 +150,9 @@ public class GroupArray { return Bukkit.getPlayer(uuid).equals(null) && !Bukkit.getOfflinePlayer(uuid).equals(null); } + public static boolean isOwner(UUID GroupUUID, UUID PlayerUUID){ + return m_GroupArray.get(GroupUUID).isOwner(PlayerUUID); + } private boolean isAvaliableGroup(UUID GroupUUID){ return m_GroupArray.containsKey(GroupUUID); } diff --git a/Nation.java b/Nation.java new file mode 100644 index 0000000..fb9c00b --- /dev/null +++ b/Nation.java @@ -0,0 +1,47 @@ +package org.poopki.duckdns.user_db; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class Nation { + private String m_Name; + private List m_MemberList = new ArrayList<>(); + UUID ZERO_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); + int MAX_FARM_NUM = 13; + + public Nation(String Name, List uuid_list){ + int index = 0; + for(UUID key: uuid_list){ + m_MemberList.add(index,key); + index++; + } + m_Name = Name; + } + + public void SignUp(UUID uuid){ + m_MemberList.add((UUID)uuid); + } + + public void Expulsion(UUID uuid){ + m_MemberList.remove(uuid); + m_MemberList.add(ZERO_UUID); + } + + public boolean isMember(UUID uuid){ + return m_MemberList.contains(uuid); + } + + public List getGroupMembers(){ return m_MemberList;} + + + public String getGroupName(){ return m_Name; } + public int isEmpty(){return m_MemberList.size();} + public boolean isOwner(UUID uuid){ + if(m_MemberList.get(0).equals(uuid)){ + return true; + }else{ + return false; + } + } +} diff --git a/NationArray.java b/NationArray.java new file mode 100644 index 0000000..d2b8603 --- /dev/null +++ b/NationArray.java @@ -0,0 +1,162 @@ +package org.poopki.duckdns.user_db; + +import org.bukkit.Bukkit; + +import java.util.*; + +public class NationArray { + + + public static Map m_GroupArray; + public static Map m_GroupNameList; + UserInfoArray m_UserInfoArray; + UUID ZERO_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); + int MAX_GROUP_NUM = 13; + + public NationArray(UserInfoArray m_UIA){ + m_UserInfoArray = m_UIA; + m_GroupArray = new HashMap(); + m_GroupNameList = new HashMap(); + } + + public boolean CreateGroup(UUID GroupUUID, String Name, UUID PlayerUUID){ + if(m_GroupNameList.containsKey(Name)){ + return false; // Conflict + } else{ + List m_MemberList = new ArrayList<>(); + m_MemberList.add(PlayerUUID); + for(int index=1; index PlayerUUID){ + List m_MemberList = new ArrayList<>(); + + Iterator it = PlayerUUID.iterator(); + + while(it.hasNext()){ + m_MemberList.add(it.next()); + } + + for(int index=MAX_GROUP_NUM-PlayerUUID.size();index K getKey(Map map, V value) { + + for (K key : map.keySet()) { + if (value.equals(map.get(key))) { + return key; + } + } + return null; + } + public boolean DeleteGroup(UUID GroupUUID, String GroupType){ + if(m_GroupArray.containsKey(GroupUUID)){ + for(UUID key: GetGroupMembersUUID(GroupUUID)){ + m_UserInfoArray.setUserGroupUUID(GroupType,key,new UUID(0,0)); + } + + m_GroupNameList.remove(GetGroupName(GroupUUID)); + m_GroupArray.remove(GroupUUID); + return true; + } + else{ + return false; // Group is not exists. + } + + } + + public Integer SignUpGroup(String GroupType, UUID GroupUUID, UUID PlayerUUID){ + if(m_GroupArray.containsKey(GroupUUID)){ + if(isOffline(PlayerUUID)){ + Nation m_Group = m_GroupArray.get(GroupUUID); + m_Group.SignUp(PlayerUUID); + m_UserInfoArray.setUserGroupUUID(GroupType,PlayerUUID,GroupUUID); + return 1; // Success + }else{ + return 2; // Offline Player + } + } else{ + return 3; // Wrong Group + } + } + + public boolean ExpulsionGroupPlayer(String GroupType, UUID GroupUUID, UUID PlayerUUID){ + if(m_GroupArray.get(GroupUUID).isMember(PlayerUUID)) { + Nation m_Group = m_GroupArray.get(GroupUUID); + m_Group.Expulsion(PlayerUUID); + m_UserInfoArray.setUserGroupUUID(GroupType,PlayerUUID,ZERO_UUID); + List temp = m_Group.getGroupMembers(); + if(temp.get(0).equals(ZERO_UUID)){ + m_GroupArray.remove(GroupUUID); + m_GroupNameList.remove(getKey(m_GroupNameList,GroupUUID)); + } + return true; + } else{ + return false; // Wrong Player Name. FIXME{Need Checking function for a validate user name's } + /* Player op = Bukkit.getPlayer(args[1]); + if (op != null) { + */ + } + } + + public List GetGroupMembersName(UUID GroupUUID){ + List MembersName = new ArrayList<>(); + for (UUID key : m_GroupArray.get(GroupUUID).getGroupMembers()){ + try { + MembersName.add(Bukkit.getPlayer(key).getDisplayName()); + } + catch(NullPointerException e){ + + } + } + return MembersName; + } + public List GetGroupMembersUUID(UUID GroupUUID){ + return m_GroupArray.get(GroupUUID).getGroupMembers(); + } + public boolean isGroupMember(UUID GroupUUID, UUID PlayerUUID){ + Nation m_Group = m_GroupArray.get(GroupUUID); + return m_Group.isMember(PlayerUUID); + } + + public Set GetGroupNameList(){ + return m_GroupNameList.keySet(); + } + + public Set getKeySet(){ // 유저 목록 return + return m_GroupArray.keySet(); + } + + public Set getNameSet() { return m_GroupNameList.keySet(); } + public String GetGroupName(UUID GroupUUID){ + Nation m_Group = m_GroupArray.get(GroupUUID); + return m_Group.getGroupName(); + } + public static boolean isOffline(UUID uuid){ + return Bukkit.getPlayer(uuid).equals(null) && !Bukkit.getOfflinePlayer(uuid).equals(null); + } + + public static boolean isOwner(UUID GroupUUID, UUID PlayerUUID){ + return m_GroupArray.get(GroupUUID).isOwner(PlayerUUID); + } + private boolean isAvaliableGroup(UUID GroupUUID){ + return m_GroupArray.containsKey(GroupUUID); + } + +} diff --git a/NationEventHandler.java b/NationEventHandler.java index 5308139..15c44e0 100644 --- a/NationEventHandler.java +++ b/NationEventHandler.java @@ -1,5 +1,6 @@ package org.poopki.duckdns.user_db; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -9,12 +10,22 @@ import java.util.UUID; public class NationEventHandler implements CommandExecutor { - GroupArray m_GroupArray; + NationArray m_NationArray; UserInfoArray m_UserInfoArray; - - public NationEventHandler(GroupArray GA, UserInfoArray UIA){ - m_GroupArray = GA; + AccountArray m_AccountArray; + UUID ZERO_UUID = new UUID(0,0); + public NationEventHandler(NationArray GA, UserInfoArray UIA, AccountArray AA){ + m_NationArray = GA; m_UserInfoArray = UIA; + m_AccountArray = AA; + } + public static boolean isStringDouble(String s) { + try { + Double.parseDouble(s); + return true; + } catch (NumberFormatException e) { + return false; + } } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -24,21 +35,47 @@ public class NationEventHandler implements CommandExecutor { switch (args[0]) { case "생성":{ UUID tempUUID = UUID.randomUUID(); - m_GroupArray.CreateGroup(tempUUID, args[1], p.getUniqueId()); - m_UserInfoArray.setUserGroupUUID("Party",p.getUniqueId(), tempUUID); - p.sendMessage(args[1]+"파티가 생성되었습니다."); - p.sendMessage("멤버 목록: "); - for (String Name: m_GroupArray.GetGroupMembersName(tempUUID)){ - p.sendMessage(Name); + if(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation").equals(ZERO_UUID)){ + if(m_NationArray.CreateGroup(tempUUID, args[1], p.getUniqueId())){ + m_UserInfoArray.setUserGroupUUID("Nation",p.getUniqueId(), tempUUID); + m_AccountArray.putAccountInfo(tempUUID,0); + p.sendMessage(args[1]+"국가 생성되었습니다."); + p.sendMessage("국가 멤버 목록: "); + for (String Name: m_NationArray.GetGroupMembersName(tempUUID)){ + p.sendMessage(Name); + } + p.sendMessage("국가 목록:"); + for (String Name: m_NationArray.GetGroupNameList()){ + p.sendMessage(Name); + } + } + else{ + p.sendMessage("이미 존재하는 국가 이름"); + } } - p.sendMessage("파티 목록:"); - for (String Name: m_GroupArray.GetGroupNameList()){ - p.sendMessage(Name); + else{ + p.sendMessage("이미 국가 소유중"+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation")); } break; } case "가입":{ - p.sendMessage("가입 테스트"); + p.sendMessage("플레이어 UUID:"+p.getUniqueId()+" 국가 UUID:"+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation")); + p.sendMessage("ZERO UUID:"+ZERO_UUID); + if(!m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation").equals(ZERO_UUID)){ + p.sendMessage("이미 가입한 국가 존재"); + } + else{ + p.sendMessage("가입 완료"); + m_NationArray.SignUpGroup("Nation",m_NationArray.m_GroupNameList.get(args[1]),p.getUniqueId()); + p.sendMessage("국가 멤버 목록: "); + for (String Name: m_NationArray.GetGroupMembersName(m_NationArray.m_GroupNameList.get(args[1]))){ + p.sendMessage(Name); + } + p.sendMessage("국가 목록:"); + for (String Name: m_NationArray.GetGroupNameList()){ + p.sendMessage(Name); + } + } break; } case "초대":{ @@ -47,12 +84,177 @@ public class NationEventHandler implements CommandExecutor { } case "강퇴":{ p.sendMessage("강퇴 테스트"); + if(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation").equals(ZERO_UUID)){ + p.sendMessage("가입한 국가 없습니다."+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation")); + } + else{ + if(m_NationArray.isOwner(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation"), p.getUniqueId())){ + if (args.length > 1) { + Player op = Bukkit.getPlayer(args[1]); + if (op != null) { + if(m_NationArray.isGroupMember(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation"),op.getUniqueId())) { + m_NationArray.ExpulsionGroupPlayer("Nation", m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation"), p.getUniqueId()); + } + else{ + p.sendMessage("국가 가입되지 않은 유저"); + } + } + else{ + p.sendMessage("잘못된 대상"); + } + } + else{ + p.sendMessage("강퇴할 대상을 지정"); + } + } + else{ + p.sendMessage("국가주만 사용 가능한 명령어"+ p.getUniqueId()); + } + } + break; + } + case "탈퇴": { + if(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation").equals(ZERO_UUID)){ + p.sendMessage("가입한 국가 없습니다."+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation")); + } + else{ + p.sendMessage("탈퇴 플레이어 UUID:"+p.getUniqueId()+" 국가 UUID:"+m_UserInfoArray.getUserGroupUUID(p.getUniqueId(), "Nation")); + m_NationArray.ExpulsionGroupPlayer("Nation",m_UserInfoArray.getUserGroupUUID(p.getUniqueId(),"Nation"),p.getUniqueId()); + + } break; } case "이름":{ - p.sendMessage("현재 파티 이름"+m_GroupArray.GetGroupName(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(),"Party"))); + p.sendMessage("현재 파티 이름"+m_NationArray.GetGroupName(m_UserInfoArray.getUserGroupUUID(p.getUniqueId(),"Party"))); break; } + case "정보": { + for (String Name: m_NationArray.GetGroupNameList()){ + p.sendMessage("국가 이름:"); + p.sendMessage(Name); + p.sendMessage("인원 목록"); + for (String Name2: m_NationArray.GetGroupMembersName(m_NationArray.m_GroupNameList.get(Name))) { + p.sendMessage(Name2); + } + p.sendMessage("계좌 잔액:"+m_AccountArray.getAccountInfo(m_NationArray.m_GroupNameList.get(Name))); + } + } + case "계좌설정":{ + if (p.isOp()) { + if (args.length > 1) { + if (args[1] != null) { + UUID op = m_NationArray.m_GroupNameList.get(args[1]); + if (op != null) { + if (args.length > 2) { + if (isStringDouble(args[2])) { + if(m_AccountArray.setAccountInfo(op, Long.parseLong(args[2]))){ + p.sendMessage(args[1] + "님의 잔액을 " + Integer.parseInt(args[2]) + "원으로 설정하였습니다."); + } else{ + p.sendMessage("최대 금액 이상으로 설정할 수 없습니다."); + } + } else { + p.sendMessage("설정 금액은 숫자만 가능합니다."); + } + } else { + p.sendMessage("설정 금액을 입력해주세요."); + } + } else { + p.sendMessage("잘못된 계좌입니다."); + } + } + }else{ + p.sendMessage("설정 대상이 지정되지 않았습니다."); + } + } else { + p.sendMessage("관리자만 실행 가능한 명령어입니다."); + } + break; + } + case "입금": { + if (args.length > 1) { + if(args.length > 2) { + UUID op = m_NationArray.m_GroupNameList.get(args[1]); + if (op != null) { + if (isStringDouble(args[2])) { + if(p.getUniqueId() != op) { + switch(m_AccountArray.transfer(p.getUniqueId(), op, Long.parseLong(args[2]))) + { + case 1: { + p.sendMessage(args[1] + "국가 " + Integer.parseInt(args[2]) + "원을 이체하였습니다."); + break; + } + case 2: { + p.sendMessage("잔액은 최대 금액을 초과할 수 없습니다."); + break; + } + case 3:{ + p.sendMessage("잔액이 부족합니다."); + break; + } + } + } else{ + p.sendMessage("자기 자신에게는 이체할 수 없습니다."); + } + } else { + p.sendMessage("이체 금액은 숫자만 가능합니다."); + } + } else { + p.sendMessage("잘못된 계좌입니다."); + } + } else { + p.sendMessage("이체 금액을 입력해주세요."); + } + } else { + p.sendMessage("이체 대상이 지정되지 않았습니다."); + } + break; + } + case "출금": { + if (args.length > 1) { + if(args.length > 2) { + UUID op = m_NationArray.m_GroupNameList.get(args[1]); + if (op != null) { + if (isStringDouble(args[2])) { + if(p.getUniqueId() != op) { + switch(m_AccountArray.transfer(op,p.getUniqueId(), Long.parseLong(args[2]))) + { + case 1: { + p.sendMessage(args[1] + "국가 " + Integer.parseInt(args[2]) + "원을 출금하였습니다."); + break; + } + case 2: { + p.sendMessage("잔액은 최대 금액을 초과할 수 없습니다."); + break; + } + case 3:{ + p.sendMessage("잔액이 부족합니다."); + break; + } + } + } else{ + p.sendMessage("자기 자신에게는 이체할 수 없습니다."); + } + } else { + p.sendMessage("이체 금액은 숫자만 가능합니다."); + } + } else { + p.sendMessage("잘못된 계좌입니다."); + } + } else { + p.sendMessage("이체 금액을 입력해주세요."); + } + } else { + p.sendMessage("이체 대상이 지정되지 않았습니다."); + } + break; + } + case "잔액": { + p.sendMessage("==잔액: " + m_AccountArray.getAccountInfo(m_NationArray.GetUUID(args[1]))+ "원=="); + break; + } + default: { + p.sendMessage("잘못된 명령어"); + } } } return true; diff --git a/User_DB.java b/User_DB.java index d4a373b..526ee5e 100644 --- a/User_DB.java +++ b/User_DB.java @@ -11,10 +11,10 @@ public final class User_DB extends JavaPlugin implements Listener { AccountArray m_AccountArrayInstance = new AccountArray(); UserInfoArray m_InfoArrayInstance = new UserInfoArray(); // UserInfo 저장하는 Array instance 생성 - GroupArray m_FarmArrayInstance = new GroupArray(m_InfoArrayInstance); - GroupArray m_NationArrayInstance = new GroupArray(m_InfoArrayInstance); - GroupArray m_SquadArrayInstance = new GroupArray(m_InfoArrayInstance); - DBUpdate Database_backup = new DBUpdate(m_InfoArrayInstance, m_AccountArrayInstance); + FarmArray m_FarmArrayInstance = new FarmArray(m_InfoArrayInstance); + NationArray m_NationArrayInstance = new NationArray(m_InfoArrayInstance); + + DBUpdate Database_backup = new DBUpdate(m_InfoArrayInstance, m_AccountArrayInstance, m_FarmArrayInstance, m_NationArrayInstance); @Override public void onEnable() { // Plugin startup logic @@ -25,9 +25,9 @@ public final class User_DB extends JavaPlugin implements Listener { throw new RuntimeException(e); } getCommand("계좌").setExecutor(new AccountEventHandler(m_AccountArrayInstance)); - getCommand("농장").setExecutor(new FarmEventHandler(m_FarmArrayInstance, m_InfoArrayInstance)); - getCommand("국가").setExecutor(new NationEventHandler(m_FarmArrayInstance, m_InfoArrayInstance)); - getCommand("분대").setExecutor(new SquadEventHandler(m_FarmArrayInstance, m_InfoArrayInstance)); + getCommand("농장").setExecutor(new FarmEventHandler(m_FarmArrayInstance, m_InfoArrayInstance, m_AccountArrayInstance)); + getCommand("국가").setExecutor(new NationEventHandler(m_NationArrayInstance, m_InfoArrayInstance, m_AccountArrayInstance)); + getServer().getPluginManager().registerEvents(new UserJoin(m_InfoArrayInstance, m_AccountArrayInstance),this); }