Skip to content

Commit

Permalink
Store blocks as set inside HieroglyphCode
Browse files Browse the repository at this point in the history
  • Loading branch information
kirderf1 committed Dec 10, 2024
1 parent 371655e commit 468c0d5
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mraof.minestuck.item.components;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.mojang.serialization.Codec;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
Expand All @@ -14,24 +14,23 @@
import java.util.List;
import java.util.Set;

public record HieroglyphCode(List<Block> hieroglyphs)
public record HieroglyphCode(Set<Block> hieroglyphs)
{
public static final Codec<HieroglyphCode> CODEC = BuiltInRegistries.BLOCK.byNameCodec().listOf()
.xmap(HieroglyphCode::new, HieroglyphCode::hieroglyphs);
.xmap(Set::copyOf, List::copyOf).xmap(HieroglyphCode::new, HieroglyphCode::hieroglyphs);
public static final StreamCodec<RegistryFriendlyByteBuf, HieroglyphCode> STREAM_CODEC = ByteBufCodecs.registry(Registries.BLOCK).apply(ByteBufCodecs.list())
.map(HieroglyphCode::new, HieroglyphCode::hieroglyphs);
.map(Set::copyOf, List::copyOf).map(HieroglyphCode::new, HieroglyphCode::hieroglyphs);

public static final HieroglyphCode EMPTY = new HieroglyphCode(Collections.emptyList());
public static final HieroglyphCode EMPTY = new HieroglyphCode(Collections.emptySet());

public static Set<Block> getBlocks(ItemStack stack)
{
HieroglyphCode hieroglyphCodes = stack.get(MSItemComponents.HIEROGLYPH_CODE);
return hieroglyphCodes != null ? Set.copyOf(hieroglyphCodes.hieroglyphs()) : Set.of();
return stack.getOrDefault(MSItemComponents.HIEROGLYPH_CODE, EMPTY).hieroglyphs();
}

public static void setBlocks(ItemStack stack, Set<Block> hieroglyphs)
{
stack.set(MSItemComponents.HIEROGLYPH_CODE, new HieroglyphCode(List.copyOf(hieroglyphs)));
stack.set(MSItemComponents.HIEROGLYPH_CODE, new HieroglyphCode(Set.copyOf(hieroglyphs)));
}

public static boolean addBlock(ItemStack stack, Block hieroglyph)
Expand All @@ -40,10 +39,10 @@ public static boolean addBlock(ItemStack stack, Block hieroglyph)
if(existingCode.hieroglyphs().contains(hieroglyph))
return false;

ImmutableList.Builder<Block> listBuilder = ImmutableList.builder();
listBuilder.add(hieroglyph);
listBuilder.addAll(existingCode.hieroglyphs());
stack.set(MSItemComponents.HIEROGLYPH_CODE, new HieroglyphCode(listBuilder.build()));
ImmutableSet.Builder<Block> builder = ImmutableSet.builder();
builder.add(hieroglyph);
builder.addAll(existingCode.hieroglyphs());
stack.set(MSItemComponents.HIEROGLYPH_CODE, new HieroglyphCode(builder.build()));
return true;
}
}

0 comments on commit 468c0d5

Please sign in to comment.