|
1 | 1 | package dev.trigam.field.component; |
2 | 2 |
|
3 | 3 | import com.mojang.serialization.Codec; |
4 | | -import com.mojang.serialization.codecs.RecordCodecBuilder; |
| 4 | +import dev.trigam.field.Field; |
| 5 | +import io.netty.buffer.ByteBuf; |
5 | 6 | import net.minecraft.block.entity.BannerBlockEntity; |
6 | 7 | import net.minecraft.nbt.NbtCompound; |
| 8 | +import net.minecraft.network.codec.PacketCodec; |
| 9 | +import net.minecraft.network.codec.PacketCodecs; |
7 | 10 | import net.minecraft.registry.RegistryWrapper; |
8 | 11 | import org.ladysnake.cca.api.v3.component.sync.AutoSyncedComponent; |
| 12 | +import java.util.ArrayList; |
9 | 13 | import java.util.Arrays; |
10 | 14 | import java.util.List; |
11 | | -import java.util.Set; |
12 | | -import java.util.TreeSet; |
13 | | -import java.util.stream.Collectors; |
14 | 15 |
|
15 | 16 | public class GlowingLayersComponent implements AutoSyncedComponent { |
16 | 17 |
|
17 | | - public Set<Integer> glowingLayers = new TreeSet<>(); |
| 18 | + public List<Integer> glowingLayers = new ArrayList<>(); |
18 | 19 | private BannerBlockEntity banner; |
19 | 20 |
|
20 | | - public GlowingLayersComponent( BannerBlockEntity banner ) { |
| 21 | + public GlowingLayersComponent () {} |
| 22 | + public GlowingLayersComponent ( BannerBlockEntity banner ) { |
21 | 23 | this.banner = banner; |
22 | 24 | } |
23 | | - public GlowingLayersComponent(Set<Integer> glowingLayers) { |
| 25 | + public GlowingLayersComponent ( List<Integer> glowingLayers ) { |
24 | 26 | this.glowingLayers = glowingLayers; |
25 | 27 | } |
26 | 28 |
|
27 | 29 | // Codecs |
28 | | - public static final Codec<GlowingLayersComponent> CODEC = RecordCodecBuilder.create(instance -> |
29 | | - instance.group( |
30 | | - Codec.list( Codec.INT ).optionalFieldOf( "glowing_layers", List.of() ) |
31 | | - .xmap(Set::copyOf, List::copyOf ) |
32 | | - .forGetter( GlowingLayersComponent::getGlowingLayers ) |
33 | | - ).apply( instance, GlowingLayersComponent::new ) |
34 | | - ); |
| 30 | + public static final Codec<GlowingLayersComponent> CODEC = Codec.INT |
| 31 | + .listOf() |
| 32 | + .xmap( GlowingLayersComponent::new, GlowingLayersComponent::getGlowingLayers ); |
| 33 | + public static final PacketCodec<ByteBuf, GlowingLayersComponent> PACKET_CODEC = PacketCodecs.INTEGER |
| 34 | + .collect( PacketCodecs.toList() ) |
| 35 | + .xmap( GlowingLayersComponent::new, GlowingLayersComponent::getGlowingLayers ); |
35 | 36 |
|
36 | | - public Set<Integer> getGlowingLayers() { return this.glowingLayers; } |
| 37 | + public List<Integer> getGlowingLayers() { return this.glowingLayers; } |
37 | 38 |
|
38 | 39 | public boolean isLayerGlowing( int layerIndex ) { |
39 | 40 | return this.glowingLayers.contains( layerIndex ); |
40 | 41 | } |
41 | 42 |
|
42 | 43 | public void setLayerGlowing( int layerIndex, boolean glowing ) { |
43 | | - if ( glowing ) this.glowingLayers.add( layerIndex ); |
44 | | - else this.glowingLayers.remove( layerIndex ); |
| 44 | + Field.LOGGER.info( "Current: {}", this.glowingLayers ); |
| 45 | + Field.LOGGER.info( "Setting layer {} to {}", layerIndex, glowing ? "glowing" : "not glowing" ); |
45 | 46 |
|
| 47 | + List<Integer> layers = new ArrayList<>( this.glowingLayers ); |
| 48 | + |
| 49 | + if ( glowing ) layers.add( layerIndex ); |
| 50 | + else layers.remove( Integer.valueOf( layerIndex ) ); |
| 51 | + |
| 52 | + this.glowingLayers = layers; |
46 | 53 | ComponentInit.GLOWING_LAYERS.sync( this.banner ); |
47 | 54 | } |
48 | 55 |
|
49 | 56 | @Override |
50 | 57 | public void readFromNbt( NbtCompound tag, RegistryWrapper.WrapperLookup wrapperLookup ) { |
51 | 58 | int[] nbtLayers = tag.getIntArray("glowing_layers"); |
52 | | - this.glowingLayers = Arrays.stream( nbtLayers ).boxed().collect( Collectors.toSet() ); |
| 59 | + this.glowingLayers = Arrays.stream( nbtLayers ).boxed().toList(); |
53 | 60 | } |
54 | 61 |
|
55 | 62 | @Override |
|
0 commit comments