Search in sources :

Example 1 with PrefilterNonMatchException

use of com.laytonsmith.core.exceptions.PrefilterNonMatchException in project CommandHelper by EngineHub.

the class EventUtils method GetMatchingEvents.

/**
 * Returns a set of events that should be triggered by this event.
 *
 * @param type
 * @param eventName
 * @param e
 * @return
 */
public static SortedSet<BoundEvent> GetMatchingEvents(Driver type, String eventName, BindableEvent e, Event driver) {
    SortedSet<BoundEvent> toRun = new TreeSet<>();
    // This is the set of bounded events of this driver type.
    // We must now look through the bound events to see if they are
    // the eventName, and if so, we will also run the prefilter.
    SortedSet<BoundEvent> bounded = GetEvents(type);
    if (bounded != null) {
        // Wrap this in a new set, so we can safely iterate it with async threads
        bounded = new TreeSet<>(bounded);
        for (BoundEvent b : bounded) {
            try {
                boolean matches = false;
                try {
                    matches = driver.matches(b.getPrefilter(), e);
                } catch (ConfigRuntimeException ex) {
                    // This can happen in limited cases, but still needs to be
                    // handled properly. This would happen if, for instance, a
                    // prefilter was configured improperly with bad runtime data.
                    // We use the environment from the bound event.
                    ConfigRuntimeException.HandleUncaughtException(ex, b.getEnvironment());
                } catch (NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError err) {
                    // This happens when a CH extension depends on a not-included or binary outdated class.
                    // Log the error and continue since there's nothing we can do about it.
                    String chBrand = Implementation.GetServerType().getBranding();
                    String chVersion = Static.getVersion().toString();
                    String culprit = chBrand;
                    outerLoop: for (ExtensionTracker tracker : ExtensionManager.getTrackers().values()) {
                        for (Event event : tracker.getEvents()) {
                            if (event.getName().equals(driver.getName())) {
                                for (Extension extension : tracker.getExtensions()) {
                                    culprit = extension.getName();
                                    break outerLoop;
                                }
                            }
                        }
                    }
                    String modVersion;
                    try {
                        modVersion = StaticLayer.GetConvertor().GetServer().getAPIVersion();
                    } catch (Exception ex) {
                        modVersion = Implementation.GetServerType().name();
                    }
                    String extensionData = "";
                    for (ExtensionTracker tracker : ExtensionManager.getTrackers().values()) {
                        for (Extension extension : tracker.getExtensions()) {
                            try {
                                extensionData += TermColors.CYAN + extension.getName() + TermColors.RED + " (" + TermColors.RESET + extension.getVersion() + TermColors.RED + ")\n";
                            } catch (AbstractMethodError ex) {
                                // This happens with an old style extensions. Just skip it.
                                extensionData += TermColors.CYAN + "Unknown Extension" + TermColors.RED + "\n";
                            }
                        }
                    }
                    if (extensionData.isEmpty()) {
                        extensionData = "NONE\n";
                    }
                    String driverEventName = driver.getName();
                    String jarName = new File(driver.getSourceJar().getFile()).getName();
                    String emsg = TermColors.RED + "Uh oh! You've found an error in the eventhandler for event " + TermColors.CYAN + driverEventName + TermColors.RED + ", implemented in " + TermColors.CYAN + culprit + " (" + jarName + ")" + TermColors.RED + ".\n" + "Please report this to the developers, and be sure to include the version numbers:\n" + TermColors.CYAN + "Server" + TermColors.RED + " version: " + TermColors.RESET + modVersion + TermColors.RED + ";\n" + TermColors.CYAN + chBrand + TermColors.RED + " version: " + TermColors.RESET + chVersion + TermColors.RED + ";\n" + "Loaded extensions and versions:\n" + extensionData + "Here's the stacktrace:\n" + TermColors.RESET + Static.GetStacktraceString(err);
                    Static.getLogger().log(Level.SEVERE, emsg);
                    // If we can't match it, it's not a match.
                    continue;
                }
                if (b.getEventName().equals(eventName) && matches) {
                    toRun.add(b);
                }
            } catch (PrefilterNonMatchException ex) {
            // Not running this one
            }
        }
    }
    return toRun;
}
Also used : PrefilterNonMatchException(com.laytonsmith.core.exceptions.PrefilterNonMatchException) ExtensionTracker(com.laytonsmith.core.extensions.ExtensionTracker) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString) EventException(com.laytonsmith.core.exceptions.EventException) CREBindException(com.laytonsmith.core.exceptions.CRE.CREBindException) CREEventException(com.laytonsmith.core.exceptions.CRE.CREEventException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) PrefilterNonMatchException(com.laytonsmith.core.exceptions.PrefilterNonMatchException) Extension(com.laytonsmith.core.extensions.Extension) TreeSet(java.util.TreeSet) File(java.io.File)

Example 2 with PrefilterNonMatchException

use of com.laytonsmith.core.exceptions.PrefilterNonMatchException in project CommandHelper by EngineHub.

the class Prefilters method MathMatch.

