NaturesAura/src/main/java/de/ellpeck/naturesaura/renderers/SupporterFancyHandler.java

102 lines
3.6 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.renderers;
2020-01-21 21:04:44 +01:00
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2021-12-08 00:31:29 +01:00
import net.minecraft.client.CameraType;
import net.minecraft.client.Minecraft;
2021-12-08 00:31:29 +01:00
import net.minecraft.client.renderer.BiomeColors;
import net.minecraft.world.entity.player.PlayerModelPart;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
public class SupporterFancyHandler {
public static final Map<String, FancyInfo> FANCY_INFOS = new HashMap<>();
public SupporterFancyHandler() {
new FetchThread();
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
if (event.phase != TickEvent.Phase.END)
return;
2021-12-08 00:31:29 +01:00
var player = event.player;
2023-07-08 12:32:27 +02:00
if (!player.level().isClientSide)
return;
2021-12-08 00:31:29 +01:00
if (player.isInvisible() || !player.isModelPartShown(PlayerModelPart.CAPE))
return;
2021-12-08 00:31:29 +01:00
var mc = Minecraft.getInstance();
if (player == mc.player && mc.options.getCameraType() == CameraType.FIRST_PERSON)
return;
2022-06-27 15:24:04 +02:00
var info = SupporterFancyHandler.FANCY_INFOS.get(player.getGameProfile().getName());
if (info == null)
return;
2023-07-08 12:32:27 +02:00
var rand = player.level().random;
if (rand.nextFloat() >= 0.75F) {
int color;
if (info.tier == 1) {
2021-12-15 16:24:53 +01:00
var pos = player.blockPosition();
2023-07-08 12:32:27 +02:00
color = BiomeColors.getAverageGrassColor(player.level(), pos);
} else {
color = info.color;
}
NaturesAuraAPI.instance().spawnMagicParticle(
2021-12-08 00:31:29 +01:00
player.getX() + rand.nextGaussian() * 0.15F,
player.getY() + rand.nextFloat() * 1.8F,
player.getZ() + rand.nextGaussian() * 0.15F,
rand.nextGaussian() * 0.01F,
rand.nextFloat() * 0.01F,
rand.nextGaussian() * 0.01F,
color, rand.nextFloat() + 1F, rand.nextInt(50) + 50, 0F, false, true);
}
}
2021-12-08 00:31:29 +01:00
public record FancyInfo(int tier, int color) {
}
private static class FetchThread extends Thread {
2021-12-08 00:31:29 +01:00
public FetchThread() {
this.setName(NaturesAura.MOD_ID + "_support_fetcher");
this.setDaemon(true);
this.start();
}
@Override
public void run() {
try {
2021-12-08 00:31:29 +01:00
var url = new URL("https://raw.githubusercontent.com/Ellpeck/NaturesAura/main/supporters.json");
var reader = new JsonReader(new InputStreamReader(url.openStream()));
2021-12-08 00:31:29 +01:00
var main = JsonParser.parseReader(reader).getAsJsonObject();
for (var entry : main.entrySet()) {
var object = entry.getValue().getAsJsonObject();
var tier = object.get("tier").getAsInt();
var color = object.has("color") ? Integer.parseInt(object.get("color").getAsString(), 16) : 0;
2022-06-27 15:24:04 +02:00
SupporterFancyHandler.FANCY_INFOS.put(entry.getKey(), new FancyInfo(tier, color));
}
reader.close();
} catch (Exception e) {
NaturesAura.LOGGER.warn("Fetching supporter information failed", e);
}
}
}
}