Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented feature from Issue #9 #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ out
target
dependency-reduced-pom.xml
*.iml
.vscode
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public IslandCommand(VSkyblock plugin) {
registerSubCommand(new IslandSetOwner(plugin));
registerSubCommand(new IslandVisit(plugin));
registerSubCommand(new IslandConfirm(plugin));
registerSubCommand(new IslandInfo(plugin));
}

@Override
Expand Down
152 changes: 152 additions & 0 deletions src/main/java/com.github.Viduality.VSkyblock/Commands/IslandInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package com.github.Viduality.VSkyblock.Commands;

import com.github.Viduality.VSkyblock.VSkyblock;
import com.github.Viduality.VSkyblock.Utilitys.ConfigShorts;
import com.github.Viduality.VSkyblock.Utilitys.PlayerInfo;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.ChatColor;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.Duration;
import java.time.Period;

public class IslandInfo extends PlayerSubCommand {

public IslandInfo(VSkyblock plugin) {
super(plugin, "info");
}

@Override
public void execute(CommandSender sender, PlayerInfo playerInfo, String[] args) {
if (playerInfo.getIslandId() == 0) {
ConfigShorts.messagefromString("NoIsland", playerInfo.getPlayer());
return;
}

ConfigShorts.messagefromString("IslandInfoLoading", sender);

plugin.getDb().getReader().getIslandsChallengePoints(playerInfo.getIslandId(), (challengePoints) -> {
plugin.getDb().getReader().getIslandMembersTotal(playerInfo.getIslandId(), (totalMembers) -> {

StringBuilder sb = new StringBuilder();

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelAge"));
Timestamp dateCreated = playerInfo.getIslandCreated();
LocalDateTime createdDateTime = dateCreated.toLocalDateTime();
LocalDateTime currentDateTime = LocalDateTime.now();

Period period = Period.between(createdDateTime.toLocalDate(), currentDateTime.toLocalDate());
Duration duration = Duration.between(createdDateTime, currentDateTime);


if (period.getYears() > 0) {
sb.append(period.getYears())
.append(" years, ")
.append(period.getMonths())
.append(" months, ")
.append(period.getDays())
.append(" days old");
} else if (period.getMonths() > 0) {
sb.append(" months, ")
.append(period.getDays())
.append(" days old");
} else if (period.getDays() > 0) {
sb.append(period.getDays())
.append(" days old");
} else if (duration.toHours() > 0) {
sb.append(duration.toHours())
.append(" hours old");
} else if (duration.toMinutes() > 0) {
sb.append(duration.toMinutes())
.append(" minutes old");
} else {
sb.append(duration.toSeconds())
.append(" seconds old");
}
sb.append("\n");

World world = plugin.getServer().getWorld(playerInfo.getIslandName());

double worldsize = world.getWorldBorder().getSize();

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelSize"));
sb.append(worldsize);
sb.append(" ")
.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelSizeUnits"));
sb.append("\n");

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelDifficulty"));
sb.append(world.getDifficulty());
sb.append("\n");

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelTotalMembers"));
sb.append(totalMembers);
sb.append("\n");

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelXBorder"));
sb.append(worldsize / -2).append(" to ").append(worldsize / 2);
sb.append("\n");

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelZBorder"));
sb.append(worldsize / -2).append(" to ").append(worldsize / 2);
sb.append("\n");

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelLevel"));
sb.append(playerInfo.getIslandLevel());
sb.append("\n");

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelProgress"));

int valueperlevel;
if (isInt(ConfigShorts.getDefConfig().getString("IslandValue"))) {
valueperlevel = ConfigShorts.getDefConfig().getInt("IslandValue");
} else {
valueperlevel = 300;
}

double value = 0;
if (isInt(ConfigShorts.getDefConfig().getString("IslandValueonStart"))) {
value = ConfigShorts.getDefConfig().getInt("IslandValueonStart");
} else {
value = 150;
}
value = value + challengePoints;

sb.append(value - (getValueIncrease() * playerInfo.getIslandLevel()))
.append("/")
.append((getValueIncrease() * playerInfo.getIslandLevel()) + valueperlevel);
sb.append("\n");

sb.append(ConfigShorts.getMessageConfig().getString("IslandInfoLabelChallenges"));
sb.append(playerInfo.getChallengesCompleted());
sb.append("\n");

sender.sendMessage(ChatColor.translateAlternateColorCodes('&', sb.toString()));
});
});
}

private static boolean isInt(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}

private static int getValueIncrease() {
String s = ConfigShorts.getDefConfig().getString("IslandValueIncreasePerLevel");
if (s != null) {
if (isInt(s)) {
return Integer.parseInt(s);
} else {
return 20;
}
} else {
return 20;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public void initTables() {
+ "cobblestonelevel BIGINT DEFAULT 0,"
+ "totalblocks BIGINT DEFAULT 140,"
+ "totalentities BIGINT DEFAULT 0,"
+ "time_created TIMESTAMP NOT NULL,"
+ "PRIMARY KEY (islandid))");
connection.createStatement().execute(
"CREATE TABLE IF NOT EXISTS VSkyblock_IslandLocations("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,17 @@ public void getPlayerData(final String uuid, final Consumer<PlayerInfo> callback
while (r1.next()) {
playerInfo.setIslandName(r1.getString("island"));
playerInfo.setIslandLevel(r1.getInt("islandlevel"));
playerInfo.setIslandCreated(r1.getTimestamp("time_created"));
}
preparedStatement1.close();

PreparedStatement preparedStatement2 = connection.prepareStatement("SELECT SUM(count) AS total FROM VSkyblock_Challenges WHERE islandid = ?");
preparedStatement2.setInt(1, playerInfo.getIslandId());
ResultSet r2 = preparedStatement2.executeQuery();
while (r2.next()) {
playerInfo.setChallengesCompleted(r2.getInt("total"));
}
preparedStatement2.close();
}
}

Expand Down Expand Up @@ -245,6 +254,31 @@ public void getIslandMembers(Integer islandid, Consumer<List<String>> callback)
});
}

