Skip to content

Commit 20c303b

Browse files
committed
Fix #13 (I hope, not tested yet)
1 parent a5da9fe commit 20c303b

File tree

3 files changed

+62
-36
lines changed

3 files changed

+62
-36
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ repositories {
2727
}
2828

2929
dependencies {
30-
compileOnly 'org.spigotmc:spigot-api:1.11.2-R0.1-SNAPSHOT'
31-
compileOnly 'org.apache.commons:commons-lang3:3.5'
30+
compile 'org.spigotmc:spigot-api:1.11.2-R0.1-SNAPSHOT'
31+
compile 'org.apache.commons:commons-lang3:3.5'
3232
}
3333

3434
jar { archiveName = 'SmartInvs-' + project.version + '.jar' }

src/main/java/fr/minuskube/inv/content/Pagination.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ public Pagination last() {
9090

9191
@Override
9292
public Pagination addToIterator(SlotIterator iterator) {
93-
for(ClickableItem item : getPageItems())
94-
iterator.set(item).next();
93+
for(ClickableItem item : getPageItems()) {
94+
iterator.next().set(item);
95+
96+
if(iterator.ended())
97+
break;
98+
}
9599

96100
return this;
97101
}

src/main/java/fr/minuskube/inv/content/SlotIterator.java

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ enum Type {
2929
int column();
3030
SlotIterator column(int column);
3131

32+
boolean started();
3233
boolean ended();
3334

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

4344
private Type type;
45+
private boolean started = false;
4446
private int row, column;
4547
private boolean allowOverride;
4648

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

7274
@Override
7375
public SlotIterator set(ClickableItem item) {
74-
contents.set(row, column, item);
76+
if(canPlace())
77+
contents.set(row, column, item);
78+
7579
return this;
7680
}
7781

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

8387
do {
84-
switch(type) {
85-
case HORIZONTAL:
86-
column--;
88+
if(!this.started) {
89+
this.started = true;
90+
}
91+
else {
92+
switch(type) {
93+
case HORIZONTAL:
94+
column--;
8795

88-
if(column == 0) {
89-
column = inv.getColumns() - 1;
96+
if(column == 0) {
97+
column = inv.getColumns() - 1;
98+
row--;
99+
}
100+
break;
101+
case VERTICAL:
90102
row--;
91-
}
92-
break;
93-
case VERTICAL:
94-
row--;
95103

96-
if(row == 0) {
97-
row = inv.getRows() - 1;
98-
column--;
99-
}
100-
break;
104+
if(row == 0) {
105+
row = inv.getRows() - 1;
106+
column--;
107+
}
108+
break;
109+
}
101110
}
102111
}
103-
while((row != 0 || column != 0) && (blacklisted.contains(SlotPos.of(row, column))
104-
|| (!allowOverride && this.get().isPresent())));
112+
while(!canPlace() && (row != 0 || column != 0));
105113

106114
return this;
107115
}
@@ -112,23 +120,27 @@ public SlotIterator next() {
112120
return this;
113121

114122
do {
115-
switch(type) {
116-
case HORIZONTAL:
117-
column = ++column % inv.getColumns();
118-
119-
if(column == 0)
120-
row++;
121-
break;
122-
case VERTICAL:
123-
row = ++row % inv.getRows();
124-
125-
if(row == 0)
126-
column++;
127-
break;
123+
if(!this.started) {
124+
this.started = true;
125+
}
126+
else {
127+
switch(type) {
128+
case HORIZONTAL:
129+
column = ++column % inv.getColumns();
130+
131+
if(column == 0)
132+
row++;
133+
break;
134+
case VERTICAL:
135+
row = ++row % inv.getRows();
136+
137+
if(row == 0)
138+
column++;
139+
break;
140+
}
128141
}
129142
}
130-
while(!ended() && (blacklisted.contains(SlotPos.of(row, column))
131-
|| (!allowOverride && this.get().isPresent())));
143+
while(!canPlace() && !ended());
132144

133145
return this;
134146
}
@@ -162,6 +174,11 @@ public SlotIterator column(int column) {
162174
return this;
163175
}
164176

177+
@Override
178+
public boolean started() {
179+
return this.started;
180+
}
181+
165182
@Override
166183
public boolean ended() {
167184
return row == inv.getRows() - 1
@@ -176,6 +193,11 @@ public SlotIterator allowOverride(boolean override) {
176193
this.allowOverride = override;
177194
return this;
178195
}
196+
197+
private boolean canPlace() {
198+
return !blacklisted.contains(SlotPos.of(row, column)) && (allowOverride || !this.get().isPresent());
199+
}
200+
179201
}
180202

181203
}

0 commit comments

Comments
 (0)