RegistrySupplier.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * This file is part of architectury.
  3. * Copyright (C) 2020, 2021 architectury
  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.architectury.registry;
  20. import net.minecraft.resources.ResourceLocation;
  21. import org.jetbrains.annotations.Nullable;
  22. import java.util.Optional;
  23. import java.util.function.Consumer;
  24. import java.util.function.Supplier;
  25. import java.util.stream.Stream;
  26. public interface RegistrySupplier<T> extends Supplier<T> {
  27. /**
  28. * @return the identifier of the registry
  29. */
  30. ResourceLocation getRegistryId();
  31. /**
  32. * @return the identifier of the entry
  33. */
  34. ResourceLocation getId();
  35. /**
  36. * @return whether the entry has been registered
  37. */
  38. boolean isPresent();
  39. @Nullable
  40. default T getOrNull() {
  41. if (isPresent()) {
  42. return get();
  43. }
  44. return null;
  45. }
  46. default Optional<T> toOptional() {
  47. return Optional.ofNullable(getOrNull());
  48. }
  49. default void ifPresent(Consumer<? super T> action) {
  50. if (isPresent()) {
  51. action.accept(get());
  52. }
  53. }
  54. default void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
  55. if (isPresent()) {
  56. action.accept(get());
  57. } else {
  58. emptyAction.run();
  59. }
  60. }
  61. default Stream<T> stream() {
  62. if (!isPresent()) {
  63. return Stream.empty();
  64. } else {
  65. return Stream.of(get());
  66. }
  67. }
  68. default T orElse(T other) {
  69. return isPresent() ? get() : other;
  70. }
  71. default T orElseGet(Supplier<? extends T> supplier) {
  72. return isPresent() ? get() : supplier.get();
  73. }
  74. }