Search in sources :

Example 1 with DataParameterConverter

use of org.spongepowered.common.data.datasync.DataParameterConverter in project SpongeCommon by SpongePowered.

the class MixinEntityDataManager 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")
@Overwrite
public <T> void set(DataParameter<T> key, T value) {
    EntityDataManager.DataEntry<T> dataentry = this.<T>getEntry(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.world != null && !this.entity.world.isRemote) {
            // We only want to spam the server world ;)
            final Optional<DataParameterConverter<T>> converter = ((IMixinDataParameter) key).getConverter();
            // 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(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(Sponge.getCauseStackManager().getCurrentCause(), transaction, (DataHolder) this.entity);
                    Sponge.getEventManager().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.getEndResult().getSuccessfulData());
                    } catch (Exception e) {
                        // Worst case scenario, we don't wnat to cause an issue, so we just set the value
                        value = incomingValue;
                    }
                }
            }
        }
        // Sponge End
        dataentry.setValue(value);
        this.entity.notifyDataManagerChange(key);
        dataentry.setDirty(true);
        this.dirty = true;
    }
}
Also used : DataParameterConverter(org.spongepowered.common.data.datasync.DataParameterConverter) IMixinDataParameter(org.spongepowered.common.interfaces.network.datasync.IMixinDataParameter) EntityDataManager(net.minecraft.network.datasync.EntityDataManager) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ChangeDataHolderEvent(org.spongepowered.api.event.data.ChangeDataHolderEvent) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

EntityDataManager (net.minecraft.network.datasync.EntityDataManager)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 ChangeDataHolderEvent (org.spongepowered.api.event.data.ChangeDataHolderEvent)1 Overwrite (org.spongepowered.asm.mixin.Overwrite)1 DataParameterConverter (org.spongepowered.common.data.datasync.DataParameterConverter)1 IMixinDataParameter (org.spongepowered.common.interfaces.network.datasync.IMixinDataParameter)1