Skip to content

Commit

Permalink
v0.0.6 - New and improved config & kick messages! More customized kic…
Browse files Browse the repository at this point in the history
…k time.
  • Loading branch information
Fido2603 committed May 26, 2019
1 parent eb00a0c commit cb13e5a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ deploy:
provider: releases
api_key:
secure: qsS6Zl57gmOjXqaVuDb7RmyVZ9MGRo4DVXQsyR6FCNStWY/UMRXVeTYjG1IMKI78L8xuAXAB2LSbZoIjB7mezIAtRRfXI5fz/eOsHXpBoOwK3c+042V2MYNDkWubt9mvO10u7nyZ6hpAqKTJ2maEbCva7/tA8v1e/nimtR6JiyysmupLT8UWIA5qT35wkX4VNUz35cazfzRyarAejcw87/IA6ihqLr4uExUva0/IjEjW9Px7CZADvgPQDLzQOvuJ8wBOPM3mXEVZ18DPwoa5j40cFdY4ilxKpv7V860jMCyGS7yAxBUJ077VDwoZdHAmx390xHDEh4Tad2VumhWA8FU3OBO0y30ZX0bufrrOhBEBXFbPIyX/5TAHdwNywAS2cJTsboB04lc/ngAHcSDuVFrQlQdX59R7wmWWU6Vfxr1TIqhKUA1GeWg1NILyI6440U12xYA0ZNrktrHOEg8S5rQAGqfKvWIkGLtY3mtvq3RaSTt6Fj+ZJxv2A/+etHiU+kh233jLg2e0z5ZrM2flTECzF407yPXitNvB97XKsOu2enfgMUKEulK3MOCx+augHFGE7QYfXrV/HE8wvYp3ytoWNL4cfh3TZ/oWub5v38AI332spNzrgnyaGZsuJFrKH0ATAoLnVaztwLuG0qcGquupXETykMzoErnguJHfpUk=
file_glob: true
file: target/Semi-Hardcore-*.jar
skip_cleanup: true
on:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>dk.fido2603</groupId>
<artifactId>semihardcore</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<name>Semi-Hardcore</name>
<repositories>
<!-- Spigot Repo for Spigot and Bukkit -->
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/dk/fido2603/semihardcore/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand Down Expand Up @@ -72,7 +71,7 @@ public void newPlayerCheck(Player player)
return;
}
if (!shouldPlayerBeUnbanned(playerId)) {
player.kickPlayer("You still have time left on your death cooldown: " + (plugin.timeToBan-timeDiff(playerId)) + " hours!");
player.kickPlayer("You still have time left on your death cooldown: " + TimeConverter.parseMillisToUFString(plugin.timeToBan-timeDiff(playerId)) + "!");
return;
}
unbanPlayer(playerId);
Expand Down Expand Up @@ -164,10 +163,10 @@ private long timeDiff(UUID playerId) {
return 0;
}

long diffInMillis = Math.abs(currentTime.getTime() - deathTime.getTime());
long diff = TimeUnit.HOURS.convert(diffInMillis, TimeUnit.MILLISECONDS);
long diff = Math.abs(currentTime.getTime() - deathTime.getTime());
//long diff = TimeUnit.HOURS.convert(diffInMillis, TimeUnit.MILLISECONDS);

plugin.logDebug("Diff in hours: " + diff);
plugin.logDebug("Diff in milliseconds: " + diff);

return diff;
}
Expand All @@ -193,7 +192,7 @@ public boolean banPlayer(Player player, UUID playerId)
// Delay the kick, to not have the console make a "Removing entity while ticking!" Exception
SemiHardcore.server.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
player.kickPlayer("You have been set on a cooldown for: " + plugin.timeToBan.toString() + " hours!");
player.kickPlayer("You have been set on a cooldown for: " + plugin.timeToBanStringUF + "!");
}
}, 1L);

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/dk/fido2603/semihardcore/SemiHardcore.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class SemiHardcore extends JavaPlugin
public boolean vaultEnabled = false;
public static Server server = null;
public boolean debug = false;
public Integer timeToBan = 24;
private String timeToBanString = "24h";
public String timeToBanStringUF = "24 hours";
public long timeToBan = 0L;
private static FileConfiguration config = null;
private static PlayerManager playerManager = null;
private static PermissionsManager permissionsManager = null;
Expand Down Expand Up @@ -138,13 +140,18 @@ public void loadSettings()
config = getConfig();

this.debug = config.getBoolean("Settings.Debug", false);
this.timeToBan = config.getInt("Settings.TimeToBan", 24);
this.timeToBanString = config.getString("Settings.TimeToBan", "24h");

this.timeToBan = TimeConverter.parseStringToMillis(timeToBanString);

this.timeToBanStringUF = TimeConverter.parseMillisToUFString(timeToBan);

}

public void saveSettings()
{
config.set("Settings.Debug", Boolean.valueOf(this.debug));
config.set("Settings.TimeToBan", this.timeToBan);
config.set("Settings.TimeToBan", this.timeToBanString);

saveConfig();
}
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/dk/fido2603/semihardcore/TimeConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dk.fido2603.semihardcore;

import java.util.concurrent.TimeUnit;

public class TimeConverter
{
// parseStringToMillis() and convert() is from https://stackoverflow.com/a/4015476
public static long parseStringToMillis(String input) {
long result = 0;
String number = "";
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isDigit(c)) {
number += c;
} else if (Character.isLetter(c) && !number.isEmpty()) {
result += convert(Integer.parseInt(number), c);
number = "";
}
}
return result;
}

private static long convert(int value, char unit) {
switch(unit) {
case 'd' : return value * 1000*60*60*24;
case 'h' : return value * 1000*60*60;
case 'm' : return value * 1000*60;
case 's' : return value * 1000;
}
return 0;
}

// Self-made from here
public static String parseMillisToUFString(long millis) {
long banDays = TimeUnit.MILLISECONDS.toDays(millis);
long banHours = TimeUnit.MILLISECONDS.toHours(millis);
long banMinutes = TimeUnit.MILLISECONDS.toMinutes(millis);

if (banDays > 0) {
if ((banHours - TimeUnit.DAYS.toHours(banDays)) > 0) {
if ((banMinutes - TimeUnit.HOURS.toMinutes(banHours)) > 0) {
return String.format("%d days, %d hours and %d minutes", banDays, banHours - TimeUnit.DAYS.toHours(banDays), banMinutes - TimeUnit.HOURS.toMinutes(banHours));
}
return String.format("%d days, %d hours", banDays, banHours - TimeUnit.DAYS.toHours(banDays));
}
return String.format("%d days", banDays);
}
if (banHours > 0) {
if ((banMinutes - TimeUnit.HOURS.toMinutes(banHours)) > 0) {
return String.format("%d hours and %d minutes", banHours, banMinutes - TimeUnit.HOURS.toMinutes(banHours));
}
return String.format("%d hours", banHours);
}
return String.format("%d minutes", banMinutes);
}

}

0 comments on commit cb13e5a

Please sign in to comment.