Skip to content

Commit

Permalink
refactor: Custom class for ID and modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
null2264 committed Dec 6, 2023
1 parent 879f32f commit 9016a6e
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 45 deletions.
57 changes: 57 additions & 0 deletions src/main/java/io/github/null2264/cobblegen/data/CGIdentifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.null2264.cobblegen.data;

import lombok.val;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;

import static io.github.null2264.cobblegen.CobbleGen.MOD_ID;

/**
* Replaces MC's ResourceLocation, in case MC's ResourceLocation changed
* @param modid
* @param name
*/
public record CGIdentifier(String modid, String name) {
public static CGIdentifier of(String id) {
if (id.equals("*")) return wildcard();

val split = id.split(":", 2);
if (split.length < 1)
throw new RuntimeException("Invalid ID");
if (split.length == 1)
return new CGIdentifier(MOD_ID, split[0]);

return new CGIdentifier(split[0], split[1]);
}

public static CGIdentifier wildcard() {
return new CGIdentifier(MOD_ID, "*");
}

public boolean isWildcard() {
return name.equals("*");
}

@Override
public String toString() {
if (isWildcard()) return "*";
return String.format("%s:%s", modid, name);
}

public static CGIdentifier fromMC(ResourceLocation location) {
return new CGIdentifier(location.getNamespace(), location.getPath());
}

public ResourceLocation toMC() {
if (isWildcard()) throw new RuntimeException("Wildcard is not a valid MC ID");
return new ResourceLocation(modid, name);
}

public void writeToBuf(FriendlyByteBuf buf) {
buf.writeUtf(this.toString());
}

public static CGIdentifier readFromBuf(FriendlyByteBuf buf) {
return of(buf.readUtf());
}
}
14 changes: 14 additions & 0 deletions src/main/java/io/github/null2264/cobblegen/data/Modifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.null2264.cobblegen.data;

import java.util.List;

