Skip to content

Commit

Permalink
Add async constructor for CancellableEvent and add ClientLocalPlayerC…
Browse files Browse the repository at this point in the history
…hatEvent
  • Loading branch information
acrylic-style committed Apr 4, 2022
1 parent e38d5bb commit b66d23a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.blueberrymc.client.event.player;

import net.blueberrymc.common.Blueberry;
import net.blueberrymc.common.bml.event.CancellableEvent;
import org.jetbrains.annotations.NotNull;

/**
* Fired when the local player (the client) tries to send a chat message. If the event is cancelled, the chat message
* will not be sent. If the message was changed through {@link #setMessage(String)}, the new message will be sent.
*/
public class ClientLocalPlayerChatEvent extends CancellableEvent {
@NotNull
private String message;

public ClientLocalPlayerChatEvent(@NotNull String message) {
super(!Blueberry.getUtil().isOnGameThread());
this.message = message;
}

/**
* Gets the message that the player is trying to send.
* @return the message
*/
@NotNull
public String getMessage() {
return message;
}

/**
* Sets the message that the player is trying to send.
* @param message the message
*/
public void setMessage(@NotNull String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ public void loadMods() {
} catch (Throwable throwable) {
LOGGER.error("Error during preprocessing {} (loaded from: {})", file.getName(), file.getAbsolutePath(), throwable);
ModLoadingErrors.add(new ModLoadingError(new SimpleModInfo(file.getName(), file.getName()), "Error during preprocessing: " + throwable.getMessage(), false));
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logError("Error during preprocessing " + file.getName() + " (loaded from: " + file.getAbsolutePath() + ")"));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logError("Error during preprocessing " + file.getName() + " (loaded from: " + file.getAbsolutePath() + ")");
}
});
toLoad.remove(file);
}
});
Expand All @@ -136,21 +141,36 @@ public void loadMods() {
String message = "Required dependency \"" + depend + "\" is missing";
LOGGER.error(modId + ": " + message);
ModLoadingErrors.add(new ModLoadingError(entry.getKey(), new UnknownModDependencyException(message), false));
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logError(modId + ": " + message));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logError(modId + ": " + message);
}
});
continue;
}
if (!entry.getKey().getDepends().contains(dependDesc.getKey().getModId())) continue;
if (!descriptions.get(depend).getKey().getDepends().contains(modId)) continue;
circularDependency.add(modId);
toLoad.remove(entry.getValue());
ModLoadingErrors.add(new ModLoadingError(entry.getKey(), new InvalidModException("Circular dependency detected with " + depend), false));
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logError("Circular dependency detected with " + depend));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logError("Circular dependency detected with " + depend);
}
});
}
});
if (!circularDependency.isEmpty()) {
String deps = ListUtils.join(circularDependency, ", ");
LOGGER.error("Following mods has circular dependency, cannot load: {}", deps);
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logError("Following mods has circular dependency, cannot load: " + deps));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logError("Following mods has circular dependency, cannot load: " + deps);
}
});
}
toLoad.forEach(file -> {
try {
Expand All @@ -163,7 +183,12 @@ public void loadMods() {
ModDescriptionFile desc = filePath2descriptionMap.get(file.getAbsolutePath()).getKey();
LOGGER.error("Could not load a mod (" + desc.getModId() + "): " + ex);
ModLoadingErrors.add(new ModLoadingError(desc, "Could not load a mod: " + ex.getMessage(), false));
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logError("Could not load a mod (" + desc.getModId() + "): " + ex.getMessage()));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logError("Could not load a mod (" + desc.getModId() + "): " + ex.getMessage());
}
});
}
});
}
Expand Down Expand Up @@ -193,11 +218,21 @@ public Map.Entry<ModDescriptionFile, File> compileSource(@NotNull File file, @No
JavaTools.UNAVAILABLE_REASON.getMessage()
);
ModLoadingErrors.add(new ModLoadingError(description, "Live compiler is unavailable: " + JavaTools.UNAVAILABLE_REASON.getMessage(), true));
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logWarning("Live compiler is unavailable: " + JavaTools.UNAVAILABLE_REASON.getMessage()));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logWarning("Live compiler is unavailable: " + JavaTools.UNAVAILABLE_REASON.getMessage());
}
});
return new AbstractMap.SimpleImmutableEntry<>(description, null);
}
LOGGER.info("Compiling the source code of mod {} ({}) [{}]", description.getName(), description.getModId(), description.getVersion());
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logModLoader(String.format("Compiling the source code of mod %s (%s) [%s]", description.getName(), description.getModId(), description.getVersion())));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logModLoader(String.format("Compiling the source code of mod %s (%s) [%s]", description.getName(), description.getModId(), description.getVersion()));
}
});
File src = description.getSourceDir() != null ? new File(description.getSourceDir()) : file;
if (!src.exists() || !src.isDirectory()) {
src = new File(file, description.getSourceDir());
Expand All @@ -221,23 +256,38 @@ public Map.Entry<ModDescriptionFile, File> compileSource(@NotNull File file, @No
include = new File(file, description.getInclude());
if (!include.exists() || !src.isDirectory()) {
LOGGER.warn("Include dir does not exist or not a directory, skipping");
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logWarning("Include directory is missing or not a directory"));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logWarning("Include directory is missing or not a directory");
}
});
}
}
if (include.exists()) {
FileUtil.copy(include, compiled);
}
}
LOGGER.info("Successfully compiled the source code of mod {} ({})", description.getName(), description.getModId());
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logModCompiler("Successfully compiled the source of mod " + description.getName() + " (" + description.getModId() + ")"));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logModCompiler("Successfully compiled the source of mod " + description.getName() + " (" + description.getModId() + ")");
}
});
//filePath2descriptionMap.put(compiled.getAbsolutePath(), new AbstractMap.SimpleImmutableEntry<>(description, compiled));
//descriptions.put(description.getModId(), new AbstractMap.SimpleImmutableEntry<>(description, compiled));
descriptions.remove(description.getModId());
return new AbstractMap.SimpleImmutableEntry<>(description, compiled);
} catch (RuntimeException ex) {
LOGGER.info("Failed to compile the source code of mod {} ({})", description.getName(), description.getModId());
ModLoadingErrors.add(new ModLoadingError(description, "Failed to compile the source code", false));
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logError("Failed to compile the source code of mod " + description.getName() + " (" + description.getModId() + ")"));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logError("Failed to compile the source code of mod " + description.getName() + " (" + description.getModId() + ")");
}
});
}
}
return null;
Expand Down Expand Up @@ -319,7 +369,12 @@ public BlueberryMod loadMod(@NotNull File file, @Nullable File sourceDir) throws
}
try {
LOGGER.info("Loading mod {} ({}) version {}", description.getName(), description.getModId(), description.getVersion());
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logModLoader(String.format("Loading mod %s (%s) version %s", description.getName(), description.getModId(), description.getVersion())));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logModLoader(String.format("Loading mod %s (%s) version %s", description.getName(), description.getModId(), description.getVersion()));
}
});
ModClassLoader modClassLoader = new ModClassLoader(this, this.getClass().getClassLoader(), description, file);
loaders.add(modClassLoader);
BlueberryMod mod = modClassLoader.mod;
Expand All @@ -330,7 +385,12 @@ public BlueberryMod loadMod(@NotNull File file, @Nullable File sourceDir) throws
mod.sourceDir = sourceDir;
}
LOGGER.info("Loaded mod {} ({}) version {}", description.getName(), description.getModId(), description.getVersion());
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logModLoader(String.format("Loaded mod %s (%s) version %s", description.getName(), description.getModId(), description.getVersion())));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logModLoader(String.format("Loaded mod %s (%s) version %s", description.getName(), description.getModId(), description.getVersion()));
}
});
return mod;
} catch (IOException ex) {
throw new InvalidModException(ex);
Expand Down Expand Up @@ -525,6 +585,7 @@ public BlueberryMod forceRegisterMod(@NotNull ModDescriptionFile description, bo
return mod;
}

@SuppressWarnings("unchecked")
@Override
public void initModResources(@NotNull BlueberryMod mod) {
BlueberryResourceManager blueberryResourceManager = new BlueberryResourceManager(mod);
Expand All @@ -534,10 +595,12 @@ public void initModResources(@NotNull BlueberryMod mod) {
CloseableResourceManager crm = (CloseableResourceManager) ReflectionHelper.getFieldWithoutException(ReloadableResourceManager.class, rm, "resources");
if (crm instanceof MultiPackResourceManager) {
List<PackResources> packs = (List<PackResources>) ReflectionHelper.getFieldWithoutException(MultiPackResourceManager.class, crm, "packs");
assert packs != null;
packs.add(blueberryResourceManager.getPackResources());
}
} else if (resourceManager instanceof MultiPackResourceManager rm) {
List<PackResources> packs = (List<PackResources>) ReflectionHelper.getFieldWithoutException(MultiPackResourceManager.class, rm, "packs");
assert packs != null;
packs.add(blueberryResourceManager.getPackResources());
} else if (resourceManager instanceof FallbackResourceManager rm) {
rm.add(blueberryResourceManager.getPackResources());
Expand All @@ -549,7 +612,12 @@ public void initModResources(@NotNull BlueberryMod mod) {
@Override
public void callPreInit() {
LOGGER.info("Entered Pre-init phase");
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logModLoader("# Pre-init"));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logModLoader("# Pre-init");
}
});
getActiveMods().forEach(mod -> {
try {
mod.getStateList().add(ModState.PRE_INIT);
Expand All @@ -565,7 +633,12 @@ public void callPreInit() {
@Override
public void callInit() {
LOGGER.info("Entered Init phase");
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logModLoader("# Init"));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logModLoader("# Init");
}
});
getActiveMods().forEach(mod -> {
try {
mod.getStateList().add(ModState.INIT);
Expand All @@ -580,7 +653,12 @@ public void callInit() {
@Override
public void callPostInit() {
LOGGER.info("Entered Post-init phase");
Blueberry.runOnClient(() -> EarlyLoadingMessageManager.logModLoader("# Post-init"));
Blueberry.safeRunOnClient(() -> new VoidSafeExecutor() {
@Override
public void execute() {
EarlyLoadingMessageManager.logModLoader("# Post-init");
}
});
getActiveMods().forEach(mod -> {
if (mod.getStateList().contains(ModState.AVAILABLE)) return;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
public abstract class CancellableEvent extends Event implements Cancellable {
protected boolean cancelled = false;

protected CancellableEvent() {
super();
}

protected CancellableEvent(boolean async) {
super(async);
}

@Override
public boolean isCancelled() {
return cancelled;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: acrylic-style <[email protected]>
Date: Wed, 10 Nov 2021 14:38:53 +0900
Subject: [PATCH] Fire network events
Date: Mon, 4 Apr 2022 13:59:39 +0900
Subject: [PATCH] Fire events


diff --git a/src/main/java/net/minecraft/client/player/LocalPlayer.java b/src/main/java/net/minecraft/client/player/LocalPlayer.java
index 78e1e2a3e068a38a406619cf3d9a17ad677b2835..5244eb5acb4b793fa07e90a4f7f113ace1ae432c 100644
--- a/src/main/java/net/minecraft/client/player/LocalPlayer.java
+++ b/src/main/java/net/minecraft/client/player/LocalPlayer.java
@@ -269,7 +269,13 @@ public class LocalPlayer extends AbstractClientPlayer {
}

public void chat(String s) {
- this.connection.send(new ServerboundChatPacket(s));
+ // Blueberry start - fire ClientLocalPlayerChatEvent
+ var event = new net.blueberrymc.client.event.player.ClientLocalPlayerChatEvent(s);
+ if (!event.callEvent()) {
+ return;
+ }
+ this.connection.send(new ServerboundChatPacket(event.getMessage()));
+ // Blueberry end - fire ClientLocalPlayerChatEvent
}

public void swing(InteractionHand interactionHand) {
diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java
index dba3ba3bd696eb6707f9f5670f42e318561cc1df..f93f1dc7c545a164d3e500da57c1326cff9f01f2 100644
--- a/src/main/java/net/minecraft/network/Connection.java
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</modules>
<name>Blueberry-Parent</name>
<properties>
<api.version>1.0.0-SNAPSHOT</api.version>
<api.version>1.1.0-SNAPSHOT</api.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down

0 comments on commit b66d23a

Please sign in to comment.