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