Skip to content

Commit

Permalink
Switch to GUI overlay to render compass info on HUD (#180)
Browse files Browse the repository at this point in the history
* Switch to GUI overlay to render compass info on HUD (#163, #168)

* Update changelog
  • Loading branch information
MattCzyr authored May 2, 2024
1 parent 1ed0ba7 commit a9af552
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 171 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 1.11.5
#### Forge 1.20.4, 1.20.2
- Fixed compass HUD info being rendered twice when certain mods are installed

## Version 1.11.4
#### Forge 1.20.4, 1.20.2
- Fixed clients not being allowed to connect to servers that did not have Nature's Compass installed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod_name=Nature's Compass
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=CC BY-NC-SA 4.0
# The mod version. See https://semver.org/
mod_version=1.20.4-1.11.4-forge
mod_version=1.20.4-1.11.5-forge
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
97 changes: 0 additions & 97 deletions src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,23 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.chaosthedude.naturescompass.client.ClientEventHandler;
import com.chaosthedude.naturescompass.config.ConfigHandler;
import com.chaosthedude.naturescompass.items.NaturesCompassItem;
import com.chaosthedude.naturescompass.network.CompassSearchPacket;
import com.chaosthedude.naturescompass.network.SyncPacket;
import com.chaosthedude.naturescompass.network.TeleportPacket;
import com.chaosthedude.naturescompass.util.CompassState;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;

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.CreativeModeTabs;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.network.Channel;
Expand Down Expand Up @@ -71,10 +56,6 @@ public NaturesCompass() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::preInit);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::buildCreativeTabContents);

DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientInit);
});

ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ConfigHandler.GENERAL_SPEC);
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ConfigHandler.CLIENT_SPEC);

Expand Down Expand Up @@ -106,82 +87,4 @@ public void registerNodes(PermissionGatherEvent.Nodes event) {
event.addNodes(TELEPORT_PERMISSION);
}

@OnlyIn(Dist.CLIENT)
public void clientInit(FMLClientSetupEvent event) {
MinecraftForge.EVENT_BUS.register(new ClientEventHandler());

event.enqueueWork(() -> {
ItemProperties.register(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) {
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;
}
});
});
}

}

This file was deleted.

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());
}

}
Loading

0 comments on commit a9af552

Please sign in to comment.