Skip to content

Commit 5f42055

Browse files
committed
update colony peripheral
1 parent 5bab060 commit 5f42055

3 files changed

Lines changed: 285 additions & 331 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ appliedmekanistics_version=5978711
3636
curios_version=9.5.1+1.21.1
3737
dimstorage_version=7333077
3838
mekanism_version=1.21.1-10.7.15.81
39-
minecolonies_version=1.1.1230-1.21.1-snapshot
39+
minecolonies_version=1.1.1300-1.21.1-snapshot
4040
patchouli_version=1.21-88
4141
powah_version=6143661
4242
refinedstorage_mekanism_version=1.1.0

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/ColonyPeripheral.java

Lines changed: 95 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;
22

33
import com.minecolonies.api.IMinecoloniesAPI;
4-
import com.minecolonies.api.colony.ICivilianData;
4+
import com.minecolonies.api.colony.ICitizenData;
55
import com.minecolonies.api.colony.IColony;
66
import com.minecolonies.api.colony.IVisitorData;
7-
import com.minecolonies.api.colony.buildings.IBuilding;
87
import com.minecolonies.api.colony.managers.interfaces.IRegisteredStructureManager;
98
import com.minecolonies.api.colony.requestsystem.manager.IRequestManager;
10-
import com.minecolonies.api.colony.requestsystem.request.IRequest;
119
import com.minecolonies.api.colony.requestsystem.requestable.IDeliverable;
1210
import com.minecolonies.api.colony.requestsystem.resolver.player.IPlayerRequestResolver;
1311
import com.minecolonies.api.colony.requestsystem.resolver.retrying.IRetryingRequestResolver;
14-
import com.minecolonies.api.colony.requestsystem.token.IToken;
1512
import com.minecolonies.api.colony.workorders.IWorkOrder;
1613
import com.minecolonies.api.research.IGlobalResearchTree;
1714
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@@ -31,13 +28,13 @@
3128
import net.minecraft.ChatFormatting;
3229
import net.minecraft.core.BlockPos;
3330
import net.minecraft.resources.ResourceLocation;
31+
import org.jetbrains.annotations.NotNull;
32+
import org.jetbrains.annotations.Nullable;
3433

35-
import java.util.ArrayList;
3634
import java.util.HashMap;
37-
import java.util.HashSet;
3835
import java.util.List;
3936
import java.util.Map;
40-
import java.util.Set;
37+
import java.util.stream.Stream;
4138

4239
public class ColonyPeripheral extends BasePeripheral<IPeripheralOwner> {
4340

@@ -66,230 +63,228 @@ public final boolean isInColony() {
6663
@LuaFunction(mainThread = true)
6764
public final boolean isWithin(Map<?, ?> posTable) throws LuaException {
6865
IColony colony = getColonyWithoutPermission();
69-
70-
if(colony == null)
66+
if (colony == null) {
7167
return false;
68+
}
7269

7370
BlockPos pos = LuaConverter.convertToBlockPos(posTable);
74-
7571
return colony.isCoordInColony(this.getLevel(), pos);
7672
}
7773

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-
9074
@LuaFunction(mainThread = true)
9175
public final int amountOfConstructionSites() throws LuaException {
92-
return MineColonies.getAmountOfConstructionSites(getColony());
76+
return MineColonies.getAmountOfConstructionSites(getColonyOrThrow());
9377
}
9478

9579
@LuaFunction(mainThread = true)
9680
public final int getColonyID() throws LuaException {
97-
IColony colony = getColony();
81+
IColony colony = getColonyOrThrow();
9882

9983
return colony.getID();
10084
}
10185

10286
@LuaFunction(mainThread = true)
10387
public final String getColonyName() throws LuaException {
104-
IColony colony = getColony();
88+
IColony colony = getColonyOrThrow();
10589

10690
return colony.getName();
10791
}
10892

10993
@LuaFunction(mainThread = true)
11094
public final String getColonyStyle() throws LuaException {
111-
IColony colony = getColony();
95+
IColony colony = getColonyOrThrow();
11296

11397
return colony.getStructurePack();
11498
}
11599

116100
@LuaFunction(mainThread = true)
117101
public final boolean isActive() throws LuaException {
118-
IColony colony = getColony();
102+
IColony colony = getColonyOrThrow();
119103

120104
return colony.isActive();
121105
}
122106

123107
@LuaFunction(mainThread = true)
124108
public final double getHappiness() throws LuaException {
125-
IColony colony = getColony();
109+
IColony colony = getColonyOrThrow();
126110

127111
return colony.getOverallHappiness();
128112
}
129113

130114
@LuaFunction(mainThread = true)
131115
public final Object getLocation() throws LuaException {
132-
IColony colony = getColony();
116+
IColony colony = getColonyOrThrow();
133117

134118
return LuaConverter.posToLua(colony.getCenter());
135119
}
136120

137121
@LuaFunction(mainThread = true)
138122
public final boolean isUnderAttack() throws LuaException {
139-
IColony colony = getColony();
123+
IColony colony = getColonyOrThrow();
140124

141125
return colony.isColonyUnderAttack();
142126
}
143127

144128
@LuaFunction(mainThread = true)
145129
public final boolean isUnderRaid() throws LuaException {
146-
IColony colony = getColony();
130+
IColony colony = getColonyOrThrow();
147131

148132
return colony.getRaiderManager().isRaided();
149133
}
150134

151135
@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();
154138

155139
return colony.getCitizenManager().getCurrentCitizenCount();
156140
}
157141

158142
@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();
161145

162146
return colony.getCitizenManager().getMaxCitizens();
163147
}
164148

