Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn Environment creation event into a BiConsumer to also take userData #697

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import at.petrak.hexcasting.api.pigment.FrozenPigment;
import at.petrak.hexcasting.api.utils.HexUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -28,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;

Expand All @@ -43,21 +45,28 @@ public abstract class CastingEnvironment {
/**
* Stores all listeners that should be notified whenever a CastingEnvironment is initialised.
*/
private static final List<Consumer<CastingEnvironment>> createEventListeners = new ArrayList<>();
private static final List<BiConsumer<CastingEnvironment, CompoundTag>> createEventListeners = new ArrayList<>();

/**
* Add a listener that will be called whenever a new CastingEnvironment is created.
*/
public static void addCreateEventListener(Consumer<CastingEnvironment> listener) {
object-Object marked this conversation as resolved.
Show resolved Hide resolved
public static void addCreateEventListener(BiConsumer<CastingEnvironment, CompoundTag> listener) {
createEventListeners.add(listener);
}

/**
* Add a listener that will be called whenever a new CastingEnvironment is created (legacy).
*/
public static void addCreateEventListener(Consumer<CastingEnvironment> listener) {
walksanatora marked this conversation as resolved.
Show resolved Hide resolved
createEventListeners.add((env, data) -> {listener.accept(env);});
}

private boolean createEventTriggered = false;

public final void triggerCreateEvent() {
public final void triggerCreateEvent(CompoundTag userData) {
if (!createEventTriggered) {
for (var listener : createEventListeners)
listener.accept(this);
listener.accept(this, userData);
createEventTriggered = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import net.minecraft.server.level.ServerLevel
*/
class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
init {
env.triggerCreateEvent()
env.triggerCreateEvent(image.userData)
}

/**
Expand Down
Loading