Skip to content

Commit

Permalink
Bumped mod version to 1.5.1 from 1.5.0
Browse files Browse the repository at this point in the history
Changed the development fabric loader to 0.13 from 0.12
Fixed a NullPointerException when breaking ArmorStands
Fixed a NullPointerException when attempting to drop backpacks into death chests
Fixed the default SQLite value from "true" to "false"
Fixed hostile mob checks (All HostileEntities are Monsters, but not all Monstes are HostileEntities)
Added a "FromSpawner" NBT attribute to mobs, which when TRUE will prevent MobAbsorption from occurring
Added a "Despawn" item property to the Config to allow setting faster despawn on dropped items
Moved the MobAbsorption blacklist to the Config
Added a Mixin to the "/WorldBorder" command to get the current players world (Changed from 1.17 -> 1.18)
Cleaned up imports
  • Loading branch information
GStefanowich committed Feb 24, 2022
1 parent 4a01cbd commit cd56252
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 84 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ org.gradle.jvmargs=-Xmx2G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.18.1
yarn_mappings=1.18.1+build.2
loader_version=0.12.11
yarn_mappings=1.18.1+build.22
loader_version=0.13.3

# Mod Properties
mod_version = 1.5.0
mod_version = 1.5.1
maven_group = net.theelm
archives_base_name = theelm-mod

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric
fabric_version=0.44.0+1.18
fabric_version=0.46.4+1.18
68 changes: 59 additions & 9 deletions src/main/java/net/TheElm/project/config/ConfigArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,67 @@

package net.TheElm.project.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import net.TheElm.project.interfaces.json.ConfigReader;
import net.TheElm.project.interfaces.json.ConfigWriter;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.DefaultedRegistry;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.stream.Collectors;

