RegistrySupplier.java 2.5 KB

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