/**
* Gets the total members of an island.
*
* @param islandid The id of an island.
* @param callback Returns the total of the island.
*/
public void getIslandMembersTotal(Integer islandid, Consumer<Integer> callback) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
int total = 0;
try (Connection connection = connector.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) AS total FROM `vskyblock_player` WHERE islandid = ?")) {
preparedStatement.setInt(1, islandid);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
total = resultSet.getInt("total");
}
} catch (SQLException e) {
e.printStackTrace();
}

int finalTotal = total;
Bukkit.getScheduler().runTask(plugin, () -> callback.accept(finalTotal));
});
}

/**
* Gets all islands without members (database action).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.Date;
import java.util.UUID;

public class DatabaseWriter {
Expand Down Expand Up @@ -76,9 +74,10 @@ public void addIsland(String island, UUID uuid, String difficutly) {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
try (Connection connection = connector.getConnection()) {
PreparedStatement preparedStatement;
preparedStatement = connection.prepareStatement("INSERT INTO VSkyblock_Island(island, difficulty) VALUES(?, ?)");
preparedStatement = connection.prepareStatement("INSERT INTO VSkyblock_Island(island, difficulty, time_created) VALUES(?, ?, ?)");
preparedStatement.setString(1, island);
preparedStatement.setString(2, difficutly);
preparedStatement.setTimestamp(3, new Timestamp(new Date().getTime()));
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.sql.Timestamp;
import java.util.UUID;

public class PlayerInfo {
Expand All @@ -14,6 +15,24 @@ public class PlayerInfo {
private String islandName = null;
private int islandLevel = 0;
private int deathCount = 0;
private Timestamp islandCreated = null;
private int challengesCompleted = 0;

public int getChallengesCompleted() {
return challengesCompleted;
}

public void setChallengesCompleted(int challengesCompleted) {
this.challengesCompleted = challengesCompleted;
}

public Timestamp getIslandCreated() {
return islandCreated;
}

public void setIslandCreated(Timestamp islandCreated) {
this.islandCreated = islandCreated;
}


/**
Expand Down
15 changes: 14 additions & 1 deletion src/main/resources/eng.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,17 @@ WorldIsNoWorld: §bGiven "world" is not a world!
WorldListHeader: '§bVSkyblock worlds:'
WorldLoaded: §bWorld loaded successfully!
WorldNotFound: §cWorld §6%replacement% §cnot found!
WorldUnloaded: §bWorld unloaded successfully!
WorldUnloaded: §bWorld unloaded successfully!

IslandInfoLoading: §aLoading your island's information...
#Supports '&' Chat colors
IslandInfoLabelAge: '&e&lIsland Age&7: &a'
IslandInfoLabelSize: '&e&lIsland Size&7: &a'
IslandInfoLabelSizeUnits: '&ablocks'
IslandInfoLabelDifficulty: '&e&lIsland Difficulty&7: &a'
IslandInfoLabelTotalMembers: '&e&lIsland Total Members&7: &a'
IslandInfoLabelZBorder: '&e&lIsland Z Border&7: &a'
IslandInfoLabelXBorder: '&e&lIsland X Border&7: &a'
IslandInfoLabelLevel: '&e&lIsland Level&7: &a'
IslandInfoLabelProgress: '&e&lLevel Progress&7: &a'
IslandInfoLabelChallenges: '&e&lChallenges Completed&7: &a'