3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-19 22:40:18 +01:00

Clean up custom block api a bit

Dieser Commit ist enthalten in:
davchoo 2022-07-10 12:26:52 -04:00
Ursprung 08c24cb462
Commit 86c822631c
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: A0168C8E45799B7D
8 geänderte Dateien mit 35 neuen und 45 gelöschten Zeilen

Datei anzeigen

@ -53,13 +53,12 @@ public interface CustomBlockData {
Builder components(@NonNull CustomBlockComponents components); Builder components(@NonNull CustomBlockComponents components);
Builder booleanProperty(@NonNull String propertyName, List<Boolean> values); Builder booleanProperty(@NonNull String propertyName);
Builder intProperty(@NonNull String propertyName, List<Integer> values); Builder intProperty(@NonNull String propertyName, List<Integer> values);
Builder stringProperty(@NonNull String propertyName, List<String> values); Builder stringProperty(@NonNull String propertyName, List<String> values);
Builder permutations(@NonNull List<CustomBlockPermutation> permutations); Builder permutations(@NonNull List<CustomBlockPermutation> permutations);
CustomBlockData build(); CustomBlockData build();

Datei anzeigen

@ -30,6 +30,8 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Map; import java.util.Map;
public interface CustomBlockState { public interface CustomBlockState {
@NonNull CustomBlockData block();
@NonNull String name(); @NonNull String name();
@NonNull <T> T property(String propertyName); @NonNull <T> T property(String propertyName);

Datei anzeigen

@ -34,5 +34,5 @@ public interface CustomBlockProperty<T> {
@NonNull List<T> values(); @NonNull List<T> values();
@NonNull PropertyType<T> type(); @NonNull PropertyType type();
} }

Datei anzeigen

@ -25,18 +25,17 @@
package org.geysermc.geyser.api.block.custom.property; package org.geysermc.geyser.api.block.custom.property;
public class PropertyType<T> { import lombok.Getter;
public static final PropertyType<Boolean> BOOLEAN = new PropertyType<>(Boolean.class);
public static final PropertyType<Integer> INTEGER = new PropertyType<>(Integer.class);
public static final PropertyType<String> STRING = new PropertyType<>(String.class);
public class PropertyType {
public static final PropertyType BOOLEAN = new PropertyType(Boolean.class);
public static final PropertyType INTEGER = new PropertyType(Integer.class);
public static final PropertyType STRING = new PropertyType(String.class);
@Getter
private final Class<?> typeClass; private final Class<?> typeClass;
private PropertyType(Class<?> typeClass) { private PropertyType(Class<?> typeClass) {
this.typeClass = typeClass; this.typeClass = typeClass;
} }
public Class<?> getTypeClass() {
return typeClass;
}
} }

Datei anzeigen

@ -28,6 +28,8 @@ package org.geysermc.geyser.level.block;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps; import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectLists; import it.unimi.dsi.fastutil.objects.ObjectLists;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Value; import lombok.Value;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.block.custom.CustomBlockData; import org.geysermc.geyser.api.block.custom.CustomBlockData;
@ -41,7 +43,11 @@ import java.util.List;
import java.util.Map; import java.util.Map;
@Value @Value
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class GeyserCustomBlockData implements CustomBlockData { public class GeyserCustomBlockData implements CustomBlockData {
public static final String GEYSER_NAMESPACE = "geyser:";
String name; String name;
CustomBlockComponents components; CustomBlockComponents components;
Map<String, CustomBlockProperty<?>> properties; Map<String, CustomBlockProperty<?>> properties;
@ -54,7 +60,7 @@ public class GeyserCustomBlockData implements CustomBlockData {
@Override @Override
public @NonNull String identifier() { public @NonNull String identifier() {
return "geyser:" + name; return GEYSER_NAMESPACE + name;
} }
@Override @Override
@ -96,8 +102,8 @@ public class GeyserCustomBlockData implements CustomBlockData {
} }
@Override @Override
public Builder booleanProperty(@NonNull String propertyName, List<Boolean> values) { public Builder booleanProperty(@NonNull String propertyName) {
this.properties.put(propertyName, new GeyserCustomBlockProperty<>(propertyName, values, PropertyType.BOOLEAN)); this.properties.put(propertyName, new GeyserCustomBlockProperty<>(propertyName, List.of(false, true), PropertyType.BOOLEAN));
return this; return this;
} }
@ -131,6 +137,9 @@ public class GeyserCustomBlockData implements CustomBlockData {
if (property.values().isEmpty() || property.values().size() > 16) { if (property.values().isEmpty() || property.values().size() > 16) {
throw new IllegalArgumentException(property.name() + " must contain 1 to 16 values."); throw new IllegalArgumentException(property.name() + " must contain 1 to 16 values.");
} }
if (property.values().stream().distinct().count() != property.values().size()) {
throw new IllegalArgumentException(property.name() + " has duplicate values.");
}
} }
} }
if (permutations == null) { if (permutations == null) {

Datei anzeigen

@ -31,30 +31,6 @@ import org.geysermc.geyser.api.block.custom.property.PropertyType;
import java.util.List; import java.util.List;
public class GeyserCustomBlockProperty<T> implements CustomBlockProperty<T> { public record GeyserCustomBlockProperty<T>(@NonNull String name, @NonNull List<T> values,
private final String name; @NonNull PropertyType type) implements CustomBlockProperty<T> {
private final List<T> values;
private final PropertyType<T> type;
public GeyserCustomBlockProperty(String name, List<T> values, PropertyType<T> type) {
this.name = name;
this.values = values;
this.type = type;
}
@Override
public @NonNull String name() {
return name;
}
@Override
public @NonNull List<T> values(){
return values;
}
@Override
public @NonNull PropertyType<T> type() {
return type;
}
} }

Datei anzeigen

@ -39,12 +39,17 @@ import java.util.Map;
@Value @Value
public class GeyserCustomBlockState implements CustomBlockState { public class GeyserCustomBlockState implements CustomBlockState {
String name; CustomBlockData block;
Map<String, Object> properties; Map<String, Object> properties;
@Override
public @NonNull CustomBlockData block() {
return block;
}
@Override @Override
public @NonNull String name() { public @NonNull String name() {
return name; return block.name();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -97,11 +102,11 @@ public class GeyserCustomBlockState implements CustomBlockState {
if (property == null) { if (property == null) {
throw new IllegalArgumentException("Unknown property: " + propertyName); throw new IllegalArgumentException("Unknown property: " + propertyName);
} else if (!property.values().contains(propertyValue)) { } else if (!property.values().contains(propertyValue)) {
throw new IllegalArgumentException("Invalid value for property: " + propertyName + " Value: " + propertyValue); throw new IllegalArgumentException("Invalid value: " + propertyValue + " for property: " + propertyName);
} }
} }
return new GeyserCustomBlockState(blockData.name(), Object2ObjectMaps.unmodifiable(properties)); return new GeyserCustomBlockState(blockData, Object2ObjectMaps.unmodifiable(properties));
} }
} }
} }

Datei anzeigen

@ -135,7 +135,7 @@ public class BlockRegistryPopulator {
.putInt("version", stateVersion) .putInt("version", stateVersion)
.putCompound("states", states) .putCompound("states", states)
.build()); .build());
customExtBlockStates.add(new GeyserCustomBlockState(customBlock.name(), states)); customExtBlockStates.add(new GeyserCustomBlockState(customBlock, states));
} }
} }