Skip to content

Commit

Permalink
1.5.45
Browse files Browse the repository at this point in the history
+TimeoutInputListener abstraction layer
SelectorListener is now an InputListener which no longer timeouts.
  • Loading branch information
anjoismysign committed Nov 27, 2022
1 parent e682a7d commit b1451f6
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public abstract class VariableSelector extends BlobInventory {
private HashMap<Integer, Object> values;
private final UUID builderId;
private VariableFiller filler;
private int page;

public static BlobInventory DEFAULT() {
FileManager fileManager = BlobLib.getInstance().getFileManager();
Expand All @@ -33,7 +34,8 @@ public VariableSelector(BlobInventory blobInventory, UUID builderId,
this.dataType = dataType;
setTitle(blobInventory.getTitle().replace("%variable%", dataType));
buildInventory();
loadPage(1, false);
this.page = 1;
loadPage(page, false);
}

@Override
Expand All @@ -44,12 +46,14 @@ public void loadDefaultButtons() {
public void loadPage(int page, boolean refill) {
int itemsPerPage = getSlots("White-Background").size();
int totalPages = filler.totalPages(itemsPerPage);
if (totalPages < page) return;
if (totalPages < page) {
return;
}
if (refill)
refillButton("White-Background");
values.clear();
VariableValue[] values = filler.page(1, itemsPerPage);
for (int i = 0; i < values.length - 1; i++) {
VariableValue[] values = filler.page(page, itemsPerPage);
for (int i = 0; i < values.length; i++) {
setValue(i, values[i]);
}
}
Expand Down Expand Up @@ -116,4 +120,21 @@ public VariableFiller getFiller() {
public int valuesSize() {
return getValues().size();
}

public void setPage(int page) {
int total = filler.totalPages(getSlots("White-Background").size());
if (page > total) {
return;
}
this.page = page;
loadPage(page);
}

public void nextPage() {
setPage(page + 1);
}

public void previousPage() {
setPage(page - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,37 @@
import java.util.List;

public class BlobSelectorListener extends SelectorListener {
private List<BlobMessage> messages;
private final List<BlobMessage> messages;
private BukkitTask messageTask;

/**
* Will run a SelectorListener which will send messages to player every 10 ticks asynchronously
*
* @param owner The owner of the SelectorListener
* @param timeout The timeout of the SelectorListener
* @param inputRunnable The runnable to run when the SelectorListener receives input
* @param timeoutRunnable The runnable to run when the SelectorListener times out
* @param messages The messages to send to the player
* @param owner The owner of the SelectorListener
* @param inputRunnable The runnable to run when the SelectorListener receives input
* @param messages The messages to send to the player
*/
public static BlobSelectorListener build(Player owner, long timeout, Runnable inputRunnable,
Runnable timeoutRunnable, List<BlobMessage> messages,
VariableSelector selector) {
return new BlobSelectorListener(owner.getName(), timeout,
inputRunnable, timeoutRunnable, messages, selector);
public static BlobSelectorListener build(Player owner, Runnable inputRunnable
, List<BlobMessage> messages, VariableSelector selector) {
return new BlobSelectorListener(owner.getName(),
inputRunnable, messages, selector);
}

/**
* Will run a SelectorListener which will send messages to player every 10 ticks asynchronously
*
* @param owner The player's name which is owner of the SelectorListener
* @param timeout The timeout of the SelectorListener
* @param inputRunnable The runnable to run when the SelectorListener receives input
* @param timeoutRunnable The runnable to run when the SelectorListener times out
* @param messages The messages to send to the player
* @param owner The player's name which is owner of the SelectorListener
* @param inputRunnable The runnable to run when the SelectorListener receives input
* @param messages The messages to send to the player
*/
private BlobSelectorListener(String owner, long timeout, Runnable inputRunnable,
Runnable timeoutRunnable, List<BlobMessage> messages,
private BlobSelectorListener(String owner, Runnable inputRunnable, List<BlobMessage> messages,
VariableSelector selector) {
super(owner, timeout, inputRunnable, timeoutRunnable, selector);
super(owner, inputRunnable, selector);
this.messages = messages;
}

@Override
public void runTasks() {
super.runTasks();
BukkitRunnable bukkitRunnable = new BukkitRunnable() {
@Override
public void run() {
Expand All @@ -65,7 +58,6 @@ public void run() {

@Override
public void cancel() {
getTask().cancel();
messageTask.cancel();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package us.mytheria.bloblib.entities.listeners;

public class ChatListener extends InputListener {
public class ChatListener extends TimeoutInputListener {
private String input;

public ChatListener(String owner, long timeout, Runnable inputRunnable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,22 @@

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import us.mytheria.bloblib.BlobLib;

import javax.annotation.Nullable;

public abstract class InputListener {
protected final String owner;
protected final long timeout;
protected final Runnable inputRunnable;
protected final Runnable timeoutRunnable;
protected BukkitTask task;

public InputListener(String owner, long timeout, Runnable inputRunnable,
Runnable timeoutRunnable) {
public InputListener(String owner, Runnable inputRunnable) {
this.owner = owner;
this.timeout = timeout;
this.inputRunnable = inputRunnable;
this.timeoutRunnable = timeoutRunnable;
}

public void runTasks() {
BukkitRunnable bukkitRunnable = new BukkitRunnable() {
@Override
public void run() {
InputListener.this.cancel();
timeoutRunnable.run();
}
};
this.task = bukkitRunnable.runTaskLater(BlobLib.getInstance(), timeout);
}

public void cancel() {
task.cancel();
}

public String getOwner() {
Expand All @@ -46,22 +28,10 @@ public Object getInput() {
return null;
}

public long getTimeout() {
return timeout;
}

public BukkitTask getTask() {
return task;
}

public Runnable getInputRunnable() {
return inputRunnable;
}

public Runnable getTimeoutRunnable() {
return timeoutRunnable;
}

@Nullable
public Player getPlayerOwner() {
return Bukkit.getServer().getPlayer(owner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.bukkit.block.Block;

public class SelPosListener extends InputListener {
public class SelPosListener extends TimeoutInputListener {
private Block input;

public SelPosListener(String owner, long timeout, Runnable inputRunnable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ public class SelectorListener extends InputListener {
private Object input;
private VariableSelector selector;

public SelectorListener(String owner, long timeout, Runnable inputRunnable,
Runnable timeoutRunnable, VariableSelector selector) {
super(owner, timeout, inputRunnable, timeoutRunnable);
public SelectorListener(String owner, Runnable inputRunnable, VariableSelector selector) {
super(owner, inputRunnable);
this.selector = selector;
register();
selector.open();
Expand All @@ -26,7 +25,6 @@ public Object getInput() {

public void setInput(Object input) {
this.input = input;
cancel();
inputRunnable.run();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package us.mytheria.bloblib.entities.listeners;

import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import us.mytheria.bloblib.BlobLib;

public abstract class TimeoutInputListener extends InputListener {
protected final long timeout;
protected final Runnable timeoutRunnable;
protected BukkitTask task;

public TimeoutInputListener(String owner, long timeout, Runnable inputRunnable,
Runnable timeoutRunnable) {
super(owner, inputRunnable);
this.timeout = timeout;
this.timeoutRunnable = timeoutRunnable;
}

@Override
public void runTasks() {
BukkitRunnable bukkitRunnable = new BukkitRunnable() {
@Override
public void run() {
TimeoutInputListener.this.cancel();
timeoutRunnable.run();
}
};
this.task = bukkitRunnable.runTaskLater(BlobLib.getInstance(), timeout);
}

@Override
public void cancel() {
task.cancel();
}

public long getTimeout() {
return timeout;
}

public BukkitTask getTask() {
return task;
}

public Runnable getTimeoutRunnable() {
return timeoutRunnable;
}
}
12 changes: 10 additions & 2 deletions src/main/java/us/mytheria/bloblib/managers/InventoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ public void onClick(InventoryClickEvent e) {
if (listener == null)
return;
e.setCancelled(true);
VariableSelector variableSelector = variableSelectors.get(player);
VariableSelector variableSelector = variableSelectors.get(player.getName());
int slot = e.getRawSlot();
if (slot > variableSelector.valuesSize() - 1)
if (slot > variableSelector.valuesSize() - 1) {
if (variableSelector.isNextPageButton(slot)) {
variableSelector.nextPage();
return;
}
if (variableSelector.isPreviousPageButton(slot)) {
variableSelector.previousPage();
}
return;
}
Object value = variableSelector.getValue(slot);
listener.setInput(value);
player.closeInventory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ public class BlockMaterialFiller implements VariableFiller {
public BlockMaterialFiller() {
materials = new ArrayList<>();
for (Material material : Material.values()) {
if (!material.name().contains("LEGACY_"))
if (material.name().contains("LEGACY"))
continue;
if (!material.isBlock())
continue;
if (new ItemStack(material).getItemMeta() == null)
continue;
materials.add(material);
}
}

@Override
public VariableValue[] page(int page, int itemsPerPage) {
int start = (page - 1) * itemsPerPage;
int end = start + (itemsPerPage - 1);
int end = start + (itemsPerPage);
ArrayList<VariableValue> values = new ArrayList<>();
for (int i = start; i < end; i++) {
Material material;
Expand All @@ -46,6 +48,6 @@ public VariableValue[] page(int page, int itemsPerPage) {

@Override
public int totalPages(int itemsPerPage) {
return (int) Math.ceil(materials.size() / itemsPerPage);
return (int) Math.ceil((double) materials.size() / (double) itemsPerPage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class MaterialFiller implements VariableFiller {
public MaterialFiller() {
materials = new ArrayList<>();
for (Material material : Material.values()) {
if (!material.name().contains("LEGACY_"))
if (material.name().contains("LEGACY"))
continue;
if (new ItemStack(material).getItemMeta() == null)
continue;
materials.add(material);
}
Expand All @@ -24,7 +26,7 @@ public MaterialFiller() {
@Override
public VariableValue[] page(int page, int itemsPerPage) {
int start = (page - 1) * itemsPerPage;
int end = start + (itemsPerPage - 1);
int end = start + (itemsPerPage);
ArrayList<VariableValue> values = new ArrayList<>();
for (int i = start; i < end; i++) {
Material material;
Expand All @@ -44,6 +46,6 @@ public VariableValue[] page(int page, int itemsPerPage) {

@Override
public int totalPages(int itemsPerPage) {
return (int) Math.ceil(materials.size() / itemsPerPage);
return (int) Math.ceil((double) materials.size() / (double) itemsPerPage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public VariableValue[] page(int page, int itemsPerPage) {

@Override
public int totalPages(int itemsPerPage) {
return (int) Math.ceil(entities.size() / itemsPerPage);
return (int) Math.ceil((double) entities.size() / (double) itemsPerPage);
}
}

0 comments on commit b1451f6

Please sign in to comment.