Skip to content

Commit 2d64cc8

Browse files
committed
Added Animate Title
1 parent 8137276 commit 2d64cc8

5 files changed

Lines changed: 63 additions & 8 deletions

File tree

src/main/java/org/brokenarrow/menu/library/MenuHolder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.brokenarrow.menu.library.builders.ButtonData;
44
import org.brokenarrow.menu.library.builders.MenuDataUtility;
55
import org.brokenarrow.menu.library.utility.Function;
6+
import org.brokenarrow.menu.library.utility.PairFunction;
67
import org.bukkit.Bukkit;
78
import org.bukkit.Location;
89
import org.bukkit.Sound;
@@ -173,6 +174,20 @@ public void setTitle(final Function<String> function) {
173174
this.function = function;
174175
}
175176

177+
178+
/**
179+
* Sets the title of the menu using the specified
180+
* function to animate title.
181+
*
182+
* @param time set how often it shall update, in seconds.
183+
* @param function a function that takes a String and boolean input, for animate title.
184+
*/
185+
public void setAnimateTitle(final int time, final PairFunction<String> function) {
186+
this.animateTitleTime = time;
187+
this.animateTitle = function;
188+
this.animateTitle();
189+
}
190+
176191
/**
177192
* Set type of inventory, defult will it use chest or hopper. If you set
178193
* the type you can´t change size.

src/main/java/org/brokenarrow/menu/library/MenuUtility.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.brokenarrow.menu.library.builders.MenuDataUtility;
77
import org.brokenarrow.menu.library.utility.Function;
88
import org.brokenarrow.menu.library.utility.Item.CreateItemStack;
9+
import org.brokenarrow.menu.library.utility.Item.Pair;
10+
import org.brokenarrow.menu.library.utility.PairFunction;
911
import org.brokenarrow.menu.library.utility.Validate;
1012
import org.bukkit.Bukkit;
1113
import org.bukkit.Location;
@@ -89,6 +91,9 @@ public MenuUtility(final @Nullable List<Integer> fillSlots, @Nullable final List
8991
protected int itemsPerPage = this.inventorySize;
9092
protected int pageNumber;
9193
protected int updateTime;
94+
protected int taskidAnimateTitle;
95+
protected int animateTitleTime;
96+
protected PairFunction<String> animateTitle;
9297

9398
protected Player player;
9499
protected Sound menuOpenSound;
@@ -735,6 +740,10 @@ protected void onMenuClose(final InventoryCloseEvent event) {
735740
Bukkit.getScheduler().cancelTask(this.taskid);
736741

737742
}
743+
if (Bukkit.getScheduler().isCurrentlyRunning(this.taskidAnimateTitle) || Bukkit.getScheduler().isQueued(this.taskidAnimateTitle)) {
744+
Bukkit.getScheduler().cancelTask(this.taskidAnimateTitle);
745+
746+
}
738747
}
739748

740749
protected Inventory loadInventory(final Player player, final boolean loadToCahe) {
@@ -1032,4 +1041,24 @@ private Set<Integer> getItemSlotsMap(final MenuDataUtility menuDataMap, final Me
10321041
return slotList;
10331042
}
10341043

1044+
protected void animateTitle() {
1045+
PairFunction<String> task = this.animateTitle;
1046+
if (task == null) return;
1047+
this.taskidAnimateTitle = new BukkitRunnable() {
1048+
1049+
@Override
1050+
public void run() {
1051+
Pair<String, Boolean> apply = task.apply();
1052+
String text = apply.getFirst();
1053+
if (text == null || !apply.getSecond()) {
1054+
this.cancel();
1055+
UpdateTittleContainers.update(player, getTitle());
1056+
return;
1057+
}
1058+
if (!text.isEmpty()) {
1059+
UpdateTittleContainers.update(player, text);
1060+
}
1061+
}
1062+
}.runTaskTimerAsynchronously(plugin, 1, 20 + this.animateTitleTime).getTaskId();
1063+
}
10351064
}

src/main/java/org/brokenarrow/menu/library/utility/Item/CreateItemStack.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class CreateItemStack {
4848
private final Iterable<?> itemArray;
4949
private final String displayName;
5050
private final List<String> lore;
51-
private final Map<Enchantment, Tuple<Integer, Boolean>> enchantments = new HashMap<>();
51+
private final Map<Enchantment, Pair<Integer, Boolean>> enchantments = new HashMap<>();
5252
private final List<ItemFlag> visibleItemFlags = new ArrayList<>();
5353
private final List<ItemFlag> flagsToHide = new ArrayList<>();
5454
private final List<Pattern> pattern = new ArrayList<>();
@@ -268,7 +268,7 @@ public CreateItemStack addPattern(final List<Pattern> pattern) {
268268
*
269269
* @return map with enchantment level and if it shall ignore level reestriction.
270270
*/
271-
public Map<Enchantment, Tuple<Integer, Boolean>> getEnchantments() {
271+
public Map<Enchantment, Pair<Integer, Boolean>> getEnchantments() {
272272
return enchantments;
273273
}
274274

@@ -393,7 +393,7 @@ public CreateItemStack addEnchantments(final Enchantment... enchantments) {
393393
* @param override the old value in the map if you set it to true.
394394
* @return this class.
395395
*/
396-
public CreateItemStack addEnchantments(final Map<Enchantment, Tuple<Integer, Boolean>> enchantmentMap, final boolean override) {
396+
public CreateItemStack addEnchantments(final Map<Enchantment, Pair<Integer, Boolean>> enchantmentMap, final boolean override) {
397397
Validate.checkNotNull(enchantmentMap, "this map is null");
398398
if (enchantmentMap.isEmpty())
399399
getLogger(Level.INFO, "This map is empty so no enchantments vill be added");
@@ -425,7 +425,7 @@ else if (enchant instanceof Enchantment)
425425
enchantment = (Enchantment) enchant;
426426

427427
if (enchantment != null)
428-
this.enchantments.put(enchantment, new Tuple<>(enchantmentLevel, levelRestriction));
428+
this.enchantments.put(enchantment, new Pair<>(enchantmentLevel, levelRestriction));
429429
else
430430
getLogger(Level.INFO, "your enchantment: " + enchant + " ,are not valid.");
431431

@@ -867,12 +867,12 @@ private void hideEnchantments(final ItemMeta itemMeta) {
867867
public boolean addEnchantments(final ItemMeta itemMeta) {
868868
if (!this.getEnchantments().isEmpty()) {
869869
boolean haveEnchant = false;
870-
for (final Map.Entry<Enchantment, Tuple<Integer, Boolean>> enchant : this.getEnchantments().entrySet()) {
870+
for (final Map.Entry<Enchantment, Pair<Integer, Boolean>> enchant : this.getEnchantments().entrySet()) {
871871
if (enchant == null) {
872872
getLogger(Level.INFO, "Your enchantment are null.");
873873
continue;
874874
}
875-
final Tuple<Integer, Boolean> level = enchant.getValue();
875+
final Pair<Integer, Boolean> level = enchant.getValue();
876876
haveEnchant = itemMeta.addEnchant(enchant.getKey(), level.getFirst() <= 0 ? 1 : level.getFirst(), level.getSecond());
877877
}
878878
if (isShowEnchantments() || !this.getFlagsToHide().isEmpty())

src/main/java/org/brokenarrow/menu/library/utility/Item/Tuple.java renamed to src/main/java/org/brokenarrow/menu/library/utility/Item/Pair.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.brokenarrow.menu.library.utility.Item;
22

3-
public class Tuple<K, V> {
3+
public class Pair<K, V> {
44

55
private final K firstValue;
66
private final V secondValue;
77

88

9-
public Tuple(K firstValue, V secondValue) {
9+
public Pair(K firstValue, V secondValue) {
1010

1111
this.firstValue = firstValue;
1212
this.secondValue = secondValue;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.brokenarrow.menu.library.utility;
2+
3+
import org.brokenarrow.menu.library.utility.Item.Pair;
4+
5+
import javax.annotation.Nonnull;
6+
7+
public interface PairFunction<T> {
8+
@Nonnull
9+
Pair<T, Boolean> apply();
10+
11+
}

0 commit comments

Comments
 (0)