|
1 | 1 | package de.srendi.advancedperipherals.common.addons.computercraft.peripheral; |
2 | 2 |
|
3 | 3 | import com.minecolonies.api.IMinecoloniesAPI; |
4 | | -import com.minecolonies.api.colony.ICivilianData; |
| 4 | +import com.minecolonies.api.colony.ICitizenData; |
5 | 5 | import com.minecolonies.api.colony.IColony; |
6 | 6 | import com.minecolonies.api.colony.IVisitorData; |
7 | | -import com.minecolonies.api.colony.buildings.IBuilding; |
8 | 7 | import com.minecolonies.api.colony.managers.interfaces.IRegisteredStructureManager; |
9 | 8 | import com.minecolonies.api.colony.requestsystem.manager.IRequestManager; |
10 | | -import com.minecolonies.api.colony.requestsystem.request.IRequest; |
11 | 9 | import com.minecolonies.api.colony.requestsystem.requestable.IDeliverable; |
12 | 10 | import com.minecolonies.api.colony.requestsystem.resolver.player.IPlayerRequestResolver; |
13 | 11 | import com.minecolonies.api.colony.requestsystem.resolver.retrying.IRetryingRequestResolver; |
14 | | -import com.minecolonies.api.colony.requestsystem.token.IToken; |
15 | 12 | import com.minecolonies.api.colony.workorders.IWorkOrder; |
16 | 13 | import com.minecolonies.api.research.IGlobalResearchTree; |
17 | 14 | import com.mojang.brigadier.exceptions.CommandSyntaxException; |
|
31 | 28 | import net.minecraft.ChatFormatting; |
32 | 29 | import net.minecraft.core.BlockPos; |
33 | 30 | import net.minecraft.resources.ResourceLocation; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | +import org.jetbrains.annotations.Nullable; |
34 | 33 |
|
35 | | -import java.util.ArrayList; |
36 | 34 | import java.util.HashMap; |
37 | | -import java.util.HashSet; |
38 | 35 | import java.util.List; |
39 | 36 | import java.util.Map; |
40 | | -import java.util.Set; |
| 37 | +import java.util.stream.Stream; |
41 | 38 |
|
42 | 39 | public class ColonyPeripheral extends BasePeripheral<IPeripheralOwner> { |
43 | 40 |
|
@@ -66,230 +63,228 @@ public final boolean isInColony() { |
66 | 63 | @LuaFunction(mainThread = true) |
67 | 64 | public final boolean isWithin(Map<?, ?> posTable) throws LuaException { |
68 | 65 | IColony colony = getColonyWithoutPermission(); |
69 | | - |
70 | | - if(colony == null) |
| 66 | + if (colony == null) { |
71 | 67 | return false; |
| 68 | + } |
72 | 69 |
|
73 | 70 | BlockPos pos = LuaConverter.convertToBlockPos(posTable); |
74 | | - |
75 | 71 | return colony.isCoordInColony(this.getLevel(), pos); |
76 | 72 | } |
77 | 73 |
|
78 | | - @LuaFunction(mainThread = true) |
79 | | - public final Object getCitizens() throws LuaException { |
80 | | - IColony colony = getColony(); |
81 | | - |
82 | | - List<Object> list = new ArrayList<>(); |
83 | | - colony.getCitizenManager().getCitizens().forEach(citizen -> { |
84 | | - list.add(MineColonies.citizenToObject(citizen)); |
85 | | - }); |
86 | | - |
87 | | - return list; |
88 | | - } |
89 | | - |
90 | 74 | @LuaFunction(mainThread = true) |
91 | 75 | public final int amountOfConstructionSites() throws LuaException { |
92 | | - return MineColonies.getAmountOfConstructionSites(getColony()); |
| 76 | + return MineColonies.getAmountOfConstructionSites(getColonyOrThrow()); |
93 | 77 | } |
94 | 78 |
|
95 | 79 | @LuaFunction(mainThread = true) |
96 | 80 | public final int getColonyID() throws LuaException { |
97 | | - IColony colony = getColony(); |
| 81 | + IColony colony = getColonyOrThrow(); |
98 | 82 |
|
99 | 83 | return colony.getID(); |
100 | 84 | } |
101 | 85 |
|
102 | 86 | @LuaFunction(mainThread = true) |
103 | 87 | public final String getColonyName() throws LuaException { |
104 | | - IColony colony = getColony(); |
| 88 | + IColony colony = getColonyOrThrow(); |
105 | 89 |
|
106 | 90 | return colony.getName(); |
107 | 91 | } |
108 | 92 |
|
109 | 93 | @LuaFunction(mainThread = true) |
110 | 94 | public final String getColonyStyle() throws LuaException { |
111 | | - IColony colony = getColony(); |
| 95 | + IColony colony = getColonyOrThrow(); |
112 | 96 |
|
113 | 97 | return colony.getStructurePack(); |
114 | 98 | } |
115 | 99 |
|
116 | 100 | @LuaFunction(mainThread = true) |
117 | 101 | public final boolean isActive() throws LuaException { |
118 | | - IColony colony = getColony(); |
| 102 | + IColony colony = getColonyOrThrow(); |
119 | 103 |
|
120 | 104 | return colony.isActive(); |
121 | 105 | } |
122 | 106 |
|
123 | 107 | @LuaFunction(mainThread = true) |
124 | 108 | public final double getHappiness() throws LuaException { |
125 | | - IColony colony = getColony(); |
| 109 | + IColony colony = getColonyOrThrow(); |
126 | 110 |
|
127 | 111 | return colony.getOverallHappiness(); |
128 | 112 | } |
129 | 113 |
|
130 | 114 | @LuaFunction(mainThread = true) |
131 | 115 | public final Object getLocation() throws LuaException { |
132 | | - IColony colony = getColony(); |
| 116 | + IColony colony = getColonyOrThrow(); |
133 | 117 |
|
134 | 118 | return LuaConverter.posToLua(colony.getCenter()); |
135 | 119 | } |
136 | 120 |
|
137 | 121 | @LuaFunction(mainThread = true) |
138 | 122 | public final boolean isUnderAttack() throws LuaException { |
139 | | - IColony colony = getColony(); |
| 123 | + IColony colony = getColonyOrThrow(); |
140 | 124 |
|
141 | 125 | return colony.isColonyUnderAttack(); |
142 | 126 | } |
143 | 127 |
|
144 | 128 | @LuaFunction(mainThread = true) |
145 | 129 | public final boolean isUnderRaid() throws LuaException { |
146 | | - IColony colony = getColony(); |
| 130 | + IColony colony = getColonyOrThrow(); |
147 | 131 |
|
148 | 132 | return colony.getRaiderManager().isRaided(); |
149 | 133 | } |
150 | 134 |
|
151 | 135 | @LuaFunction(mainThread = true) |
152 | | - public final int amountOfCitizens() throws LuaException { |
153 | | - IColony colony = getColony(); |
| 136 | + public final int getCitizenCount() throws LuaException { |
| 137 | + IColony colony = getColonyOrThrow(); |
154 | 138 |
|
155 | 139 | return colony.getCitizenManager().getCurrentCitizenCount(); |
156 | 140 | } |
157 | 141 |
|
158 | 142 | @LuaFunction(mainThread = true) |
159 | | - public final int maxOfCitizens() throws LuaException { |
160 | | - IColony colony = getColony(); |
| 143 | + public final int getCitizenLimit() throws LuaException { |
| 144 | + IColony colony = getColonyOrThrow(); |
161 | 145 |
|
162 | 146 | return colony.getCitizenManager().getMaxCitizens(); |
163 | 147 | } |
164 | 148 |
|
165 | 149 | @LuaFunction(mainThread = true) |
166 | | - public final int amountOfGraves() throws LuaException { |
167 | | - IColony colony = getColony(); |
| 150 | + public final int getGraveCount() throws LuaException { |
| 151 | + IColony colony = getColonyOrThrow(); |
168 | 152 |
|
169 | 153 | return colony.getGraveManager().getGraves().size(); |
170 | 154 | } |
171 | 155 |
|
172 | 156 | @LuaFunction(mainThread = true) |
173 | | - public final Object getVisitors() throws LuaException { |
174 | | - IColony colony = getColony(); |
175 | | - |
176 | | - List<Object> list = new ArrayList<>(); |
177 | | - for (ICivilianData civilian : colony.getVisitorManager().getCivilianDataMap().values()) { |
178 | | - if (!(civilian instanceof IVisitorData visitorData)) |
179 | | - continue; |
180 | | - list.add(MineColonies.visitorToObject(visitorData)); |
181 | | - } |
182 | | - return list; |
| 157 | + public final List<Map<String, Object>> getCitizens() throws LuaException { |
| 158 | + IColony colony = getColonyOrThrow(); |
| 159 | + |
| 160 | + return colony.getCitizenManager().getCitizens().stream() |
| 161 | + .map(MineColonies::citizenToLua) |
| 162 | + .toList(); |
183 | 163 | } |
184 | 164 |
|
185 | 165 | @LuaFunction(mainThread = true) |
186 | | - public final Object getBuildings() throws LuaException { |
187 | | - IColony colony = getColony(); |
| 166 | + public final Map<String, Object> getCitizen(int id) throws LuaException { |
| 167 | + IColony colony = getColonyOrThrow(); |
188 | 168 |
|
189 | | - IRegisteredStructureManager manager = colony.getBuildingManager(); |
190 | | - List<Object> buildingData = new ArrayList<>(); |
191 | | - for (Map.Entry<BlockPos, IBuilding> building : manager.getBuildings().entrySet()) { |
192 | | - buildingData.add(MineColonies.buildingToObject(manager, building.getValue(), building.getKey())); |
| 169 | + ICitizenData citizen = colony.getCitizenManager().getCivilian(id); |
| 170 | + if (citizen == null) { |
| 171 | + return null; |
193 | 172 | } |
| 173 | + return MineColonies.citizenToLua(citizen); |
| 174 | + } |
194 | 175 |
|
195 | | - return buildingData; |
| 176 | + @LuaFunction(mainThread = true) |
| 177 | + public final List<Map<String, Object>> getVisitors() throws LuaException { |
| 178 | + IColony colony = getColonyOrThrow(); |
| 179 | + |
| 180 | + return colony.getVisitorManager().getCivilianDataMap().values().stream() |
| 181 | + .filter(IVisitorData.class::isInstance) |
| 182 | + .map(IVisitorData.class::cast) |
| 183 | + .map(MineColonies::visitorToLua) |
| 184 | + .toList(); |
196 | 185 | } |
197 | 186 |
|
198 | 187 | @LuaFunction(mainThread = true) |
199 | | - public final Object getWorkOrders() throws LuaException { |
200 | | - IColony colony = getColony(); |
| 188 | + public final List<Map<String, Object>> getBuildings() throws LuaException { |
| 189 | + IColony colony = getColonyOrThrow(); |
201 | 190 |
|
202 | | - List<Object> worksData = new ArrayList<>(); |
203 | | - for (IWorkOrder workOrder : colony.getWorkManager().getWorkOrders().values()) |
204 | | - worksData.add(MineColonies.workOrderToObject(workOrder)); |
| 191 | + IRegisteredStructureManager buildingManager = colony.getServerBuildingManager(); |
| 192 | + return buildingManager.getBuildings().values().stream() |
| 193 | + .map(building -> MineColonies.buildingToLua(building, buildingManager)) |
| 194 | + .toList(); |
| 195 | + } |
| 196 | + |
| 197 | + @LuaFunction(mainThread = true) |
| 198 | + public final List<Map<String, Object>> getWorkOrders() throws LuaException { |
| 199 | + IColony colony = getColonyOrThrow(); |
205 | 200 |
|
206 | | - return worksData; |
| 201 | + IRegisteredStructureManager buildingManager = colony.getServerBuildingManager(); |
| 202 | + return colony.getWorkManager().getWorkOrders().values().stream() |
| 203 | + .map(workOrder -> MineColonies.workOrderToLua(workOrder, buildingManager)) |
| 204 | + .toList(); |
207 | 205 | } |
208 | 206 |
|
209 | 207 | @LuaFunction(mainThread = true) |
210 | | - public final Object getResearch() throws LuaException { |
211 | | - IColony colony = getColony(); |
| 208 | + public final Object getResearches() throws LuaException { |
| 209 | + IColony colony = getColonyOrThrow(); |
212 | 210 |
|
213 | 211 | IGlobalResearchTree globalTree = IGlobalResearchTree.getInstance(); |
214 | 212 |
|
215 | 213 | Map<String, Object> result = new HashMap<>(); |
216 | 214 | for (ResourceLocation branch : globalTree.getBranches()) { |
217 | 215 | try { |
218 | | - result.put(branch.toString(), MineColonies.getResearch(branch, globalTree.getPrimaryResearch(branch), colony)); |
| 216 | + result.put(branch.toString(), MineColonies.getResearches(branch, globalTree.getPrimaryResearch(branch), colony)); |
219 | 217 | } catch (CommandSyntaxException ex) { |
220 | 218 | AdvancedPeripherals.debug(org.apache.logging.log4j.Level.WARN, "Error getting research for branch {}: {}", branch, ex.getMessage()); |
221 | 219 | ex.printStackTrace(); |
222 | 220 | } |
223 | 221 | } |
224 | | - |
225 | 222 | return result; |
226 | 223 | } |
227 | 224 |
|
228 | 225 | @LuaFunction(mainThread = true) |
229 | 226 | public final Object getWorkOrderResources(int id) throws LuaException { |
230 | | - IColony colony = getColony(); |
| 227 | + IColony colony = getColonyOrThrow(); |
231 | 228 |
|
232 | 229 | IWorkOrder workOrder = colony.getWorkManager().getWorkOrder(id); |
233 | | - if (workOrder == null) return null; |
234 | | - |
235 | | - return MineColonies.builderResourcesToObject(colony, workOrder.getClaimedBy()); |
| 230 | + if (workOrder == null) { |
| 231 | + return null; |
| 232 | + } |
| 233 | + return MineColonies.builderResourcesToLua(colony, colony.getServerBuildingManager().getBuilding(workOrder.getClaimedBy())); |
236 | 234 | } |
237 | 235 |
|
238 | 236 | @LuaFunction(mainThread = true) |
239 | 237 | public final Object getBuilderResources(Map<?, ?> posTable) throws LuaException { |
240 | | - IColony colony = getColony(); |
| 238 | + IColony colony = getColonyOrThrow(); |
241 | 239 |
|
242 | 240 | BlockPos blockPos = LuaConverter.convertToBlockPos(posTable); |
243 | | - |
244 | | - return MineColonies.builderResourcesToObject(colony, blockPos); |
| 241 | + return MineColonies.builderResourcesToLua(colony, colony.getServerBuildingManager().getBuilding(blockPos)); |
245 | 242 | } |
246 | 243 |
|
247 | 244 | @LuaFunction(mainThread = true) |
248 | 245 | public final Object getRequests() throws LuaException { |
249 | | - IColony colony = getColony(); |
| 246 | + IColony colony = getColonyOrThrow(); |
250 | 247 |
|
251 | 248 | IRequestManager requestManager = colony.getRequestManager(); |
252 | 249 | //All requests assigned to players |
253 | 250 | IPlayerRequestResolver playerResolver = requestManager.getPlayerResolver(); |
254 | 251 | //All requests not assigned to players |
255 | 252 | IRetryingRequestResolver requestResolver = requestManager.getRetryingRequestResolver(); |
256 | 253 |
|
257 | | - Set<IToken<?>> tokens = new HashSet<>(); |
258 | | - tokens.addAll(playerResolver.getAllAssignedRequests()); |
259 | | - tokens.addAll(requestResolver.getAllAssignedRequests()); |
260 | | - |
261 | | - List<IRequest<?>> requests = new ArrayList<>(); |
262 | | - for (IToken<?> token : tokens) { |
263 | | - IRequest<?> request = requestManager.getRequestForToken(token); |
264 | | - if (request.getRequest() instanceof IDeliverable) |
265 | | - requests.add(request); |
266 | | - } |
267 | | - |
268 | | - List<Object> result = new ArrayList<>(); |
269 | | - requests.forEach(request -> { |
270 | | - IDeliverable deliverableRequest = (IDeliverable) request.getRequest(); |
271 | | - Map<String, Object> map = new HashMap<>(); |
272 | | - map.put("id", request.getId().getIdentifier().toString()); |
273 | | - map.put("name", ChatFormatting.stripFormatting(request.getShortDisplayString().getString())); |
274 | | - map.put("desc", ChatFormatting.stripFormatting(request.getLongDisplayString().getString())); |
275 | | - map.put("state", request.getState().toString()); |
276 | | - map.put("count", deliverableRequest.getCount()); |
277 | | - map.put("minCount", deliverableRequest.getMinimumCount()); |
278 | | - map.put("items", request.getDisplayStacks().stream().map(item -> LuaConverter.itemStackToLua(item)).toList()); |
279 | | - map.put("target", request.getRequester().getRequesterDisplayName(requestManager, request).getString()); |
280 | | - result.add(map); |
281 | | - }); |
282 | | - return result; |
| 254 | + return Stream.concat( |
| 255 | + playerResolver.getAllAssignedRequests().stream(), |
| 256 | + requestResolver.getAllAssignedRequests().stream() |
| 257 | + ) |
| 258 | + .distinct() |
| 259 | + .map(requestManager::getRequestForToken) |
| 260 | + .filter(request -> request.getRequest() instanceof IDeliverable) |
| 261 | + .map(request -> { |
| 262 | + IDeliverable deliverableRequest = (IDeliverable) request.getRequest(); |
| 263 | + Map<String, Object> map = new HashMap<>(); |
| 264 | + map.put("id", request.getId().getIdentifier().toString()); |
| 265 | + map.put("displayName", ChatFormatting.stripFormatting(request.getShortDisplayString().getString())); |
| 266 | + map.put("description", ChatFormatting.stripFormatting(request.getLongDisplayString().getString())); |
| 267 | + map.put("state", request.getState().name()); |
| 268 | + map.put("count", deliverableRequest.getCount()); |
| 269 | + map.put("minCount", deliverableRequest.getMinimumCount()); |
| 270 | + map.put("items", request.getDisplayStacks().stream().map(item -> LuaConverter.itemStackToLua(item)).toList()); |
| 271 | + map.put("target", LuaConverter.posToLua(request.getRequester().getLocation().getInDimensionLocation())); |
| 272 | + return map; |
| 273 | + }) |
| 274 | + .toList(); |
283 | 275 | } |
284 | 276 |
|
285 | | - private IColony getColony() throws LuaException { |
| 277 | + @NotNull |
| 278 | + private IColony getColonyOrThrow() throws LuaException { |
286 | 279 | IColony colony = getColonyWithoutPermission(); |
287 | 280 | this.hasPermission = !(owner instanceof PocketPeripheralOwner) || MineColonies.hasAccess(owner.getOwner(), colony); |
288 | | - if (colony == null || !this.hasPermission) |
| 281 | + if (colony == null || !this.hasPermission) { |
289 | 282 | throw new LuaException("Here is no colony or you don't have the right permissions"); |
| 283 | + } |
290 | 284 | return colony; |
291 | 285 | } |
292 | 286 |
|
| 287 | + @Nullable |
293 | 288 | private IColony getColonyWithoutPermission() { |
294 | 289 | IMinecoloniesAPI api = IMinecoloniesAPI.getInstance(); |
295 | 290 | return api.getColonyManager().getColonyByPosFromWorld(getLevel(), getPos()); |
|
0 commit comments