Skip to content

Commit b4362b8

Browse files
[Chore] Replace enum with code generator for fluids (#27)
* Move Fluids to CodeGenerator * [Chore] Replace enum with registry * Refactor: Move 'Fluids' interface from FluidImpl implements to Fluid extends --------- Co-authored-by: OneLiteFeather <seelenretterin@onelitefeather.net>
1 parent ed2211d commit b4362b8

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.minestom.server.fluid;
2+
3+
/**
4+
* Code autogenerated, do not edit!
5+
*/
6+
@SuppressWarnings("unused")
7+
interface Fluids {
8+
Fluid EMPTY = FluidImpl.get("minecraft:empty");
9+
10+
Fluid FLOWING_WATER = FluidImpl.get("minecraft:flowing_water");
11+
12+
Fluid WATER = FluidImpl.get("minecraft:water");
13+
14+
Fluid FLOWING_LAVA = FluidImpl.get("minecraft:flowing_lava");
15+
16+
Fluid LAVA = FluidImpl.get("minecraft:lava");
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.minestom.server.fluid;
2+
3+
import net.minestom.server.registry.Registry;
4+
import net.minestom.server.registry.StaticProtocolObject;
5+
import net.minestom.server.utils.NamespaceID;
6+
import org.jetbrains.annotations.Contract;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import java.util.Collection;
11+
12+
public sealed interface Fluid extends StaticProtocolObject, Fluids permits FluidImpl {
13+
/**
14+
* Returns the entity registry.
15+
*
16+
* @return the entity registry or null if it was created with a builder
17+
*/
18+
@Contract(pure = true)
19+
@Nullable
20+
Registry.FluidEntry registry();
21+
22+
@Override
23+
@NotNull
24+
NamespaceID namespace();
25+
26+
static @NotNull Collection<@NotNull Fluid> values() {
27+
return FluidImpl.values();
28+
}
29+
30+
static @Nullable Fluid fromNamespaceId(@NotNull String namespaceID) {
31+
return FluidImpl.getSafe(namespaceID);
32+
}
33+
34+
static @Nullable Fluid fromNamespaceId(@NotNull NamespaceID namespaceID) {
35+
return fromNamespaceId(namespaceID.asString());
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.minestom.server.fluid;
2+
3+
import net.minestom.server.registry.Registry;
4+
import net.minestom.server.utils.NamespaceID;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.util.Collection;
8+
import java.util.concurrent.atomic.AtomicInteger;
9+
10+
public record FluidImpl(Registry.FluidEntry registry, NamespaceID namespace, int id) implements Fluid {
11+
12+
private static final Registry.DynamicContainer<Fluid> CONTAINER = Registry.createDynamicContainer(Registry.Resource.FLUIDS, FluidImpl::createImpl);
13+
private static final AtomicInteger INDEX = new AtomicInteger();
14+
15+
private static FluidImpl createImpl(String namespace, Registry.Properties properties) {
16+
return new FluidImpl(Registry.fluidEntry(namespace, properties));
17+
}
18+
19+
private FluidImpl(Registry.FluidEntry registry) {
20+
this(registry, registry.namespace(), INDEX.getAndIncrement());
21+
}
22+
23+
static Collection<Fluid> values() {
24+
return CONTAINER.values();
25+
}
26+
27+
public static Fluid get(@NotNull String namespace) {
28+
return CONTAINER.get(namespace);
29+
}
30+
31+
static Fluid getSafe(@NotNull String namespace) {
32+
return CONTAINER.getSafe(namespace);
33+
}
34+
}

0 commit comments

Comments
 (0)