Skip to content

Commit

Permalink
Add masks
Browse files Browse the repository at this point in the history
  • Loading branch information
stefvanschie committed Dec 30, 2019
1 parent 830994e commit fe874f6
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14-pre5-SNAPSHOT</version>
<version>1.15-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.stefvanschie.inventoryframework.Gui;
import com.github.stefvanschie.inventoryframework.GuiItem;
import com.github.stefvanschie.inventoryframework.exception.XMLLoadException;
import com.github.stefvanschie.inventoryframework.pane.util.Mask;
import com.github.stefvanschie.inventoryframework.util.GeometryUtil;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
Expand Down Expand Up @@ -54,6 +55,12 @@ public class OutlinePane extends Pane implements Flippable, Orientable, Rotatabl
*/
private boolean flipHorizontally, flipVertically;

/**
* The mask for this pane
*/
@NotNull
private Mask mask;

/**
* {@inheritDoc}
*/
Expand All @@ -62,6 +69,17 @@ public OutlinePane(int x, int y, int length, int height, @NotNull Priority prior

this.items = new ArrayList<>(length * height);
this.orientation = Orientation.HORIZONTAL;

String[] mask = new String[height];
StringBuilder maskString = new StringBuilder();

for (int i = 0; i < length; i++) {
maskString.append('1');
}

Arrays.fill(mask, maskString.toString());

this.mask = new Mask(mask);
}

/**
Expand All @@ -75,10 +93,7 @@ public OutlinePane(int x, int y, int length, int height) {
* {@inheritDoc}
*/
public OutlinePane(int length, int height) {
super(length, height);

this.items = new ArrayList<>(length * height);
this.orientation = Orientation.HORIZONTAL;
this(0, 0, length, height);
}

/**
Expand All @@ -90,10 +105,42 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
int length = Math.min(this.length, maxLength);
int height = Math.min(this.height, maxHeight);

int x = 0;
int y = 0;
int x = 0, y = 0;

for (int i = 0; i < (repeat ? length * height : items.size()); i++) {
if (orientation == Orientation.HORIZONTAL) {
outerloop:
for (int rowIndex = 0; rowIndex < mask.getHeight(); rowIndex++) {
boolean[] row = mask.getRow(rowIndex);

for (int columnIndex = 0; columnIndex < row.length; columnIndex++) {
if (!row[columnIndex]) {
continue;
}

x = columnIndex;
y = rowIndex;
break outerloop;
}
}
} else if (orientation == Orientation.VERTICAL) {
outerloop:
for (int columnIndex = 0; columnIndex < mask.getLength(); columnIndex++) {
boolean[] column = mask.getColumn(columnIndex);

for (int rowIndex = 0; rowIndex < column.length; rowIndex++) {
if (!column[rowIndex]) {
continue;
}

x = columnIndex;
y = rowIndex;
break outerloop;
}
}
}

outerloop:
for (int i = 0; i < mask.amountOfEnabledSlots(); i++) {
GuiItem item = items.get(i % items.size());

if (!item.isVisible())
Expand Down Expand Up @@ -125,26 +172,34 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
inventory.setItem(finalRow * 9 + finalColumn, item.getItem());
}

//increment positions
if (orientation == Orientation.HORIZONTAL) {
x += gap + 1;
int gapCount = gap;

if (x >= length) {
y += x / length;
x %= length;
do {
if (orientation == Orientation.HORIZONTAL) {
x++;

if (x >= length) {
y++;
x = 0;
}
} else if (orientation == Orientation.VERTICAL) {
y++;

if (y >= height) {
x++;
y = 0;
}
}
} else if (orientation == Orientation.VERTICAL) {
y += gap + 1;

if (y >= height) {
x += y / height;
y %= height;
//stop the loop when there is no more space in the pane
if (x >= length || y >= height) {
break outerloop;
}
}

//stop the loop when there is no more space in the pane
if (x >= length || y >= height)
break;
if (mask.isEnabled(x, y)) {
gapCount--;
}
} while (gapCount >= 0);
}
}

Expand Down Expand Up @@ -251,6 +306,22 @@ public void clear() {
items.clear();
}

/**
* Applies a custom mask to this pane. This will throw an {@link IllegalArgumentException} when the mask's dimension
* differs from this pane's dimension.
*
* @param mask the mask to apply to this pane
* @throws IllegalArgumentException when the mask's dimension is incorrect
* @since 0.5.16
*/
public void applyMask(@NotNull Mask mask) {
if (length != mask.getLength() || height != mask.getHeight()) {
throw new IllegalArgumentException("Mask's dimension must be the same as the pane's dimension");
}

this.mask = mask;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.stefvanschie.inventoryframework.pane;

import com.github.stefvanschie.inventoryframework.pane.util.Mask;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class OutlinePaneTest {

@Test
void testApplyMaskInvalidDimensions() {
assertThrows(IllegalArgumentException.class, () ->
new OutlinePane(3, 7).applyMask(new Mask("0", "1")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.github.stefvanschie.inventoryframework.pane.util;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class MaskTest {

@Test
void testConstructorIncorrectCharacters() {
assertThrows(IllegalArgumentException.class, () -> new Mask("missingno."));
}

@Test
void testConstructorInvalidLengths() {
assertThrows(IllegalArgumentException.class, () -> new Mask(
"0",
"0000000000"
));
}

@Test
void testAmountOfEnabledSlots() {
assertEquals(4, new Mask(
"1001",
"1001"
).amountOfEnabledSlots());
}

@Test
void testGetColumn() {
assertArrayEquals(new boolean[] {true, false}, new Mask(
"10",
"00"
).getColumn(0));
}

@Test
void testGetRow() {
assertArrayEquals(new boolean[] {true, false}, new Mask(
"10",
"00"
).getRow(0));
}

@Test
void testIsEnabled() {
assertTrue(new Mask(
"10",
"00"
).isEnabled(0, 0));
}

@Test
void testGetLength() {
assertEquals(2, new Mask(
"10",
"00",
"01"
).getLength());
}

@Test
void testGetHeight() {
assertEquals(3, new Mask(
"10",
"00",
"01"
).getHeight());
}
}

0 comments on commit fe874f6

Please sign in to comment.