Skip to content

Commit

Permalink
Merge pull request #146 from FlintMC/fix/pre-config-value-update-event
Browse files Browse the repository at this point in the history
Fix newValue in the pre phase of the ConfigValueUpdateEvent
  • Loading branch information
juliarn committed Feb 27, 2021
2 parents 4522007 + 5c32f26 commit 0b0b29b
Showing 1 changed file with 23 additions and 13 deletions.
Expand Up @@ -160,27 +160,36 @@ public void insertSaveConfig(ConfigMethodInfo info, String getterName, CtMethod
.target(declaring)
.generate();

method.addLocalVariable("event", this.eventClass);

CtField referenceField = this.getReferenceField(
declaring, "configReference" + info.getConfigName());

String eventVar = String.format(
"event = %s.create(this.%s, ($w) this.%s());",
eventFactory.getName(),
referenceField.getName(),
getterName);

String fire = String.format(
"%s.fireEvent(event, %s.%%s);",
"%s.fireEvent(%%s, %s.%%s);",
eventBus.getName(),
Phase.class.getName());

String preSrc = eventVar + String.format(fire, "PRE");
String event = String.format(
"%s.create(this.%s, ($w) %%s)",
eventFactory.getName(),
referenceField.getName());

String postEvent = String.format(event, "this." + getterName + "()");

String preEvent;
if (method.getParameterTypes().length == 1) {
// Simple setters should be fired with the actual new value
preEvent = String.format(event, "$1");
} else {
// The value of multi getters keeps being the same instance (one map) and
// doesn't need to be the new value
preEvent = postEvent;
}
String preFire = String.format(fire, preEvent, "PRE");

if (method.getMethodInfo().getCodeAttribute() != null) {
method.insertBefore(preSrc);
method.insertBefore(preFire);
} else {
method.setBody(preSrc);
method.setBody(preFire);
}

String writeSrc = String.format(
Expand All @@ -189,7 +198,8 @@ public void insertSaveConfig(ConfigMethodInfo info, String getterName, CtMethod
ParsedConfig.class.getName(),
field.getName());

method.insertAfter(writeSrc + String.format(fire, "POST"));
String postFire = String.format(fire, postEvent, "POST");
method.insertAfter(writeSrc + postFire);
}

private CtField getReferenceField(CtClass declaring, String name) throws CannotCompileException {
Expand Down

0 comments on commit 0b0b29b

Please sign in to comment.