Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This file is part of Sponge, licensed under the MIT License (MIT).
- *
- * Copyright (c) SpongePowered <https://www.spongepowered.org>
- * Copyright (c) contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- package org.spongepowered.common.data.processor.data.entity;
- import static com.google.common.base.Preconditions.checkNotNull;
- import static org.spongepowered.common.data.util.DataUtil.getData;
- import com.google.common.base.Optional;
- import org.spongepowered.api.data.DataContainer;
- import org.spongepowered.api.data.DataHolder;
- import org.spongepowered.api.data.DataTransactionBuilder;
- import org.spongepowered.api.data.DataTransactionResult;
- import org.spongepowered.api.data.key.Key;
- import org.spongepowered.api.data.key.Keys;
- import org.spongepowered.api.data.manipulator.immutable.entity.ImmutableIgniteableData;
- import org.spongepowered.api.data.manipulator.mutable.entity.IgniteableData;
- import org.spongepowered.api.data.merge.MergeFunction;
- import org.spongepowered.api.data.value.BaseValue;
- import org.spongepowered.api.data.value.immutable.ImmutableValue;
- import org.spongepowered.common.data.ImmutableDataCachingUtil;
- import org.spongepowered.common.data.manipulator.immutable.entity.ImmutableSpongeIgniteableData;
- import org.spongepowered.common.data.manipulator.mutable.entity.SpongeIgniteableData;
- import org.spongepowered.common.data.processor.common.AbstractSpongeDataProcessor;
- import org.spongepowered.common.data.value.immutable.ImmutableSpongeValue;
- import java.util.ArrayList;
- import java.util.Collection;
- public class IgniteableDataProcessor extends AbstractSpongeDataProcessor<IgniteableData, ImmutableIgniteableData> {
- @Override
- public boolean supports(DataHolder dataHolder) {
- return dataHolder instanceof net.minecraft.entity.Entity;
- }
- @SuppressWarnings("unused")
- @Override
- public Optional<IgniteableData> from(DataHolder dataHolder) {
- if (supports(dataHolder)) {
- final SpongeIgniteableData igniteableData = new SpongeIgniteableData();
- final net.minecraft.entity.Entity entity = (net.minecraft.entity.Entity) (dataHolder);
- return Optional.<IgniteableData>of(igniteableData.setFireTicks(entity.fire).setFireDelay(entity.fireResistance));
- } else {
- return Optional.absent();
- }
- }
- @Override
- public Optional<IgniteableData> fill(DataHolder dataHolder, IgniteableData manipulator, MergeFunction overlap) {
- if (supports(dataHolder)) {
- final IgniteableData merged = overlap.merge(checkNotNull(manipulator).copy(), from(dataHolder).get());
- manipulator.set(Keys.FIRE_DAMAGE_DELAY, merged.fireDelay().get()).set(Keys.FIRE_TICKS, merged.fireTicks().get());
- return Optional.of(manipulator);
- }
- return Optional.absent();
- }
- @Override
- public Optional<IgniteableData> fill(DataContainer container, IgniteableData igniteableData) {
- igniteableData.set(Keys.FIRE_TICKS, getData(container, Keys.FIRE_TICKS));
- igniteableData.set(Keys.FIRE_DAMAGE_DELAY, getData(container, Keys.FIRE_DAMAGE_DELAY));
- return Optional.of(igniteableData);
- }
- @Override
- public DataTransactionResult set(DataHolder dataHolder, IgniteableData manipulator, MergeFunction function) {
- final int oldFireDamageDelay = manipulator.fireDelay().get();
- final int oldFireTicks = manipulator.fireTicks().get();
- final IgniteableData merged =
- checkNotNull(function, "MergeFunction cannot be null!").merge(checkNotNull(manipulator).copy(), from(dataHolder).get());
- manipulator.set(Keys.FIRE_DAMAGE_DELAY, merged.fireDelay().get());
- manipulator.set(Keys.FIRE_TICKS, merged.fireTicks().get());
- final int fireDamageDelay = merged.fireDelay().get();
- final int fireTicks = merged.fireTicks().get();
- if (this.supports(dataHolder)) {
- net.minecraft.entity.Entity entity = (net.minecraft.entity.Entity) dataHolder;
- entity.fire = fireTicks;
- entity.fireResistance = fireDamageDelay;
- Collection<ImmutableValue<Integer>> successful = new ArrayList<ImmutableValue<Integer>>();
- Collection<ImmutableValue<Integer>> replaced = new ArrayList<ImmutableValue<Integer>>();
- successful.add(new ImmutableSpongeValue<Integer>(Keys.FIRE_TICKS, oldFireTicks));
- replaced.add(new ImmutableSpongeValue<Integer>(Keys.FIRE_TICKS, fireTicks));
- successful.add(new ImmutableSpongeValue<Integer>(Keys.FIRE_DAMAGE_DELAY, oldFireDamageDelay));
- replaced.add(new ImmutableSpongeValue<Integer>(Keys.FIRE_DAMAGE_DELAY, fireDamageDelay));
- return DataTransactionBuilder.successReplaceResult(successful, replaced);
- }
- return DataTransactionBuilder.failResult(manipulator.getValues());
- }
- @Override
- public Optional<ImmutableIgniteableData> with(Key<? extends BaseValue<?>> key, Object value, ImmutableIgniteableData immutable) {
- if (key.equals(Keys.FIRE_DAMAGE_DELAY)) {
- return Optional.<ImmutableIgniteableData>of(ImmutableDataCachingUtil.getManipulator(ImmutableSpongeIgniteableData.class, value));
- } else if (key.equals(Keys.FIRE_TICKS)) {
- return Optional.<ImmutableIgniteableData>of(ImmutableDataCachingUtil.getManipulator(ImmutableSpongeIgniteableData.class, value));
- }
- return Optional.absent();
- }
- @Override
- public DataTransactionResult remove(DataHolder dataHolder) {
- if (supports(dataHolder)) {
- if (((net.minecraft.entity.Entity) dataHolder).isRiding()) {
- ((net.minecraft.entity.Entity) dataHolder).ridingEntity = null;
- return DataTransactionBuilder.builder().result(DataTransactionResult.Type.SUCCESS).build();
- } else {
- return DataTransactionBuilder.builder().result(DataTransactionResult.Type.FAILURE).build();
- }
- } else {
- return DataTransactionBuilder.builder().result(DataTransactionResult.Type.FAILURE).build();
- }
- }
- @Override
- public Optional<IgniteableData> createFrom(DataHolder dataHolder) {
- if (supports(dataHolder)) {
- final int fireTicks = ((net.minecraft.entity.Entity) dataHolder).fire;
- final int fireDelay = ((net.minecraft.entity.Entity) dataHolder).fireResistance;
- return Optional.<IgniteableData>of(new SpongeIgniteableData(fireTicks, fireDelay));
- }
- return Optional.absent();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment