Skip to content

Commit

Permalink
Set spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBunnyDc committed Sep 27, 2024
1 parent 6feff0a commit 91f4717
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public List<WuidebookImplementation> gatherImplementations() {
return List.of();
}

@Override
public String getCurrentLoadedModId() {
return FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace();
}

public Path getConfigPath() {
return FabricLoader.getInstance().getConfigDir();
}
Expand Down
21 changes: 19 additions & 2 deletions XPlat/src/main/java/de/wonejo/wuidebook/WuidebookCommonMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import de.wonejo.wuidebook.api.config.ConfigManager;
import de.wonejo.wuidebook.api.util.Constants;
import de.wonejo.wuidebook.api.util.McEnvironment;
import de.wonejo.wuidebook.api.util.TriState;
import de.wonejo.wuidebook.api.wgc.WgcValueTypeRegistry;
import de.wonejo.wuidebook.api.wgc.value.*;
import de.wonejo.wuidebook.impl.config.serializer.*;
import de.wonejo.wuidebook.impl.config.serializer.color.ColorConfigSerializer;
import de.wonejo.wuidebook.impl.service.ModServices;
Expand All @@ -22,6 +25,7 @@ public class WuidebookCommonMod {
private static final WuidebookCommonMod INSTANCE = new WuidebookCommonMod();
private static Path CONFIG_DIRECTORY = ModServices.ABSTRACTION.getConfigPath();
private final ConfigManager configManager = ConfigManager.get();
private final WgcValueTypeRegistry wgcValueTypeRegistry = WgcValueTypeRegistry.get();

private WuidebookCommonMod () {}

Expand All @@ -37,9 +41,23 @@ public void setup () {
}

this.setupConfigAPI (implementations);
this.setupWgcFiles(implementations);
}

