CerbonXD

Untitled

Dec 23rd, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | Source Code | 0 0
  1. package com.cerbon.cerbons_api.capability.providers;
  2.  
  3. import com.cerbon.cerbons_api.api.general.data.HistoricalData;
  4. import net.minecraft.core.BlockPos;
  5. import net.minecraft.core.Direction;
  6. import net.minecraft.nbt.CompoundTag;
  7. import net.minecraftforge.common.capabilities.*;
  8. import net.minecraftforge.common.util.LazyOptional;
  9. import org.jetbrains.annotations.NotNull;
  10. import org.jetbrains.annotations.Nullable;
  11.  
  12. import java.util.Arrays;
  13. import java.util.List;
  14.  
  15. @AutoRegisterCapability
  16. public class PlayerBlockPosHistoryProvider implements ICapabilitySerializable<CompoundTag> {
  17.     public static final Capability<HistoricalData<BlockPos>> HISTORICAL_DATA = CapabilityManager.get(new CapabilityToken<>() {});
  18.  
  19.     private HistoricalData<BlockPos> positionalHistory;
  20.     private final LazyOptional<HistoricalData<BlockPos>> optional = LazyOptional.of(this::createHistoricalData);
  21.  
  22.     private HistoricalData<BlockPos> createHistoricalData() {
  23.         if(this.positionalHistory == null)
  24.             this.positionalHistory = new HistoricalData<>(new BlockPos(0, 0, 0), 10);
  25.  
  26.         return this.positionalHistory;
  27.     }
  28.  
  29.     @Override
  30.     public CompoundTag serializeNBT() {
  31.         CompoundTag tag = new CompoundTag();
  32.         if (positionalHistory == null) return tag;
  33.         tag.putLongArray("LastBlocksPos", positionalHistory.stream().mapToLong(BlockPos::asLong).toArray());
  34.         return tag;
  35.     }
  36.  
  37.     @Override
  38.     public void deserializeNBT(CompoundTag tag) {
  39.         if (!tag.contains("LastBlocksPos")) return;
  40.         List<BlockPos> blocksPos = Arrays.stream(tag.getLongArray("LastBlocksPos")).mapToObj(BlockPos::of).toList();
  41.  
  42.         this.positionalHistory = new HistoricalData<>(new BlockPos(0, 0, 0), 10);
  43.         this.positionalHistory.remove(0);
  44.         this.positionalHistory.addAll(blocksPos);
  45.     }
  46.  
  47.     @Override
  48.     public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
  49.         return HISTORICAL_DATA.orEmpty(cap, optional);
  50.     }
  51. }
Add Comment
Please, Sign In to add comment