public final class ConfigArray<T extends Object> extends ConfigBase<T> {

private final Function<JsonElement, T> setter;
private final ConfigReader<T> setter;
private final List<T> value;
private @Nullable ConfigWriter<T> serializer;

public ConfigArray(@NotNull String location, Function<JsonElement, T> setter) {
public ConfigArray(@NotNull String location, ConfigReader<T> setter) {
this(location, new ArrayList<>(), setter);
}
public ConfigArray(@NotNull String location, List<T> defaultValue, Function<JsonElement, T> setter) {
public ConfigArray(@NotNull String location, @NotNull List<T> defaultValue, ConfigReader<T> setter) {
super(location);

this.value = defaultValue;
this.setter = setter;
}
public ConfigArray(@NotNull String location, @NotNull List<T> defaultValue, @NotNull ConfigReader<T> setter, @Nullable ConfigWriter<T> serializer) {
super(location);

this.value = defaultValue;
this.setter = setter;
this.serializer = serializer;
}

public ConfigArray<T> serializer(ConfigWriter<T> serializer) {
this.serializer = serializer;
return this;
}

static <T> JsonElement convertToJSON(@NotNull List<T> list, @Nullable ConfigWriter<T> serializer) {
Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.create();
JsonArray array = new JsonArray();
for (T el : list) {
array.add(serializer == null ? gson.toJsonTree(el) : serializer.serialize(el, gson));
}

return array;
}

@Override
JsonElement getElement() {
return new GsonBuilder()
.disableHtmlEscaping().create().toJsonTree(this.value);
return ConfigArray.convertToJSON(this.value, this.serializer);
}
List<T> get() {
return this.value;
Expand All @@ -77,9 +108,9 @@ void set(JsonElement value) {

// Add all values
if (!(value instanceof JsonArray))
this.value.add(this.setter.apply(value));
this.value.add(this.setter.deserialize(value));
else for (JsonElement element : value.getAsJsonArray())
this.value.add(this.setter.apply(element));
this.value.add(this.setter.deserialize(element));
}

public static @NotNull ConfigArray<Integer> jInt(@NotNull String location) {
Expand All @@ -94,4 +125,23 @@ else for (JsonElement element : value.getAsJsonArray())
public static @NotNull ConfigArray<String> jString(@NotNull String location) {
return new ConfigArray<>(location, JsonElement::getAsString);
}
public static @NotNull <T> ConfigArray<RegistryKey<T>> registry(String location, RegistryKey<? extends Registry<T>> registry, List<RegistryKey<T>> def) {
return new ConfigArray<>(location, def, (element) -> {
// Convert from String to RegistryKey
return RegistryKey.of(registry, new Identifier(element.getAsString()));
}, (type, gson) -> {
// Convert from RegistryKey to String
return gson.toJsonTree(type.getValue().toString());
});
}
public static @NotNull <T> ConfigArray<T> fromRegistry(String location, DefaultedRegistry<T> registry, List<T> defaults) {
List<T> cast = defaults.stream().map((type) -> registry.get(registry.getId(type))).collect(Collectors.toList());
return new ConfigArray<>(location, cast, (element) -> {
// Convert from String to RegistryKey
return registry.get(new Identifier(element.getAsString()));
}, (type, gson) -> {
// Convert from RegistryKey to String
return gson.toJsonTree(registry.getId(type).toString());
});
}
}
50 changes: 24 additions & 26 deletions src/main/java/net/TheElm/project/config/ConfigOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

package net.TheElm.project.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import net.TheElm.project.CoreMod;
import net.TheElm.project.interfaces.json.ConfigReader;
import net.TheElm.project.interfaces.json.ConfigWriter;
import net.TheElm.project.objects.ChatFormat;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
Expand All @@ -40,21 +42,19 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

public class ConfigOption<T extends Object> extends ConfigBase<T> {
public final class ConfigOption<T extends Object> extends ConfigBase<T> {

private final @NotNull Function<JsonElement, T> setter;
private final @NotNull ConfigReader<T> setter;
private @Nullable T value;
private @Nullable JsonSerializer<T> serializer;
private @Nullable ConfigWriter<T> serializer;

public ConfigOption(@NotNull String location, @NotNull Function<JsonElement, T> setter) {
public ConfigOption(@NotNull String location, @NotNull ConfigReader<T> setter) {
this(location, null, setter);
}
public ConfigOption(@NotNull String location, @Nullable T defaultValue, @NotNull Function<JsonElement, T> setter) {
public ConfigOption(@NotNull String location, @Nullable T defaultValue, @NotNull ConfigReader<T> setter) {
this(location, defaultValue, setter, null);
}
public ConfigOption(@NotNull String location, @Nullable T defaultValue, @NotNull Function<JsonElement, T> setter, @Nullable JsonSerializer<T> serializer) {
public ConfigOption(@NotNull String location, @Nullable T defaultValue, @NotNull ConfigReader<T> setter, @Nullable ConfigWriter<T> serializer) {
super(location);

this.value = defaultValue;
Expand All @@ -63,34 +63,32 @@ public ConfigOption(@NotNull String location, @Nullable T defaultValue, @NotNull
}

@Override
final void set( JsonElement value ) {
this.value = ( value == null ? null : this.setter.apply(value) );
void set( JsonElement value ) {
this.value = ( value == null ? null : this.setter.deserialize(value) );
}
final void set( T value ) {
void set( T value ) {
this.value = value;
}
final T get() {
T get() {
return this.value;
}

@Override
final JsonElement getElement() {
JsonElement getElement() {
return ConfigOption.convertToJSON(this.value, this.serializer);
}

public final ConfigOption<T> serializer(JsonSerializer<T> serializer) {
public ConfigOption<T> serializer(ConfigWriter<T> serializer) {
this.serializer = serializer;
return this;
}

public static JsonElement convertToJSON( @Nullable Object src ) {
return ConfigOption.convertToJSON( src, null );
}
public static <T> JsonElement convertToJSON(@Nullable T src, @Nullable JsonSerializer<T> serializer ) {
GsonBuilder builder = new GsonBuilder();
if (src != null && serializer != null)
builder.registerTypeAdapter(src.getClass(), serializer);
return builder.create().toJsonTree( src );
static <T> JsonElement convertToJSON(@Nullable T src, @Nullable ConfigWriter<T> serializer) {
Gson gson = new GsonBuilder().create();
return serializer == null ? gson.toJsonTree(src) : serializer.serialize(src, gson);
}

public static @NotNull ConfigOption<ChatFormat> chat(@NotNull String location, @NotNull String defaultFormat) {
Expand Down Expand Up @@ -157,7 +155,7 @@ public static <T> JsonElement convertToJSON(@Nullable T src, @Nullable JsonSeria
CoreMod.logError(new Exception("Failed to parse BlockPos in config, using fallback value", ex));
}
return BlockPos.ORIGIN;
}, (s, type, json) -> {
}, (s, json) -> {
// Create an array
JsonArray array = new JsonArray();

Expand All @@ -169,19 +167,19 @@ public static <T> JsonElement convertToJSON(@Nullable T src, @Nullable JsonSeria
return array;
});
}
public static @NotNull <T extends Object & Comparable<? super T>> ConfigOption<T> comparable(@NotNull String location, @Nullable T def, final @NotNull T minimum, final @NotNull T maximum, final @NotNull Function<JsonElement, T> setter ) {
public static @NotNull <T extends Object & Comparable<? super T>> ConfigOption<T> comparable(@NotNull String location, @Nullable T def, final @NotNull T minimum, final @NotNull T maximum, final @NotNull ConfigReader<T> setter ) {
return new ConfigOption<>(location, def, (raw) -> {
T setTo = setter.apply(raw);
T setTo = setter.deserialize(raw);
return (setTo.compareTo(minimum) < 0 || maximum.compareTo(setTo) < 0) ? def : setTo;
});
}
public static @NotNull <T> ConfigOption<RegistryKey<T>> registry(String location, RegistryKey<T> def, RegistryKey<? extends Registry<T>> registry) {
public static @NotNull <T> ConfigOption<RegistryKey<T>> registry(String location, RegistryKey<? extends Registry<T>> registry, RegistryKey<T> def) {
return new ConfigOption<>(location, def, (element) -> {
// Convert from String to RegistryKey
return RegistryKey.of(registry, new Identifier(element.getAsString()));
}, (world, type, json) -> {
}, (type, json) -> {
// Convert from RegistryKey to String
return json.serialize(world.getValue().toString());
return json.toJsonTree(type.getValue().toString());
});
}
}
Loading

0 comments on commit cd56252

Please sign in to comment.