ExampleInits.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * This file is part of Cloth Config.
  3. * Copyright (C) 2020 - 2021 shedaniel
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.autoconfig.example;
  20. import me.shedaniel.autoconfig.AutoConfig;
  21. import me.shedaniel.autoconfig.ConfigHolder;
  22. import me.shedaniel.autoconfig.serializer.DummyConfigSerializer;
  23. import me.shedaniel.autoconfig.serializer.PartitioningSerializer;
  24. import net.fabricmc.api.EnvType;
  25. import net.fabricmc.api.Environment;
  26. import net.minecraft.world.InteractionResult;
  27. import org.jetbrains.annotations.ApiStatus;
  28. @ApiStatus.Internal
  29. public class ExampleInits {
  30. public static void exampleCommonInit() {
  31. // how to register a config:
  32. ConfigHolder<ExampleConfig> holder = AutoConfig.register(
  33. ExampleConfig.class,
  34. PartitioningSerializer.wrap(DummyConfigSerializer::new)
  35. );
  36. // how to read a config:
  37. holder.getConfig();
  38. // or (please cache this value, and listen to load to re-cache)
  39. AutoConfig.getConfigHolder(ExampleConfig.class).getConfig();
  40. // this event allows you to change or register specific listeners
  41. // for when the config has changed
  42. AutoConfig.getConfigHolder(ExampleConfig.class).registerSaveListener((manager, data) -> {
  43. return InteractionResult.SUCCESS;
  44. });
  45. AutoConfig.getConfigHolder(ExampleConfig.class).registerLoadListener((manager, newData) -> {
  46. return InteractionResult.SUCCESS;
  47. });
  48. }
  49. @Environment(EnvType.CLIENT)
  50. public static void exampleClientInit() {
  51. // how to get the gui registry for custom gui handlers
  52. AutoConfig.getGuiRegistry(ExampleConfig.class);
  53. }
  54. }