Skip to content

Commit

Permalink
Merge pull request #3 from focamacho/dev
Browse files Browse the repository at this point in the history
bunch of fixes
  • Loading branch information
focamacho authored Jul 18, 2023
2 parents d15c6be + 9169347 commit 6753856
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group = 'com.focamacho'
version = '1.0.10'
version = '1.1.0'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bukkit.scheduler.BukkitTask;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

@SuppressWarnings({"unused", "UnusedReturnValue"})
Expand Down Expand Up @@ -51,14 +52,14 @@ public class ChestMenu {
@Getter @Setter private Consumer<InventoryClickEvent> onNumber = (click) -> {};

//Items
protected Map<Integer, MenuItem> items = new HashMap<>();
protected Map<Integer, MenuItem> items = new ConcurrentHashMap<>();

//Bukkit Inventory
@Getter protected Inventory inventory;
protected final Set<Integer> slotsRequiringUpdate = Sets.newHashSet();
@Getter(AccessLevel.PROTECTED) @Setter(AccessLevel.PROTECTED) private BukkitTask updateItemsTask = null;

ChestMenu(String title, int rows, JavaPlugin plugin) {
protected ChestMenu(String title, int rows, JavaPlugin plugin) {
if(rows <= 0 || rows > 6) throw new IllegalArgumentException("The number of rows for a menu must be >= 1 && <= 6.");

this.title = Objects.requireNonNull(title);
Expand Down Expand Up @@ -236,7 +237,7 @@ public void open(Player player) {
Bukkit.getScheduler().runTask(this.plugin, () -> {
if(slotsRequiringUpdate.size() > 0) {
if (slotsRequiringUpdate.contains(null)) update();
else slotsRequiringUpdate.forEach(this::update);
else new HashSet<>(slotsRequiringUpdate).forEach(this::update);
}

Listener listener = SealMenus.registeredListeners.get(this.plugin);
Expand Down Expand Up @@ -322,7 +323,7 @@ static class Listener implements org.bukkit.event.Listener {
private static final MenuItem dummyItem = ClickableItem.create(new ItemStack(Material.AIR));

private final JavaPlugin plugin;
private final Set<ChestMenu> chestMenus = Collections.synchronizedSet(Sets.newHashSet());
private final Set<ChestMenu> chestMenus = Sets.newConcurrentHashSet();

@EventHandler
public void onClick(InventoryClickEvent ce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class PageableChestMenu extends ChestMenu {

@Getter private final int[] itemSlots;
private List<MenuItem> pageableItems;
protected List<MenuItem> pageableItems;

private int page;

Expand All @@ -31,7 +31,7 @@ public class PageableChestMenu extends ChestMenu {
private List<PageableChestMenu> mirrorMenus = Lists.newArrayList();
private PageableChestMenu fatherMenu;

PageableChestMenu(String title, int rows, int[] itemSlots, JavaPlugin plugin) {
protected PageableChestMenu(String title, int rows, int[] itemSlots, JavaPlugin plugin) {
super(title, rows, plugin);
this.itemSlots = itemSlots;
this.page = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
public class ClickableItem extends MenuItem {

private boolean dirty = false;

/**
* Private constructor. Use the static method ClickableItem#create.
*/
Expand All @@ -25,6 +27,22 @@ public static ClickableItem create(@NonNull ItemStack item) {
return new ClickableItem(item);
}

@Override
public MenuItem setItem(@NonNull ItemStack item) {
this.dirty = true;
return super.setItem(item);
}

@Override
public boolean update() {
if(this.dirty) {
this.dirty = false;
return true;
}

return false;
}

@Override
public MenuItem copy() {
return create(this.getItem().clone())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.spongepowered.api.text.Text;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

@SuppressWarnings({"unused", "UnusedReturnValue"})
Expand Down Expand Up @@ -49,15 +50,15 @@ public class ChestMenu {
@Getter @Setter private Consumer<ClickInventoryEvent.NumberPress> onNumber = (click) -> {};

//Items
protected Map<Integer, MenuItem> items = new HashMap<>();
protected Map<Integer, MenuItem> items = new ConcurrentHashMap<>();

//Sponge Inventory
@Getter protected Inventory inventory;
protected List<Player> playersViewing = Lists.newArrayList();
protected final Set<Integer> slotsRequiringUpdate = Sets.newHashSet();
@Getter(AccessLevel.PROTECTED) @Setter(AccessLevel.PROTECTED) private Task updateItemsTask = null;

ChestMenu(String title, int rows, Object plugin) {
protected ChestMenu(String title, int rows, Object plugin) {
if(rows <= 0 || rows > 6) throw new IllegalArgumentException("The number of rows for a menu must be >= 1 && <= 6.");

this.title = Objects.requireNonNull(title);
Expand Down Expand Up @@ -333,7 +334,7 @@ public void open(Player player) {
Task.builder().execute(() -> {
if(slotsRequiringUpdate.size() > 0)
if(slotsRequiringUpdate.contains(null)) update();
else slotsRequiringUpdate.forEach(this::update);
else new HashSet<>(slotsRequiringUpdate).forEach(this::update);
player.closeInventory();
player.openInventory(this.inventory).ifPresent(container -> playersViewing.add(player));
}).submit(this.plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class PageableChestMenu extends ChestMenu {

@Getter private final int[] itemSlots;
private List<MenuItem> pageableItems;
protected List<MenuItem> pageableItems;

private int page;

Expand All @@ -29,7 +29,7 @@ public class PageableChestMenu extends ChestMenu {
private List<PageableChestMenu> mirrorMenus = Lists.newArrayList();
private PageableChestMenu fatherMenu;

PageableChestMenu(String title, int rows, int[] itemSlots, Object plugin) {
protected PageableChestMenu(String title, int rows, int[] itemSlots, Object plugin) {
super(title, rows, plugin);
this.itemSlots = itemSlots;
this.page = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
public class ClickableItem extends MenuItem {

private boolean dirty = false;

/**
* Private constructor. Use the static method ClickableItem#create.
*/
Expand All @@ -25,6 +27,22 @@ public static ClickableItem create(@NonNull ItemStack item) {
return new ClickableItem(item);
}

@Override
public MenuItem setItem(@NonNull ItemStack item) {
this.dirty = true;
return super.setItem(item);
}

@Override
public boolean update() {
if(this.dirty) {
this.dirty = false;
return true;
}

return false;
}

@Override
public MenuItem copy() {
return create(this.getItem().copy())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Base class for menu items.
*/
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Accessors(chain = true)
public abstract class MenuItem {

Expand Down

0 comments on commit 6753856

Please sign in to comment.