Search in sources :

Example 1 with Tuple

use of org.spongepowered.api.util.Tuple in project SpongeCommon by SpongePowered.

the class SpongeGameRegistry method preRegistryInit.

public void preRegistryInit() {
    CommonModuleRegistry.getInstance().registerDefaultModules();
    final DirectedGraph<Class<? extends RegistryModule>> graph = new DirectedGraph<>();
    for (RegistryModule module : this.registryModules) {
        this.classMap.put(module.getClass(), module);
        addToGraph(module, graph);
    }
    // Now we need ot do the catalog ones
    for (CatalogRegistryModule<?> module : this.catalogRegistryMap.values()) {
        this.classMap.put(module.getClass(), module);
        addToGraph(module, graph);
    }
    try {
        this.orderedModules.addAll(TopologicalOrder.createOrderedLoad(graph));
    } catch (CyclicGraphException e) {
        StringBuilder msg = new StringBuilder();
        msg.append("Registry module dependencies are cyclical!\n");
        msg.append("Dependency loops are:\n");
        for (DataNode<?>[] cycle : e.getCycles()) {
            msg.append("[");
            for (DataNode<?> node : cycle) {
                msg.append(node.getData().toString()).append(" ");
            }
            msg.append("]\n");
        }
        SpongeImpl.getLogger().fatal(msg.toString());
        throw new RuntimeException("Registry modules dependencies error.");
    }
    registerModulePhase();
    SpongeVillagerRegistry.registerVanillaTrades();
    DataRegistrar.setupSerialization();
    final List<Tuple<Class<? extends CatalogType>, CatalogRegistryModule<?>>> modules = new ArrayList<>();
    for (Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> entry : this.catalogRegistryMap.entrySet()) {
        modules.add(new Tuple<>(entry.getKey(), entry.getValue()));
    }
    modules.sort(Comparator.comparing(tuple -> tuple.getFirst().getSimpleName()));
    if (PRINT_CATALOG_TYPES) {
        // Lol... this gets spammy really fast.... Probably at some point should be put to file.
        final PrettyPrinter printer = new PrettyPrinter(100).add("Printing all Catalogs and their ID's").centre().hr().addWrapped("This is a test to print out all registered catalogs during initialization for their mapping, id's, and objects themselves.");
        for (Tuple<Class<? extends CatalogType>, CatalogRegistryModule<?>> module : modules) {
            printer.add(" %s : %s", "CatalogType", module.getFirst().getSimpleName());
            final Collection<? extends CatalogType> all = module.getSecond().getAll();
            final List<CatalogType> catalogTypes = new ArrayList<>(all);
            catalogTypes.sort(Comparator.comparing(CatalogType::getId));
            for (CatalogType catalogType : catalogTypes) {
                printer.add("  -%s", catalogType.getId());
            }
            printer.hr();
        }
        printer.trace(System.err, SpongeImpl.getLogger(), Level.DEBUG);
    }
}
Also used : DataNode(org.spongepowered.common.util.graph.DirectedGraph.DataNode) ValueFactory(org.spongepowered.api.data.value.ValueFactory) BlockStatistic(org.spongepowered.api.statistic.BlockStatistic) TextSerializerFactory(org.spongepowered.api.text.serializer.TextSerializerFactory) Level(org.apache.logging.log4j.Level) FurnaceRecipes(net.minecraft.item.crafting.FurnaceRecipes) CyclicGraphException(org.spongepowered.common.util.graph.CyclicGraphException) BookView(org.spongepowered.api.text.BookView) ExtentBufferFactory(org.spongepowered.api.world.extent.ExtentBufferFactory) Block(net.minecraft.block.Block) TextColor(org.spongepowered.api.text.format.TextColor) RegistryModuleAlreadyRegisteredException(org.spongepowered.api.registry.RegistryModuleAlreadyRegisteredException) SpongeSelectorFactory(org.spongepowered.common.text.selector.SpongeSelectorFactory) Map(java.util.Map) Path(java.nio.file.Path) SpongeTextSerializerFactory(org.spongepowered.common.text.serializer.SpongeTextSerializerFactory) ItemStatistic(org.spongepowered.api.statistic.ItemStatistic) EntityList(net.minecraft.entity.EntityList) DisplaySlotRegistryModule(org.spongepowered.common.registry.type.scoreboard.DisplaySlotRegistryModule) AITaskTypeModule(org.spongepowered.common.registry.type.entity.AITaskTypeModule) RegistryModuleLoader(org.spongepowered.common.registry.util.RegistryModuleLoader) Set(java.util.Set) DisplaySlot(org.spongepowered.api.scoreboard.displayslot.DisplaySlot) DirectedGraph(org.spongepowered.common.util.graph.DirectedGraph) AITaskType(org.spongepowered.api.entity.ai.task.AITaskType) Favicon(org.spongepowered.api.network.status.Favicon) Singleton(com.google.inject.Singleton) DataRegistration(org.spongepowered.api.data.DataRegistration) Agent(org.spongepowered.api.entity.living.Agent) PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) Supplier(java.util.function.Supplier) SpongeDataManager(org.spongepowered.common.data.SpongeDataManager) ArrayList(java.util.ArrayList) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) Rotation(org.spongepowered.api.util.rotation.Rotation) Text(org.spongepowered.api.text.Text) SmeltingRecipeRegistry(org.spongepowered.api.item.recipe.smelting.SmeltingRecipeRegistry) TopologicalOrder(org.spongepowered.common.util.graph.TopologicalOrder) Nullable(javax.annotation.Nullable) StatList(net.minecraft.stats.StatList) CraftingRecipeRegistry(org.spongepowered.api.item.recipe.crafting.CraftingRecipeRegistry) IOException(java.io.IOException) Field(java.lang.reflect.Field) EntityStatistic(org.spongepowered.api.statistic.EntityStatistic) SpongeTranslation(org.spongepowered.common.text.translation.SpongeTranslation) AbstractAITask(org.spongepowered.api.entity.ai.task.AbstractAITask) ResettableBuilder(org.spongepowered.api.util.ResettableBuilder) TypeSerializers(ninja.leaping.configurate.objectmapping.serialize.TypeSerializers) ResourceLocation(net.minecraft.util.ResourceLocation) RegistryModule(org.spongepowered.api.registry.RegistryModule) PluginProvidedRegistryModule(org.spongepowered.api.registry.util.PluginProvidedRegistryModule) SpongePropertyRegistry(org.spongepowered.common.data.property.SpongePropertyRegistry) Item(net.minecraft.item.Item) ExtraClassCatalogRegistryModule(org.spongepowered.api.registry.ExtraClassCatalogRegistryModule) URL(java.net.URL) Inject(com.google.inject.Inject) DefaultGameRules(org.spongepowered.api.world.gamerule.DefaultGameRules) LocaleCache(org.spongepowered.common.util.LocaleCache) GameRegistry(org.spongepowered.api.GameRegistry) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) RotationRegistryModule(org.spongepowered.common.registry.type.block.RotationRegistryModule) Locale(java.util.Locale) RegistrationDependency(org.spongepowered.api.registry.util.RegistrationDependency) StatisticTypes(org.spongepowered.api.statistic.StatisticTypes) BookViewDataBuilder(org.spongepowered.api.text.serializer.BookViewDataBuilder) IdentityHashMap(java.util.IdentityHashMap) BufferedImage(java.awt.image.BufferedImage) Collection(java.util.Collection) RegistrationPhase(org.spongepowered.api.registry.RegistrationPhase) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) Tuple(org.spongepowered.api.util.Tuple) SpongeFavicon(org.spongepowered.common.network.status.SpongeFavicon) SetSerializer(org.spongepowered.common.util.SetSerializer) SpongeGameRegistryRegisterEvent(org.spongepowered.common.event.registry.SpongeGameRegistryRegisterEvent) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) BlockType(org.spongepowered.api.block.BlockType) Optional(java.util.Optional) EventContext(org.spongepowered.api.event.cause.EventContext) SpongeImpl(org.spongepowered.common.SpongeImpl) CatalogTypeTypeSerializer(org.spongepowered.common.config.CatalogTypeTypeSerializer) TextTemplate(org.spongepowered.api.text.TextTemplate) SpongeCraftingRecipeRegistry(org.spongepowered.common.item.recipe.crafting.SpongeCraftingRecipeRegistry) TypeToken(com.google.common.reflect.TypeToken) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) TextConfigSerializer(org.spongepowered.api.text.serializer.TextConfigSerializer) VillagerRegistry(org.spongepowered.api.item.merchant.VillagerRegistry) DataRegistrar(org.spongepowered.common.data.DataRegistrar) Translation(org.spongepowered.api.text.translation.Translation) TextTemplateConfigSerializer(org.spongepowered.api.text.serializer.TextTemplateConfigSerializer) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SpongeExtentBufferFactory(org.spongepowered.common.world.extent.SpongeExtentBufferFactory) StatisticType(org.spongepowered.api.statistic.StatisticType) SpongeValueFactory(org.spongepowered.common.data.value.SpongeValueFactory) EntityType(org.spongepowered.api.entity.EntityType) Comparator(java.util.Comparator) ItemType(org.spongepowered.api.item.ItemType) Collections(java.util.Collections) InputStream(java.io.InputStream) DisplaySlotRegistryModule(org.spongepowered.common.registry.type.scoreboard.DisplaySlotRegistryModule) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) RegistryModule(org.spongepowered.api.registry.RegistryModule) PluginProvidedRegistryModule(org.spongepowered.api.registry.util.PluginProvidedRegistryModule) ExtraClassCatalogRegistryModule(org.spongepowered.api.registry.ExtraClassCatalogRegistryModule) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) RotationRegistryModule(org.spongepowered.common.registry.type.block.RotationRegistryModule) ArrayList(java.util.ArrayList) CyclicGraphException(org.spongepowered.common.util.graph.CyclicGraphException) CatalogType(org.spongepowered.api.CatalogType) PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) ExtraClassCatalogRegistryModule(org.spongepowered.api.registry.ExtraClassCatalogRegistryModule) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) DirectedGraph(org.spongepowered.common.util.graph.DirectedGraph) DataNode(org.spongepowered.common.util.graph.DirectedGraph.DataNode) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) Tuple(org.spongepowered.api.util.Tuple)

