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();
}
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;
}
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);
}
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);
}
Aggregations