hassansyyid

Untitled

Aug 22nd, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.56 KB | None | 0 0
  1. /*
  2.  * This file is part of Sponge, licensed under the MIT License (MIT).
  3.  *
  4.  * Copyright (c) SpongePowered <https://www.spongepowered.org>
  5.  * Copyright (c) contributors
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in
  15.  * all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23.  * THE SOFTWARE.
  24.  */
  25. package org.spongepowered.common.data.processor.data.entity;
  26.  
  27. import static com.google.common.base.Preconditions.checkNotNull;
  28. import static org.spongepowered.common.data.util.DataUtil.getData;
  29.  
  30. import com.google.common.base.Optional;
  31. import org.spongepowered.api.data.DataContainer;
  32. import org.spongepowered.api.data.DataHolder;
  33. import org.spongepowered.api.data.DataTransactionBuilder;
  34. import org.spongepowered.api.data.DataTransactionResult;
  35. import org.spongepowered.api.data.key.Key;
  36. import org.spongepowered.api.data.key.Keys;
  37. import org.spongepowered.api.data.manipulator.immutable.entity.ImmutableIgniteableData;
  38. import org.spongepowered.api.data.manipulator.mutable.entity.IgniteableData;
  39. import org.spongepowered.api.data.merge.MergeFunction;
  40. import org.spongepowered.api.data.value.BaseValue;
  41. import org.spongepowered.api.data.value.immutable.ImmutableValue;
  42. import org.spongepowered.common.data.ImmutableDataCachingUtil;
  43. import org.spongepowered.common.data.manipulator.immutable.entity.ImmutableSpongeIgniteableData;
  44. import org.spongepowered.common.data.manipulator.mutable.entity.SpongeIgniteableData;
  45. import org.spongepowered.common.data.processor.common.AbstractSpongeDataProcessor;
  46. import org.spongepowered.common.data.value.immutable.ImmutableSpongeValue;
  47.  
  48. import java.util.ArrayList;
  49. import java.util.Collection;
  50.  
  51. public class IgniteableDataProcessor extends AbstractSpongeDataProcessor<IgniteableData, ImmutableIgniteableData> {
  52.  
  53.     @Override
  54.     public boolean supports(DataHolder dataHolder) {
  55.         return dataHolder instanceof net.minecraft.entity.Entity;
  56.     }
  57.  
  58.     @SuppressWarnings("unused")
  59.     @Override
  60.     public Optional<IgniteableData> from(DataHolder dataHolder) {
  61.         if (supports(dataHolder)) {
  62.             final SpongeIgniteableData igniteableData = new SpongeIgniteableData();
  63.             final net.minecraft.entity.Entity entity = (net.minecraft.entity.Entity) (dataHolder);
  64.             return Optional.<IgniteableData>of(igniteableData.setFireTicks(entity.fire).setFireDelay(entity.fireResistance));
  65.         } else {
  66.             return Optional.absent();
  67.         }
  68.  
  69.     }
  70.  
  71.     @Override
  72.     public Optional<IgniteableData> fill(DataHolder dataHolder, IgniteableData manipulator, MergeFunction overlap) {
  73.         if (supports(dataHolder)) {
  74.             final IgniteableData merged = overlap.merge(checkNotNull(manipulator).copy(), from(dataHolder).get());
  75.             manipulator.set(Keys.FIRE_DAMAGE_DELAY, merged.fireDelay().get()).set(Keys.FIRE_TICKS, merged.fireTicks().get());
  76.             return Optional.of(manipulator);
  77.         }
  78.         return Optional.absent();
  79.     }
  80.  
  81.     @Override
  82.     public Optional<IgniteableData> fill(DataContainer container, IgniteableData igniteableData) {
  83.         igniteableData.set(Keys.FIRE_TICKS, getData(container, Keys.FIRE_TICKS));
  84.         igniteableData.set(Keys.FIRE_DAMAGE_DELAY, getData(container, Keys.FIRE_DAMAGE_DELAY));
  85.         return Optional.of(igniteableData);
  86.     }
  87.  
  88.     @Override
  89.     public DataTransactionResult set(DataHolder dataHolder, IgniteableData manipulator, MergeFunction function) {
  90.         final int oldFireDamageDelay = manipulator.fireDelay().get();
  91.         final int oldFireTicks = manipulator.fireTicks().get();
  92.         final IgniteableData merged =
  93.                 checkNotNull(function, "MergeFunction cannot be null!").merge(checkNotNull(manipulator).copy(), from(dataHolder).get());
  94.         manipulator.set(Keys.FIRE_DAMAGE_DELAY, merged.fireDelay().get());
  95.         manipulator.set(Keys.FIRE_TICKS, merged.fireTicks().get());
  96.         final int fireDamageDelay = merged.fireDelay().get();
  97.         final int fireTicks = merged.fireTicks().get();
  98.         if (this.supports(dataHolder)) {
  99.             net.minecraft.entity.Entity entity = (net.minecraft.entity.Entity) dataHolder;
  100.             entity.fire = fireTicks;
  101.             entity.fireResistance = fireDamageDelay;
  102.             Collection<ImmutableValue<Integer>> successful = new ArrayList<ImmutableValue<Integer>>();
  103.             Collection<ImmutableValue<Integer>> replaced = new ArrayList<ImmutableValue<Integer>>();
  104.             successful.add(new ImmutableSpongeValue<Integer>(Keys.FIRE_TICKS, oldFireTicks));
  105.             replaced.add(new ImmutableSpongeValue<Integer>(Keys.FIRE_TICKS, fireTicks));
  106.             successful.add(new ImmutableSpongeValue<Integer>(Keys.FIRE_DAMAGE_DELAY, oldFireDamageDelay));
  107.             replaced.add(new ImmutableSpongeValue<Integer>(Keys.FIRE_DAMAGE_DELAY, fireDamageDelay));
  108.             return DataTransactionBuilder.successReplaceResult(successful, replaced);
  109.         }
  110.         return DataTransactionBuilder.failResult(manipulator.getValues());
  111.     }
  112.  
  113.     @Override
  114.     public Optional<ImmutableIgniteableData> with(Key<? extends BaseValue<?>> key, Object value, ImmutableIgniteableData immutable) {
  115.         if (key.equals(Keys.FIRE_DAMAGE_DELAY)) {
  116.             return Optional.<ImmutableIgniteableData>of(ImmutableDataCachingUtil.getManipulator(ImmutableSpongeIgniteableData.class, value));
  117.         } else if (key.equals(Keys.FIRE_TICKS)) {
  118.             return Optional.<ImmutableIgniteableData>of(ImmutableDataCachingUtil.getManipulator(ImmutableSpongeIgniteableData.class, value));
  119.         }
  120.         return Optional.absent();
  121.     }
  122.  
  123.     @Override
  124.     public DataTransactionResult remove(DataHolder dataHolder) {
  125.         if (supports(dataHolder)) {
  126.             if (((net.minecraft.entity.Entity) dataHolder).isRiding()) {
  127.                 ((net.minecraft.entity.Entity) dataHolder).ridingEntity = null;
  128.                 return DataTransactionBuilder.builder().result(DataTransactionResult.Type.SUCCESS).build();
  129.             } else {
  130.                 return DataTransactionBuilder.builder().result(DataTransactionResult.Type.FAILURE).build();
  131.             }
  132.         } else {
  133.             return DataTransactionBuilder.builder().result(DataTransactionResult.Type.FAILURE).build();
  134.         }
  135.     }
  136.  
  137.     @Override
  138.     public Optional<IgniteableData> createFrom(DataHolder dataHolder) {
  139.         if (supports(dataHolder)) {
  140.             final int fireTicks = ((net.minecraft.entity.Entity) dataHolder).fire;
  141.             final int fireDelay = ((net.minecraft.entity.Entity) dataHolder).fireResistance;
  142.             return Optional.<IgniteableData>of(new SpongeIgniteableData(fireTicks, fireDelay));
  143.         }
  144.         return Optional.absent();
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment