DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
SlotIterator.java
Go to the documentation of this file.
1package com.dreamfirestudios.dreamcore.DreamSmartInvs.content;
2
3import com.dreamfirestudios.dreamcore.DreamSmartInvs.ClickableItem;
4import com.dreamfirestudios.dreamcore.DreamSmartInvs.SmartInventory;
5
6import java.util.HashSet;
7import java.util.Optional;
8import java.util.Set;
9
10public interface SlotIterator {
11
12 enum Type {
14 VERTICAL
15 }
16
17 Optional<ClickableItem> get();
19
22
25
26 int row();
28
29 int column();
31
32 boolean started();
33 boolean ended();
34
36 SlotIterator allowOverride(boolean override);
37
38
39 class Impl implements SlotIterator {
40
41 private InventoryContents contents;
42 private SmartInventory inv;
43
44 private Type type;
45 private boolean started = false;
46 private boolean allowOverride = true;
47 private int row, column;
48
49 private Set<SlotPos> blacklisted = new HashSet<>();
50
52 Type type, int startRow, int startColumn) {
53
54 this.contents = contents;
55 this.inv = inv;
56
57 this.type = type;
58
59 this.row = startRow;
60 this.column = startColumn;
61 }
62
64 Type type) {
65
66 this(contents, inv, type, 0, 0);
67 }
68
69 @Override
70 public Optional<ClickableItem> get() {
71 return contents.get(row, column);
72 }
73
74 @Override
75 public SlotIterator set(ClickableItem item) {
76 if(canPlace())
77 contents.set(row, column, item);
78
79 return this;
80 }
81
82 @Override
84 if(row == 0 && column == 0) {
85 this.started = true;
86 return this;
87 }
88
89 do {
90 if(!this.started) {
91 this.started = true;
92 }
93 else {
94 switch(type) {
95 case HORIZONTAL:
96 column--;
97
98 if(column == 0) {
99 column = inv.getColumns() - 1;
100 row--;
101 }
102 break;
103 case VERTICAL:
104 row--;
105
106 if(row == 0) {
107 row = inv.getRows() - 1;
108 column--;
109 }
110 break;
111 }
112 }
113 }
114 while(!canPlace() && (row != 0 || column != 0));
115
116 return this;
117 }
118
119 @Override
121 if(ended()) {
122 this.started = true;
123 return this;
124 }
125
126 do {
127 if(!this.started) {
128 this.started = true;
129 }
130 else {
131 switch(type) {
132 case HORIZONTAL:
133 column = ++column % inv.getColumns();
134
135 if(column == 0)
136 row++;
137 break;
138 case VERTICAL:
139 row = ++row % inv.getRows();
140
141 if(row == 0)
142 column++;
143 break;
144 }
145 }
146 }
147 while(!canPlace() && !ended());
148
149 return this;
150 }
151
152 @Override
153 public SlotIterator blacklist(int row, int column) {
154 this.blacklisted.add(SlotPos.of(row, column));
155 return this;
156 }
157
158 @Override
160 return blacklist(slotPos.getRow(), slotPos.getColumn());
161 }
162
163 @Override
164 public int row() { return row; }
165
166 @Override
167 public SlotIterator row(int row) {
168 this.row = row;
169 return this;
170 }
171
172 @Override
173 public int column() { return column; }
174
175 @Override
176 public SlotIterator column(int column) {
177 this.column = column;
178 return this;
179 }
180
181 @Override
182 public boolean started() {
183 return this.started;
184 }
185
186 @Override
187 public boolean ended() {
188 return row == inv.getRows() - 1
189 && column == inv.getColumns() - 1;
190 }
191
192 @Override
193 public boolean doesAllowOverride() { return allowOverride; }
194
195 @Override
196 public SlotIterator allowOverride(boolean override) {
197 this.allowOverride = override;
198 return this;
199 }
200
201 private boolean canPlace() {
202 return !blacklisted.contains(SlotPos.of(row, column)) && (allowOverride || !this.get().isPresent());
203 }
204
205 }
206
207}
Impl(InventoryContents contents, SmartInventory inv, Type type)
Impl(InventoryContents contents, SmartInventory inv, Type type, int startRow, int startColumn)
InventoryContents set(int row, int column, ClickableItem item)
Optional< ClickableItem > get(int row, int column)