Skip to content

Commit

Permalink
Improve ability to access modifier hooks
Browse files Browse the repository at this point in the history
In most cases, we want to get the hook or default, but sometimes you may want to know if a hook is set. Expose the hook map for that usecase and include a method to get or null to reduce map lookups
  • Loading branch information
KnightMiner committed Dec 17, 2022
1 parent c6fffed commit 347da06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slimeknights.tconstruct.library.modifiers;

import com.google.gson.JsonObject;
import lombok.Getter;
import net.minecraft.ChatFormatting;
import net.minecraft.core.Direction;
import net.minecraft.network.FriendlyByteBuf;
Expand Down Expand Up @@ -119,6 +120,7 @@ public void toNetwork(Modifier object, FriendlyByteBuf buffer) {}
@Nullable
private Component description;
/** Map of all modifier hooks registered to this modifier */
@Getter
private final ModifierHookMap hooks;

/** Creates a new modifier using the given hook map */
Expand Down Expand Up @@ -1065,7 +1067,7 @@ public <T> T getModule(Class<T> type) {
* @return Submodule implementing the hook, or default instance if its not implemented
*/
public final <T> T getHook(ModifierHook<T> hook) {
return hooks.getHook(hook);
return hooks.getOrDefault(hook);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private ModifierHooks() {}
public static ModifierHook<?> getHook(ResourceLocation name) {
return HOOKS.get(name);
}

/**
* Registers a new hook
* @param hook Hook to register
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;
import slimeknights.tconstruct.library.modifiers.ModifierHook;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand All @@ -24,10 +25,16 @@ public boolean hasHook(ModifierHook<?> hook) {
return modules.containsKey(hook);
}

/** Gets the module matching the given hook */
/** Gets the module matching the given hook, or null if not defined */
@SuppressWarnings("unchecked")
public <T> T getHook(ModifierHook<T> hook) {
T object = (T)modules.get(hook);
@Nullable
public <T> T getOrNull(ModifierHook<T> hook) {
return (T)modules.get(hook);
}

/** Gets the module matching the given hook */
public <T> T getOrDefault(ModifierHook<T> hook) {
T object = getOrNull(hook);
if (object != null) {
return object;
}
Expand Down

0 comments on commit 347da06

Please sign in to comment.