Search in sources :

Example 1 with EntityDataAccessorBridge

use of org.spongepowered.common.bridge.network.syncher.EntityDataAccessorBridge in project SpongeCommon by SpongePowered.

the class SynchedEntityDataMixin method set.

/**
 * @author gabizou December 27th, 2017
 * @reason Inject ChangeValueEvent for entities by utilizing keys. Keys are registered
 *     based on the entity of which they belong to. This is to make the events more streamlined
 *     with regards to "when" they are being changed.
 * @param key The parameter key
 * @param value The value
 * @param <T> The type of value
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Overwrite
public <T> void set(final EntityDataAccessor<T> key, T value) {
    final SynchedEntityData.DataItem<T> dataentry = this.<T>getItem(key);
    // Sponge Start - set up the current value, so we don't have to retrieve it multiple times
    final T currentValue = dataentry.getValue();
    final T incomingValue = value;
    if (ObjectUtils.notEqual(value, currentValue)) {
        // I don't know, ask Grum....
        if (this.entity != null && this.entity.level != null && !this.entity.level.isClientSide && !((EntityBridge) this.entity).bridge$isConstructing()) {
            // We only want to spam the server world ;)
            final Optional<DataParameterConverter<T>> converter = ((EntityDataAccessorBridge) key).bridge$getDataConverter();
            // At this point it is changing
            if (converter.isPresent()) {
                // Ok, we have a key ready to use the converter
                final Optional<DataTransactionResult> optional = converter.get().createTransaction(this.entity, currentValue, value);
                if (optional.isPresent()) {
                    // Only need to make a transaction if there are actual changes necessary.
                    final DataTransactionResult transaction = optional.get();
                    final ChangeDataHolderEvent.ValueChange event = SpongeEventFactory.createChangeDataHolderEventValueChange(PhaseTracker.getCauseStackManager().currentCause(), transaction, (DataHolder.Mutable) this.entity);
                    Sponge.eventManager().post(event);
                    if (event.isCancelled()) {
                        // If the event is cancelled, well, don't change the underlying value.
                        return;
                    }
                    try {
                        value = converter.get().getValueFromEvent(currentValue, event.endResult());
                    } catch (final Exception e) {
                        // Worst case scenario, we don't want to cause an issue, so we just set the value
                        value = incomingValue;
                    }
                }
            }
        }
        // Sponge End
        dataentry.setValue(value);
        this.entity.onSyncedDataUpdated(key);
        dataentry.setDirty(true);
        this.isDirty = true;
    }
}
Also used : SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) EntityDataAccessorBridge(org.spongepowered.common.bridge.network.syncher.EntityDataAccessorBridge) DataParameterConverter(org.spongepowered.common.data.datasync.DataParameterConverter) DataHolder(org.spongepowered.api.data.DataHolder) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) EntityBridge(org.spongepowered.common.bridge.world.entity.EntityBridge) ChangeDataHolderEvent(org.spongepowered.api.event.data.ChangeDataHolderEvent) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

SynchedEntityData (net.minecraft.network.syncher.SynchedEntityData)1 DataHolder (org.spongepowered.api.data.DataHolder)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 ChangeDataHolderEvent (org.spongepowered.api.event.data.ChangeDataHolderEvent)1 Overwrite (org.spongepowered.asm.mixin.Overwrite)1 EntityDataAccessorBridge (org.spongepowered.common.bridge.network.syncher.EntityDataAccessorBridge)1 EntityBridge (org.spongepowered.common.bridge.world.entity.EntityBridge)1 DataParameterConverter (org.spongepowered.common.data.datasync.DataParameterConverter)1