Skip to content

Commit 18d4db2

Browse files
committed
feat(visual/elements): start working on this
1 parent e0e690c commit 18d4db2

6 files changed

Lines changed: 273 additions & 0 deletions

File tree

main/src/main/java/me/outspending/biomesapi/renderer/ParticleRenderer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public record ParticleRenderer(@NotNull Map<@NotNull AmbientParticle, @NotNull F
7171
return new ParticleRenderer(Map.of(AmbientParticle.CLOUD, 0.008F));
7272
}
7373

74+
public List<net.minecraft.world.attribute.AmbientParticle> getDelegateParticles() {
75+
return ambientParticles.entrySet().stream()
76+
.map(entry -> new net.minecraft.world.attribute.AmbientParticle(
77+
entry.getKey().getSimpleParticle(),
78+
entry.getValue()
79+
))
80+
.toList();
81+
}
82+
7483

7584
@Override
7685
public boolean equals(Object obj) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.outspending.biomesapi.wrapper.newa;
2+
3+
import net.minecraft.world.attribute.EnvironmentAttribute;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
public interface AbstractWrappedEnvironmentAttribute<T> {
7+
8+
EnvironmentAttribute<@NotNull T> getAttribute();
9+
10+
@NotNull T getValue();
11+
12+
void setValue(@NotNull Object value);
13+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package me.outspending.biomesapi.wrapper.newa;
2+
3+
import com.mojang.datafixers.util.Pair;
4+
import me.outspending.biomesapi.wrapper.WrappedEnvironmentAttribute;
5+
import net.minecraft.world.attribute.EnvironmentAttribute;
6+
import net.minecraft.world.attribute.EnvironmentAttributeMap;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class EnvironmentAttributeMapTransformer {
12+
13+
14+
private final AbstractWrappedEnvironmentAttribute<?>[] attributes;
15+
16+
public EnvironmentAttributeMapTransformer(AbstractWrappedEnvironmentAttribute<?>... attributes) {
17+
this.attributes = attributes;
18+
}
19+
20+
public EnvironmentAttributeMap transform() {
21+
EnvironmentAttributeMap.Builder builder = EnvironmentAttributeMap.builder();
22+
for (AbstractWrappedEnvironmentAttribute<?> wrappedAttribute : attributes) {
23+
addToBuilder(builder, wrappedAttribute);
24+
}
25+
return builder.build();
26+
}
27+
28+
private <T> void addToBuilder(EnvironmentAttributeMap.Builder builder, AbstractWrappedEnvironmentAttribute<T> wrappedAttribute) {
29+
builder.set(wrappedAttribute.getAttribute(), wrappedAttribute.getValue());
30+
}
31+
32+
33+
public static Builder builder() {
34+
return new Builder();
35+
}
36+
37+
public static class Builder {
38+
39+
private final List<AbstractWrappedEnvironmentAttribute<?>> attributes = new ArrayList<>();
40+
41+
public Builder addWrappedAttribute(AbstractWrappedEnvironmentAttribute<?> attribute) {
42+
attributes.add(attribute);
43+
return this;
44+
}
45+
46+
public <T> Builder addExposedAttribute(WrappedEnvironmentAttributes constant, T value) {
47+
AbstractWrappedEnvironmentAttribute<T> wrappedEnvironmentAttribute = (AbstractWrappedEnvironmentAttribute<T>) constant.getWrappedAttributeSupplier();
48+
wrappedEnvironmentAttribute.setValue(value);
49+
attributes.add(wrappedEnvironmentAttribute);
50+
return this;
51+
}
52+
53+
public <T, K> Builder addHiddenAttribute(WrappedEnvironmentAttributes constant, K exposedValue) {
54+
AbstractWrappedEnvironmentAttribute<T> wrappedEnvironmentAttribute = (AbstractWrappedEnvironmentAttribute<T>) constant.getWrappedAttributeSupplier();
55+
wrappedEnvironmentAttribute.setValue(exposedValue);
56+
attributes.add(wrappedEnvironmentAttribute);
57+
return this;
58+
}
59+
60+
public <V> Builder add(WrappedEnvironmentAttributes constant, V exposedValue) {
61+
AbstractWrappedEnvironmentAttribute<?> wrappedEnvironmentAttribute = constant.getWrappedAttributeSupplier();
62+
wrappedEnvironmentAttribute.setValue(exposedValue);
63+
attributes.add(wrappedEnvironmentAttribute);
64+
return this;
65+
}
66+
67+
public EnvironmentAttributeMapTransformer build() {
68+
return new EnvironmentAttributeMapTransformer(attributes.toArray(new AbstractWrappedEnvironmentAttribute[0]));
69+
}
70+
}
71+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package me.outspending.biomesapi.wrapper.newa;
2+
3+
import net.minecraft.world.attribute.EnvironmentAttribute;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
public class ExposedEnvironmentAttribute<T> implements AbstractWrappedEnvironmentAttribute<T> {
7+
8+
private final EnvironmentAttribute<@NotNull T> delegateAttribute;
9+
private T value;
10+
11+
public ExposedEnvironmentAttribute(EnvironmentAttribute<@NotNull T> delegateAttribute, @NotNull T value) {
12+
this.delegateAttribute = delegateAttribute;
13+
this.value = value;
14+
}
15+
16+
public ExposedEnvironmentAttribute(EnvironmentAttribute<@NotNull T> delegateAttribute) {
17+
this.delegateAttribute = delegateAttribute;
18+
this.value = delegateAttribute.defaultValue();
19+
}
20+
21+
@Override
22+
public EnvironmentAttribute<@NotNull T> getAttribute() {
23+
return delegateAttribute;
24+
}
25+
26+
@Override
27+
public @NotNull T getValue() {
28+
return value;
29+
}
30+
31+
@Override
32+
public void setValue(@NotNull Object value) {
33+
this.value = (T) value;
34+
}
35+
36+
public static <T> ExposedEnvironmentAttribute<T> of(EnvironmentAttribute<@NotNull T> attribute, @NotNull T value) {
37+
return new ExposedEnvironmentAttribute<>(attribute, value);
38+
}
39+
40+
public static <T> ExposedEnvironmentAttribute<T> of(EnvironmentAttribute<@NotNull T> attribute) {
41+
return new ExposedEnvironmentAttribute<>(attribute);
42+
}
43+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package me.outspending.biomesapi.wrapper.newa;
2+
3+
import com.google.common.base.Preconditions;
4+
import net.minecraft.world.attribute.EnvironmentAttribute;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
public class HiddenEnvironmentAttribute<T, K> implements AbstractWrappedEnvironmentAttribute<T> {
9+
10+
private final EnvironmentAttribute<T> exposedAttribute;
11+
private final Converter<T, K> converter;
12+
private K exposedValue;
13+
14+
15+
public HiddenEnvironmentAttribute(EnvironmentAttribute<@NotNull T> exposedAttribute, @NotNull Converter<T, K> converter, @Nullable K exposedValue) {
16+
this.exposedAttribute = exposedAttribute;
17+
this.converter = converter;
18+
this.exposedValue = exposedValue;
19+
}
20+
21+
22+
public EnvironmentAttribute<@NotNull T> getAttribute() {
23+
return exposedAttribute;
24+
}
25+
26+
@Override
27+
public @NotNull T getValue() {
28+
return getConvertedValue();
29+
}
30+
31+
@Override
32+
public void setValue(@NotNull Object value) {
33+
this.exposedValue = (K) value;
34+
}
35+
36+
public K getExposedValue() {
37+
return exposedValue;
38+
}
39+
40+
public void setExposedValue(@NotNull K exposedValue) {
41+
this.exposedValue = exposedValue;
42+
}
43+
44+
45+
public T getConvertedValue() {
46+
Preconditions.checkNotNull(exposedValue, "exposedValue must be set before conversion");
47+
Preconditions.checkNotNull(converter, "converter cannot be null");
48+
return converter.convert(exposedValue);
49+
}
50+
51+
52+
public static <T, K> HiddenEnvironmentAttribute<T, K> of(EnvironmentAttribute<@NotNull T> attribute, @NotNull Converter<T, K> converter, @NotNull K exposedValue) {
53+
return new HiddenEnvironmentAttribute<>(attribute, converter, exposedValue);
54+
}
55+
56+
57+
/**
58+
* Converts K to T.
59+
* @param <T> The type of the EnvironmentAttribute.
60+
* @param <K> The type of the exposed value.
61+
*/
62+
public interface Converter<T, K> {
63+
T convert(K value);
64+
}
65+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package me.outspending.biomesapi.wrapper.newa;
2+
3+
import me.outspending.biomesapi.renderer.ParticleRenderer;
4+
import me.outspending.biomesapi.wrapper.AmbientParticle;
5+
import me.outspending.biomesapi.wrapper.MoonPhase;
6+
import net.minecraft.world.attribute.AttributeType;
7+
import net.minecraft.world.attribute.EnvironmentAttribute;
8+
import net.minecraft.world.attribute.EnvironmentAttributes;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import java.util.function.Supplier;
12+
13+
public enum WrappedEnvironmentAttributes {
14+
15+
FOG_COLOR(EnvironmentAttributes.FOG_COLOR),
16+
FOG_START_DISTANCE(EnvironmentAttributes.FOG_START_DISTANCE),
17+
FOG_END_DISTANCE(EnvironmentAttributes.FOG_END_DISTANCE),
18+
SKY_FOG_END_DISTANCE(EnvironmentAttributes.SKY_FOG_END_DISTANCE),
19+
CLOUD_FOG_END_DISTANCE(EnvironmentAttributes.CLOUD_FOG_END_DISTANCE),
20+
WATER_FOG_COLOR(EnvironmentAttributes.WATER_FOG_COLOR),
21+
WATER_FOG_START_DISTANCE(EnvironmentAttributes.WATER_FOG_START_DISTANCE),
22+
WATER_FOG_END_DISTANCE(EnvironmentAttributes.WATER_FOG_END_DISTANCE),
23+
SKY_COLOR(EnvironmentAttributes.SKY_COLOR),
24+
SUNRISE_SUNSET_COLOR(EnvironmentAttributes.SUNRISE_SUNSET_COLOR),
25+
CLOUD_COLOR(EnvironmentAttributes.CLOUD_COLOR),
26+
CLOUD_HEIGHT(EnvironmentAttributes.CLOUD_HEIGHT),
27+
SUN_ANGLE(EnvironmentAttributes.SUN_ANGLE),
28+
MOON_ANGLE(EnvironmentAttributes.MOON_ANGLE),
29+
STAR_ANGLE(EnvironmentAttributes.STAR_ANGLE),
30+
MOON_PHASE(EnvironmentAttributes.MOON_PHASE, MoonPhase.class, MoonPhase::getDelegatePhase),
31+
STAR_BRIGHTNESS(EnvironmentAttributes.STAR_BRIGHTNESS),
32+
SKY_LIGHT_COLOR(EnvironmentAttributes.SKY_LIGHT_COLOR),
33+
SKY_LIGHT_FACTOR(EnvironmentAttributes.SKY_LIGHT_FACTOR),
34+
DEFAULT_DRIPSTONE_PARTICLE(EnvironmentAttributes.DEFAULT_DRIPSTONE_PARTICLE, AmbientParticle.class, AmbientParticle::getSimpleParticle),
35+
AMBIENT_PARTICLES(EnvironmentAttributes.AMBIENT_PARTICLES, ParticleRenderer.class, ParticleRenderer::getDelegateParticles),
36+
;
37+
38+
private final Supplier<AbstractWrappedEnvironmentAttribute<?>> wrappedAttributeSupplier;
39+
private final Class<?> delegateAttributeType;
40+
private final Class<?> exposedAttributeType;
41+
42+
<T> WrappedEnvironmentAttributes(EnvironmentAttribute<@NotNull T> attribute) {
43+
this.wrappedAttributeSupplier = () -> new ExposedEnvironmentAttribute<>(attribute);
44+
this.delegateAttributeType = attribute.defaultValue().getClass();
45+
this.exposedAttributeType = delegateAttributeType;
46+
}
47+
48+
<T, K> WrappedEnvironmentAttributes(EnvironmentAttribute<@NotNull T> attribute, Class<K> exposedAttributeType, HiddenEnvironmentAttribute.Converter<T, K> converter) {
49+
this.wrappedAttributeSupplier = () -> new HiddenEnvironmentAttribute<>(attribute, converter, null);
50+
this.delegateAttributeType = attribute.defaultValue().getClass();
51+
this.exposedAttributeType = exposedAttributeType;
52+
}
53+
54+
55+
public AbstractWrappedEnvironmentAttribute<?> getWrappedAttributeSupplier() {
56+
return wrappedAttributeSupplier.get();
57+
}
58+
59+
public Class<?> getDelegateAttributeType() {
60+
return delegateAttributeType;
61+
}
62+
63+
public Class<?> getExposedAttributeType() {
64+
return exposedAttributeType;
65+
}
66+
67+
68+
public <T> Class<T> getDelegateAttributeTypeAs(Class<T> clazz) {
69+
return (Class<T>) delegateAttributeType;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)