Search in sources :

Example 6 with DataHolder

use of org.spongepowered.api.data.DataHolder in project LanternServer by LanternPowered.

the class CompositeValueStoreHelper method processDataTransactionResult.

protected static DataTransactionResult processDataTransactionResult(ICompositeValueStore store, DataTransactionResult result, BooleanSupplier hasListeners) {
    if (!(store instanceof DataHolder) || !result.isSuccessful() || !hasListeners.getAsBoolean()) {
        return result;
    }
    final Cause cause = CauseStack.currentOrEmpty().getCurrentCause();
    final ChangeDataHolderEvent.ValueChange event = SpongeEventFactory.createChangeDataHolderEventValueChange(cause, result, (DataHolder) store);
    Lantern.getGame().getEventManager().post(event);
    // Nothing is allowed to change, revert everything fast
    if (event.isCancelled()) {
        store.undoFastNoEvents(result);
        return DataTransactionResult.failNoData();
    }
    final DataTransactionResult original = result;
    result = event.getEndResult();
    // Check if something actually changed
    if (result != original) {
        final Map<Key<?>, ImmutableValue<?>> success = new HashMap<>();
        for (ImmutableValue<?> value : original.getSuccessfulData()) {
            success.put(value.getKey(), value);
        }
        for (ImmutableValue<?> value : result.getSuccessfulData()) {
            final ImmutableValue<?> value1 = success.remove(value.getKey());
            if (value1 == null || value1.get() != value.get()) {
                store.offerNoEvents(value);
            }
        }
        // A previously successful offering got removed, revert this
        if (!success.isEmpty()) {
            for (ImmutableValue<?> value : original.getReplacedData()) {
                if (success.containsKey(value.getKey())) {
                    store.offerNoEvents(value);
                }
            }
        }
    }
    return event.getEndResult();
}
Also used : ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) HashMap(java.util.HashMap) DataHolder(org.spongepowered.api.data.DataHolder) Cause(org.spongepowered.api.event.cause.Cause) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ChangeDataHolderEvent(org.spongepowered.api.event.data.ChangeDataHolderEvent) LanternKey(org.lanternpowered.server.data.key.LanternKey) Key(org.spongepowered.api.data.key.Key)

Example 7 with DataHolder

use of org.spongepowered.api.data.DataHolder in project LanternServer by LanternPowered.

the class CompositeValueStoreHelper method hasListeners.

protected static boolean hasListeners(ICompositeValueStore store, Iterable<Key<?>> keys) {
    if (!(store instanceof DataHolder)) {
        return false;
    }
    for (Key<?> key : keys) {
        final DataHolder dataHolder = (DataHolder) store;
        final List<RegisteredListener<ChangeDataHolderEvent.ValueChange>> listeners = ((LanternKey) key).getListeners();
        for (RegisteredListener<ChangeDataHolderEvent.ValueChange> listener : listeners) {
            if (((KeyEventListener) listener.getHandler()).getDataHolderPredicate().test(dataHolder)) {
                return true;
            }
        }
    }
    return false;
}
Also used : DataHolder(org.spongepowered.api.data.DataHolder) LanternKey(org.lanternpowered.server.data.key.LanternKey) RegisteredListener(org.lanternpowered.server.event.RegisteredListener) ChangeDataHolderEvent(org.spongepowered.api.event.data.ChangeDataHolderEvent)

Example 8 with DataHolder

use of org.spongepowered.api.data.DataHolder in project LanternServer by LanternPowered.

the class HasDataFilterDelegate method write.

@Override
public void write(ClassWriter cw, MethodVisitor mv, Method method, Parameter param, int localParam) {
    if (!DataHolder.class.isAssignableFrom(param.getType())) {
        throw new IllegalStateException("Annotated type for data filter is not a DataHolder");
    }
    mv.visitVarInsn(ALOAD, localParam);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(DataHolder.class));
    mv.visitLdcInsn(Type.getType(this.anno.value()));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(DataHolder.class), "get", "(Ljava/lang/Class;)Ljava/util/Optional;", true);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Optional", "isPresent", "()Z", false);
    Label success = new Label();
    if (this.anno.inverse()) {
        mv.visitJumpInsn(IFEQ, success);
    } else {
        mv.visitJumpInsn(IFNE, success);
    }
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(success);
}
Also used : DataHolder(org.spongepowered.api.data.DataHolder) Label(org.objectweb.asm.Label)

Example 9 with DataHolder

use of org.spongepowered.api.data.DataHolder in project LanternServer by LanternPowered.

the class SupportsDataFilterDelegate method write.

@Override
public void write(ClassWriter cw, MethodVisitor mv, Method method, Parameter param, int localParam) {
    if (!DataHolder.class.isAssignableFrom(param.getType())) {
        throw new IllegalStateException("Annotated type for data filter is not a DataHolder");
    }
    mv.visitVarInsn(ALOAD, localParam);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(DataHolder.class));
    mv.visitLdcInsn(Type.getType(this.anno.value()));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(DataHolder.class), "supports", "(Ljava/lang/Class;)Z", true);
    Label success = new Label();
    if (this.anno.inverse()) {
        mv.visitJumpInsn(IFEQ, success);
    } else {
        mv.visitJumpInsn(IFNE, success);
    }
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(success);
}
Also used : DataHolder(org.spongepowered.api.data.DataHolder) Label(org.objectweb.asm.Label)

Aggregations

DataHolder (org.spongepowered.api.data.DataHolder)9 Label (org.objectweb.asm.Label)4 LanternKey (org.lanternpowered.server.data.key.LanternKey)3 UUID (java.util.UUID)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 NBTTagList (net.minecraft.nbt.NBTTagList)2 Key (org.spongepowered.api.data.key.Key)2 ChangeDataHolderEvent (org.spongepowered.api.event.data.ChangeDataHolderEvent)2 RespawnLocation (org.spongepowered.api.util.RespawnLocation)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 RegisteredListener (org.lanternpowered.server.event.RegisteredListener)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)1 CompositeValueStore (org.spongepowered.api.data.value.mutable.CompositeValueStore)1 Cause (org.spongepowered.api.event.cause.Cause)1