Skip to content

Commit

Permalink
improve block spec null handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursivePineapple committed Jan 4, 2025
1 parent ec4d542 commit 8b5c0c1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void init(FMLInitializationEvent event) {
public void postInit(FMLPostInitializationEvent event) {
ManipulatorRecipes.addRecipes();
BlockPropertyRegistry.init();
BlockSpec.init();
}

public void serverStarting(FMLServerStartingEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,17 @@ public static BlockSpec fromBlock(BlockSpec pooled, World world, int x, int y, i
return spec;
}

public static final ImmutableBlockSpec AIR = new BlockSpec();
public static final ImmutableBlockSpec AIR = air();

public static void init() {
((BlockSpec)AIR).setObject(Blocks.air, 0);
public static BlockSpec air() {
BlockSpec spec = new BlockSpec();

spec.block = Blocks.air;
spec.item = Optional.empty();
spec.itemId = Optional.empty();
spec.stack = Optional.empty();

return spec;
}

public static ImmutableBlockSpec choose(List<BlockSpec> specs, Random rng) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public class WeightedSpecList {

public ArrayList<ObjectIntMutablePair<BlockSpec>> specs = new ArrayList<>();

public WeightedSpecList(BlockSpec... values) {
for (BlockSpec spec : values) {
add(spec);
}
}

public void add(BlockSpec spec) {
for (var p : specs) {
if (Objects.equals(p.first(), spec)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ public class MMConfig {
// if any are non-null, then the corresponding block is being moved
public Vector3i coordAOffset, coordBOffset, coordCOffset;

public WeightedSpecList corners, edges, faces, volumes;
public BlockSpec cables;
public WeightedSpecList corners = new WeightedSpecList(BlockSpec.air());
public WeightedSpecList edges = new WeightedSpecList(BlockSpec.air());
public WeightedSpecList faces = new WeightedSpecList(BlockSpec.air());
public WeightedSpecList volumes = new WeightedSpecList(BlockSpec.air());
public BlockSpec cables = BlockSpec.air();

/** These blocks are what gets removed when exchanging */
public WeightedSpecList replaceWhitelist;
public WeightedSpecList replaceWhitelist = new WeightedSpecList(BlockSpec.air());
/** These blocks are what gets placed when exchanging */
public WeightedSpecList replaceWith;
public WeightedSpecList replaceWith = new WeightedSpecList(BlockSpec.air());

@Nullable
public Transform transform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
Expand Down Expand Up @@ -40,6 +41,12 @@ public WeightedSpecList deserialize(JsonElement json, Type typeOfT, JsonDeserial

@Override
public JsonElement serialize(WeightedSpecList src, Type typeOfSrc, JsonSerializationContext context) {
if (src.specs.size() == 1) {
BlockSpec spec = src.specs.get(0).left();

if (spec == null || spec.isAir()) return JsonNull.INSTANCE;
}

JsonArray array = new JsonArray();

for (var p : src.specs) {
Expand Down

0 comments on commit 8b5c0c1

Please sign in to comment.