Skip to content

Commit

Permalink
request modid for better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
GiantLuigi4 committed Oct 30, 2023
1 parent 75b077d commit 3420952
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/main/java/turniplabs/halplibe/helper/BlockHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ public static int findLength(int id, int terminate) {

/**
* Allows halplibe to automatically figure out where to insert the runs
* @param modid an identifier for the mod, can be anything, but should be something the user can identify
* @param runs a toml object representing configured registry runs
* @param neededIds the number of needed ids
* if this changes after the mod has been configured (i.e. mod updated and now has more blocks) it'll find new, valid runs to put those blocks into
* @param function the function to run for registering items
*/
public static void reserveRuns(Toml runs, int neededIds, Consumer<IdSupplier> function) {
public static void reserveRuns(String modid, Toml runs, int neededIds, Consumer<IdSupplier> function) {
RunLengthConfig cfg = new RunLengthConfig(runs, neededIds);
cfg.register(reserves);
RegistryHelper.scheduleSmartRegistry(
() -> {
IdSupplier supplier = new IdSupplier(reserves, cfg, neededIds);
IdSupplier supplier = new IdSupplier(modid, reserves, cfg, neededIds);
function.accept(supplier);
supplier.validate();
}
);
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/turniplabs/halplibe/helper/ItemHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ public static int findLength(int id, int terminate) {

/**
* Allows halplibe to automatically figure out where to insert the runs
* @param modid an identifier for the mod, can be anything, but should be something the user can identify
* @param runs a toml object representing configured registry runs
* @param neededIds the number of needed ids
* if this changes after the mod has been configured (i.e. mod updated and now has more items) it'll find new, valid runs to put those items into
* @param function the function to run for registering items
*/
public static void reserveRuns(Toml runs, int neededIds, Consumer<IdSupplier> function) {
public static void reserveRuns(String modid, Toml runs, int neededIds, Consumer<IdSupplier> function) {
RunLengthConfig cfg = new RunLengthConfig(runs, neededIds);
cfg.register(reserves);
RegistryHelper.scheduleSmartRegistry(
() -> {
IdSupplier supplier = new IdSupplier(reserves, cfg, neededIds);
IdSupplier supplier = new IdSupplier(modid, reserves, cfg, neededIds);
function.accept(supplier);
supplier.validate();
}
);
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/turniplabs/halplibe/util/registry/IdSupplier.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package turniplabs.halplibe.util.registry;

import turniplabs.halplibe.util.registry.error.RequestOutOfBounds;
import turniplabs.halplibe.util.registry.error.RequestCutShortException;
import turniplabs.halplibe.util.registry.error.RequestOutOfBoundsException;

import java.util.ArrayList;
import java.util.List;

public class IdSupplier {
String modid;
RunReserves reserves;
RunLengthConfig cfg;
int max;
Expand All @@ -15,7 +17,8 @@ public class IdSupplier {
/* if this is true, the reservations will get optimized when being written */
boolean hasUnreserved = false;

public IdSupplier(RunReserves reserves, RunLengthConfig cfg, int max) {
public IdSupplier(String modid, RunReserves reserves, RunLengthConfig cfg, int max) {
this.modid = modid;
this.reserves = reserves;
this.cfg = cfg;
this.max = max;
Expand Down Expand Up @@ -80,7 +83,7 @@ public int next() {
}

if (done > max) {
throw new RequestOutOfBounds("A mod has gotten more ids than it has requested.");
throw new RequestOutOfBoundsException(modid + " has grabbed more ids than it has requested.");
}

done++;
Expand Down Expand Up @@ -152,4 +155,10 @@ public void ensureFree(int amount) {
reservationEnd = reservationStart + reserves.runLengthFinder.apply(reservationStart, max - done);
current = 0;
}

public void validate() {
if (done != max) {
throw new RequestCutShortException(modid + " did not use up all requested ids.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package turniplabs.halplibe.util.registry.error;

public class RequestCutShortException extends RuntimeException {
public RequestCutShortException(String message) {
super(message);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package turniplabs.halplibe.util.registry.error;

public class RequestOutOfBoundsException extends RuntimeException {
public RequestOutOfBoundsException(String message) {
super(message);
}
}

0 comments on commit 3420952

Please sign in to comment.