/**
* Class to holds modifier as Map key
*/
public class Modifier {
List<CGIdentifier> modifiers;

public Modifier(List<CGIdentifier> modifiers) {
this.modifiers = modifiers;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package io.github.null2264.cobblegen.data.generator;

import io.github.null2264.cobblegen.config.WeightedBlock;
import io.github.null2264.cobblegen.data.CGIdentifier;
import io.github.null2264.cobblegen.data.model.BlockGenerator;
import io.github.null2264.cobblegen.data.model.Generator;
import io.github.null2264.cobblegen.util.CGLog;
import io.github.null2264.cobblegen.util.GeneratorType;
import io.github.null2264.cobblegen.util.Util;
import lombok.val;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
Expand All @@ -19,25 +18,36 @@
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.Collectors;

public class BasaltGenerator extends BlockGenerator
{
private final Map<String, List<WeightedBlock>> possibleBlocks;
private final Map<CGIdentifier, List<WeightedBlock>> possibleBlocks;
private final Block block;
private final boolean silent;

public BasaltGenerator(List<WeightedBlock> possibleBlocks, Block block, boolean silent) {
this(Map.of(Util.getBlockId(Blocks.SOUL_SOIL).toString(), possibleBlocks), block, silent);
this(Map.of(CGIdentifier.fromMC(Util.getBlockId(Blocks.SOUL_SOIL)), possibleBlocks), block, silent);
}

public BasaltGenerator(Map<String, List<WeightedBlock>> possibleBlocks, Block block, boolean silent) {
public BasaltGenerator(Map<CGIdentifier, List<WeightedBlock>> possibleBlocks, Block block, boolean silent) {
this.possibleBlocks = possibleBlocks;
this.block = block;
this.silent = silent;
}

public static BasaltGenerator fromString(Map<String, List<WeightedBlock>> possibleBlocks, Block block, boolean silent) {
return new BasaltGenerator(
possibleBlocks.entrySet().stream()
.map(e -> Map.entry(CGIdentifier.of(e.getKey()), e.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
block,
silent
);
}

@Override
public @NotNull Map<String, List<WeightedBlock>> getOutput() {
public @NotNull Map<CGIdentifier, List<WeightedBlock>> getOutput() {
return possibleBlocks;
}

Expand Down Expand Up @@ -80,7 +90,7 @@ public void toPacket(FriendlyByteBuf buf) {
val outMap = getOutput();
buf.writeMap(
outMap,
FriendlyByteBuf::writeUtf, (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
(o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
);
}

Expand All @@ -89,8 +99,8 @@ public static Generator fromPacket(FriendlyByteBuf buf) {
val block = Util.getBlock(buf.readResourceLocation());
val silent = buf.readBoolean();

Map<String, List<WeightedBlock>> outMap =
buf.readMap(FriendlyByteBuf::readUtf, (o) -> o.readList(WeightedBlock::fromPacket));
Map<CGIdentifier, List<WeightedBlock>> outMap =
buf.readMap(CGIdentifier::readFromBuf, (o) -> o.readList(WeightedBlock::fromPacket));

return new BasaltGenerator(outMap, block, silent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.null2264.cobblegen.CobbleGen;
import io.github.null2264.cobblegen.config.ConfigMetaData;
import io.github.null2264.cobblegen.config.WeightedBlock;
import io.github.null2264.cobblegen.data.CGIdentifier;
import io.github.null2264.cobblegen.data.model.BlockGenerator;
import io.github.null2264.cobblegen.data.model.Generator;
import io.github.null2264.cobblegen.util.GeneratorType;
Expand All @@ -22,37 +23,50 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

public class CobbleGenerator extends BlockGenerator
{
private final Map<String, List<WeightedBlock>> possibleBlocks;
private final Map<String, List<WeightedBlock>> obsidianReplacements;
private final Map<CGIdentifier, List<WeightedBlock>> possibleBlocks;
private final Map<CGIdentifier, List<WeightedBlock>> obsidianReplacements;
private Fluid fluid;
private final boolean silent;

public CobbleGenerator(List<WeightedBlock> possibleBlocks, Fluid fluid, boolean silent) {
this(possibleBlocks, fluid, silent, Map.of());
}

public CobbleGenerator(List<WeightedBlock> possibleBlocks, Fluid fluid, boolean silent, Map<String, List<WeightedBlock>> obsidianReplacements) {
this(Map.of("*", possibleBlocks), fluid, silent, obsidianReplacements);
public CobbleGenerator(List<WeightedBlock> possibleBlocks, Fluid fluid, boolean silent, Map<CGIdentifier, List<WeightedBlock>> obsidianReplacements) {
this(Map.of(CGIdentifier.wildcard(), possibleBlocks), fluid, silent, obsidianReplacements);
}

public CobbleGenerator(Map<String, List<WeightedBlock>> possibleBlocks, Fluid fluid, boolean silent, Map<String, List<WeightedBlock>> obsidianReplacements) {
public CobbleGenerator(Map<CGIdentifier, List<WeightedBlock>> possibleBlocks, Fluid fluid, boolean silent, Map<CGIdentifier, List<WeightedBlock>> obsidianReplacements) {
this.possibleBlocks = possibleBlocks;
this.obsidianReplacements = obsidianReplacements;
this.fluid = fluid;
this.silent = silent;
}

public static CobbleGenerator fromString(Map<String, List<WeightedBlock>> possibleBlocks, Fluid fluid, boolean silent, Map<String, List<WeightedBlock>> obsidianReplacements) {
return new CobbleGenerator(
possibleBlocks.entrySet().stream()
.map(e -> Map.entry(CGIdentifier.of(e.getKey()), e.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
fluid,
silent,
obsidianReplacements.entrySet().stream()
.map(e -> Map.entry(CGIdentifier.of(e.getKey()), e.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
);
}

@Override
public @NotNull Map<String, List<WeightedBlock>> getOutput() {
public @NotNull Map<CGIdentifier, List<WeightedBlock>> getOutput() {
return possibleBlocks;
}

@Override
public Map<String, List<WeightedBlock>> getObsidianOutput() {
public Map<CGIdentifier, List<WeightedBlock>> getObsidianOutput() {
return obsidianReplacements;
}

Expand Down Expand Up @@ -110,11 +124,11 @@ public void toPacket(FriendlyByteBuf buf) {

buf.writeMap(
getOutput(),
FriendlyByteBuf::writeUtf, (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
(o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
);
buf.writeMap(
getObsidianOutput(),
FriendlyByteBuf::writeUtf, (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
(o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
);
}

Expand All @@ -123,10 +137,10 @@ public static Generator fromPacket(FriendlyByteBuf buf) {
val fluid = Util.getFluid(buf.readResourceLocation());
val silent = buf.readBoolean();

Map<String, List<WeightedBlock>> outMap =
buf.readMap(FriendlyByteBuf::readUtf, (o) -> o.readList(WeightedBlock::fromPacket));
Map<String, List<WeightedBlock>> obiMap =
buf.readMap(FriendlyByteBuf::readUtf, (o) -> o.readList(WeightedBlock::fromPacket));
Map<CGIdentifier, List<WeightedBlock>> outMap =
buf.readMap(CGIdentifier::readFromBuf, (o) -> o.readList(WeightedBlock::fromPacket));
Map<CGIdentifier, List<WeightedBlock>> obiMap =
buf.readMap(CGIdentifier::readFromBuf, (o) -> o.readList(WeightedBlock::fromPacket));

return new CobbleGenerator(outMap, fluid, silent, obiMap);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.null2264.cobblegen.data.generator;

import io.github.null2264.cobblegen.config.WeightedBlock;
import io.github.null2264.cobblegen.data.CGIdentifier;
import io.github.null2264.cobblegen.data.model.BuiltInGenerator;
import io.github.null2264.cobblegen.data.model.Generator;
import io.github.null2264.cobblegen.util.GeneratorType;
Expand All @@ -18,31 +19,42 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.stream.Collectors;

public class StoneGenerator implements BuiltInGenerator
{
private final Map<String, List<WeightedBlock>> possibleBlocks;
private final Map<CGIdentifier, List<WeightedBlock>> possibleBlocks;
private final Fluid fluid;
private final boolean silent;

public StoneGenerator(List<WeightedBlock> possibleBlocks, Fluid fluid, boolean silent) {
this(Map.of("*", possibleBlocks), fluid, silent);
this(Map.of(CGIdentifier.wildcard(), possibleBlocks), fluid, silent);
}

public StoneGenerator(Map<String, List<WeightedBlock>> possibleBlocks, Fluid fluid, boolean silent) {
public StoneGenerator(Map<CGIdentifier, List<WeightedBlock>> possibleBlocks, Fluid fluid, boolean silent) {
this.possibleBlocks = possibleBlocks;
this.fluid = fluid;
this.silent = silent;
}

public static StoneGenerator fromString(Map<String, List<WeightedBlock>> possibleBlocks, Fluid fluid, boolean silent) {
return new StoneGenerator(
possibleBlocks.entrySet().stream()
.map(e -> Map.entry(CGIdentifier.of(e.getKey()), e.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
fluid,
silent
);
}

@Override
public @NotNull Map<String, List<WeightedBlock>> getOutput() {
public @NotNull Map<CGIdentifier, List<WeightedBlock>> getOutput() {
return possibleBlocks;
}

@Override
public Map<String, List<WeightedBlock>> getObsidianOutput() {
return Map.of("*", List.of(WeightedBlock.fromBlock(Blocks.STONE, 100D)));
public Map<CGIdentifier, List<WeightedBlock>> getObsidianOutput() {
return Map.of(CGIdentifier.wildcard(), List.of(WeightedBlock.fromBlock(Blocks.STONE, 100D)));
}

@Override
Expand Down Expand Up @@ -95,7 +107,7 @@ public void toPacket(FriendlyByteBuf buf) {
val outMap = getOutput();
buf.writeMap(
outMap,
FriendlyByteBuf::writeUtf, (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
(o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
);
}

Expand All @@ -104,8 +116,8 @@ public static Generator fromPacket(FriendlyByteBuf buf) {
val fluid = Util.getFluid(buf.readResourceLocation());
val silent = buf.readBoolean();

Map<String, List<WeightedBlock>> outMap =
buf.readMap(FriendlyByteBuf::readUtf, (o) -> o.readList(WeightedBlock::fromPacket));
Map<CGIdentifier, List<WeightedBlock>> outMap =
buf.readMap(CGIdentifier::readFromBuf, (o) -> o.readList(WeightedBlock::fromPacket));

return new StoneGenerator(outMap, fluid, silent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.null2264.cobblegen.data.model;

import io.github.null2264.cobblegen.config.WeightedBlock;
import io.github.null2264.cobblegen.data.CGIdentifier;
import io.github.null2264.cobblegen.util.Util;
import lombok.val;
import net.minecraft.core.BlockPos;
Expand All @@ -20,10 +21,10 @@
public interface BuiltInGenerator extends Generator
{
// https://stackoverflow.com/a/6737362
private String randomizeBlockId(Block key, String dim, Integer yLevel, Map<String, List<WeightedBlock>> candidates) {
private String randomizeBlockId(Block key, String dim, Integer yLevel, Map<CGIdentifier, List<WeightedBlock>> candidates) {
val blockIds = candidates.getOrDefault(
Util.getBlockId(key).toString(),
candidates.getOrDefault("*", List.of())
CGIdentifier.fromMC(Util.getBlockId(key)),
candidates.getOrDefault(CGIdentifier.wildcard(), List.of())
);

ArrayList<WeightedBlock> filteredBlockIds = new ArrayList<>();
Expand Down Expand Up @@ -63,11 +64,11 @@ private String randomizeBlockId(Block key, String dim, Integer yLevel, Map<Strin
return filteredBlockIds.get(idx).id;
}

default Optional<BlockState> getBlockCandidate(LevelAccessor level, BlockPos pos, Map<String, List<WeightedBlock>> candidates) {
default Optional<BlockState> getBlockCandidate(LevelAccessor level, BlockPos pos, Map<CGIdentifier, List<WeightedBlock>> candidates) {
return getBlockCandidate(level, pos, candidates, null);
}

default Optional<BlockState> getBlockCandidate(LevelAccessor level, BlockPos pos, Map<String, List<WeightedBlock>> candidates, Block defaultBlock) {
default Optional<BlockState> getBlockCandidate(LevelAccessor level, BlockPos pos, Map<CGIdentifier, List<WeightedBlock>> candidates, Block defaultBlock) {
val replacementId = randomizeBlockId(
level.getBlockState(pos.below()).getBlock(),
Util.getDimension(level),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.null2264.cobblegen.data.model;

import io.github.null2264.cobblegen.config.WeightedBlock;
import io.github.null2264.cobblegen.data.CGIdentifier;
import io.github.null2264.cobblegen.util.CGLog;
import io.github.null2264.cobblegen.util.GeneratorType;
import lombok.val;
Expand Down Expand Up @@ -52,12 +53,12 @@ default Optional<BlockState> tryGenerate(LevelAccessor level, BlockPos pos, Flui
}

@NotNull
Map<String, List<WeightedBlock>> getOutput();
Map<CGIdentifier, List<WeightedBlock>> getOutput();

/**
* @return The output block when a source fluid met another fluid (e.g. Water -> Stone / Lava -> Obsidian)
*/
default Map<String, List<WeightedBlock>> getObsidianOutput() {
default Map<CGIdentifier, List<WeightedBlock>> getObsidianOutput() {
return Map.of();
}

Expand Down
Loading

0 comments on commit 9016a6e

Please sign in to comment.