Skip to content

Commit

Permalink
add custom BlockPlacer
Browse files Browse the repository at this point in the history
  • Loading branch information
ToCraft committed Oct 24, 2024
1 parent 99e0a74 commit 8845eff
Show file tree
Hide file tree
Showing 28 changed files with 283 additions and 122 deletions.
64 changes: 51 additions & 13 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,89 @@
# Crafted License

Version 1.0, 18 September 2024
Copyright (c) To_Craft Development, https://tocraft.dev

This document explains the conditions, terms, and permissions of the licensed project applying to the licensee.

## 1. Definitions

This section defines the used definitions in this license.

### a) project
"Projects" are references in terms of the result of planned tasks to accomplish a goal. Projects are also publicly available with or without their source.

"Projects" are references in terms of the result of planned tasks to accomplish a goal. Projects are also publicly
available with or without their source.

### b) library
This license defines a "library" as an interface that does not provide any end-user features. In specific, a library cannot be used without writing additional code directly or indirectly interacting with the licensed files.

This license defines a "library" as an interface that does not provide any end-user features. In specific, a library
cannot be used without writing additional code directly or indirectly interacting with the licensed files.

### c) application
"Applications" represent a licensed project that, in contrast to libraries, actively provides end-user features and does not have to be used in other projects with or without additional code in order to fulfill their purpose.

"Applications" represent a licensed project that, in contrast to libraries, actively provides end-user features and does
not have to be used in other projects with or without additional code in order to fulfill their purpose.

### d) accessibility
"Accessibility" is used to describe that the end-user must be able to find the accessible object when downloading, viewing, or using the work of the licensee without more than 3 mouse clicks, in the case of a digital project, or by directly being linked to it in the project description. In any way, the licensee must ensure the end-user does not have to pay any amount of money or leak any personal information to reach the project source.
In most cases, this is guaranteed by linking the accessible object in the project meta, the project description, and the project source.

"Accessibility" is used to describe that the end-user must be able to find the accessible object when downloading,
viewing, or using the work of the licensee without more than 3 mouse clicks, in the case of a digital project, or by
directly being linked to it in the project description. In any way, the licensee must ensure the end-user does not have
to pay any amount of money or leak any personal information to reach the project source.
In most cases, this is guaranteed by linking the accessible object in the project meta, the project description, and the
project source.

## 2. Warranty and Liability
With this license, the author of the licensed project clarifies they will not provide any kind of warranty or liability for the licensed project. Any misuse is the liability of the actor and not the author of this project. By declaring this, the author also agrees the licensed project is not intended for any kind of illegal or unethical usage.

With this license, the author of the licensed project clarifies they will not provide any kind of warranty or liability
for the licensed project. Any misuse is the liability of the actor and not the author of this project. By declaring
this, the author also agrees the licensed project is not intended for any kind of illegal or unethical usage.

## 3. Validity
This license only applies to the states of the project containing this license. Any version not containing the same Crafted License is subject to the corresponding license. However, this license must not be provided for each file or part of the project. A non-misleading file name in the directory of the project or including this license in the project description automatically licenses every file and/or part of the project with this license if this file or part isn't already licensed with another license or other version of Crafted License.

This license only applies to the states of the project containing this license. Any version not containing the same
Crafted License is subject to the corresponding license. However, this license must not be provided for each file or
part of the project. A non-misleading file name in the directory of the project or including this license in the project
description automatically licenses every file and/or part of the project with this license if this file or part isn't
already licensed with another license or other version of Crafted License.

## 4. Private Use
If the licensee does not publish their work using the licensed project or any modification of it, nor shares it with more than 15 personally known people without any commercial interest, the licensee is free to use the licensed project in their work without applying the restrictions as defined in Section 5.

If the licensee does not publish their work using the licensed project or any modification of it, nor shares it with
more than 15 personally known people without any commercial interest, the licensee is free to use the licensed project
in their work without applying the restrictions as defined in Section 5.

## 5. Public Use