Example 2 with Tuple

use of org.spongepowered.api.util.Tuple in project SpongeCommon by SpongePowered.

the class MixinChunk method getIntersectingEntities.

private void getIntersectingEntities(Collection<Entity> entities, Vector3d start, Vector3d direction, double distance, java.util.function.Predicate<EntityHit> filter, Set<EntityHit> intersections) {
    // Check each entity in the list
    for (Entity entity : entities) {
        final org.spongepowered.api.entity.Entity spongeEntity = (org.spongepowered.api.entity.Entity) entity;
        final Optional<AABB> box = spongeEntity.getBoundingBox();
        // Can't intersect if the entity doesn't have a bounding box
        if (!box.isPresent()) {
            continue;
        }
        // Ignore entities that didn't intersect
        final Optional<Tuple<Vector3d, Vector3d>> optionalIntersection = box.get().intersects(start, direction);
        if (!optionalIntersection.isPresent()) {
            continue;
        }
        // Check that the entity isn't too far away
        final Tuple<Vector3d, Vector3d> intersection = optionalIntersection.get();
        final double distanceSquared = intersection.getFirst().sub(start).lengthSquared();
        if (distanceSquared > distance * distance) {
            continue;
        }
        // Now test the filter on the entity and intersection
        final EntityHit hit = new EntityHit(spongeEntity, intersection.getFirst(), intersection.getSecond(), Math.sqrt(distanceSquared));
        if (!filter.test(hit)) {
            continue;
        }
        // If everything passes we have an intersection!
        intersections.add(hit);
        // If the entity has part, recurse on these
        final Entity[] parts = entity.getParts();
        if (parts != null && parts.length > 0) {
            getIntersectingEntities(Arrays.asList(parts), start, direction, distance, filter, intersections);
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) Entity(net.minecraft.entity.Entity) Vector3d(com.flowpowered.math.vector.Vector3d) AABB(org.spongepowered.api.util.AABB) Tuple(org.spongepowered.api.util.Tuple)

Example 3 with Tuple

use of org.spongepowered.api.util.Tuple in project LanternServer by LanternPowered.

the class FilterGenerator method generateClass.

byte[] generateClass(String name, Method method) {
    name = name.replace('.', '/');
    final Parameter[] params = method.getParameters();
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, "java/lang/Object", new String[] { Type.getInternalName(EventFilter.class) });
    SubtypeFilterDelegate sfilter = null;
    final List<FilterDelegate> additional = new ArrayList<>();
    boolean cancellation = false;
    for (Annotation anno : method.getAnnotations()) {
        Object obj = filterFromAnnotation(anno.annotationType());
        if (obj == null) {
            continue;
        }
        if (obj instanceof SubtypeFilter) {
            if (sfilter != null) {
                throw new IllegalStateException("Cannot have both @Include and @Exclude annotations present at once");
            }
            sfilter = ((SubtypeFilter) obj).getDelegate(anno);
        } else if (obj instanceof EventTypeFilter) {
            final EventTypeFilter etf = (EventTypeFilter) obj;
            additional.add(etf.getDelegate(anno));
            if (etf == EventTypeFilter.CANCELLATION) {
                cancellation = true;
            }
        }
    }
    if (!cancellation && Cancellable.class.isAssignableFrom(method.getParameterTypes()[0])) {
        additional.add(new CancellationEventFilterDelegate(Tristate.FALSE));
    }
    if (sfilter != null) {
        sfilter.createFields(cw);
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        if (sfilter != null) {
            sfilter.writeCtor(name, cw, mv);
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "filter", "(" + Type.getDescriptor(Event.class) + ")[Ljava/lang/Object;", null, null);
        mv.visitCode();
        // index of the next available local variable
        int local = 2;
        if (sfilter != null) {
            local = sfilter.write(name, cw, mv, method, local);
        }
        for (FilterDelegate eventFilter : additional) {
            local = eventFilter.write(name, cw, mv, method, local);
        }
        // local var indices of the parameters values
        final int[] plocals = new int[params.length - 1];
        for (int i = 1; i < params.length; i++) {
            final Parameter param = params[i];
            ParameterFilterSourceDelegate source = null;
            final List<ParameterFilterDelegate> paramFilters = new ArrayList<>();
            for (Annotation anno : param.getAnnotations()) {
                Object obj = filterFromAnnotation(anno.annotationType());
                if (obj == null) {
                    continue;
                }
                if (obj instanceof ParameterSource) {
                    if (source != null) {
                        throw new IllegalStateException("Cannot have multiple parameter filter source annotations (for " + param.getName() + ")");
                    }
                    source = ((ParameterSource) obj).getDelegate(anno);
                } else if (obj instanceof ParameterFilter) {
                    paramFilters.add(((ParameterFilter) obj).getDelegate(anno));
                }
            }
            if (source == null) {
                throw new IllegalStateException("Cannot have additional parameters filters without a source (for " + param.getName() + ")");
            }
            if (source instanceof AllCauseFilterSourceDelegate && !paramFilters.isEmpty()) {
                // TODO until better handling for filtering arrays is added
                throw new IllegalStateException("Cannot have additional parameters filters without an array source (for " + param.getName() + ")");
            }
            final Tuple<Integer, Integer> localState = source.write(cw, mv, method, param, local);
            local = localState.getFirst();
            plocals[i - 1] = localState.getSecond();
            for (ParameterFilterDelegate paramFilter : paramFilters) {
                paramFilter.write(cw, mv, method, param, plocals[i - 1]);
            }
        }
        // create the return array
        if (params.length == 1) {
            mv.visitInsn(ICONST_1);
        } else {
            mv.visitIntInsn(BIPUSH, params.length);
        }
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
        // load the event into the array
        mv.visitInsn(DUP);
        mv.visitInsn(ICONST_0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(AASTORE);
        // load all the params into the array
        for (int i = 1; i < params.length; i++) {
            mv.visitInsn(DUP);
            mv.visitIntInsn(BIPUSH, i);
            Type paramType = Type.getType(params[i].getType());
            mv.visitVarInsn(paramType.getOpcode(ILOAD), plocals[i - 1]);
            GeneratorUtils.visitBoxingMethod(mv, paramType);
            mv.visitInsn(AASTORE);
        }
        mv.visitInsn(ARETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    final byte[] data = cw.toByteArray();
    if (FILTER_DEBUG) {
        final Path outDir = Paths.get(".sponge.debug.out");
        final Path outFile = outDir.resolve(name + ".class");
        if (!Files.exists(outFile.getParent())) {
            try {
                Files.createDirectories(outFile.getParent());
            } catch (IOException e) {
                Lantern.getLogger().warn("Unable to create the filter debug directory.", e);
            }
        }
        try (final OutputStream out = Files.newOutputStream(outFile)) {
            out.write(data);
        } catch (IOException e) {
            Lantern.getLogger().warn("Unable to create the filter debug class.", e);
        }
    }
    return data;
}
Also used : CancellationEventFilterDelegate(org.lanternpowered.server.event.filter.delegate.CancellationEventFilterDelegate) Cancellable(org.spongepowered.api.event.Cancellable) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ParameterFilterDelegate(org.lanternpowered.server.event.filter.delegate.ParameterFilterDelegate) MethodVisitor(org.objectweb.asm.MethodVisitor) List(java.util.List) ArrayList(java.util.ArrayList) Path(java.nio.file.Path) FilterDelegate(org.lanternpowered.server.event.filter.delegate.FilterDelegate) IncludeSubtypeFilterDelegate(org.lanternpowered.server.event.filter.delegate.IncludeSubtypeFilterDelegate) CancellationEventFilterDelegate(org.lanternpowered.server.event.filter.delegate.CancellationEventFilterDelegate) SubtypeFilterDelegate(org.lanternpowered.server.event.filter.delegate.SubtypeFilterDelegate) SupportsDataFilterDelegate(org.lanternpowered.server.event.filter.delegate.SupportsDataFilterDelegate) ParameterFilterDelegate(org.lanternpowered.server.event.filter.delegate.ParameterFilterDelegate) HasDataFilterDelegate(org.lanternpowered.server.event.filter.delegate.HasDataFilterDelegate) ExcludeSubtypeFilterDelegate(org.lanternpowered.server.event.filter.delegate.ExcludeSubtypeFilterDelegate) ParameterFilterSourceDelegate(org.lanternpowered.server.event.filter.delegate.ParameterFilterSourceDelegate) AllCauseFilterSourceDelegate(org.lanternpowered.server.event.filter.delegate.AllCauseFilterSourceDelegate) IOException(java.io.IOException) ClassWriter(org.objectweb.asm.ClassWriter) Annotation(java.lang.annotation.Annotation) IncludeSubtypeFilterDelegate(org.lanternpowered.server.event.filter.delegate.IncludeSubtypeFilterDelegate) SubtypeFilterDelegate(org.lanternpowered.server.event.filter.delegate.SubtypeFilterDelegate) ExcludeSubtypeFilterDelegate(org.lanternpowered.server.event.filter.delegate.ExcludeSubtypeFilterDelegate) Type(org.objectweb.asm.Type) Parameter(java.lang.reflect.Parameter) Event(org.spongepowered.api.event.Event) Tuple(org.spongepowered.api.util.Tuple)

Example 4 with Tuple

use of org.spongepowered.api.util.Tuple in project LanternServer by LanternPowered.

the class GetterFilterSourceDelegate method write.

@Override
public Tuple<Integer, Integer> write(ClassWriter cw, MethodVisitor mv, Method method, Parameter param, int local) {
    Class<?> targetType = param.getType();
    Class<?> eventClass = method.getParameterTypes()[0];
    String targetMethod = this.anno.value();
    Method targetMethodObj;
    try {
        targetMethodObj = eventClass.getMethod(targetMethod);
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException(String.format("Method %s specified by getter annotation was not found in type %s", targetMethod, eventClass.getName()));
    }
    if (targetMethodObj.getParameterCount() != 0) {
        throw new IllegalArgumentException("Method " + targetMethodObj.toGenericString() + " specified by getter annotation has non-zero parameter count");
    }
    if (!targetMethodObj.getReturnType().equals(Optional.class) && !targetMethodObj.getReturnType().isAssignableFrom(targetType)) {
        throw new IllegalArgumentException("Method " + targetMethodObj.toGenericString() + " does not return the correct type. Expected: " + targetType.getName() + " Found: " + targetMethodObj.getReturnType().getName());
    }
    Type returnType = Type.getReturnType(targetMethodObj);
    Class<?> declaringClass = targetMethodObj.getDeclaringClass();
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(declaringClass));
    int op = declaringClass.isInterface() ? INVOKEINTERFACE : INVOKEVIRTUAL;
    mv.visitMethodInsn(op, Type.getInternalName(declaringClass), targetMethod, "()" + returnType.getDescriptor(), declaringClass.isInterface());
    int paramLocal = local++;
    mv.visitVarInsn(returnType.getOpcode(ISTORE), paramLocal);
    if (!targetMethodObj.getReturnType().isPrimitive()) {
        Label failure = new Label();
        Label success = new Label();
        if (Optional.class.equals(targetMethodObj.getReturnType()) && !Optional.class.equals(targetType)) {
            // Unwrap the optional
            mv.visitVarInsn(ALOAD, paramLocal);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Optional", "isPresent", "()Z", false);
            mv.visitJumpInsn(IFEQ, failure);
            mv.visitVarInsn(ALOAD, paramLocal);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Optional", "get", "()Ljava/lang/Object;", false);
            mv.visitInsn(DUP);
            mv.visitVarInsn(ASTORE, paramLocal);
        } else {
            mv.visitVarInsn(returnType.getOpcode(ILOAD), paramLocal);
        }
        mv.visitTypeInsn(INSTANCEOF, Type.getInternalName(targetType));
        mv.visitJumpInsn(IFNE, success);
        mv.visitLabel(failure);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitLabel(success);
    }
    return new Tuple<>(local, paramLocal);
}
Also used : Type(org.objectweb.asm.Type) Optional(java.util.Optional) Label(org.objectweb.asm.Label) Method(java.lang.reflect.Method) Tuple(org.spongepowered.api.util.Tuple)

Example 5 with Tuple

use of org.spongepowered.api.util.Tuple in project LanternServer by LanternPowered.

the class LanternEntity method damage.

@Override
public boolean damage(double damage, DamageSource damageSource) {
    checkNotNull(damageSource, "damageSource");
    final Optional<Double> optHealth = get(Keys.HEALTH);
    if (!optHealth.isPresent()) {
        // entities without health, instantly destroying them.
        if (damageSource.getType() == DamageTypes.VOID) {
            remove(RemoveState.DESTROYED);
            return true;
        }
        return false;
    }
    // Always throw the event. Plugins may want to override
    // default checking behavior.
    boolean cancelled = false;
    // if the player is in creative mode
    if (!damageSource.doesAffectCreative() && get(Keys.GAME_MODE).orElse(null) == GameModes.CREATIVE) {
        cancelled = true;
    }
    final List<Tuple<DamageFunction, Consumer<DamageEntityEvent>>> damageFunctions = new ArrayList<>();
    // Only collect damage modifiers if the event isn't cancelled
    if (!cancelled) {
        collectDamageFunctions(damageFunctions);
    }
    // TODO: Damage modifiers, etc.
    final CauseStack causeStack = CauseStack.current();
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        frame.pushCause(damageSource);
        frame.addContext(EventContextKeys.DAMAGE_TYPE, damageSource.getType());
        final DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(frame.getCurrentCause(), damageFunctions.stream().map(Tuple::getFirst).collect(Collectors.toList()), this, damage);
        event.setCancelled(cancelled);
        Sponge.getEventManager().post(event);
        if (event.isCancelled()) {
            return false;
        }
        damageFunctions.forEach(tuple -> tuple.getSecond().accept(event));
        damage = event.getFinalDamage();
        if (damage > 0) {
            offer(Keys.HEALTH, Math.max(optHealth.get() - damage, 0));
        }
        final double exhaustion = damageSource.getExhaustion();
        getValue(Keys.EXHAUSTION).ifPresent(value -> offer(Keys.EXHAUSTION, Math.min(value.getMaxValue(), value.get() + exhaustion)));
    }
    triggerEvent(DamagedEntityEvent.of());
    return true;
}
Also used : DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) CauseStack(org.lanternpowered.server.event.CauseStack) ArrayList(java.util.ArrayList) Tuple(org.spongepowered.api.util.Tuple)

Aggregations

Tuple (org.spongepowered.api.util.Tuple)23 List (java.util.List)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 Optional (java.util.Optional)7 Type (org.objectweb.asm.Type)7 IOException (java.io.IOException)6 Collectors (java.util.stream.Collectors)6 Text (org.spongepowered.api.text.Text)5 Vector3d (com.flowpowered.math.vector.Vector3d)3 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 Label (org.objectweb.asm.Label)3 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 TypeToken (com.google.common.reflect.TypeToken)2 Util (io.github.nucleuspowered.nucleus.Util)2 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)2 File (java.io.File)2