Search in sources :

Example 16 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult in project Nucleus by NucleusPowered.

the class RepairCommand method repairStack.

private RepairResult repairStack(ItemStack stack, boolean checkRestrictions) {
    if (checkRestrictions && (whitelist && !restrictions.contains(stack.getType()) || restrictions.contains(stack.getType()))) {
        return new RepairResult(stack, ResultType.RESTRICTED);
    }
    if (stack.get(DurabilityData.class).isPresent()) {
        DurabilityData durabilityData = stack.get(DurabilityData.class).get();
        DataTransactionResult transactionResult = stack.offer(Keys.ITEM_DURABILITY, durabilityData.durability().getMaxValue());
        if (transactionResult.isSuccessful()) {
            return new RepairResult(stack, ResultType.SUCCESS);
        } else {
            return new RepairResult(stack, ResultType.ERROR);
        }
    } else {
        return new RepairResult(stack, ResultType.NO_DURABILITY);
    }
}
Also used : DurabilityData(org.spongepowered.api.data.manipulator.mutable.item.DurabilityData) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult)

Example 17 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult in project Nucleus by NucleusPowered.

the class SpeedCommand method executeWithPlayer.

@Override
public CommandResult executeWithPlayer(CommandSource src, Player pl, CommandContext args, boolean isSelf) throws Exception {
    Optional<Integer> ospeed = args.getOne(speedKey);
    if (!ospeed.isPresent()) {
        Text t = Text.builder().append(plugin.getMessageProvider().getTextMessageWithFormat("command.speed.walk")).append(Text.of(" ")).append(Text.of(TextColors.YELLOW, Math.round(pl.get(Keys.WALKING_SPEED).orElse(0.1d) * 20))).append(Text.builder().append(Text.of(TextColors.GREEN, ", ")).append(plugin.getMessageProvider().getTextMessageWithFormat("command.speed.flying")).build()).append(Text.of(" ")).append(Text.of(TextColors.YELLOW, Math.round(pl.get(Keys.FLYING_SPEED).orElse(0.05d) * 20))).append(Text.of(TextColors.GREEN, ".")).build();
        src.sendMessage(t);
        // Don't trigger cooldowns
        return CommandResult.empty();
    }
    SpeedType key = args.<SpeedType>getOne(typeKey).orElseGet(() -> pl.get(Keys.IS_FLYING).orElse(false) ? SpeedType.FLYING : SpeedType.WALKING);
    int speed = ospeed.get();
    if (speed < 0) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.speed.negative"));
        return CommandResult.empty();
    }
    if (!permissions.testSuffix(src, "exempt.max", src, true) && maxSpeed < speed) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.speed.max", String.valueOf(maxSpeed)));
        return CommandResult.empty();
    }
    DataTransactionResult dtr = pl.offer(key.speedKey, (double) speed / (double) multiplier);
    if (dtr.isSuccessful()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.speed.success.base", key.name, String.valueOf(speed)));
        if (!isSelf) {
            src.sendMessages(plugin.getMessageProvider().getTextMessageWithFormat("command.speed.success.other", pl.getName(), key.name, String.valueOf(speed)));
        }
        return CommandResult.success();
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.speed.fail", key.name));
    return CommandResult.empty();
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) Text(org.spongepowered.api.text.Text)

Example 18 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult 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)

Example 19 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult in project SpongeCommon by SpongePowered.

the class MixinDataHolder method offer.

@Override
public <E> DataTransactionResult offer(Key<? extends BaseValue<E>> key, E value) {
    TimingsManager.DATA_GROUP_HANDLER.startTimingIfSync();
    SpongeTimings.dataOfferKey.startTimingIfSync();
    final Optional<ValueProcessor<E, ? extends BaseValue<E>>> optional = DataUtil.getBaseValueProcessor(key);
    if (optional.isPresent()) {
        final DataTransactionResult result = optional.get().offerToStore(this, value);
        SpongeTimings.dataOfferKey.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    } else if (this instanceof IMixinCustomDataHolder) {
        final DataTransactionResult result = ((IMixinCustomDataHolder) this).offerCustom(key, value);
        SpongeTimings.dataOfferKey.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    }
    SpongeTimings.dataOfferKey.stopTimingIfSync();
    TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
    return DataTransactionResult.failNoData();
}
Also used : BaseValue(org.spongepowered.api.data.value.BaseValue) ValueProcessor(org.spongepowered.common.data.ValueProcessor) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 20 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult in project SpongeCommon by SpongePowered.

the class SpongeItemStackSnapshot method transform.

@Override
public <E> Optional<ItemStackSnapshot> transform(Key<? extends BaseValue<E>> key, Function<E, E> function) {
    final ItemStack copy = this.privateStack.copy();
    final DataTransactionResult result = copy.transform(key, function);
    if (result.getType() != DataTransactionResult.Type.SUCCESS) {
        return Optional.empty();
    }
    return Optional.of(copy.createSnapshot());
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)29 ItemStack (org.spongepowered.api.item.inventory.ItemStack)6 DataHolder (org.spongepowered.api.data.DataHolder)4 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BlockState (org.spongepowered.api.block.BlockState)3 BaseValue (org.spongepowered.api.data.value.BaseValue)3 Player (org.spongepowered.api.entity.living.player.Player)3 IMixinCustomDataHolder (org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Set (java.util.Set)2 EnumFacing (net.minecraft.util.EnumFacing)2 Sponge (org.spongepowered.api.Sponge)2 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 DataView (org.spongepowered.api.data.DataView)2