-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to GUI overlay to render compass info on HUD (#180)
- Loading branch information
Showing
7 changed files
with
189 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
src/main/java/com/chaosthedude/naturescompass/client/ClientEventHandler.java
This file was deleted.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
src/main/java/com/chaosthedude/naturescompass/client/NaturesCompassClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.chaosthedude.naturescompass.client; | ||
|
||
import com.chaosthedude.naturescompass.NaturesCompass; | ||
import com.chaosthedude.naturescompass.items.NaturesCompassItem; | ||
import com.chaosthedude.naturescompass.util.CompassState; | ||
|
||
import net.minecraft.client.multiplayer.ClientLevel; | ||
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction; | ||
import net.minecraft.client.renderer.item.ItemProperties; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.util.Mth; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.entity.decoration.ItemFrame; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraftforge.api.distmarker.Dist; | ||
import net.minecraftforge.api.distmarker.OnlyIn; | ||
import net.minecraftforge.client.event.RegisterGuiOverlaysEvent; | ||
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay; | ||
import net.minecraftforge.eventbus.api.SubscribeEvent; | ||
import net.minecraftforge.fml.common.Mod; | ||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; | ||
|
||
@Mod.EventBusSubscriber(modid = NaturesCompass.MODID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) | ||
public class NaturesCompassClient { | ||
|
||
@SubscribeEvent | ||
public static void clientInit(FMLClientSetupEvent event) { | ||
event.enqueueWork(() -> { | ||
ItemProperties.register(NaturesCompass.naturesCompass, new ResourceLocation("angle"), new ClampedItemPropertyFunction() { | ||
@OnlyIn(Dist.CLIENT) | ||
private double rotation; | ||
@OnlyIn(Dist.CLIENT) | ||
private double rota; | ||
@OnlyIn(Dist.CLIENT) | ||
private long lastUpdateTick; | ||
|
||
@OnlyIn(Dist.CLIENT) | ||
@Override | ||
public float unclampedCall(ItemStack stack, ClientLevel world, LivingEntity entityLiving, int seed) { | ||
if (entityLiving == null && !stack.isFramed()) { | ||
return 0.0F; | ||
} else { | ||
final boolean entityExists = entityLiving != null; | ||
final Entity entity = (Entity) (entityExists ? entityLiving : stack.getFrame()); | ||
if (world == null && entity.level() instanceof ClientLevel) { | ||
world = (ClientLevel) entity.level(); | ||
} | ||
|
||
double rotation = entityExists ? (double) entity.getYRot() : getFrameRotation((ItemFrame) entity); | ||
rotation = rotation % 360.0D; | ||
double adjusted = Math.PI - ((rotation - 90.0D) * 0.01745329238474369D - getAngle(world, entity, stack)); | ||
|
||
if (entityExists) { | ||
adjusted = wobble(world, adjusted); | ||
} | ||
|
||
final float f = (float) (adjusted / (Math.PI * 2D)); | ||
return Mth.positiveModulo(f, 1.0F); | ||
} | ||
} | ||
|
||
@OnlyIn(Dist.CLIENT) | ||
private double wobble(ClientLevel world, double amount) { | ||
if (world.getGameTime() != lastUpdateTick) { | ||
lastUpdateTick = world.getGameTime(); | ||
double d0 = amount - rotation; | ||
d0 = Mth.positiveModulo(d0 + Math.PI, Math.PI * 2D) - Math.PI; | ||
d0 = Mth.clamp(d0, -1.0D, 1.0D); | ||
rota += d0 * 0.1D; | ||
rota *= 0.8D; | ||
rotation += rota; | ||
} | ||
|
||
return rotation; | ||
} | ||
|
||
@OnlyIn(Dist.CLIENT) | ||
private double getFrameRotation(ItemFrame itemFrame) { | ||
Direction direction = itemFrame.getDirection(); | ||
int i = direction.getAxis().isVertical() ? 90 * direction.getAxisDirection().getStep() : 0; | ||
return (double) Mth.wrapDegrees(180 + direction.get2DDataValue() * 90 + itemFrame.getRotation() * 45 + i); | ||
} | ||
|
||
@OnlyIn(Dist.CLIENT) | ||
private double getAngle(ClientLevel world, Entity entity, ItemStack stack) { | ||
if (stack.getItem() == NaturesCompass.naturesCompass) { | ||
NaturesCompassItem compassItem = (NaturesCompassItem) stack.getItem(); | ||
BlockPos pos; | ||
if (compassItem.getState(stack) == CompassState.FOUND) { | ||
pos = new BlockPos(compassItem.getFoundBiomeX(stack), 0, compassItem.getFoundBiomeZ(stack)); | ||
} else { | ||
pos = world.getSharedSpawnPos(); | ||
} | ||
return Math.atan2((double) pos.getZ() - entity.position().z(), (double) pos.getX() - entity.position().x()); | ||
} | ||
return 0.0D; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
@SubscribeEvent | ||
public static void registerOverlay(RegisterGuiOverlaysEvent event) { | ||
event.registerAbove(VanillaGuiOverlay.BOSS_EVENT_PROGRESS.id(), "natures_compass", new NaturesCompassOverlay()); | ||
} | ||
|
||
} |
Oops, something went wrong.