Periods parser
This commit is contained in:
parent
358aa4e2dc
commit
38e9c458e9
@ -6,7 +6,10 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public final class ExtendedPistons extends JavaPlugin {
|
public final class ExtendedPistons extends JavaPlugin {
|
||||||
|
|
||||||
@ -14,6 +17,9 @@ public final class ExtendedPistons extends JavaPlugin {
|
|||||||
|
|
||||||
public FileConfiguration conf;
|
public FileConfiguration conf;
|
||||||
public Permission permsService = null;
|
public Permission permsService = null;
|
||||||
|
public SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
||||||
|
public SimpleDateFormat dayOfWeek = new SimpleDateFormat("EEE",Locale.ENGLISH);
|
||||||
|
public HashMap<String,ArrayList<Period>> weekPeriods;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
@ -26,14 +32,62 @@ public final class ExtendedPistons extends JavaPlugin {
|
|||||||
put("vip", 12);
|
put("vip", 12);
|
||||||
put("premium", 20);
|
put("premium", 20);
|
||||||
}});
|
}});
|
||||||
|
if(!conf.isSet("enabled-times.Mon"))
|
||||||
|
conf.set("enabled-times.Mon",new ArrayList<String>() {{add("14:00-18:00");}});
|
||||||
|
if(!conf.isSet("enabled-times.Tue"))
|
||||||
|
conf.set("enabled-times.Tue",new ArrayList<String>() {{add("14:00-18:00");}});
|
||||||
|
if(!conf.isSet("enabled-times.Wed"))
|
||||||
|
conf.set("enabled-times.Wed",new ArrayList<String>() {{add("14:00-18:00");}});
|
||||||
|
if(!conf.isSet("enabled-times.Thu"))
|
||||||
|
conf.set("enabled-times.Thu",new ArrayList<String>() {{add("14:00-18:00");}});
|
||||||
|
if(!conf.isSet("enabled-times.Fri"))
|
||||||
|
conf.set("enabled-times.Fri",new ArrayList<String>() {{add("14:00-18:00");}});
|
||||||
|
if(!conf.isSet("enabled-times.Sat"))
|
||||||
|
conf.set("enabled-times.Sat",new ArrayList<String>() {{add("14:00-18:00");}});
|
||||||
|
if(!conf.isSet("enabled-times.Sun"))
|
||||||
|
conf.set("enabled-times.Sun",new ArrayList<String>() {{add("14:00-18:00");}});
|
||||||
|
|
||||||
saveConfig();
|
saveConfig();
|
||||||
|
|
||||||
|
weekPeriods = new HashMap<>();
|
||||||
|
parsePeriods("Mon");
|
||||||
|
parsePeriods("Tue");
|
||||||
|
parsePeriods("Wed");
|
||||||
|
parsePeriods("Thu");
|
||||||
|
parsePeriods("Fri");
|
||||||
|
parsePeriods("Sat");
|
||||||
|
parsePeriods("Sun");
|
||||||
|
|
||||||
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
|
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
|
||||||
permsService = rsp.getProvider();
|
permsService = rsp.getProvider();
|
||||||
|
|
||||||
Bukkit.getPluginManager().registerEvents(new WorldListener(), this);
|
Bukkit.getPluginManager().registerEvents(new WorldListener(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void parsePeriods(String dow) {
|
||||||
|
List<String> times = conf.getStringList("enabled-times."+dow);
|
||||||
|
ArrayList<Period> periods = new ArrayList<>();
|
||||||
|
for (String time : times) {
|
||||||
|
try {
|
||||||
|
String[] t = time.split("-");
|
||||||
|
periods.add(new Period(sdf.parse(t[0]), sdf.parse(t[1])));
|
||||||
|
} catch (ParseException e) {
|
||||||
|
getLogger().warning("Unable to parse time period :"+time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
weekPeriods.put(dow,periods);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAllowedTime() {
|
||||||
|
String dow = dayOfWeek.format(Calendar.getInstance(Locale.ENGLISH).getTime());
|
||||||
|
ArrayList<Period> periods = weekPeriods.get(dow);
|
||||||
|
Instant now = Instant.now();
|
||||||
|
for (Period period : periods) {
|
||||||
|
if(period.isBehind(now)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
|
||||||
|
30
src/main/java/ru/redguy/extendedpistons/Period.java
Normal file
30
src/main/java/ru/redguy/extendedpistons/Period.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package ru.redguy.extendedpistons;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
public class Period {
|
||||||
|
private final Instant from;
|
||||||
|
private final Instant to;
|
||||||
|
|
||||||
|
public Period(Date from, Date to) {
|
||||||
|
this.from = nowtifyDate(from);
|
||||||
|
this.to = nowtifyDate(to);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBehind(Instant date) {
|
||||||
|
return from.isBefore(date)&&to.isAfter(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Instant nowtifyDate(Date ins) {
|
||||||
|
Calendar now = GregorianCalendar.getInstance();
|
||||||
|
Calendar cal = GregorianCalendar.getInstance();
|
||||||
|
cal.setTime(ins);
|
||||||
|
cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
|
||||||
|
cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
|
||||||
|
cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));
|
||||||
|
return cal.getTime().toInstant();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user