Public use is any kind of use that cannot be counted as private use as defined in Section 4.
Public commercial use is not allowed without the approval of the author of the licensed project. Commercial use hereby means usage explicitly for earning any form of money. Sponsors or advertisements do not count as commercial use if the end-user is still able to use the work of the licensee without having to evade or attend to sponsoring or advertising.
Public commercial use is not allowed without the approval of the author of the licensed project. Commercial use hereby
means usage explicitly for earning any form of money. Sponsors or advertisements do not count as commercial use if the
end-user is still able to use the work of the licensee without having to evade or attend to sponsoring or advertising.
The licensee must provide their source files, or in the case of coded projects, their source code.
This source files must be accessibly linked.

### a) Forking, Modifying, and Distributing

Every condition as specified in Section 5 applies.
Any work not only using the licensed project but forking and/or modifying it must contain the exact same license as the original project and must accessibly link the original project and its license.
Any work not only using the licensed project but forking and/or modifying it must contain the exact same license as the
original project and must accessibly link the original project and its license.

### b) Using Libraries

Every condition as specified in Section 5 applies.
If the licensed project is a library, the licensee does not have to link the library's license to their work as long as they only use it without publishing any parts of the licensed project in any form except the releases of the licensee's work. To ensure this condition in the case of Gradle projects, it's recommended to use Maven Artifacts instead of uploading any jar library of the licensed project.
If the licensed project is a library, the licensee does not have to link the library's license to their work as long as
they only use it without publishing any parts of the licensed project in any form except the releases of the licensee's
work. To ensure this condition in the case of Gradle projects, it's recommended to use Maven Artifacts instead of
uploading any jar library of the licensed project.

### c) Other usage

Every condition as specified in Section 5 applies.
When using a licensed application or other licensed project that cannot be described as a library, the licensee must accessibly link an official source to the source licensed project and its license, if the license is not already explicitly linked in the official source.
When using a licensed application or other licensed project that cannot be described as a library, the licensee must
accessibly link an official source to the source licensed project and its license, if the license is not already
explicitly linked in the official source.

## 6. Additions
The author reserves themselves the right to explicitly lower conditions for a licensee but does not have to name any reason not to do so. However, they cannot limit the permissions guaranteed in this license.

The author reserves themselves the right to explicitly lower conditions for a licensee but does not have to name any
reason not to do so. However, they cannot limit the permissions guaranteed in this license.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# Crafted Terrain Generation Mod

*Crafted Terrain Generation Mod* (CTGen) allows you to generate entire Minecraft worlds with just one map image!
It's fully data-driven, so you can configure it entirely via datapacks- no need for java code, even though it's still possible if you prefer it.
It's fully data-driven, so you can configure it entirely via data packs - no need for java code, even though it's still
possible if you prefer it.
For an example implementation, you can take a look at [A Storm of Sorts](https://github.com/Deathslayor/AStormOfSwords).

### License
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.tocraft.crafted.ctgen;

import dev.tocraft.crafted.ctgen.blockplacer.BlockPlacer;
import dev.tocraft.crafted.ctgen.zone.Zone;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.tocraft.crafted.ctgen.blockplacer;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.crafted.ctgen.CTerrainGeneration;
import dev.tocraft.crafted.ctgen.zone.Codecs;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.NotNull;

public class BasicPlacer extends BlockPlacer {
@NotNull
private final Block value;

public BasicPlacer(@NotNull Block value) {
this.value = value;
}

public Block get(SimplexNoise noise, double x, double y, double z, String layer) {
return value;
}

@Override
public Block get(SimplexNoise noise, double x, double z, String layer) {
return value;
}

public @NotNull Block getValue() {
return value;
}

public static final Codec<BasicPlacer> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codecs.BLOCK.fieldOf("value").forGetter(o -> o.value)
).apply(instance, instance.stable(BasicPlacer::new)));

public static final ResourceLocation ID = CTerrainGeneration.id("basic");

