Skip to content

Commit 7829bee

Browse files
committed
Properly update config system to also handle per book configuration
1 parent 325afb7 commit 7829bee

File tree

7 files changed

+40
-50
lines changed

7 files changed

+40
-50
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dependencies {
4545
])
4646
compileOnly fg.deobf("mezz.jei:jei-${project.minecraft_version}:${project.jei_version}:api") //API for compile
4747
runtimeOnly fg.deobf("mezz.jei:jei-${project.minecraft_version}:${project.jei_version}") //Full for runtime
48+
//runtimeClasspath 'com.electronwill.night-config:hocon:3.6.2'
4849
}
4950

5051
sourceSets {
Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,56 @@
11
package amerifrance.guideapi;
22

33

4+
import amerifrance.guideapi.api.GuideAPI;
5+
import amerifrance.guideapi.api.impl.Book;
6+
import com.electronwill.nightconfig.core.CommentedConfig;
7+
import com.google.common.collect.Maps;
48
import net.minecraftforge.common.ForgeConfigSpec;
9+
import net.minecraftforge.fml.ModContainer;
510
import net.minecraftforge.fml.ModLoadingContext;
11+
import net.minecraftforge.fml.config.ConfigFileTypeHandler;
612
import net.minecraftforge.fml.config.ModConfig;
713
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
814
import org.apache.commons.lang3.tuple.Pair;
915

16+
import java.util.HashMap;
17+
import java.util.Map;
18+
1019
public class GuideConfig {
11-
/**
12-
* Synced to clients.
13-
* Only loaded on world load
14-
*/
15-
public static final Server SERVER;
20+
1621
/**
1722
* For side independent configuration. Not synced.
1823
* Loaded after registry events but before setup
1924
*/
20-
public static final Common COMMON;
21-
22-
private static final ForgeConfigSpec serverSpec;
23-
private static final ForgeConfigSpec commonSpec;
24-
25-
26-
static {
27-
final Pair<Server, ForgeConfigSpec> specPair = new ForgeConfigSpec.Builder().configure(Server::new);
28-
serverSpec = specPair.getRight();
29-
SERVER = specPair.getLeft();
30-
}
31-
32-
static {
33-
final Pair<Common, ForgeConfigSpec> specPair = new ForgeConfigSpec.Builder().configure(Common::new);
34-
commonSpec = specPair.getRight();
35-
COMMON = specPair.getLeft();
36-
}
37-
38-
public static void registerConfigs() {
39-
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, commonSpec);
40-
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, serverSpec);
41-
FMLJavaModLoadingContext.get().getModEventBus().register(GuideConfig.class);
42-
}
25+
public static Common COMMON;
4326

44-
public static class Server {
27+
public static class Common {
4528

4629
public final ForgeConfigSpec.BooleanValue canSpawnWithBook;
4730

48-
Server(ForgeConfigSpec.Builder builder) {
49-
builder.comment("Server configuration settings").push("server");
50-
canSpawnWithBook = builder.comment("Allows books to spawn with new players.\nThis is a global override for all books.").define("canSpawnWithBook",true);
51-
builder.pop();
52-
}
53-
54-
// for ( Book book : GuideAPI.getBooks().values()) TODO
55-
// SPAWN_BOOKS.put(book, config.getBoolean(book.getRegistryName().toString(), "Books.Spawn", book.shouldSpawnWithBook(), ""));
56-
//
57-
// config.setCategoryComment("Books.Spawn", "If true, the user will spawn with the book.\nThis defaults to the value the book owner has set and is overridden by this config.");
58-
}
59-
60-
public static class Common {
61-
6231
public final ForgeConfigSpec.BooleanValue enableLogging;
32+
public final Map<Book,ForgeConfigSpec.BooleanValue> SPAWN_BOOKS = new HashMap<>();
6333

6434

6535
Common(ForgeConfigSpec.Builder builder) {
6636
builder.comment("Common configurations settings").push("common");
6737
enableLogging = builder.comment("Enables extra information being printed to the console.").define("enableLogging",true);
38+
canSpawnWithBook = builder.comment("Allows books to spawn with new players.\nThis is a global override for all books if set to false.").define("canSpawnWithBook",true);
39+
builder.comment("If the player should spawn with this book").push("spawnBook");
40+
for(Book book : GuideAPI.getBooks().values()){
41+
SPAWN_BOOKS.put(book,builder.define(book.getRegistryName().getNamespace()+"-"+book.getRegistryName().getPath(),book.shouldSpawnWithBook()));
42+
}
43+
builder.pop();
6844
builder.pop();
6945
}
7046
}
7147

48+
public static void buildConfiguration(){
49+
final Pair<Common, ForgeConfigSpec> specPair = new ForgeConfigSpec.Builder().configure(Common::new);
50+
ForgeConfigSpec commonSpec = specPair.getRight();
51+
COMMON = specPair.getLeft();
52+
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, commonSpec);
53+
FMLJavaModLoadingContext.get().getModEventBus().register(GuideConfig.class);
54+
}
55+
7256
}

