Skip to content

Commit

Permalink
add target redirector api
Browse files Browse the repository at this point in the history
(cherry picked from commit 3af621d)
  • Loading branch information
deirn committed Mar 24, 2024
1 parent bf4f590 commit 8f4b41e
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 59 deletions.
19 changes: 19 additions & 0 deletions src/api/java/mcp/mobius/waila/api/IBlockComponentProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ public interface IBlockComponentProvider {
*/
BlockState EMPTY_BLOCK_STATE = Internals.unsafeAlloc(BlockState.class);

/**
* Redirect the ray cast hit result to target other object.
*
* @param redirect the redirector
* @param accessor contains most of the relevant information about the current environment.
* Note that {@link IBlockAccessor#getData()} will always be empty at this time
* @param config current plugin configuration
*
* @return {@code null} if this method doesn't redirect to anything,
* any result from one of {@link ITargetRedirector}'s methods otherwise
*
* @see IRegistrar#addRedirect(IBlockComponentProvider, Class)
*/
@Nullable
@ApiStatus.Experimental
default ITargetRedirector.Result redirect(ITargetRedirector redirect, IBlockAccessor accessor, IPluginConfig config) {
return null;
}

/**
* Callback used to send additional context to {@link IDataProvider}s.
*
Expand Down
19 changes: 19 additions & 0 deletions src/api/java/mcp/mobius/waila/api/IEntityComponentProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ public interface IEntityComponentProvider {
*/
Entity EMPTY_ENTITY = Internals.unsafeAlloc(AreaEffectCloud.class);

/**
* Redirect the ray cast hit result to target other object.
*
* @param redirect the redirector
* @param accessor contains most of the relevant information about the current environment.
* Note that {@link IEntityAccessor#getData()} will always be empty at this time
* @param config current plugin configuration
*
* @return {@code null} if this method doesn't redirect to anything,
* any result from one of {@link ITargetRedirector}'s methods otherwise
*
* @see IRegistrar#addRedirect(IEntityComponentProvider, Class)
*/
@Nullable
@ApiStatus.Experimental
default ITargetRedirector.Result redirect(ITargetRedirector redirect, IEntityAccessor accessor, IPluginConfig config) {
return null;
}

/**
* Callback used to send additional context to {@link IDataProvider}s.
*
Expand Down
52 changes: 52 additions & 0 deletions src/api/java/mcp/mobius/waila/api/IRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,33 @@ default void addEventListener(IEventListener listener) {
*/
void addBlacklist(BlockEntityType<?>... blockEntityTypes);

/**
* Registers an {@link IBlockComponentProvider} instance to allow redirecting the object being displayed.
* A {@link BlockEntity} is also an acceptable class type.
*
* @param provider the data provider instance
* @param clazz the highest level class to apply to
* @param priority the priority of this provider <b>0 is the minimum, lower number will be called first</b>
*
* @see #DEFAULT_PRIORITY
*/
@ApiStatus.Experimental
<T> void addRedirect(IBlockComponentProvider provider, Class<T> clazz, int priority);

/**
* Registers an {@link IBlockComponentProvider} instance to allow redirecting the object being displayed.
* A {@link BlockEntity} is also an acceptable class type.
*
* @param provider the data provider instance
* @param clazz the highest level class to apply to
*
* @see #DEFAULT_PRIORITY
*/
@ApiStatus.Experimental
default <T> void addRedirect(IBlockComponentProvider provider, Class<T> clazz) {
addRedirect(provider, clazz, DEFAULT_PRIORITY);
}

/**
* Registers an {@link IBlockComponentProvider} instance to allow overriding the block being displayed.
* A {@link BlockEntity} is also an acceptable class type.
Expand Down Expand Up @@ -441,6 +468,31 @@ default <T, BE extends BlockEntity> void addBlockData(IDataProvider<BE> provider
*/
void addBlacklist(EntityType<?>... entityTypes);

/**
* Registers an {@link IEntityComponentProvider} instance to allow redirecting the object being displayed.
*
* @param provider the data provider instance
* @param clazz the highest level class to apply to
* @param priority the priority of this provider <b>0 is the minimum, lower number will be called first</b>
*
* @see #DEFAULT_PRIORITY
*/
@ApiStatus.Experimental
<T> void addRedirect(IEntityComponentProvider provider, Class<T> clazz, int priority);

/**
* Registers an {@link IEntityComponentProvider} instance to allow redirecting the object being displayed.
*
* @param provider the data provider instance
* @param clazz the highest level class to apply to
*
* @see #DEFAULT_PRIORITY
*/
@ApiStatus.Experimental
default <T> void addRedirect(IEntityComponentProvider provider, Class<T> clazz) {
addRedirect(provider, clazz, DEFAULT_PRIORITY);
}

/**
* Registers an {@link IEntityComponentProvider} instance to allow overriding the entity being displayed.
*
Expand Down
55 changes: 55 additions & 0 deletions src/api/java/mcp/mobius/waila/api/ITargetRedirector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package mcp.mobius.waila.api;

import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import org.jetbrains.annotations.ApiStatus;

/**
* Used to redirect the targeted objet to other object.
*
* @see IBlockComponentProvider#redirect(ITargetRedirector, IBlockAccessor, IPluginConfig)
* @see IEntityComponentProvider#redirect(ITargetRedirector, IEntityAccessor, IPluginConfig)
*/
@ApiStatus.Experimental
@ApiStatus.NonExtendable
public interface ITargetRedirector {

/**
* Redirect the current object to itself.
* <p>
* This also restrict lower priority provider from redirecting the object,
* return {@code null} instead if the caller doesn't redirect.
*/
Result toSelf();

/**
* Redirect to nowhere, disabling the tooltip to be displayed.
*/
Result toNowhere();

/**
* Redirect to whatever object behind this object, essentially making this object appear invisible.
*/
Result toBehind();

/**
* Redirect to specific object at the hit result's position.
* <p>
* <b>Note:</b> {@link BlockHitResult#withPosition(BlockPos)} will have the original
* {@linkplain HitResult#getLocation() hit location} (not to be confused with
* {@linkplain BlockHitResult#getBlockPos() block position}), you need to take
* account if the redirect target is not from something you own (e.g. redirecting
* to other mod's block).
*/
Result to(HitResult hitResult);

/**
* Redirection result, only here to make sure it's not called multiple times.
*/
@ApiStatus.NonExtendable
interface Result {

}

}
51 changes: 51 additions & 0 deletions src/main/java/mcp/mobius/waila/gui/hud/TargetRedirector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mcp.mobius.waila.gui.hud;

import mcp.mobius.waila.api.ITargetRedirector;
import net.minecraft.world.phys.HitResult;
import org.jetbrains.annotations.Nullable;

public class TargetRedirector implements ITargetRedirector {

private static final TargetRedirector INSTANCE = new TargetRedirector();
private static final Result RESULT = new Result();

public boolean self, nowhere, behind;
public @Nullable HitResult to;

public static TargetRedirector get() {
INSTANCE.self = false;
INSTANCE.nowhere = false;
INSTANCE.behind = false;
INSTANCE.to = null;
return INSTANCE;
}

@Override
public Result toSelf() {
self = true;
return RESULT;
}

@Override
public Result toNowhere() {
nowhere = true;
return RESULT;
}

@Override
public Result toBehind() {
behind = true;
return RESULT;
}

@Override
public Result to(HitResult hitResult) {
to = hitResult;
return RESULT;
}

public static class Result implements ITargetRedirector.Result {

}

}
Loading

0 comments on commit 8f4b41e

Please sign in to comment.