Skip to content

Commit

Permalink
Fix #13 (I hope, not tested yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinusKube committed Jul 8, 2018
1 parent a5da9fe commit 20c303b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 36 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ repositories {
}

dependencies {
compileOnly 'org.spigotmc:spigot-api:1.11.2-R0.1-SNAPSHOT'
compileOnly 'org.apache.commons:commons-lang3:3.5'
compile 'org.spigotmc:spigot-api:1.11.2-R0.1-SNAPSHOT'
compile 'org.apache.commons:commons-lang3:3.5'
}

jar { archiveName = 'SmartInvs-' + project.version + '.jar' }
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/fr/minuskube/inv/content/Pagination.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ public Pagination last() {

@Override
public Pagination addToIterator(SlotIterator iterator) {
for(ClickableItem item : getPageItems())
iterator.set(item).next();
for(ClickableItem item : getPageItems()) {
iterator.next().set(item);

if(iterator.ended())
break;
}

return this;
}
Expand Down
86 changes: 54 additions & 32 deletions src/main/java/fr/minuskube/inv/content/SlotIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum Type {
int column();
SlotIterator column(int column);

boolean started();
boolean ended();

boolean doesAllowOverride();
Expand All @@ -41,6 +42,7 @@ class Impl implements SlotIterator {
private SmartInventory inv;

private Type type;
private boolean started = false;
private int row, column;
private boolean allowOverride;

Expand Down Expand Up @@ -71,7 +73,9 @@ public Optional<ClickableItem> get() {

@Override
public SlotIterator set(ClickableItem item) {
contents.set(row, column, item);
if(canPlace())
contents.set(row, column, item);

return this;
}

Expand All @@ -81,27 +85,31 @@ public SlotIterator previous() {
return this;

do {
switch(type) {
case HORIZONTAL:
column--;
if(!this.started) {
this.started = true;
}
else {
switch(type) {
case HORIZONTAL:
column--;

if(column == 0) {
column = inv.getColumns() - 1;
if(column == 0) {
column = inv.getColumns() - 1;
row--;
}
break;
case VERTICAL:
row--;
}
break;
case VERTICAL:
row--;

if(row == 0) {
row = inv.getRows() - 1;
column--;
}
break;
if(row == 0) {
row = inv.getRows() - 1;
column--;
}
break;
}
}
}
while((row != 0 || column != 0) && (blacklisted.contains(SlotPos.of(row, column))
|| (!allowOverride && this.get().isPresent())));
while(!canPlace() && (row != 0 || column != 0));

return this;
}
Expand All @@ -112,23 +120,27 @@ public SlotIterator next() {
return this;

do {
switch(type) {
case HORIZONTAL:
column = ++column % inv.getColumns();

if(column == 0)
row++;
break;
case VERTICAL:
row = ++row % inv.getRows();

if(row == 0)
column++;
break;
if(!this.started) {
this.started = true;
}
else {
switch(type) {
case HORIZONTAL:
column = ++column % inv.getColumns();

if(column == 0)
row++;
break;
case VERTICAL:
row = ++row % inv.getRows();

if(row == 0)
column++;
break;
}
}
}
while(!ended() && (blacklisted.contains(SlotPos.of(row, column))
|| (!allowOverride && this.get().isPresent())));
while(!canPlace() && !ended());

return this;
}
Expand Down Expand Up @@ -162,6 +174,11 @@ public SlotIterator column(int column) {
return this;
}

@Override
public boolean started() {
return this.started;
}

@Override
public boolean ended() {
return row == inv.getRows() - 1
Expand All @@ -176,6 +193,11 @@ public SlotIterator allowOverride(boolean override) {
this.allowOverride = override;
return this;
}

private boolean canPlace() {
return !blacklisted.contains(SlotPos.of(row, column)) && (allowOverride || !this.get().isPresent());
}

}

}

0 comments on commit 20c303b

Please sign in to comment.