private void setupConfigAPI (@NotNull List<WuidebookImplementation> pImplementation) {
private void setupWgcFiles ( @NotNull List<WuidebookImplementation> pImplementation ) {
this.wgcValueTypeRegistry.registerType(ByteWgcValue.TYPE.id(), (value) -> new ByteWgcValue (Byte.parseByte(value)));
this.wgcValueTypeRegistry.registerType(ShortWgcValue.TYPE.id(), (value) -> new ShortWgcValue (Short.parseShort(value)));
this.wgcValueTypeRegistry.registerType(IntWgcValue.TYPE.id(), (value) -> new IntWgcValue (Integer.parseInt(value)));
this.wgcValueTypeRegistry.registerType(FloatWgcValue.TYPE.id(), (value) -> new FloatWgcValue (Float.parseFloat(value)));
this.wgcValueTypeRegistry.registerType(DoubleWgcValue.TYPE.id(), (value) -> new DoubleWgcValue (Double.parseDouble(value)));
this.wgcValueTypeRegistry.registerType(BooleanWgcValue.TYPE.id(), (value) -> new BooleanWgcValue (TriState.fromBooleanText(value)));
this.wgcValueTypeRegistry.registerType(StringWgcValue.TYPE.id(), StringWgcValue::new);
this.wgcValueTypeRegistry.registerType(NullWgcValue.TYPE.id(), (value) -> new NullWgcValue());

for ( WuidebookImplementation impl : pImplementation ) impl.onRegisterCustomWGCValueType(this.wgcValueTypeRegistry);
}

private void setupConfigAPI ( @NotNull List<WuidebookImplementation> pImplementation ) {
this.configManager.registerSerializer(Constants.CFG_STR_SERIALIZER, new StringCfgSerializer());
this.configManager.registerSerializer(Constants.CFG_INT_SERIALIZER, new IntCfgSerializer());
this.configManager.registerSerializer(Constants.CFG_FLOAT_SERIALIZER, new FloatCfgSerializer());
Expand All @@ -50,7 +68,6 @@ private void setupConfigAPI (@NotNull List<WuidebookImplementation> pImplementat

for ( WuidebookImplementation impl : pImplementation ) {
impl.onRegisterConfigSerializer(this.configManager);
impl.onRegisterConfigFiles(this.configManager);
}

this.configManager.getFileList(McEnvironment.COMMON).forEach(ConfigFile::loadFile);
Expand Down
7 changes: 7 additions & 0 deletions XPlat/src/main/java/de/wonejo/wuidebook/api/book/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.wonejo.wuidebook.api.book;

public interface Book {

BookInfo getInformation ();

}
44 changes: 44 additions & 0 deletions XPlat/src/main/java/de/wonejo/wuidebook/api/book/BookInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.wonejo.wuidebook.api.book;

import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.awt.*;

public interface BookInfo {

static Builder builder () {
return de.wonejo.wuidebook.impl.book.BookInfoImpl.builderImpl();
}

ResourceLocation bookId ();

ResourceLocation modelLocation ();
ResourceLocation mainBookGUITexture ();
ResourceLocation pageBookGUITexture ();

Component title ();
Component header ();
Component itemName ();
Component author ();

Color bookColor ();

boolean shouldSpawnWithBook ();

interface Builder {
Builder withId ( ResourceLocation pBookId );
Builder withModelLocation ( ResourceLocation pLocation );
Builder withMainBookTexture ( ResourceLocation pLocation );
Builder withPageBookTexture ( ResourceLocation pLocation );
Builder setTitle ( Component pTitle );
Builder setHeader ( Component pHeader );
Builder setItemName ( Component pItemName );
Builder setAuthor ( Component pAuthor );
Builder withBookColor ( Color pColor );
Builder shouldSpawnWithBook ();

BookInfo build ();
}

}
63 changes: 63 additions & 0 deletions XPlat/src/main/java/de/wonejo/wuidebook/api/book/BookRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.wonejo.wuidebook.api.book;

import com.google.common.collect.Maps;
import com.mojang.datafixers.util.Pair;
import de.wonejo.wuidebook.api.util.Constants;
import de.wonejo.wuidebook.api.util.WuidebookRegistryException;
import de.wonejo.wuidebook.impl.service.ModServices;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Optional;

public final class BookRegistry {
private static final Logger LOGGER = LogManager.getLogger();
private final Map<ResourceLocation, Pair<BookInfo, ResourceLocation>> bookInformation = Maps.newHashMap();
private final Map<ResourceLocation, Book> books = Maps.newHashMap();
private boolean areBooksBuilt;

private BookRegistry () {}

@ApiStatus.Internal
public void buildBooks () {
if (this.areBooksBuilt) return;
if (!ModServices.ABSTRACTION.getCurrentLoadedModId().equals(Constants.MOD_ID)) throw new RuntimeException("Can not use Wuidebook buildBooks on other mods code!");

this.areBooksBuilt = true;
}

public void registerBook ( @NotNull BookInfo pInfo, ResourceLocation pBaseFileLocation ) {
if ( this.bookInformation.putIfAbsent(pInfo.bookId(), Pair.of(pInfo, pBaseFileLocation)) != null )
throw new WuidebookRegistryException("Can not register two ( or more ) book information with id: " + pInfo.bookId() + ", it wouldn't be registered!");
}

public @NotNull Book getBook (ResourceLocation pBookId ) {
Book book = this.books.get(pBookId);
if (!areBooksBuilt) throw new RuntimeException("Can not get wuidebook guide, books aren't build.");
if ( book == null ) throw new RuntimeException("Can not get book with id: " + pBookId + " because it isn't present.");
return book;
}

public Optional<Book> getOptBook ( ResourceLocation pBookId ) {
Book book = this.books.get(pBookId);
if (!areBooksBuilt) throw new RuntimeException("Can not get wuidebook guide, books aren't build.");
return Optional.ofNullable(book);
}

public BookInfo getInfo ( ResourceLocation pBookId ) {
Pair<BookInfo, ResourceLocation> pair = this.bookInformation.get(pBookId);
if ( pair == null ) throw new NullPointerException("Can not get book information, there isn't a book ith id: " + pBookId);
return pair.getFirst();
}

public ResourceLocation getBaseFileLocation ( ResourceLocation pBookId ) {
Pair<BookInfo, ResourceLocation> pair = this.bookInformation.get(pBookId);
if ( pair == null ) throw new NullPointerException("Can not get base file location, there isn't a book with id: " + pBookId);
return pair.getSecond();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.wonejo.wuidebook.api.config.ConfigManager;
import de.wonejo.wuidebook.api.config.ConfigProviderBuilder;
import de.wonejo.wuidebook.api.wgc.WgcValueTypeRegistry;

public interface WuidebookImplementation {

Expand All @@ -13,14 +14,6 @@ public interface WuidebookImplementation {
*/
default void onRegisterConfigSerializer ( ConfigManager pManager ) {}

/**
* Register custom config files for configs.
* <p/>
* Note: Do not use this method to register serializers, only config files.
* @since 4.0.0-dev2
*/
default void onRegisterConfigFiles ( ConfigManager pManager ) {}

/**
* PLEASE, if you use any of these three methods, specify in your config the modId, sommething like: 'exampleModId.exampleConfig' PLEASE.
* <p/>
Expand All @@ -31,4 +24,10 @@ default void onRegisterCustomClientConfig ( ConfigProviderBuilder pBuilder ) {}
default void onRegisterCustomServerConfig ( ConfigProviderBuilder pBuilder ) {}
default void onRegisterCustomCommonConfig ( ConfigProviderBuilder pBuilder ) {}

/**
* If add custom WGC content templates and need to add other value type use this, might help.
* @since 4.0.0-dev2
*/
default void onRegisterCustomWGCValueType ( WgcValueTypeRegistry pRegistry ) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Maps;
import de.wonejo.wuidebook.api.config.serialization.ConfigValueSerializer;
import de.wonejo.wuidebook.api.util.McEnvironment;
import de.wonejo.wuidebook.api.util.WuidebookRegistryException;
import net.minecraft.resources.ResourceLocation;
import org.apache.commons.compress.utils.Lists;
import org.apache.logging.log4j.LogManager;
Expand All @@ -21,7 +22,7 @@ private ConfigManager () {}

public void registerSerializer ( ResourceLocation pSerializerId, ConfigValueSerializer<?> pSerializer ) {
if ( this.serializers.putIfAbsent(pSerializerId, pSerializer) != null )
LOGGER.warn("Can not register serializer with id: {}, there is already a serializer with that id.", pSerializer);
throw new WuidebookRegistryException("Can not register serializer with id: '%s', there is already a serializer with that id.".formatted(pSerializer));
}

public void registerFile ( ConfigFile pFile ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,23 @@ interface DeserializeResult<T> {
* Create a success deserialization result.
* @since 4.0.0-dev2
*/
@NotNull
static <B> DeserializeResult<B> success ( B pResult ) {
@NotNull static <B> DeserializeResult<B> success ( B pResult ) {
return DeserializeResultImpl.success(pResult);
}

/**
* Create a deserialization result that can have the result and errors.
* @since 4.0.0-dev2
*/
@NotNull
static <B> DeserializeResult<B> haveBoth ( B pResult, List<String> pErrors ) {
@NotNull static <B> DeserializeResult<B> haveBoth ( B pResult, List<String> pErrors ) {
return DeserializeResultImpl.haveBoth(pResult, pErrors);
}

/**
* Create a success deserialization result.
* @since 4.0.0-dev2
*/
@NotNull
static <B> DeserializeResult<B> fail ( List<String> pErrors ) {
@NotNull static <B> DeserializeResult<B> fail ( List<String> pErrors ) {
return DeserializeResultImpl.fail(pErrors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface XPlatAbstraction {

List<WuidebookImplementation> gatherImplementations ();

String getCurrentLoadedModId ();

Path getConfigPath ();
boolean isDevWorkspace ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@

public class ResourceLocationUtils {

@NotNull
public static ResourceLocation minecraft (String pPath) {
@NotNull public static ResourceLocation minecraft (String pPath) {
return ResourceLocation.withDefaultNamespace(pPath);
}

@NotNull
public static ResourceLocation wuidebook ( String pPath ) {
@NotNull public static ResourceLocation wuidebook ( String pPath ) {
return ResourceLocation.fromNamespaceAndPath(Constants.MOD_ID, pPath);
}

@NotNull
public static ResourceLocation common ( String pPath ) {
@NotNull public static ResourceLocation common ( String pPath ) {
return ResourceLocation.fromNamespaceAndPath("c", pPath);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.wonejo.wuidebook.api.util;

public class WuidebookRegistryException extends RuntimeException {

public WuidebookRegistryException ( String pMessage ) {
super(pMessage);
}

public WuidebookRegistryException ( String pMessage, Throwable pThrowable ) {
super(pMessage, pThrowable);
}
}
9 changes: 3 additions & 6 deletions XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

public interface WgcTag {

@NotNull
static WgcTag createTag (String pKey, Map<ResourceLocation, Property<?>> pProperties) {
@NotNull static WgcTag createTag (String pKey, Map<ResourceLocation, Property<?>> pProperties) {
return WgcTagImpl.createTag(pKey, pProperties);
}

Expand All @@ -25,13 +24,11 @@ static WgcTag createTag (String pKey, Map<ResourceLocation, Property<?>> pProper

interface Property<T> {

@NotNull
static <B> WgcTag.Property<B> createProperty ( ResourceLocation pKey, ResourceLocation pValueType, String pValue ) {
@NotNull static <B> WgcTag.Property<B> createProperty ( ResourceLocation pKey, ResourceLocation pValueType, String pValue ) {
return WgcTagImpl.PropertyImpl.createProperty(pKey, pValueType, pValue);
}

@NotNull
static <B> WgcTag.Property<B> createProperty ( ResourceLocation pKey, WgcTagValue<B> pValue ) {
@NotNull static <B> WgcTag.Property<B> createProperty ( ResourceLocation pKey, WgcTagValue<B> pValue ) {
return WgcTagImpl.PropertyImpl.createProperty(pKey, pValue);
}

Expand Down
Loading

0 comments on commit 91f4717

Please sign in to comment.