@Override
public Codec<BasicPlacer> codec() {
return CODEC;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
package dev.tocraft.crafted.ctgen.blockplacer;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import dev.tocraft.crafted.ctgen.CTerrainGeneration;
import dev.tocraft.crafted.ctgen.impl.services.ServerPlatform;
import dev.tocraft.crafted.ctgen.util.CTRegistries;
import dev.tocraft.crafted.ctgen.zone.Codecs;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;

import java.util.Optional;
import java.util.function.Function;

public abstract class BlockPlacer {
public static final ResourceKey<Registry<Codec<? extends BlockPlacer>>> BLOCK_PLAYER_REGISTRY_KEY = ResourceKey.createRegistryKey(CTerrainGeneration.id("block_placer"));
public static final Registry<Codec<? extends BlockPlacer>> BLOCK_PLACER_REGISTRY = ServerPlatform.INSTANCE.simpleRegistry(BLOCK_PLAYER_REGISTRY_KEY);
public static final Codec<BlockPlacer> DIRECT_CODEC = CTRegistries.BLOCK_PLACER.byNameCodec().dispatchStable(BlockPlacer::codec, Function.identity());
public static final Codec<BlockPlacer> CODEC = Codec.either(Codecs.BLOCK, DIRECT_CODEC).xmap(either -> {
Optional<Block> left = either.left();
if (left.isPresent()) {
return new BasicPlacer(left.get());
} else {
return either.right().orElseThrow();
}
}, placer -> {
if (placer instanceof BasicPlacer basic) {
return Either.left(basic.getValue());
} else {
return Either.right(placer);
}
});

public static final Codec<BlockPlacer> CODEC = BLOCK_PLACER_REGISTRY.byNameCodec().dispatchStable(BlockPlacer::codec, Function.identity());
public static void register() {
Registry.register(CTRegistries.BLOCK_PLACER, BasicPlacer.ID, BasicPlacer.CODEC);
Registry.register(CTRegistries.BLOCK_PLACER, NoisePlacer.ID, NoisePlacer.CODEC);
}

public abstract Block get(SimplexNoise noise, double x, double y, double z);
public abstract Block get(SimplexNoise noise, double x, double y, double z, String layer);

public abstract Block get(SimplexNoise noise, double x, double z);
public abstract Block get(SimplexNoise noise, double x, double z, String layer);

protected abstract Codec<? extends BlockPlacer> codec();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package dev.tocraft.crafted.ctgen.blockplacer;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.crafted.ctgen.CTerrainGeneration;
import dev.tocraft.crafted.ctgen.util.Noise;
import dev.tocraft.crafted.ctgen.zone.Codecs;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.Contract;
Expand All @@ -22,21 +23,12 @@ public class NoisePlacer extends BlockPlacer {
@NotNull
private final Block defaultValue;

private NoisePlacer(Block value) {
this(null, new HashMap<>(), value);
}

private NoisePlacer(@Nullable Noise noise, @NotNull Map<Double,Block> thresholdMap, @NotNull Block defaultValue) {
private NoisePlacer(@Nullable Noise noise, @NotNull Map<Double, Block> thresholdMap, @NotNull Block defaultValue) {
this.noise = noise;
this.thresholdMap = thresholdMap;
this.defaultValue = defaultValue;
}

@Contract("_ -> new")
public static @NotNull NoisePlacer of(@NotNull Block value) {
return new NoisePlacer(value);
}

@Contract(value = "_, _, _ -> new", pure = true)
public static @NotNull NoisePlacer of(Noise noise, Map<Double, Block> thresholdMap, Block value) {
return new NoisePlacer(noise, thresholdMap, value);
Expand All @@ -46,12 +38,12 @@ public boolean hasNoise() {
return noise != null && !thresholdMap.isEmpty();
}

public Block get(SimplexNoise noise, double x, double y, double z) {
public Block get(SimplexNoise noise, double x, double y, double z, String layer) {
double perlin;

if (this.noise != null && hasNoise()) {
perlin = this.noise.getPerlin(noise, x, y, z);
} else {
} else {
perlin = 0;
}

Expand All @@ -68,12 +60,12 @@ public Block get(SimplexNoise noise, double x, double y, double z) {
}

@Override
public Block get(SimplexNoise noise, double x, double z) {
public Block get(SimplexNoise noise, double x, double z, String layer) {
double perlin;

if (this.noise != null && hasNoise()) {
perlin = this.noise.getPerlin(noise, x, z);
} else {
} else {
perlin = 0;
}

Expand Down Expand Up @@ -106,14 +98,13 @@ public Block get(SimplexNoise noise, double x, double z) {
}
);

public static final Codec<NoisePlacer> DIRECT_CODEC = RecordCodecBuilder.create(instance -> instance.group(
public static final Codec<NoisePlacer> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Noise.CODEC.optionalFieldOf("noise", Noise.DEFAULT).forGetter(o -> o.noise),
VALUE_MAP_CODEC.optionalFieldOf("values", new HashMap<>()).forGetter(o -> o.thresholdMap),
VALUE_MAP_CODEC.fieldOf("values").forGetter(o -> o.thresholdMap),
Codecs.BLOCK.fieldOf("default").forGetter(o -> o.defaultValue)
).apply(instance, instance.stable(NoisePlacer::new)));

public static final Codec<NoisePlacer> CODEC = Codec.either(Codecs.BLOCK, DIRECT_CODEC)
.xmap(either -> either.right().orElseGet(() -> of(either.left().orElseThrow())), selector -> selector.hasNoise() ? Either.right(selector) : Either.left(selector.defaultValue));
public static final ResourceLocation ID = CTerrainGeneration.id("noise");

@Override
public Codec<NoisePlacer> codec() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public void encode(@NotNull FriendlyByteBuf buf) {
int mapWidth = buf.readInt();
int mapHeight = buf.readInt();
return new SyncMapPacket(mapId, xOffset, yOffset, mapWidth, mapHeight);
}
else {
} else {
return empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,37 @@ public double getRatio() {
return ratio;
}

public void resetTextureOffsets() {
// calculate pixel pos for the player
double pixelX;
double pixelY;
if (minecraft.player != null) {
BlockPos blockPos = minecraft.player.blockPosition();
pixelX = (blockPos.getX() >> 2) + pixelOffsetX;
pixelY = (blockPos.getZ() >> 2) + pixelOffsetY;
} else {
pixelX = pixelOffsetX;
pixelY = pixelOffsetY;
}

// calculate offset for pixel pos
int tX = (int) (pixelX / mapWidth * zoomedWidth + getTextureX());
int tY = (int) (pixelY / mapHeight * zoomedHeight + getTextureY());

setTextureOffsetX(tX);
setTextureOffsetY(tY);
}

public void setTextureOffsetX(double textureOffsetX) {
this.textureOffsetX = textureOffsetX;
updateZoomedWidth();
}

public void setTextureOffsetY(double textureOffsetY) {
this.textureOffsetY = textureOffsetY;
updateZoomedHeight();
}

/**
* @return the virtual y point, where the map texture starts
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;

import java.util.function.Consumer;

@Environment(EnvType.CLIENT)
public interface ClientPlatform extends PlatformService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.tocraft.crafted.ctgen.impl.services;

import dev.tocraft.crafted.ctgen.impl.network.SyncMapPacket;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.tocraft.crafted.ctgen.layer;

import com.mojang.serialization.Codec;
import dev.tocraft.crafted.ctgen.util.CTRegistries;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;

import java.util.function.Function;

public abstract class BlockLayer {
public static final Codec<BlockLayer> CODEC = CTRegistries.BLOCK_LAYER.byNameCodec().dispatchStable(BlockLayer::codec, Function.identity());

protected final String name;

public BlockLayer(String name) {
this.name = name;
}

public abstract boolean is(SimplexNoise noise, double x, double y, double z);

public String getName() {
return this.name;
}

;

protected abstract Codec<? extends BlockLayer> codec();
}
Loading

0 comments on commit 8845eff

Please sign in to comment.