165149
@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();
168152

169153
return colony.getGraveManager().getGraves().size();
170154
}
171155

172156
@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();
183163
}
184164

185165
@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();
188168

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;
193172
}
173+
return MineColonies.citizenToLua(citizen);
174+
}
194175

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();
196185
}
197186

198187
@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();
201190

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();
205200

206-
return worksData;
201+
IRegisteredStructureManager buildingManager = colony.getServerBuildingManager();
202+
return colony.getWorkManager().getWorkOrders().values().stream()
203+
.map(workOrder -> MineColonies.workOrderToLua(workOrder, buildingManager))
204+
.toList();
207205
}
208206

209207
@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();
212210

213211
IGlobalResearchTree globalTree = IGlobalResearchTree.getInstance();
214212

215213
Map<String, Object> result = new HashMap<>();
216214
for (ResourceLocation branch : globalTree.getBranches()) {
217215
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));
219217
} catch (CommandSyntaxException ex) {
220218
AdvancedPeripherals.debug(org.apache.logging.log4j.Level.WARN, "Error getting research for branch {}: {}", branch, ex.getMessage());
221219
ex.printStackTrace();
222220
}
223221
}
224-
225222
return result;
226223
}
227224

228225
@LuaFunction(mainThread = true)
229226
public final Object getWorkOrderResources(int id) throws LuaException {
230-
IColony colony = getColony();
227+
IColony colony = getColonyOrThrow();
231228

232229
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()));
236234
}
237235

238236
@LuaFunction(mainThread = true)
239237
public final Object getBuilderResources(Map<?, ?> posTable) throws LuaException {
240-
IColony colony = getColony();
238+
IColony colony = getColonyOrThrow();
241239

242240
BlockPos blockPos = LuaConverter.convertToBlockPos(posTable);
243-
244-
return MineColonies.builderResourcesToObject(colony, blockPos);
241+
return MineColonies.builderResourcesToLua(colony, colony.getServerBuildingManager().getBuilding(blockPos));
245242
}
246243

247244
@LuaFunction(mainThread = true)
248245
public final Object getRequests() throws LuaException {
249-
IColony colony = getColony();
246+
IColony colony = getColonyOrThrow();
250247

251248
IRequestManager requestManager = colony.getRequestManager();
252249
//All requests assigned to players
253250
IPlayerRequestResolver playerResolver = requestManager.getPlayerResolver();
254251
//All requests not assigned to players
255252
IRetryingRequestResolver requestResolver = requestManager.getRetryingRequestResolver();
256253

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();
283275
}
284276

285-
private IColony getColony() throws LuaException {
277+
@NotNull
278+
private IColony getColonyOrThrow() throws LuaException {
286279
IColony colony = getColonyWithoutPermission();
287280
this.hasPermission = !(owner instanceof PocketPeripheralOwner) || MineColonies.hasAccess(owner.getOwner(), colony);
288-
if (colony == null || !this.hasPermission)
281+
if (colony == null || !this.hasPermission) {
289282
throw new LuaException("Here is no colony or you don't have the right permissions");
283+
}
290284
return colony;
291285
}
292286

287+
@Nullable
293288
private IColony getColonyWithoutPermission() {
294289
IMinecoloniesAPI api = IMinecoloniesAPI.getInstance();
295290
return api.getColonyManager().getColonyByPosFromWorld(getLevel(), getPos());

0 commit comments

Comments
 (0)