Merge pull request #229 from Project-Cepi/better-catching

Better catching for events/commands
This commit is contained in:
TheMode 2021-05-15 22:51:20 +02:00 committed by GitHub
commit d31fe2d88e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 16 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.server.command.builder;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.condition.CommandCondition;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
@ -54,11 +55,19 @@ public class ParsedCommand {
final CommandCondition commandCondition = syntax.getCommandCondition();
if (commandCondition == null || commandCondition.canUse(source, commandString)) {
context.retrieveDefaultValues(syntax.getDefaultValuesMap());
executor.apply(source, context);
try {
executor.apply(source, context);
} catch (Exception exception) {
MinecraftServer.getExceptionManager().handleException(exception);
}
}
} else {
// The executor is probably the default one
executor.apply(source, context);
try {
executor.apply(source, context);
} catch (Exception exception) {
MinecraftServer.getExceptionManager().handleException(exception);
}
}
} else if (callback != null && argumentSyntaxException != null) {
// No syntax has been validated but the faulty argument with a callback has been found

View File

@ -108,22 +108,27 @@ public interface EventHandler extends IExtensionObserver {
*/
default <E extends Event> void callEvent(@NotNull Class<E> eventClass, @NotNull E event) {
// Global listeners
if (!(this instanceof GlobalEventHandler)) {
final GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
runEvent(globalEventHandler.getEventCallbacks(eventClass), event);
}
try {
// Local listeners
final Collection<EventCallback> eventCallbacks = getEventCallbacks(eventClass);
runEvent(eventCallbacks, event);
// Call the same event for the current entity instance
if (this instanceof Entity) {
final Instance instance = ((Entity) this).getInstance();
if (instance != null) {
runEvent(instance.getEventCallbacks(eventClass), event);
// Global listeners
if (!(this instanceof GlobalEventHandler)) {
final GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
runEvent(globalEventHandler.getEventCallbacks(eventClass), event);
}
// Local listeners
final Collection<EventCallback> eventCallbacks = getEventCallbacks(eventClass);
runEvent(eventCallbacks, event);
// Call the same event for the current entity instance
if (this instanceof Entity) {
final Instance instance = ((Entity) this).getInstance();
if (instance != null) {
runEvent(instance.getEventCallbacks(eventClass), event);
}
}
} catch (Exception exception) {
MinecraftServer.getExceptionManager().handleException(exception);
}
}