diff --git a/build.gradle b/build.gradle index 5544048..8824e57 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'com.github.johnrengelman.shadow' version '4.0.4' } group = 'me.skymc' -version = '5.04' +version = '5.05' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/scala/io/izzel/taboolib/cronus/util/Time.java b/src/main/scala/io/izzel/taboolib/cronus/util/Time.java new file mode 100644 index 0000000..02288d6 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/cronus/util/Time.java @@ -0,0 +1,198 @@ +package io.izzel.taboolib.cronus.util; + +import com.google.common.collect.Maps; +import io.izzel.taboolib.cronus.CronusUtils; +import org.bukkit.util.NumberConversions; + +import java.util.Calendar; +import java.util.Map; + +/** + * @Author 坏黑 + * @Since 2019-05-28 17:28 + */ +public class Time { + + private static Map cacheMap = Maps.newHashMap(); + private TimeType type; + private int day; + private int hour; + private int minute; + private long time; + private Map cacheEnd = Maps.newHashMap(); + private Calendar end; + private String origin; + + public Time(String libTime) { + this(CronusUtils.toMillis(libTime)); + } + + public Time(long time) { + this.type = TimeType.TIME; + this.time = time; + } + + public Time(int hour, int minute) { + this.type = TimeType.DAY; + this.hour = hour; + this.minute = minute; + } + + public Time(TimeType type, int day, int hour, int minute) { + this.type = type; + this.day = day; + this.hour = hour; + this.minute = minute; + } + + public Time origin(String origin) { + this.origin = origin; + return this; + } + + public Time in(long start) { + if (this.cacheEnd.containsKey(start)) { + this.end = this.cacheEnd.get(start); + } else { + Calendar calendar = Calendar.getInstance(); + Calendar startCal = (Calendar) calendar.clone(); + startCal.setTimeInMillis(start); + this.end = (Calendar) calendar.clone(); + this.cacheEnd.put(start, this.end); + this.end.set(Calendar.SECOND, 0); + this.end.set(Calendar.MILLISECOND, 0); + if (this.type != TimeType.TIME) { + switch (this.type) { + case DAY: + this.end.set(Calendar.HOUR_OF_DAY, hour); + this.end.set(Calendar.MINUTE, minute); + if (startCal.after(this.end)) { + this.end.add(Calendar.DATE, 1); + } + break; + case WEEK: + this.end.set(Calendar.DAY_OF_WEEK, day + 1); + this.end.set(Calendar.HOUR_OF_DAY, hour); + this.end.set(Calendar.MINUTE, minute); + if (startCal.after(this.end)) { + this.end.add(Calendar.DATE, 7); + } + break; + case MONTH: + this.end.set(Calendar.DAY_OF_MONTH, day); + this.end.set(Calendar.HOUR_OF_DAY, hour); + this.end.set(Calendar.MINUTE, minute); + if (startCal.after(this.end)) { + this.end.add(Calendar.MONTH, 1); + } + break; + } + } + } + return this; + } + + public boolean isTimeout(long start) { + return type == TimeType.TIME ? start + time < System.currentTimeMillis() : isTimeout(); + } + + public boolean isTimeout() { + Calendar calendar = Calendar.getInstance(); + switch (type) { + case DAY: { + return calendar.after(this.end); + } + case WEEK: { + return calendar.after(this.end); + } + case MONTH: { + return calendar.after(this.end); + } + default: + return false; + } + } + + public boolean isEquals() { + Calendar calendar = Calendar.getInstance(); + switch (type) { + case DAY: { + return calendar.get(Calendar.HOUR_OF_DAY) == hour && calendar.get(Calendar.MINUTE) == minute; + } + case WEEK: { + return calendar.get(Calendar.DAY_OF_WEEK) == day && calendar.get(Calendar.HOUR_OF_DAY) == hour && calendar.get(Calendar.MINUTE) == minute; + } + case MONTH: { + return calendar.get(Calendar.DAY_OF_MONTH) == day && calendar.get(Calendar.HOUR_OF_DAY) == hour && calendar.get(Calendar.MINUTE) == minute; + } + default: + return false; + } + } + + public static Time parse(String in) { + return cacheMap.computeIfAbsent(in, n -> parse0(in)); + } + + public static Time parse0(String in) { + if (in == null) { + return null; + } + in = in.toLowerCase(); + if (in.equalsIgnoreCase("never") || in.equals("-1")) { + return null; + } else if (in.startsWith("day:")) { + String[] v = in.substring("day:".length()).split(":"); + return new Time(NumberConversions.toInt(v[0]), NumberConversions.toInt(v.length > 1 ? v[1] : 0)).origin(in); + } else if (in.startsWith("week:")) { + String[] v = in.substring("week:".length()).split(":"); + return new Time(TimeType.WEEK, NumberConversions.toInt(v[0]), NumberConversions.toInt(v.length > 1 ? v[1] : 0), NumberConversions.toInt(v.length > 2 ? v[2] : 0)).origin(in); + } else if (in.startsWith("month:")) { + String[] v = in.substring("month:".length()).split(":"); + return new Time(TimeType.MONTH, NumberConversions.toInt(v[0]), NumberConversions.toInt(v.length > 1 ? v[1] : 0), NumberConversions.toInt(v.length > 1 ? v[2] : 0)).origin(in); + } else { + return new Time(in).origin(in); + } + } + + public static Time parseNoTime(String in) { + if (in == null) { + return null; + } + in = in.toLowerCase(); + if (in.startsWith("week:")) { + String[] v = in.substring("week:".length()).split(":"); + return new Time(TimeType.WEEK, NumberConversions.toInt(v[0]), NumberConversions.toInt(v.length > 1 ? v[1] : 0), NumberConversions.toInt(v.length > 2 ? v[2] : 0)).origin(in); + } else if (in.startsWith("month:")) { + String[] v = in.substring("month:".length()).split(":"); + return new Time(TimeType.MONTH, NumberConversions.toInt(v[0]), NumberConversions.toInt(v.length > 1 ? v[1] : 0), NumberConversions.toInt(v.length > 1 ? v[2] : 0)).origin(in); + } else { + String[] v = in.split(":"); + return new Time(NumberConversions.toInt(v[0]), NumberConversions.toInt(v.length > 1 ? v[1] : 0)).origin(in); + } + } + + public TimeType getType() { + return type; + } + + public int getDay() { + return day; + } + + public int getHour() { + return hour; + } + + public int getMinute() { + return minute; + } + + public long getTime() { + return time; + } + + public String getOrigin() { + return origin; + } +} \ No newline at end of file diff --git a/src/main/scala/io/izzel/taboolib/cronus/util/TimeType.java b/src/main/scala/io/izzel/taboolib/cronus/util/TimeType.java new file mode 100644 index 0000000..0c093c5 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/cronus/util/TimeType.java @@ -0,0 +1,6 @@ +package io.izzel.taboolib.cronus.util; + +public enum TimeType { + + TIME, DAY, WEEK, MONTH +} \ No newline at end of file diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java b/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java index 788c171..290e5d0 100644 --- a/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java +++ b/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java @@ -31,7 +31,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { private PluginCommand registerCommand; private List> linkClasses = new CopyOnWriteArrayList<>(); - private List subCommands = new CopyOnWriteArrayList<>(); + private List subCommands = new CopyOnWriteArrayList<>(); public static BaseMainCommand createCommandExecutor(String command, BaseMainCommand baseMainCommand) { Preconditions.checkArgument(Bukkit.getPluginCommand(command) != null, "PluginCommand \"" + command + "\" not found"); @@ -147,7 +147,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { TLocale.sendTo(sender, "COMMANDS.INTERNAL.TYPE-ERROR", args[0], TLocale.asString("COMMANDS.INTERNAL.TYPE-" + subCommand.getType())); return true; } - String[] subCommandArgs = ArrayUtil.removeFirst(args); + String[] subCommandArgs = removeFirst(args); if (subCommand.isParameterConform(subCommandArgs)) { subCommand.onCommand(sender, command, label, subCommand.ignoredLabel() ? subCommandArgs : args); } else { @@ -230,4 +230,13 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { private boolean hasPermission(CommandSender sender, BaseSubCommand baseSubCommand) { return baseSubCommand == null || baseSubCommand.getPermission() == null || sender.hasPermission(baseSubCommand.getPermission()); } + + private String[] removeFirst(String[] args) { + if (args.length <= 1) { + return new String[0]; + } + List list = ArrayUtil.asList(args); + list.remove(0); + return list.toArray(new String[0]); + } } diff --git a/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java b/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java index 5327b3a..02020a9 100644 --- a/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java @@ -56,6 +56,7 @@ public class TInjectLoader implements TabooLibLoader.Loader { injectTypes.put(TConfig.class, (plugin, field, args, pluginClass, instance) -> { try { TConfig config = TConfig.create(plugin, args.value().length == 0 ? "config.yml" : args.value()[0]); + field.set(instance, config); if (!args.reload().isEmpty()) { try { Method declaredMethod = pluginClass.getDeclaredMethod(args.reload()); @@ -73,7 +74,6 @@ public class TInjectLoader implements TabooLibLoader.Loader { t.printStackTrace(); } } - field.set(instance, config); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/scala/io/izzel/taboolib/util/ArrayUtil.java b/src/main/scala/io/izzel/taboolib/util/ArrayUtil.java index 17c96f5..0de8063 100644 --- a/src/main/scala/io/izzel/taboolib/util/ArrayUtil.java +++ b/src/main/scala/io/izzel/taboolib/util/ArrayUtil.java @@ -1,6 +1,9 @@ package io.izzel.taboolib.util; -import java.util.ArrayList; +import com.google.common.collect.Lists; + +import java.lang.reflect.Array; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -12,19 +15,48 @@ import java.util.stream.IntStream; */ public class ArrayUtil { - public static int indexOf(T[] array, T obj) { - return array == null || array.length == 0 ? -1 : IntStream.range(0, array.length).filter(i -> array[i] != null && array[i].equals(obj)).findFirst().orElse(-1); - } - public static boolean contains(T[] array, T obj) { return indexOf(array, obj) != -1; } + + public static int indexOf(T[] array, T obj) { + return array == null || array.length == 0 ? -1 : IntStream.range(0, array.length).filter(i -> array[i] != null && array[i].equals(obj)).findFirst().orElse(-1); + } @SafeVarargs public static T[] asArray(T... args) { return args; } + public static T[] toArray(List list) { + T[] a = (T[]) Array.newInstance(list.getClass().getComponentType(), list.size()); + Arrays.setAll(a, list::get); + return a; + } + + @SafeVarargs + public static List asList(T... args) { + List list = Lists.newArrayList(); + Collections.addAll(list, args); + return list; + } + + public static T skipEmpty(T obj) { + return skipEmpty(obj, null); + } + + public static T[] skipEmpty(T[] obj) { + return skipEmpty(obj, null); + } + + public static T skipEmpty(T obj, T def) { + return Strings.isEmpty(String.valueOf(obj)) ? def : obj; + } + + public static T[] skipEmpty(T[] obj, T[] def) { + return obj.length == 0 ? def : skipEmpty(obj[0]) == null ? def : obj; + } + public static String arrayJoin(String[] args, int start) { return IntStream.range(start, args.length).mapToObj(i -> args[i] + " ").collect(Collectors.joining()).trim(); } @@ -43,72 +75,17 @@ public class ArrayUtil { @SuppressWarnings("SuspiciousSystemArraycopy") public static T arrayExpand(T oldArray, int expand) { - int length = java.lang.reflect.Array.getLength(oldArray); - Object newArray = java.lang.reflect.Array.newInstance(oldArray.getClass().getComponentType(), length + expand); + int length = Array.getLength(oldArray); + Object newArray = Array.newInstance(oldArray.getClass().getComponentType(), length + expand); System.arraycopy(oldArray, 0, newArray, 0, length); return (T) newArray; } @SuppressWarnings("SuspiciousSystemArraycopy") public static T arrayExpandAtFirst(T oldArray, int expand) { - int length = java.lang.reflect.Array.getLength(oldArray); - Object newArray = java.lang.reflect.Array.newInstance(oldArray.getClass().getComponentType(), length + expand); + int length = Array.getLength(oldArray); + Object newArray = Array.newInstance(oldArray.getClass().getComponentType(), length + expand); System.arraycopy(oldArray, 0, newArray, expand, length); return (T) newArray; } - - public static T skipEmpty(T obj) { - return skipEmpty(obj, null); - } - - public static T[] skipEmpty(T[] obj) { - return skipEmpty(obj, null); - } - - public static T skipEmpty(T obj, T def) { - return Strings.isEmpty(String.valueOf(obj)) ? def : obj; - } - - public static T[] skipEmpty(T[] obj, T[] def) { - if (obj.length == 0) { - return def; - } - T firstElement = skipEmpty(obj[0]); - return firstElement == null ? def : obj; - } - - @SafeVarargs - public static List asList(T... args) { - List list = new ArrayList<>(); - Collections.addAll(list, args); - return list; - } - - // ********************************* - // - // Deprecated - // - // ********************************* - - @Deprecated - public static String[] addFirst(String[] args, String... value) { - if (args.length < 1) { - return value; - } - List list = asList(args); - for (int i = value.length - 1; i >= 0; i--) { - list.add(0, value[i]); - } - return list.toArray(new String[0]); - } - - @Deprecated - public static String[] removeFirst(String[] args) { - if (args.length <= 1) { - return new String[0]; - } - List list = asList(args); - list.remove(0); - return list.toArray(new String[0]); - } } diff --git a/src/main/scala/io/izzel/taboolib/util/item/inventory/ClickEvent.java b/src/main/scala/io/izzel/taboolib/util/item/inventory/ClickEvent.java index 6d50c32..d286939 100644 --- a/src/main/scala/io/izzel/taboolib/util/item/inventory/ClickEvent.java +++ b/src/main/scala/io/izzel/taboolib/util/item/inventory/ClickEvent.java @@ -1,10 +1,16 @@ package io.izzel.taboolib.util.item.inventory; +import com.google.common.collect.Lists; +import io.izzel.taboolib.util.lite.Servers; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryInteractEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.List; /** * @Author 坏黑 @@ -30,6 +36,10 @@ public class ClickEvent { return (InventoryDragEvent) event; } + public int getRawSlot() { + return clickType == ClickType.CLICK ? castClick().getRawSlot() : -1; + } + public char getSlot() { return slot; } @@ -41,4 +51,16 @@ public class ClickEvent { public Player getClicker() { return (Player) ((InventoryInteractEvent) event).getWhoClicked(); } + + public List getAffectItems() { + return clickType == ClickType.CLICK ? Servers.getAffectItemInClickEvent((InventoryClickEvent) event) : Lists.newArrayList(); + } + + public void setCancelled(boolean c) { + ((Cancellable) event).setCancelled(true); + } + + public boolean isCancelled() { + return ((Cancellable) event).isCancelled(); + } } diff --git a/src/main/scala/io/izzel/taboolib/util/item/inventory/MenuBuilder.java b/src/main/scala/io/izzel/taboolib/util/item/inventory/MenuBuilder.java index 8921eee..240f2a6 100644 --- a/src/main/scala/io/izzel/taboolib/util/item/inventory/MenuBuilder.java +++ b/src/main/scala/io/izzel/taboolib/util/item/inventory/MenuBuilder.java @@ -21,7 +21,7 @@ public class MenuBuilder { private Plugin plugin; private String title; private int rows; - private char[][] items ; + private char[][] items = new char[0][0]; private ClickTask clickTask; private CloseTask closeTask; private boolean lockHand; @@ -86,7 +86,7 @@ public class MenuBuilder { } return inventory; } - + public char getSlot(int slot) { for (int i = 0; i < items.length && i < rows; i++) { char[] line = items[i]; diff --git a/src/main/scala/io/izzel/taboolib/util/lite/Servers.java b/src/main/scala/io/izzel/taboolib/util/lite/Servers.java index 2e13e99..79cf4f1 100644 --- a/src/main/scala/io/izzel/taboolib/util/lite/Servers.java +++ b/src/main/scala/io/izzel/taboolib/util/lite/Servers.java @@ -1,12 +1,18 @@ package io.izzel.taboolib.util.lite; +import com.google.common.collect.Lists; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemStack; import java.lang.reflect.Field; +import java.util.List; +import java.util.Optional; /** * @Author 坏黑 @@ -19,8 +25,7 @@ public class Servers { Field f = Enchantment.class.getDeclaredField("acceptingNew"); f.setAccessible(true); f.set(null, value); - } - catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } } @@ -44,4 +49,13 @@ public class Servers { return null; } } + + public static List getAffectItemInClickEvent(InventoryClickEvent e) { + List list = Lists.newArrayList(); + if (e.getClick() == ClickType.NUMBER_KEY) { + Optional.ofNullable(e.getWhoClicked().getInventory().getItem(e.getHotbarButton())).ifPresent(list::add); + } + Optional.ofNullable(e.getCurrentItem()).ifPresent(list::add); + return list; + } }