Skip to content

Commit

Permalink
add MaskSlot for dynamic slot setting
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Feb 2, 2024
1 parent ff26453 commit f7f1fb0
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package me.hsgamer.hscore.minecraft.gui.mask;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* The slot for the mask
*/
public interface MaskSlot {
/**
* Create a mask slot from the slots
*
* @param slots the slots
*
* @return the mask slot
*/
@NotNull
static MaskSlot of(@NotNull List<@NotNull Integer> slots) {
return () -> slots;
}

/**
* Create a mask slot from the slots
*
* @param slots the slots
*
* @return the mask slot
*/
@NotNull
static MaskSlot of(@NotNull Integer... slots) {
List<Integer> slotList = Arrays.asList(slots);
return () -> slotList;
}

/**
* Create a mask slot from the slot stream
*
* @param slotStream the slot stream
*
* @return the mask slot
*/
@NotNull
static MaskSlot of(IntStream slotStream) {
return of(slotStream.boxed().collect(Collectors.toList()));
}

/**
* Get the slots
*
* @return the slots
*/
@NotNull
List<Integer> getSlots();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.hsgamer.hscore.minecraft.gui.mask.impl;

import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.mask.MaskSlot;
import org.jetbrains.annotations.NotNull;

import java.util.*;
Expand All @@ -9,27 +10,27 @@
* The button paginated mask, those with a long list of {@link Button} divided into pages.
*/
public abstract class ButtonPaginatedMask extends PaginatedMask {
protected final List<Integer> slots = new ArrayList<>();
private final MaskSlot maskSlot;

/**
* Create a new mask
*
* @param name the name of the mask
* @param slots the slots
* @param name the name of the mask
* @param maskSlot the mask slot
*/
protected ButtonPaginatedMask(@NotNull String name, @NotNull List<@NotNull Integer> slots) {
protected ButtonPaginatedMask(@NotNull String name, @NotNull MaskSlot maskSlot) {
super(name);
this.slots.addAll(slots);
this.maskSlot = maskSlot;
}

/**
* Get the slots
* Get the mask slot
*
* @return the slots
* @return the mask slot
*/
@NotNull
public List<@NotNull Integer> getSlots() {
return Collections.unmodifiableList(slots);
public MaskSlot getMaskSlot() {
return maskSlot;
}

/**
Expand All @@ -45,13 +46,14 @@ protected ButtonPaginatedMask(@NotNull String name, @NotNull List<@NotNull Integ
@Override
public @NotNull Map<Integer, Button> generateButtons(@NotNull UUID uuid, int size) {
List<Button> buttons = getButtons(uuid);
if (buttons.isEmpty() || this.slots.isEmpty()) {
List<Integer> slots = this.maskSlot.getSlots();
if (buttons.isEmpty() || slots.isEmpty()) {
return Collections.emptyMap();
}

Map<Integer, Button> map = new HashMap<>();
int pageNumber = this.getPage(uuid);
int slotsSize = this.slots.size();
int slotsSize = slots.size();
int offset = pageNumber * slotsSize;
int buttonsSize = buttons.size();

Expand All @@ -60,19 +62,19 @@ protected ButtonPaginatedMask(@NotNull String name, @NotNull List<@NotNull Integ
if (index >= buttonsSize) {
break;
}
map.put(this.slots.get(i), buttons.get(index));
map.put(slots.get(i), buttons.get(index));
}
return map;
}

@Override
public void stop() {
this.slots.clear();
this.pageNumberMap.clear();
}

@Override
public int getPageAmount(@NotNull UUID uuid) {
return this.slots.isEmpty() ? 0 : (int) Math.ceil((double) getButtons(uuid).size() / this.slots.size());
List<Integer> slots = this.maskSlot.getSlots();
return slots.isEmpty() ? 0 : (int) Math.ceil((double) getButtons(uuid).size() / slots.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.mask.BaseMask;
import me.hsgamer.hscore.minecraft.gui.mask.MaskSlot;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,18 +12,18 @@
* The masks with multiple slot
*/
public class MultiSlotsMask extends BaseMask {
protected final List<Integer> slots = new ArrayList<>();
protected final MaskSlot maskSlot;
protected final List<Button> buttons = new ArrayList<>();

/**
* Create a new mask
*
* @param name the name of the mask
* @param slots the slots
* @param name the name of the mask
* @param maskSlot the mask slot
*/
public MultiSlotsMask(@NotNull String name, @NotNull List<@NotNull Integer> slots) {
public MultiSlotsMask(@NotNull String name, @NotNull MaskSlot maskSlot) {
super(name);
this.slots.addAll(slots);
this.maskSlot = maskSlot;
}

/**
Expand Down Expand Up @@ -52,13 +53,13 @@ public MultiSlotsMask addButton(@NotNull Button... button) {
}

/**
* Get the slots
* Get the mask slot
*
* @return the slots
* @return the mask slot
*/
@NotNull
public List<@NotNull Integer> getSlots() {
return Collections.unmodifiableList(slots);
public MaskSlot getMaskSlot() {
return maskSlot;
}

/**
Expand All @@ -74,11 +75,12 @@ public MultiSlotsMask addButton(@NotNull Button... button) {
@Override
public @NotNull Map<Integer, Button> generateButtons(@NotNull UUID uuid, int size) {
Map<Integer, Button> map = new HashMap<>();
if (!this.buttons.isEmpty()) {
int slotsSize = this.slots.size();
List<Integer> slots = this.maskSlot.getSlots();
if (!this.buttons.isEmpty() && !slots.isEmpty()) {
int slotsSize = slots.size();
int buttonsSize = this.buttons.size();
for (int i = 0; i < slotsSize; i++) {
map.put(this.slots.get(i), this.buttons.get(i % buttonsSize));
map.put(slots.get(i), this.buttons.get(i % buttonsSize));
}
}
return map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.hsgamer.hscore.minecraft.gui.mask.impl;

import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.mask.MaskSlot;
import org.jetbrains.annotations.NotNull;

import java.util.*;
Expand All @@ -9,27 +10,27 @@
* A button paginated mask, where each {@link Button} is a page
*/
public abstract class SequencePaginatedMask extends PaginatedMask {
protected final List<Integer> slots = new ArrayList<>();
protected final MaskSlot maskSlot;

/**
* Create a new mask
*
* @param name the name of the mask
* @param slots the slots
* @param name the name of the mask
* @param maskSlot the mask slot
*/
protected SequencePaginatedMask(@NotNull String name, @NotNull List<@NotNull Integer> slots) {
protected SequencePaginatedMask(@NotNull String name, @NotNull MaskSlot maskSlot) {
super(name);
this.slots.addAll(slots);
this.maskSlot = maskSlot;
}

/**
* Get the slots
* Get the mask slot
*
* @return the slots
* @return the mask slot
*/
@NotNull
public List<@NotNull Integer> getSlots() {
return Collections.unmodifiableList(slots);
public MaskSlot getMaskSlot() {
return this.maskSlot;
}

/**
Expand All @@ -45,14 +46,15 @@ protected SequencePaginatedMask(@NotNull String name, @NotNull List<@NotNull Int
@Override
public @NotNull Map<Integer, Button> generateButtons(@NotNull UUID uuid, int size) {
List<Button> buttons = getButtons(uuid);
if (buttons.isEmpty() || this.slots.isEmpty()) {
List<Integer> slots = this.maskSlot.getSlots();
if (buttons.isEmpty() || slots.isEmpty()) {
return Collections.emptyMap();
}

Map<Integer, Button> map = new HashMap<>();
int basePage = this.getPage(uuid);
int buttonsSize = buttons.size();
int slotsSize = this.slots.size();
int slotsSize = slots.size();

for (int i = 0; i < slotsSize; i++) {
int index = i + basePage;
Expand All @@ -61,7 +63,7 @@ protected SequencePaginatedMask(@NotNull String name, @NotNull List<@NotNull Int
} else if (index >= buttonsSize) {
break;
}
map.put(this.slots.get(i), buttons.get(index));
map.put(slots.get(i), buttons.get(index));
}
return map;
}
Expand All @@ -73,7 +75,6 @@ public int getPageAmount(@NotNull UUID uuid) {

@Override
public void stop() {
this.slots.clear();
this.pageNumberMap.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.hsgamer.hscore.minecraft.gui.mask.impl;

import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.mask.MaskSlot;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand All @@ -15,11 +16,11 @@ public class StaticButtonPaginatedMask extends ButtonPaginatedMask {
/**
* Create a new mask
*
* @param name the name of the mask
* @param slots the slots
* @param name the name of the mask
* @param maskSlot the mask slot
*/
public StaticButtonPaginatedMask(@NotNull String name, @NotNull List<@NotNull Integer> slots) {
super(name, slots);
public StaticButtonPaginatedMask(@NotNull String name, @NotNull MaskSlot maskSlot) {
super(name, maskSlot);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.hsgamer.hscore.minecraft.gui.mask.impl;

import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.mask.MaskSlot;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand All @@ -15,11 +16,11 @@ public class StaticSequencePaginatedMask extends SequencePaginatedMask {
/**
* Create a new mask
*
* @param name the name of the mask
* @param slots the slots
* @param name the name of the mask
* @param maskSlot the mask slot
*/
public StaticSequencePaginatedMask(@NotNull String name, @NotNull List<@NotNull Integer> slots) {
super(name, slots);
public StaticSequencePaginatedMask(@NotNull String name, @NotNull MaskSlot maskSlot) {
super(name, maskSlot);
}

/**
Expand Down

0 comments on commit f7f1fb0

Please sign in to comment.