diff --git a/Account.java b/Account.java new file mode 100644 index 0000000..3a86823 --- /dev/null +++ b/Account.java @@ -0,0 +1,32 @@ +package org.poopki.duckdns.user_db; + +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class Account { + private int m_Balance; + + public Account(){ + m_Balance = 0; + } + + public Account(int Balance){ + m_Balance = Balance; + } + + public int getBalance(){ return m_Balance; } + public boolean credit(int Amount){ + m_Balance +=Amount; + return true; + } + public boolean withdraw(int Amount){ + if((m_Balance - Amount) > 0){ + m_Balance -= Amount; + return true; + } + return false; + } +} diff --git a/AccountArray.java b/AccountArray.java new file mode 100644 index 0000000..f05ebb5 --- /dev/null +++ b/AccountArray.java @@ -0,0 +1,37 @@ +package org.poopki.duckdns.user_db; + +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class AccountArray { + public static Map m_AccountArray = new HashMap(); + + public void transfer(UUID Src, UUID Des, int Amount){ + Account m_Src = m_AccountArray.get(Src); + Account m_Des = m_AccountArray.get(Des); + + if(m_Src.getBalance()-Amount>0){ + m_Src.withdraw(Amount); + m_Des.credit(Amount); + } + } + + public int getAccountInfo(UUID uuid) { + Account m_Account = m_AccountArray.get(uuid); + return m_Account.getBalance(); + } + + public Account putAccountInfo(UUID uuid) { + m_AccountArray.put(uuid, new Account()); + Account m_Account = m_AccountArray.get(uuid); + return m_Account; + } + public Account putAccountInfo(UUID uuid, int Amount) { + m_AccountArray.put(uuid, new Account(Amount)); + Account m_Account = m_AccountArray.get(uuid); + return m_Account; + } +} diff --git a/DB/DBUpdate.java b/DB/DBUpdate.java index 92cfd0b..af6147f 100644 --- a/DB/DBUpdate.java +++ b/DB/DBUpdate.java @@ -1,12 +1,10 @@ package org.poopki.duckdns.user_db.DB; +import org.poopki.duckdns.user_db.AccountArray; import org.poopki.duckdns.user_db.UserInfo; import org.poopki.duckdns.user_db.UserInfoArray; import java.sql.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; import java.util.UUID; public class DBUpdate { @@ -22,7 +20,7 @@ public class DBUpdate { public static void InitDB() throws SQLException { Connection conn = null; - Statement stmt = null; + Statement stmt; try { Class.forName("org.mariadb.jdbc.Driver"); @@ -35,7 +33,8 @@ public class DBUpdate { .append("UUID varchar(36) PRIMARY KEY,") .append("Nation int(8),") //public .append("Squad int(8),") //public - .append("Farm int(8)") //public + .append("Farm int(8),") //public + .append("Account int(10)") //public /* .append("Occupation int(8),") //private .append("Account int(8),") //private @@ -57,9 +56,9 @@ public class DBUpdate { } } } - public static void LoadDB() throws SQLException { + public static void LoadDB(AccountArray m_AccountArrayInstance) throws SQLException { Connection conn = null; - Statement stmt = null; + Statement stmt; try { Class.forName("org.mariadb.jdbc.Driver"); @@ -73,7 +72,11 @@ public class DBUpdate { m_InfoArrayInstance.m_UserInfoArray.put(UUID.fromString(rs.getString(1)), new UserInfo(UUID.fromString(rs.getString(1)), Integer.parseInt(rs.getString(2)), Integer.parseInt(rs.getString(3)), - Integer.parseInt(rs.getString(4)))); + Integer.parseInt(rs.getString(4)), + m_AccountArrayInstance.putAccountInfo( + UUID.fromString( + rs.getString(1)), + Integer.parseInt(rs.getString(5))))); } } catch (ClassNotFoundException e) { @@ -93,7 +96,7 @@ public class DBUpdate { public void UpdateDB() throws SQLException { Connection conn = null; - PreparedStatement pstmt = null; + PreparedStatement pstmt; try{ Class.forName("org.mariadb.jdbc.Driver"); @@ -103,14 +106,15 @@ public class DBUpdate { //sql = sb.append("insert into UserInfo values('"+key+"',"+0+","+0+","+0+","+0+","+0+")").toString(); //String sql = sb.append("insert into UserInfo values ('test', 0)").toString(); //stmt.execute(sql); - String sql = "REPLACE INTO UserInfo VALUES (?,?,?,?)"; + String sql = "REPLACE INTO UserInfo VALUES (?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); //pstmt.setString(1, key.toString()); - pstmt.setString(1, String.valueOf(m_InfoArrayInstance.m_UserInfoArray.get(key).getUserUUID())); + UserInfo temp = m_InfoArrayInstance.m_UserInfoArray.get(key); + pstmt.setString(1, String.valueOf(key.toString())); pstmt.setString(2, String.valueOf(11)); pstmt.setString(3, String.valueOf(23)); pstmt.setString(4, String.valueOf(56)); - + pstmt.setString(5, String.valueOf(temp.getBalance())); int cnt = pstmt.executeUpdate(); if( cnt == 0 ){ System.out.println("데이터 입력 실패"); diff --git a/UserInfo.java b/UserInfo.java index b419591..239a99b 100644 --- a/UserInfo.java +++ b/UserInfo.java @@ -10,29 +10,25 @@ public class UserInfo { private int m_Nation; private int m_Squad; private int m_Farm; + private Account m_Account; - UserInfo(Player p){ - m_UUID = p.getUniqueId(); - m_Nation = 0; - m_Squad = 0; - m_Farm = 0; - } - public UserInfo(UUID uuid, int Nation, int Squad, int Farm){ + public UserInfo(UUID uuid, int Nation, int Squad, int Farm, Account Instance){ m_UUID = uuid; m_Nation = Nation; m_Squad = Squad; - m_Farm = Farm; + m_Account = Instance; } public void JoinNation(int Nation) { m_Nation = Nation; } public void JoinSquad(int Squad) { m_Squad = Squad; } public void JoinFarm(int Farm) { m_Farm = Farm; } + public int getBalance(){ return m_Account.getBalance();} public String getUserName(){ return m_Name; } public UUID getUserUUID(){ - return m_UUID; + return m_UUID; } diff --git a/UserInfoArray.java b/UserInfoArray.java index 5183a00..229607d 100644 --- a/UserInfoArray.java +++ b/UserInfoArray.java @@ -7,16 +7,23 @@ import java.util.Map; import java.util.UUID; public class UserInfoArray { + private AccountArray m_AccountArrayInstance; public static Map m_UserInfoArray = new HashMap(); + UserInfoArray(AccountArray Instance){ + m_AccountArrayInstance = Instance; + } + public void getUserInfo(Player p) { UserInfo m_UserInfo = m_UserInfoArray.get(p.getUniqueId()); p.sendMessage(m_UserInfo.getUserName() + m_UserInfo.getUserUUID() + ("중복 입장")); + p.sendMessage(m_UserInfo.getUserUUID() +("Balance ")+m_AccountArrayInstance.getAccountInfo(p.getUniqueId())); } - public void putUserInfo(Player p) { - m_UserInfoArray.put(p.getUniqueId(), new UserInfo(p)); + public void NewUserInfo(Player p) { + m_UserInfoArray.put(p.getUniqueId(), new UserInfo(p.getUniqueId(), 0, 0, 0, m_AccountArrayInstance.putAccountInfo(p.getUniqueId()))); UserInfo m_UserInfo = m_UserInfoArray.get(p.getUniqueId()); p.sendMessage(m_UserInfo.getUserName() + m_UserInfo.getUserUUID() + ("초기 입장")); + p.sendMessage(m_UserInfo.getUserUUID() +("Balance ")+m_AccountArrayInstance.getAccountInfo(p.getUniqueId())); } } \ No newline at end of file diff --git a/UserJoin.java b/UserJoin.java index 3d322de..21360f2 100644 --- a/UserJoin.java +++ b/UserJoin.java @@ -14,9 +14,10 @@ public class UserJoin implements Listener { Player p = e.getPlayer(); if(m_InfoArrayInstance.m_UserInfoArray.containsKey(p.getUniqueId())){ m_InfoArrayInstance.getUserInfo(p); + } else{ - m_InfoArrayInstance.putUserInfo(p); + m_InfoArrayInstance.NewUserInfo(p); } } } diff --git a/User_DB.java b/User_DB.java index 8e1d211..14b7050 100644 --- a/User_DB.java +++ b/User_DB.java @@ -9,19 +9,24 @@ import java.sql.Statement; public final class User_DB extends JavaPlugin implements Listener { - UserInfoArray m_InfoArrayInstance = new UserInfoArray(); + + AccountArray m_AccountArrayInstance = new AccountArray(); + UserInfoArray m_InfoArrayInstance = new UserInfoArray(m_AccountArrayInstance); // UserInfo저장하는 Array instance 생성 + + DBUpdate Database_backup = new DBUpdate(m_InfoArrayInstance); @Override public void onEnable() { // Plugin startup logic try { Database_backup.InitDB(); - Database_backup.LoadDB(); + Database_backup.LoadDB(m_AccountArrayInstance); } catch (SQLException e) { throw new RuntimeException(e); } getServer().getPluginManager().registerEvents(new UserJoin(m_InfoArrayInstance),this); + } @Override