Create a good design for operator creation - to avoid class loader issues and recreation
athrane opened this issue ยท 0 comments
Classes like ProcessBlockDirectivesEventHandler
and ReflectEffectEventHandler
has a static creation of operators:
`
/**
* Operator instance (can be null due to class loading).
*/
static Optional optOp = Optional.empty();
/**
* Create operators.
*/
static Supplier<Operator2> splOp = () -> {
// return operator instance if defined
if (optOp.isPresent()) {
return optOp.get();
}
// if mod configuration hasn't been class loaded yet, the return null operator
if (spawnedBlockParticles == null) {
return new NullOp2();
}
// create particle config from configuration
ParticleRenderingInfo particleConfig = createInfoFromConfig(spawnedBlockParticles);
// create particle op from configuration
Operator2 op = new AddParticlesFromPosAtClient2(particleConfig);
optOp = Optional.of(op);
return optOp.get();
};
`
The design must be made more elegant to reduce boilerplate code and support reusability
The good thing about the above is that the code isn't re-initialized every time
run(ports, splOp.get());
is invoked..
Alternate solution:
/**
* Operator instance (can be null due to class loading).
*/
static Optional<Operator2> optOp = Optional.empty();
/**
* Create operators.
*/
static Supplier<Operator2> splOp = () -> {
Operator2 op = new Sequence2(..);
optOp = Optional.of(op);
return optOp.get();
};
run(ports, optOp.orElseGet(splOp));
Maybe a final solution:
static Supplier<Operator2> splDecoyOp = () -> {
Function<Ports, LivingEntity> fnGetTarget = getFnGetLivingEntity2();
BiConsumer<Ports, EffectInstance> bcSetEffectInstance = getBcSetEffectInstance1();
return new Sequence2(new SpawnDecoy2(), new AddEffect2(fnGetTarget, bcSetEffectInstance,
RECEIVE_AGGRO_EFFECT.get(), receiveAggroEffectDuration.get(), receiveAggroEffectAmplifier.get()));
};
static final Operator2 decoyOp = of(splDecoyOp);
..
run(ports, decoyOp);