Skip to content

Commit e7c1073

Browse files
jurrejellescreret
andauthored
RE-OPENED: fix material registration event listener running after Registrate (and before KubeJS) (#4383)
Co-authored-by: screret <[email protected]>
1 parent b95b5ec commit e7c1073

File tree

8 files changed

+112
-85
lines changed

8 files changed

+112
-85
lines changed

src/main/java/com/gregtechceu/gtceu/api/fluid/FluidBuilder.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public FluidBuilder() {}
9696
* @param temperature the temperature of the fluid in Kelvin
9797
* @return this;
9898
*/
99-
public @NotNull FluidBuilder temperature(int temperature) {
99+
public FluidBuilder temperature(int temperature) {
100100
Preconditions.checkArgument(temperature > 0, "temperature must be > 0");
101101
this.temperature = temperature;
102102
return this;
@@ -109,7 +109,7 @@ public FluidBuilder() {}
109109
* @param color the color
110110
* @return this
111111
*/
112-
public @NotNull FluidBuilder color(int color) {
112+
public FluidBuilder color(int color) {
113113
this.color = GTUtil.convertRGBtoARGB(color);
114114
if (this.color == INFER_COLOR) {
115115
return disableColor();
@@ -122,7 +122,7 @@ public FluidBuilder() {}
122122
*
123123
* @return this
124124
*/
125-
public @NotNull FluidBuilder disableColor() {
125+
public FluidBuilder disableColor() {
126126
this.isColorEnabled = false;
127127
return this;
128128
}
@@ -132,7 +132,7 @@ public FluidBuilder() {}
132132
* @return this
133133
*/
134134
@Tolerate
135-
public @NotNull FluidBuilder density(double density) {
135+
public FluidBuilder density(double density) {
136136
return density(convertToMCDensity(density));
137137
}
138138

@@ -156,7 +156,7 @@ private static int convertToMCDensity(double density) {
156156
* @param luminosity of the fluid from [0, 16)
157157
* @return this
158158
*/
159-
public @NotNull FluidBuilder luminosity(int luminosity) {
159+
public FluidBuilder luminosity(int luminosity) {
160160
Preconditions.checkArgument(luminosity >= 0 && luminosity < 16, "luminosity must be >= 0 and < 16");
161161
this.luminosity = luminosity;
162162
return this;
@@ -166,7 +166,7 @@ private static int convertToMCDensity(double density) {
166166
* @param mcViscosity the MC viscosity of the fluid
167167
* @return this
168168
*/
169-
public @NotNull FluidBuilder viscosity(int mcViscosity) {
169+
public FluidBuilder viscosity(int mcViscosity) {
170170
Preconditions.checkArgument(mcViscosity >= 0, "viscosity must be >= 0");
171171
this.viscosity = mcViscosity;
172172
return this;
@@ -176,7 +176,7 @@ private static int convertToMCDensity(double density) {
176176
* @param viscosity the viscosity of the fluid in Poise
177177
* @return this
178178
*/
179-
public @NotNull FluidBuilder viscosity(double viscosity) {
179+
public FluidBuilder viscosity(double viscosity) {
180180
return viscosity(convertViscosity(viscosity));
181181
}
182182

@@ -194,7 +194,7 @@ private static int convertViscosity(double viscosity) {
194194
* @param attribute the attribute to add
195195
* @return this
196196
*/
197-
public @NotNull FluidBuilder attribute(@NotNull FluidAttribute attribute) {
197+
public FluidBuilder attribute(FluidAttribute attribute) {
198198
this.attributes.add(attribute);
199199
return this;
200200
}
@@ -203,7 +203,7 @@ private static int convertViscosity(double viscosity) {
203203
* @param attributes the attributes to add
204204
* @return this
205205
*/
206-
public @NotNull FluidBuilder attributes(@NotNull FluidAttribute @NotNull... attributes) {
206+
public FluidBuilder attributes(FluidAttribute @NotNull... attributes) {
207207
Collections.addAll(this.attributes, attributes);
208208
return this;
209209
}
@@ -213,15 +213,15 @@ private static int convertViscosity(double viscosity) {
213213
*
214214
* @return this
215215
*/
216-
public @NotNull FluidBuilder customStill() {
216+
public FluidBuilder customStill() {
217217
return textures(true);
218218
}
219219

220220
/**
221221
* @param hasCustomStill if the fluid has a custom still texture
222222
* @return this
223223
*/
224-
public @NotNull FluidBuilder textures(boolean hasCustomStill) {
224+
public FluidBuilder textures(boolean hasCustomStill) {
225225
this.hasCustomStill = hasCustomStill;
226226
this.isColorEnabled = false;
227227
return this;
@@ -232,7 +232,7 @@ private static int convertViscosity(double viscosity) {
232232
* @param hasCustomFlowing if the fluid has a custom flowing texture
233233
* @return this
234234
*/
235-
public @NotNull FluidBuilder textures(boolean hasCustomStill, boolean hasCustomFlowing) {
235+
public FluidBuilder textures(boolean hasCustomStill, boolean hasCustomFlowing) {
236236
this.hasCustomStill = hasCustomStill;
237237
this.hasCustomFlowing = hasCustomFlowing;
238238
this.isColorEnabled = false;
@@ -244,7 +244,7 @@ private static int convertViscosity(double viscosity) {
244244
*
245245
* @return this
246246
*/
247-
public @NotNull FluidBuilder block() {
247+
public FluidBuilder block() {
248248
this.hasFluidBlock = true;
249249
return this;
250250
}
@@ -254,14 +254,13 @@ private static int convertViscosity(double viscosity) {
254254
*
255255
* @return this
256256
*/
257-
public @NotNull FluidBuilder disableBucket() {
257+
public FluidBuilder disableBucket() {
258258
this.hasBucket = false;
259259
return this;
260260
}
261261

262262
@SuppressWarnings("UnstableApiUsage")
263-
public @NotNull Supplier<? extends Fluid> build(Material material, @NotNull FluidStorageKey key,
264-
GTRegistrate registrate) {
263+
public Supplier<? extends Fluid> build(Material material, FluidStorageKey key, GTRegistrate registrate) {
265264
determineName(material, key);
266265
determineTextures(material, key);
267266

@@ -293,9 +292,6 @@ private static int convertViscosity(double viscosity) {
293292
.setData(ProviderType.LANG, NonNullBiConsumer.noop());
294293
if (this.hasFluidBlock) {
295294
builder.block()
296-
.blockstate((ctx, prov) -> prov
297-
.simpleBlock(ctx.getEntry(), prov.models().getBuilder(this.name)
298-
.texture("particle", this.still)))
299295
.color(() -> () -> (state, level, pos, index) -> {
300296
return IClientFluidTypeExtensions.of(state.getFluidState())
301297
.getTintColor(state.getFluidState(), level, pos);
@@ -324,14 +320,14 @@ private static int convertViscosity(double viscosity) {
324320
return builder.register()::getSource;
325321
}
326322

327-
private void determineName(@NotNull Material material, @Nullable FluidStorageKey key) {
323+
private void determineName(Material material, @Nullable FluidStorageKey key) {
328324
if (name != null) return;
329325
if (material.isNull() || key == null) throw new IllegalArgumentException("Fluid must have a name");
330326
name = key.getRegistryNameFor(material);
331327
}
332328

333329
@ApiStatus.Internal
334-
public void determineTextures(@NotNull Material material, @NotNull FluidStorageKey key) {
330+
public void determineTextures(Material material, FluidStorageKey key) {
335331
if (hasCustomStill || material.isNull()) {
336332
still = ResourceLocation.fromNamespaceAndPath(material.getModid(), "block/fluids/fluid." + name);
337333
} else {
@@ -347,7 +343,7 @@ public void determineTextures(@NotNull Material material, @NotNull FluidStorageK
347343
}
348344
}
349345

350-
private void determineTemperature(@NotNull Material material) {
346+
private void determineTemperature(Material material) {
351347
if (temperature != INFER_TEMPERATURE) return;
352348
if (material.isNull()) {
353349
temperature = ROOM_TEMPERATURE;
@@ -380,7 +376,7 @@ private void determineTemperature(@NotNull Material material) {
380376
}
381377
}
382378

383-
private void determineColor(@NotNull Material material) {
379+
private void determineColor(Material material) {
384380
if (color != INFER_COLOR) return;
385381
if (isColorEnabled && !material.isNull()) {
386382
color = GTUtil.convertRGBtoARGB(material.getMaterialRGB());
@@ -396,7 +392,7 @@ private void determineDensity() {
396392
};
397393
}
398394

399-
private void determineLuminosity(@NotNull Material material) {
395+
private void determineLuminosity(Material material) {
400396
if (luminosity != INFER_LUMINOSITY) return;
401397
if (state == FluidState.PLASMA) {
402398
luminosity = 15;
@@ -414,7 +410,7 @@ private void determineLuminosity(@NotNull Material material) {
414410
}
415411
}
416412

417-
private void determineViscosity(@NotNull Material material) {
413+
private void determineViscosity(Material material) {
418414
if (viscosity != INFER_VISCOSITY) return;
419415
viscosity = switch (state) {
420416
case LIQUID -> {

src/main/java/com/gregtechceu/gtceu/api/material/material/Material.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
import com.google.common.base.Preconditions;
3838
import com.google.common.collect.ImmutableList;
39-
import dev.latvian.mods.rhino.util.HideFromJS;
4039
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
4140
import it.unimi.dsi.fastutil.ints.IntArrayList;
4241
import it.unimi.dsi.fastutil.ints.IntList;
@@ -1837,7 +1836,6 @@ public Builder addDefaultEnchant(ResourceKey<Enchantment> enchant, int level) {
18371836
*
18381837
* @return The finalized Material.
18391838
*/
1840-
@HideFromJS
18411839
public Material buildAndRegister() {
18421840
materialInfo.componentList = composition.isEmpty() && this.compositionSupplier != null ?
18431841
ImmutableList.copyOf(compositionSupplier.stream().map(MaterialStackWrapper::toMatStack)

src/main/java/com/gregtechceu/gtceu/api/registry/registrate/GTRegistrate.java

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,22 @@ public ResourceLocation makeResourceLocation(String path) {
7676
* @return The {@link GTRegistrate} instance
7777
*/
7878
public static GTRegistrate create(String modId) {
79-
return innerCreate(modId, true);
79+
return create(modId, true);
80+
}
81+
82+
/**
83+
* Get or create a new {@link GTRegistrate} and conditionally register event listeners.
84+
* A new {@code GTRegistrate} instance is only made if one doesn't already exist in the cache.
85+
* <br>
86+
* Note that if you do not allow event listeners to be registered automatically, you <strong>must</strong>
87+
* call {@link #registerEventListeners(IEventBus)} yourself with your {@link IEventBus mod event bus}.
88+
*
89+
* @param modId The mod ID for which objects will be registered
90+
* @param registerEvents Whether to register required event listeners.
91+
* @return The {@link GTRegistrate} instance
92+
*/
93+
public static GTRegistrate create(String modId, boolean registerEvents) {
94+
return innerCreate(modId, false, registerEvents);
8095
}
8196

8297
/**
@@ -90,53 +105,57 @@ public static GTRegistrate create(String modId) {
90105
*/
91106
@ApiStatus.Internal
92107
public static GTRegistrate createIgnoringListenerErrors(String modId) {
93-
return innerCreate(modId, false);
108+
return innerCreate(modId, true, false);
94109
}
95110

96-
private static GTRegistrate innerCreate(String modId, boolean strict) {
111+
private static GTRegistrate innerCreate(String modId, boolean registerEvents, boolean requireValidEventBus) {
97112
if (EXISTING_REGISTRATES.containsKey(modId)) {
98113
return EXISTING_REGISTRATES.get(modId);
99114
}
100115
var registrate = new GTRegistrate(modId);
101-
Optional<IEventBus> modEventBus = ModList.get().getModContainerById(modId).map(ModContainer::getEventBus);
102-
if (strict) {
103-
modEventBus.ifPresentOrElse(registrate::registerEventListeners, () -> {
104-
String message = "# [GTRegistrate] Failed to register eventListeners for mod " + modId +
105-
", This should be reported to this mod's dev #";
106-
String hashtags = "#".repeat(message.length());
107-
GTCEu.LOGGER.fatal(hashtags);
108-
GTCEu.LOGGER.fatal(message);
109-
GTCEu.LOGGER.fatal(hashtags);
110-
});
111-
} else {
112-
registrate.registerEventListeners(modEventBus.orElse(GTCEu.gtModBus));
116+
if (registerEvents) {
117+
Optional<IEventBus> modEventBus = ModList.get().getModContainerById(modId).map(ModContainer::getEventBus);
118+
if (requireValidEventBus) {
119+
modEventBus.ifPresentOrElse(registrate::registerEventListeners, () -> {
120+
String message = "# [GTRegistrate] Failed to register eventListeners for mod " + modId +
121+
", This should be reported to this mod's dev #";
122+
String hashtags = "#".repeat(message.length());
123+
GTCEu.LOGGER.fatal(hashtags);
124+
GTCEu.LOGGER.fatal(message);
125+
GTCEu.LOGGER.fatal(hashtags);
126+
});
127+
} else {
128+
registrate.registerEventListeners(modEventBus.orElse(GTCEu.gtModBus));
129+
}
113130
}
114131
EXISTING_REGISTRATES.put(modId, registrate);
115132
return registrate;
116133
}
117134

118135
@Override
119136
public GTRegistrate registerEventListeners(IEventBus bus) {
120-
if (!registered.getAndSet(true)) {
121-
if (((AbstractRegistrateAccessor) this).getModEventBus() == null) {
122-
((AbstractRegistrateAccessor) this).setModEventBus(bus);
123-
}
124-
// recreate the super method so we can register the event listener with LOW priority.
125-
Consumer<RegisterEvent> onRegister = this::onRegister;
126-
Consumer<RegisterEvent> onRegisterLate = this::onRegisterLate;
127-
bus.addListener(EventPriority.LOW, onRegister);
128-
bus.addListener(EventPriority.LOWEST, onRegisterLate);
129-
130-
// Fired multiple times when ever tabs need contents rebuilt (changing op tab perms for example)
131-
bus.addListener(this::onBuildCreativeModeTabContents);
132-
// Register events fire multiple times, so clean them up on common setup
133-
OneTimeEventReceiver.addModListener(this, FMLCommonSetupEvent.class, $ -> {
134-
OneTimeEventReceiver.unregister(this, onRegister, RegisterEvent.class);
135-
OneTimeEventReceiver.unregister(this, onRegisterLate, RegisterEvent.class);
136-
});
137-
if (((AbstractRegistrateAccessor) this).getDoDatagen().get()) {
138-
OneTimeEventReceiver.addModListener(this, GatherDataEvent.class, this::onData);
139-
}
137+
if (registered.getAndSet(true)) {
138+
// early exit if event listeners are already registered
139+
return this;
140+
}
141+
if (this.getModEventBus() == null) {
142+
this.setModEventBus(bus);
143+
}
144+
// recreate the super method so we can register the event listener with LOW priority.
145+
Consumer<RegisterEvent> onRegister = this::onRegister;
146+
Consumer<RegisterEvent> onRegisterLate = this::onRegisterLate;
147+
bus.addListener(EventPriority.LOW, onRegister);
148+
bus.addListener(EventPriority.LOWEST, onRegisterLate);
149+
150+
// Fired multiple times when ever tabs need contents rebuilt (changing op tab perms for example)
151+
bus.addListener(this::onBuildCreativeModeTabContents);
152+
// Register events fire multiple times, so clean them up on common setup
153+
OneTimeEventReceiver.addModListener(this, FMLCommonSetupEvent.class, $ -> {
154+
OneTimeEventReceiver.unregister(this, onRegister, RegisterEvent.class);
155+
OneTimeEventReceiver.unregister(this, onRegisterLate, RegisterEvent.class);
156+
});
157+
if (((AbstractRegistrateAccessor) this).getDoDatagen().get()) {
158+
OneTimeEventReceiver.addModListener(this, GatherDataEvent.class, this::onData);
140159
}
141160
return this;
142161
}
@@ -216,11 +235,10 @@ public <T extends Block, P> GTBlockBuilder<T, P> block(P parent, String name,
216235
callback -> GTBlockBuilder.create(this, parent, name, callback, factory));
217236
}
218237

219-
@Nullable
220-
private RegistryEntry<CreativeModeTab, ? extends CreativeModeTab> currentTab;
238+
private @Nullable RegistryEntry<CreativeModeTab, ? extends CreativeModeTab> currentTab;
221239
private static final Map<RegistryEntry<?, ?>, RegistryEntry<CreativeModeTab, ? extends CreativeModeTab>> TAB_LOOKUP = new IdentityHashMap<>();
222240

223-
public RegistryEntry<CreativeModeTab, ? extends CreativeModeTab> creativeModeTab() {
241+
public @Nullable RegistryEntry<CreativeModeTab, ? extends CreativeModeTab> creativeModeTab() {
224242
return this.currentTab;
225243
}
226244

0 commit comments

Comments
 (0)