Skip to content

Commit

Permalink
Move init and load updates around a bit to subvert unknown Forge asyn…
Browse files Browse the repository at this point in the history
…c classloading bug and cleanup mitigation code
  • Loading branch information
brentmaas committed Aug 29, 2024
1 parent 8ab217d commit caad4cd
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class AbstractRenderHandler {
public void render() {
pushProfiler(BuildGuide.modid);

if(BuildGuide.stateManager.isStatePresent() && BuildGuide.stateManager.getState().enabled && BuildGuide.stateManager.getState().isShapeAvailable() && BuildGuide.stateManager.getState().getCurrentShapeSet().origin != null) { // isStatePresent: quickfix for weird ClassNotFoundException when opening the GUI
if(BuildGuide.stateManager.getState().enabled && BuildGuide.stateManager.getState().isShapeAvailable() && BuildGuide.stateManager.getState().getCurrentShapeSet().origin != null) {
for(ShapeSet s: BuildGuide.stateManager.getState().shapeSets) renderShapeSet(s);
}

Expand Down
12 changes: 11 additions & 1 deletion common/src/brentmaas/buildguide/common/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class State {
private static final String PERSISTENCE_ISHAPENEW = "iShapeNew";

private boolean initialised = false;
private boolean fromPersistence = false;
public ArrayList<ShapeSet> shapeSets = new ArrayList<ShapeSet>();
protected int iShapeSet = 0;
public int iShapeNew = ShapeRegistry.getShapeId(ShapeCircle.class);
Expand Down Expand Up @@ -82,20 +83,27 @@ public void pushNewShapeSet() {
newShapeSet.colourOriginG = random.nextFloat();
newShapeSet.colourOriginB = random.nextFloat();
}
newShapeSet.updateAllShapes();
shapeSets.add(newShapeSet);
}

public void initCheck() {
if(!initialised) {
if(fromPersistence) {
for(ShapeSet shapeSet: shapeSets) {
shapeSet.updateAllShapes();
}
}

if(shapeSets.size() == 0) {
pushNewShapeSet();
shapeSets.get(0).updateAllShapes();
}
for(ShapeSet s: shapeSets) {
if(s.origin == null) {
s.resetOrigin();
}
}

initialised = true;
}
}
Expand Down Expand Up @@ -222,6 +230,8 @@ public void loadPersistence(File persistenceFile) throws IOException {
scanner.close();
iShapeSet = Math.max(0, Math.min(shapeSets.size() - 1, iShapeSet));
iShapeNew = Math.max(0, Math.min(ShapeRegistry.getNumberOfShapes() - 1, iShapeNew));

fromPersistence = true;
}

public void savePersistence(File persistenceFile) throws IOException {
Expand Down
2 changes: 2 additions & 0 deletions common/src/brentmaas/buildguide/common/screen/BaseScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public void init() {
addWidget(buttonVisualisation);
addWidget(buttonShapeList);
addWidget(buttonConfiguration);

BuildGuide.stateManager.getState().initCheck();
}

public void render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public class ShapeScreen extends BaseScreen{
public void init() {
super.init();

BuildGuide.stateManager.getState().initCheck();

if(!BuildGuide.stateManager.getState().isShapeAvailable()) {
buttonShapePrevious.setActive(false);
buttonShapeNext.setActive(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ShapelistScreen extends BaseScreen {
private IButton buttonNewShapeNext = BuildGuide.widgetHandler.createButton(145, 70, 20, 20, "->", () -> updateNewShape(1));
private IButton buttonAdd = BuildGuide.widgetHandler.createButton(5, 90, 160, 20, BuildGuide.screenHandler.translate("screen.buildguide.add"), () -> {
BuildGuide.stateManager.getState().pushNewShapeSet();
BuildGuide.stateManager.getState().getShapeSet(BuildGuide.stateManager.getState().getNumberOfShapeSets() - 1).updateAllShapes();
shapeList.addEntry(BuildGuide.stateManager.getState().getNumberOfShapeSets() - 1);

checkActive();
Expand Down
1 change: 0 additions & 1 deletion common/src/brentmaas/buildguide/common/shape/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public void restorePersistence(String persistenceData) {
for(int i = 0;i < properties.size();++i) {
success = success & properties.get(i).setValueFromString(splitData[i]);
}
update();
error = !success;
}else {
error = true;
Expand Down
6 changes: 1 addition & 5 deletions common/src/brentmaas/buildguide/common/shape/ShapeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public ShapeSet(int startIndex) {
private Shape initialiseShape(String shapeId) {
Shape newShape = ShapeRegistry.getNewInstance(shapeId);
newShape.shapeSet = this;
newShape.update();
return newShape;
}

Expand Down Expand Up @@ -98,6 +97,7 @@ public boolean isShapeAvailable(int index) {
public Shape getShape() {
if(shapes[index] == null) {
shapes[index] = initialiseShape(ShapeRegistry.getClassIdentifier(index));
shapes[index].update();
}

return shapes[index];
Expand Down Expand Up @@ -164,11 +164,7 @@ public void restorePersistence(String persistenceData) {
int index = ShapeRegistry.getShapeId(key);
if(index >= 0) {
shapes[index] = initialiseShape(key);
// Lock wizardry due to classloading issues on Forge
shapes[index].cancelFuture();
shapes[index].lock.lock();
shapes[index].restorePersistence(value);
shapes[index].lock.unlock();
}
}
}
Expand Down

0 comments on commit caad4cd

Please sign in to comment.