ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemEngineerGoggles.java

99 lines
3.6 KiB
Java
Raw Normal View History

2017-02-13 18:42:33 +01:00
/*
* This file ("ItemInfraredGoggles.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.items;
2017-02-14 18:18:38 +01:00
import de.ellpeck.actuallyadditions.api.misc.IGoggles;
2017-02-13 18:42:33 +01:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemArmorAA;
import de.ellpeck.actuallyadditions.mod.material.ArmorMaterials;
2017-02-13 18:42:33 +01:00
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2017-02-13 20:12:47 +01:00
import io.netty.util.internal.ConcurrentSet;
2023-01-15 00:26:37 +01:00
import net.minecraft.client.Minecraft;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.AABB;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.TickEvent;
import java.util.List;
import java.util.Set;
2017-02-13 18:42:33 +01:00
2019-05-02 09:10:29 +02:00
public class ItemEngineerGoggles extends ItemArmorAA implements IGoggles {
2017-02-13 18:42:33 +01:00
2019-02-27 19:53:05 +01:00
private final Set<Entity> cachedGlowingEntities = new ConcurrentSet<>();
2017-02-13 20:12:47 +01:00
2017-02-14 18:18:38 +01:00
private final boolean displayMobs;
public ItemEngineerGoggles(boolean displayMobs) {
2024-03-03 01:20:53 +01:00
super(ArmorMaterials.GOGGLES, Type.HELMET, ActuallyItems.defaultProps().setNoRepair().durability(0));
2017-02-14 18:18:38 +01:00
this.displayMobs = displayMobs;
2017-02-13 18:42:33 +01:00
2024-03-04 20:21:48 +01:00
if (FMLEnvironment.dist.isClient()) {
NeoForge.EVENT_BUS.register(this);
}
2023-01-16 22:47:31 +01:00
2017-02-13 18:42:33 +01:00
}
2024-03-02 21:23:08 +01:00
public static boolean isWearing(Player player) {
ItemStack face = player.getInventory().armor.get(3);
2017-04-01 19:40:39 +02:00
return StackUtil.isValid(face) && face.getItem() instanceof IGoggles;
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2017-02-13 18:42:33 +01:00
@SubscribeEvent
public void onClientTick(TickEvent.ClientTickEvent event) {
2024-03-02 21:23:08 +01:00
Player player = Minecraft.getInstance().player;
2019-05-02 09:10:29 +02:00
if (player != null && isWearing(player)) {
2024-03-02 21:23:08 +01:00
ItemStack face = player.getInventory().armor.get(3);
2019-05-02 09:10:29 +02:00
if (((IGoggles) face.getItem()).displaySpectralMobs()) {
2017-02-14 18:18:38 +01:00
double range = 8;
2024-03-02 21:23:08 +01:00
AABB aabb = new AABB(player.getX() - range, player.getY() - range, player.getZ() - range, player.getX() + range, player.getY() + range, player.getZ() + range);
2024-03-03 01:20:53 +01:00
List<Entity> entities = player.level().getEntitiesOfClass(Entity.class, aabb);
2019-05-02 09:10:29 +02:00
if (entities != null && !entities.isEmpty()) {
2017-02-14 18:18:38 +01:00
this.cachedGlowingEntities.addAll(entities);
}
2017-02-13 18:42:33 +01:00
2019-05-02 09:10:29 +02:00
if (!this.cachedGlowingEntities.isEmpty()) {
for (Entity entity : this.cachedGlowingEntities) {
if (!entity.isAlive() || entity.distanceToSqr(player.getX(), player.getY(), player.getZ()) > range * range) {
2024-03-02 21:23:08 +01:00
entity.setGlowingTag(false);
2017-02-13 20:12:47 +01:00
2017-02-14 18:18:38 +01:00
this.cachedGlowingEntities.remove(entity);
2019-05-02 09:10:29 +02:00
} else {
2024-03-02 21:23:08 +01:00
entity.setGlowingTag(true);
2017-02-14 18:18:38 +01:00
}
2017-02-13 20:12:47 +01:00
}
}
2017-02-14 18:18:38 +01:00
return;
2017-02-13 20:12:47 +01:00
}
}
2017-02-14 18:18:38 +01:00
2019-05-02 09:10:29 +02:00
if (!this.cachedGlowingEntities.isEmpty()) {
for (Entity entity : this.cachedGlowingEntities) {
if (entity.isAlive()) {
2024-03-02 21:23:08 +01:00
entity.setGlowingTag(false);
2017-02-13 18:42:33 +01:00
}
}
2017-02-14 18:18:38 +01:00
this.cachedGlowingEntities.clear();
2023-01-15 00:26:37 +01:00
}
2017-02-13 18:42:33 +01:00
}
2017-02-14 18:18:38 +01:00
@Override
2019-05-02 09:10:29 +02:00
public boolean displaySpectralMobs() {
2017-02-14 18:18:38 +01:00
return this.displayMobs;
}
2017-02-13 18:42:33 +01:00
}