2019-03-01 23:54:38 +01:00
|
|
|
package de.ellpeck.naturesaura.renderers;
|
|
|
|
|
2020-01-21 21:04:44 +01:00
|
|
|
import com.google.gson.JsonParser;
|
2019-03-01 23:54:38 +01:00
|
|
|
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;
|
2019-03-01 23:54:38 +01:00
|
|
|
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;
|
2019-03-01 23:54:38 +01:00
|
|
|
|
|
|
|
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)
|
2019-03-01 23:54:38 +01:00
|
|
|
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;
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!player.level.isClientSide)
|
2019-03-01 23:54:38 +01:00
|
|
|
return;
|
2021-12-08 00:31:29 +01:00
|
|
|
if (player.isInvisible() || !player.isModelPartShown(PlayerModelPart.CAPE))
|
2019-03-01 23:54:38 +01:00
|
|
|
return;
|
2021-12-08 00:31:29 +01:00
|
|
|
var mc = Minecraft.getInstance();
|
|
|
|
if (player == mc.player && mc.options.getCameraType() == CameraType.FIRST_PERSON)
|
2019-03-01 23:54:38 +01:00
|
|
|
return;
|
2021-12-08 00:31:29 +01:00
|
|
|
var info = FANCY_INFOS.get(player.getGameProfile().getName());
|
2019-03-01 23:54:38 +01:00
|
|
|
if (info == null)
|
|
|
|
return;
|
|
|
|
|
2021-12-08 00:31:29 +01:00
|
|
|
var rand = player.level.random;
|
2019-03-01 23:54:38 +01:00
|
|
|
if (rand.nextFloat() >= 0.75F) {
|
|
|
|
int color;
|
|
|
|
if (info.tier == 1) {
|
2021-12-08 00:31:29 +01:00
|
|
|
var pos = player.getOnPos();
|
|
|
|
color = BiomeColors.getAverageGrassColor(player.level, pos);
|
2019-03-01 23:54:38 +01:00
|
|
|
} 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,
|
2019-03-01 23:54:38 +01:00
|
|
|
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) {
|
2019-03-01 23:54:38 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class FetchThread extends Thread {
|
2021-12-08 00:31:29 +01:00
|
|
|
|
2019-03-01 23:54:38 +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()));
|
2019-03-01 23:54:38 +01:00
|
|
|
|
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;
|
2019-03-01 23:54:38 +01:00
|
|
|
FANCY_INFOS.put(entry.getKey(), new FancyInfo(tier, color));
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
NaturesAura.LOGGER.warn("Fetching supporter information failed", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|