Skip to content

Commit e7cfc44

Browse files
committed
Switch to using a List
1 parent 36e7322 commit e7cfc44

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/main/java/dev/trigam/field/component/GlowingLayersComponent.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
11
package dev.trigam.field.component;
22

33
import com.mojang.serialization.Codec;
4-
import com.mojang.serialization.codecs.RecordCodecBuilder;
4+
import dev.trigam.field.Field;
5+
import io.netty.buffer.ByteBuf;
56
import net.minecraft.block.entity.BannerBlockEntity;
67
import net.minecraft.nbt.NbtCompound;
8+
import net.minecraft.network.codec.PacketCodec;
9+
import net.minecraft.network.codec.PacketCodecs;
710
import net.minecraft.registry.RegistryWrapper;
811
import org.ladysnake.cca.api.v3.component.sync.AutoSyncedComponent;
12+
import java.util.ArrayList;
913
import java.util.Arrays;
1014
import java.util.List;
11-
import java.util.Set;
12-
import java.util.TreeSet;
13-
import java.util.stream.Collectors;
1415

1516
public class GlowingLayersComponent implements AutoSyncedComponent {
1617

17-
public Set<Integer> glowingLayers = new TreeSet<>();
18+
public List<Integer> glowingLayers = new ArrayList<>();
1819
private BannerBlockEntity banner;
1920

20-
public GlowingLayersComponent( BannerBlockEntity banner ) {
21+
public GlowingLayersComponent () {}
22+
public GlowingLayersComponent ( BannerBlockEntity banner ) {
2123
this.banner = banner;
2224
}
23-
public GlowingLayersComponent(Set<Integer> glowingLayers) {
25+
public GlowingLayersComponent ( List<Integer> glowingLayers ) {
2426
this.glowingLayers = glowingLayers;
2527
}
2628

2729
// 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 );
3536

36-
public Set<Integer> getGlowingLayers() { return this.glowingLayers; }
37+
public List<Integer> getGlowingLayers() { return this.glowingLayers; }
3738

3839
public boolean isLayerGlowing( int layerIndex ) {
3940
return this.glowingLayers.contains( layerIndex );
4041
}
4142

4243
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" );
4546

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;
4653
ComponentInit.GLOWING_LAYERS.sync( this.banner );
4754
}
4855

4956
@Override
5057
public void readFromNbt( NbtCompound tag, RegistryWrapper.WrapperLookup wrapperLookup ) {
5158
int[] nbtLayers = tag.getIntArray("glowing_layers");
52-
this.glowingLayers = Arrays.stream( nbtLayers ).boxed().collect( Collectors.toSet() );
59+
this.glowingLayers = Arrays.stream( nbtLayers ).boxed().toList();
5360
}
5461

5562
@Override

src/main/java/dev/trigam/field/event/ToggleBannerGlow.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static ActionResult toggleBannerGlow( PlayerEntity player, World world, H
2929
ItemStack usedItem = player.getStackInHand( hand );
3030
BlockPos pos = result.getBlockPos();
3131
BlockEntity blockEntity = world.getBlockEntity( pos );
32+
3233
// Used item
3334
boolean addGlowing = usedItem.isOf( Items.GLOW_INK_SAC );
3435
boolean removeGlowing = usedItem.isOf( Items.INK_SAC );

0 commit comments

Comments
 (0)