src/main/java/amerifrance/guideapi/GuideMod.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public GuideMod(){
4646
}
4747

4848
private void setup(final FMLCommonSetupEvent event){
49+
if(GuideConfig.COMMON==null){
50+
throw new IllegalStateException("Did not build configuration, before configuration load. Make sure to call GuideConfig#buildConfiguration during one of the registry events");
51+
}
4952
PacketHandler.registerPackets();
5053
}
5154

src/main/java/amerifrance/guideapi/RegistrarGuideAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class RegistrarGuideAPI {
3131
@SubscribeEvent
3232
public static void registerItems(RegistryEvent.Register<Item> event) {
3333
AnnotationHandler.gatherBooks();
34-
34+
GuideConfig.buildConfiguration();//Build configuration now that we know all added books
3535
for (Book book : GuideAPI.getBooks().values()) {
3636
Item guideBook = new ItemGuideBook(book);
3737
guideBook.setRegistryName(book.getRegistryName().toString().replace(":", "-"));

src/main/java/amerifrance/guideapi/test/TestBook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class TestBook implements IGuideBook {
4242
@Override
4343
public Book buildBook() {
4444
BookBinder binder = new BookBinder(new ResourceLocation("guideapi","test_book"));
45-
binder.setAuthor("TehNut").setColor(Color.PINK).setItemName("Display Name").setHeader("Hello there").setGuideTitle("Title message").setContentProvider(this::buildContent);
45+
binder.setAuthor("TehNut").setColor(Color.PINK).setItemName("Display Name").setHeader("Hello there").setGuideTitle("Title message").setSpawnWithBook().setContentProvider(this::buildContent);
4646
return (book = binder.build());
4747
}
4848

src/main/java/amerifrance/guideapi/test/TestBook2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class TestBook2 implements IGuideBook {
3535
@Override
3636
public Book buildBook() {
3737
BookBinder binder = new BookBinder(new ResourceLocation("guideapi","test_book2"));
38-
binder.setAuthor("TehNut").setColor(new Color(80,50,5)).setItemName("Display Name").setHeader("Hello there").setGuideTitle("Title message").setContentProvider(this::buildContent);
38+
binder.setAuthor("TehNut").setColor(new Color(80,50,5)).setItemName("Display Name").setHeader("Hello there").setSpawnWithBook().setGuideTitle("Title message").setContentProvider(this::buildContent);
3939

4040

4141

src/main/java/amerifrance/guideapi/util/EventHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraftforge.api.distmarker.Dist;
2929
import net.minecraftforge.api.distmarker.OnlyIn;
3030
import net.minecraftforge.client.event.RenderGameOverlayEvent;
31+
import net.minecraftforge.common.ForgeConfigSpec;
3132
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
3233
import net.minecraftforge.eventbus.api.SubscribeEvent;
3334
import net.minecraftforge.fml.common.Mod;
@@ -43,12 +44,13 @@ public static void onPlayerJoinWorld(EntityJoinWorldEvent event) {
4344
if (!event.getEntity().world.isRemote && event.getEntity() instanceof PlayerEntity) {
4445
PlayerEntity player = (PlayerEntity) event.getEntity();
4546
CompoundNBT tag = getModTag(player, GuideMod.ID);
46-
if (GuideConfig.SERVER.canSpawnWithBook.get()) {
47+
if (GuideConfig.COMMON.canSpawnWithBook.get()) {
4748
for (Book book : GuideAPI.getBooks().values()) {
48-
//if (ConfigHandler.SPAWN_BOOKS.getOrDefault(book, false) && !tag.getBoolean("hasInitial" + book.getTitle())) { TODO
49+
ForgeConfigSpec.BooleanValue bookSpawnConfig = GuideConfig.COMMON.SPAWN_BOOKS.get(book);
50+
if((bookSpawnConfig==null||bookSpawnConfig.get()) && !tag.getBoolean("hasInitial" + book.getRegistryName().toString())){
4951
ItemHandlerHelper.giveItemToPlayer(player, GuideAPI.getStackFromBook(book));
50-
tag.putBoolean("hasInitial" + book.getTitle(), true);
51-
//}
52+
tag.putBoolean("hasInitial" + book.getRegistryName().toString(), true);
53+
}
5254
}
5355
}
5456
}

0 commit comments

Comments
 (0)