private static void MathMatch(Construct one, Construct two) throws PrefilterNonMatchException {
    try {
        double dOne = Static.getNumber(one, Target.UNKNOWN);
        double dTwo = Static.getNumber(two, Target.UNKNOWN);
        if (dOne != dTwo) {
            throw new PrefilterNonMatchException();
        }
    } catch (ConfigRuntimeException e) {
        throw new PrefilterNonMatchException();
    }
}
Also used : PrefilterNonMatchException(com.laytonsmith.core.exceptions.PrefilterNonMatchException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException)

Example 3 with PrefilterNonMatchException

use of com.laytonsmith.core.exceptions.PrefilterNonMatchException in project CommandHelper by EngineHub.

the class Prefilters method ExpressionMatch.

private static void ExpressionMatch(Construct expression, String key, Construct dvalue) throws PrefilterNonMatchException {
    String exp = expression.val().substring(1, expression.val().length() - 1);
    boolean inequalityMode = false;
    if (exp.contains("<") || exp.contains(">") || exp.contains("==")) {
        inequalityMode = true;
    }
    String eClass = "com.sk89q.worldedit.internal.expression.Expression";
    String errClass = "com.sk89q.worldedit.internal.expression.ExpressionException";
    Class eClazz, errClazz;
    try {
        eClazz = Class.forName(eClass);
        errClazz = Class.forName(errClass);
    } catch (ClassNotFoundException cnf) {
        throw new CREPluginInternalException("You are missing a required dependency: " + eClass, expression.getTarget(), cnf);
    }
    try {
        Object e = ReflectionUtils.invokeMethod(eClazz, null, "compile", new Class[] { String.class, String[].class }, new Object[] { exp, new String[] { key } });
        double val = (double) ReflectionUtils.invokeMethod(eClazz, e, "evaluate", new Class[] { double[].class }, new Object[] { new double[] { Static.getDouble(dvalue, Target.UNKNOWN) } });
        if (inequalityMode) {
            if (val == 0) {
                throw new PrefilterNonMatchException();
            }
        } else {
            if (val != Static.getDouble(dvalue, Target.UNKNOWN)) {
                throw new PrefilterNonMatchException();
            }
        }
    } catch (ReflectionUtils.ReflectionException rex) {
        if (rex.getCause().getClass().isAssignableFrom(errClazz)) {
            throw new CREPluginInternalException("Your expression was invalidly formatted", expression.getTarget(), rex.getCause());
        } else {
            throw new CREPluginInternalException(rex.getMessage(), expression.getTarget(), rex.getCause());
        }
    }
}
Also used : PrefilterNonMatchException(com.laytonsmith.core.exceptions.PrefilterNonMatchException) CREPluginInternalException(com.laytonsmith.core.exceptions.CRE.CREPluginInternalException) CString(com.laytonsmith.core.constructs.CString) ReflectionUtils(com.laytonsmith.PureUtilities.Common.ReflectionUtils)

Example 4 with PrefilterNonMatchException

use of com.laytonsmith.core.exceptions.PrefilterNonMatchException in project CommandHelper by EngineHub.

the class Prefilters method LocationMatch.

private static void LocationMatch(Construct location1, Construct location2) throws PrefilterNonMatchException {
    MCLocation l1 = ObjectGenerator.GetGenerator().location(location1, null, location1.getTarget());
    MCLocation l2 = ObjectGenerator.GetGenerator().location(location2, null, Target.UNKNOWN);
    if ((!l1.getWorld().equals(l2.getWorld())) || (l1.getBlockX() != l2.getBlockX()) || (l1.getBlockY() != l2.getBlockY()) || (l1.getBlockZ() != l2.getBlockZ())) {
        throw new PrefilterNonMatchException();
    }
}
Also used : MCLocation(com.laytonsmith.abstraction.MCLocation) PrefilterNonMatchException(com.laytonsmith.core.exceptions.PrefilterNonMatchException)

Example 5 with PrefilterNonMatchException

use of com.laytonsmith.core.exceptions.PrefilterNonMatchException in project CommandHelper by EngineHub.

the class Prefilters method ItemMatch.

private static void ItemMatch(Construct item1, Construct item2) throws PrefilterNonMatchException {
    String i1 = item1.val().split(":")[0];
    String i2 = item2.val().split(":")[0];
    if (!i1.trim().equals(i2)) {
        throw new PrefilterNonMatchException();
    }
}
Also used : PrefilterNonMatchException(com.laytonsmith.core.exceptions.PrefilterNonMatchException) CString(com.laytonsmith.core.constructs.CString)

Aggregations

PrefilterNonMatchException (com.laytonsmith.core.exceptions.PrefilterNonMatchException)5 CString (com.laytonsmith.core.constructs.CString)3 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)2 ReflectionUtils (com.laytonsmith.PureUtilities.Common.ReflectionUtils)1 MCLocation (com.laytonsmith.abstraction.MCLocation)1 CREBindException (com.laytonsmith.core.exceptions.CRE.CREBindException)1 CREEventException (com.laytonsmith.core.exceptions.CRE.CREEventException)1 CREPluginInternalException (com.laytonsmith.core.exceptions.CRE.CREPluginInternalException)1 EventException (com.laytonsmith.core.exceptions.EventException)1 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)1 Extension (com.laytonsmith.core.extensions.Extension)1 ExtensionTracker (com.laytonsmith.core.extensions.ExtensionTracker)1 File (java.io.File)1 TreeSet (java.util.TreeSet)1