Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/client/java/yellowbirb/birbaddons/config/ConfigInteger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package yellowbirb.birbaddons.config;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import yellowbirb.birbaddons.util.Utils;

import java.util.function.Consumer;

public class ConfigInteger extends ConfigValue<Integer>{
public ConfigInteger(String featureKey, String valueKey, Integer defaultValue) {
super(featureKey, valueKey, defaultValue);
}

@Override
public Integer getFromJsonElement(JsonElement json) {
return json.getAsInt();
}

@Override
public JsonElement getAsJsonElement(Integer value) {
return new JsonPrimitive(value);
}

@Override
public LiteralArgumentBuilder<FabricClientCommandSource> getCommand(Consumer<Integer> consumer) {
return ClientCommands.literal(getValueKey().toLowerCase())
.then(ClientCommands.argument("integer", IntegerArgumentType.integer())
.executes((ctx) -> {
int arg = IntegerArgumentType.getInteger(ctx, "integer");
set(arg);
consumer.accept(arg);
Utils.displayMessage(ctx.getSource().getPlayer(), "set " + getValueKey() + " to " + arg);
return 1;
}));
}
}
188 changes: 143 additions & 45 deletions src/client/java/yellowbirb/birbaddons/feature/impl/Theodolite.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,178 @@
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLevelEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.StringTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import yellowbirb.birbaddons.config.ConfigBoolean;
import yellowbirb.birbaddons.config.ConfigInteger;
import yellowbirb.birbaddons.event.ReceiveGameMessageEvent;
import yellowbirb.birbaddons.feature.Feature;
import yellowbirb.birbaddons.gui.mainmenu.FeatureSettingsPopup;
import yellowbirb.birbaddons.render.RenderManager;
import yellowbirb.birbaddons.render.RenderUtils;
import yellowbirb.birbaddons.util.Utils;

import java.awt.*;
import java.util.function.Consumer;

public class Theodolite extends Feature {

private static final String THEODOLITE_MESSAGE = "The target is around [0-9]+ blocks (below|above), at a [1-9][0-9]? degrees angle!";
private static final String PELT_REWARD_MESSAGE = "Killing the animal rewarded you [1-9][0-9]? pelts.";
private final ConfigInteger maxResults;
private final ConfigBoolean allowInputZero;
private final ConfigBoolean colorfulRings;

private static final String THEODOLITE_MESSAGE_A = "The target is around \\d+ blocks (below|above), at a \\d+ degrees angle!";
private static final String THEODOLITE_MESSAGE_B = "You are at the exact height!";
private static final String TASK_COMPLETED_MESSAGE = "Return to the Trapper soon to get a new animal to hunt!";

private Vec3 lastUsedAt = Vec3.ZERO;
private int boxyRingCounter = 0;

public Theodolite() {
super("Theodolite", "Theodolite");

ReceiveGameMessageEvent.register(THEODOLITE_MESSAGE, (msg) -> {
if (enabled.get()) {
String[] words = msg.split("\\s");
maxResults = new ConfigInteger(ID, "maxResults", 3);
allowInputZero = new ConfigBoolean(ID, "allowInputZero", false);
colorfulRings = new ConfigBoolean(ID, "colorfulRings", false);

UseItemCallback.EVENT.register((player, level, hand) -> {
if (isTheodoliteItem(player.getItemInHand(hand)))
lastUsedAt = player.position();
return InteractionResult.PASS;
});

int deltaY = Integer.parseInt(words[4]);
double alpha = Math.toRadians(90 - Integer.parseInt(words[9]));
UseBlockCallback.EVENT.register((player, level, hand, result) -> {
if (isTheodoliteItem(player.getItemInHand(hand)))
lastUsedAt = player.position();
return InteractionResult.PASS;
});

LocalPlayer player = Minecraft.getInstance().player;
// can only be called in-game, so there must for sure be a player riiiiight
assert player != null;
ReceiveGameMessageEvent.register(THEODOLITE_MESSAGE_A, (msg) -> {
if (enabled.get()) {
String[] words = msg.split("\\s");
int rawDeltaY = Integer.parseInt(words[4]);
int rawAngle = Integer.parseInt(words[9]);

if (Integer.parseInt(words[9]) == 0) {
Utils.displayMessage("Cannot calculate with 0 degree angle");
if (!allowInputZero.get() && rawAngle == 0) {
Utils.displayMessage("Skipping 0 degree angle");
return;
}

float deltaY = rawDeltaY;
float deltaY1 = rawDeltaY + 2.5F;
float deltaY2 = rawDeltaY == 5 ? 0.5F : rawDeltaY - 2.5F;

if (words[6].equals("below,")) {
deltaY = -deltaY;
deltaY1 = -deltaY1;
deltaY2 = -deltaY2;
}

float angleMargin = 2.5F;
float heightMargin = 2.5F;

int boxyRingInnerColorR = 255;
int boxyRingInnerColorG = 0;
int boxyRingInnerColorB = 0;
int boxyRingInnerColorA = 255;
boolean boxyRingInnerVisibleThroughWalls = true;

int boxyRingOuterCornerColorR = 0;
int boxyRingOuterCornerColorG = 255;
int boxyRingOuterCornerColorB = 0;
int boxyRingOuterCornerColorA = 255;
boolean boxyRingOuterCornerVisibleThroughWalls = false;

int boxyRingOuterPlaneColorR = 0;
int boxyRingOuterPlaneColorG = 255;
int boxyRingOuterPlaneColorB = 0;
int boxyRingOuterPlaneColorA = 50;
boolean boxyRingOuterPlaneVisibleThroughWalls = false;

RenderManager.add(RenderUtils.createBoxyRing(ID, "boxyRing" + boxyRingCounter++,
alpha, deltaY, (float) player.getX(), (float) player.getY(), (float) player.getZ(),
angleMargin, heightMargin,
boxyRingInnerColorR, boxyRingInnerColorG, boxyRingInnerColorB, boxyRingInnerColorA, boxyRingInnerVisibleThroughWalls,
boxyRingOuterCornerColorR, boxyRingOuterCornerColorG, boxyRingOuterCornerColorB, boxyRingOuterCornerColorA, boxyRingOuterCornerVisibleThroughWalls,
boxyRingOuterPlaneColorR, boxyRingOuterPlaneColorG, boxyRingOuterPlaneColorB, boxyRingOuterPlaneColorA, boxyRingOuterPlaneVisibleThroughWalls));
double angle = 90 - rawAngle;
double angle1 = Math.min(angle + 2.5, 90);
double angle2 = Math.max(angle - 2.5, 0);

process(deltaY, deltaY1, deltaY2, angle, angle1, angle2);
}
});

ReceiveGameMessageEvent.register(THEODOLITE_MESSAGE_B, (_) -> {
if (enabled.get()) {
if (!allowInputZero.get()) {
Utils.displayMessage("Skipping exact height");
return;
}

process(0.0F, 0.5F, -0.5F,
0, 90, 0);
}
});

ReceiveGameMessageEvent.register(PELT_REWARD_MESSAGE, (_) -> RenderManager.clear(ID));
ReceiveGameMessageEvent.register(TASK_COMPLETED_MESSAGE, (_) -> clear());

ClientPlayConnectionEvents.DISCONNECT.register((_, _) -> clear());
ClientLevelEvents.AFTER_CLIENT_LEVEL_CHANGE.register((_, _) -> clear());
}

private static boolean isTheodoliteItem(ItemStack itemStack) {
var tmp = itemStack.get(DataComponents.CUSTOM_DATA);
if (tmp != null) {
var tmp2 = tmp.copyTag().get("id");
if (tmp2 instanceof StringTag(String value)) {
return "TALBOTS_THEODOLITE".equals(value);
}
}
return false;
}

private void process(float deltaY, float deltaY1, float deltaY2,
double angle, double angle1, double angle2) {

int boxyRingInnerColor, boxyRingOuterCornerColor, boxyRingOuterPlaneColor;

if (colorfulRings.get()) {
float hue = (float) boxyRingCounter / maxResults.get() + 1.0F / 3;

boxyRingInnerColor = Color.HSBtoRGB(hue, 1.0F, 0.8F);
boxyRingOuterCornerColor = Color.HSBtoRGB(hue, 1.0F, 1.0F);
boxyRingOuterPlaneColor = Color.HSBtoRGB(hue, 0.8F, 1.0F) & 0x00FFFFFF | 0x32000000;
} else {
boxyRingInnerColor = 0xFFFF0000;
boxyRingOuterCornerColor = 0xFF00FF00;
boxyRingOuterPlaneColor = 0x3200FF00;
}

boolean boxyRingInnerVisibleThroughWalls = true;
boolean boxyRingOuterCornerVisibleThroughWalls = false;
boolean boxyRingOuterPlaneVisibleThroughWalls = false;

boxyRingCounter = (boxyRingCounter + 1) % maxResults.get();

RenderManager.add(RenderUtils.createBoxyRing(ID, "boxyRing" + boxyRingCounter,
(float) lastUsedAt.x, (float) lastUsedAt.y, (float) lastUsedAt.z,
Math.toRadians(angle), Math.toRadians(angle1), Math.toRadians(angle2),
deltaY, deltaY1, deltaY2,
boxyRingInnerColor, boxyRingInnerVisibleThroughWalls,
boxyRingOuterCornerColor, boxyRingOuterCornerVisibleThroughWalls,
boxyRingOuterPlaneColor, boxyRingOuterPlaneVisibleThroughWalls));
}

ClientPlayConnectionEvents.DISCONNECT.register((_, _) -> RenderManager.clear(ID));
ClientLevelEvents.AFTER_CLIENT_LEVEL_CHANGE.register((_, _) -> RenderManager.clear(ID));
private void clear() {
RenderManager.clear(ID);
boxyRingCounter = 0;
}

@Override
public void onDisable() {
RenderManager.clear(ID);
clear();
}

@Override
public LiteralArgumentBuilder<FabricClientCommandSource> getCommand() {
LiteralArgumentBuilder<FabricClientCommandSource> command = super.getCommand();

LiteralArgumentBuilder<FabricClientCommandSource> set = ClientCommands.literal("set");

set.then(maxResults.getCommand());
set.then(allowInputZero.getCommand());
set.then(colorfulRings.getCommand());

command.then(set);

LiteralArgumentBuilder<FabricClientCommandSource> clear = ClientCommands.literal("clear").executes((context) -> {
RenderManager.clear(ID);
clear();
Utils.displayMessage(context.getSource().getPlayer(), "Cleared all Theodolite objects drawn in the world");
return 1;
});
Expand All @@ -107,6 +193,7 @@ public LiteralArgumentBuilder<FabricClientCommandSource> getCommand() {
if (!enabled()) {
Utils.displayMessage("Feature is not enabled!");
} else {
lastUsedAt = context.getSource().getPlayer().position();
int deltay = IntegerArgumentType.getInteger(context, "deltay");
int angle = IntegerArgumentType.getInteger(context, "angle");
if (deltay == 0) {
Expand All @@ -130,4 +217,15 @@ public LiteralArgumentBuilder<FabricClientCommandSource> getCommand() {
return command;
}

@Override
public FeatureSettingsPopup getSettingsPopup(int x, int y, int width, int height, Consumer<GuiEventListener> removeWidgetConsumer) {
return new FeatureSettingsPopup(x, y, width, height, removeWidgetConsumer, this.name){
@Override
protected void initSettings() {
addSwitch("Allow 0 as input", allowInputZero);
addSwitch("Colorful Rings", colorfulRings);
}
};
}

}
47 changes: 23 additions & 24 deletions src/client/java/yellowbirb/birbaddons/render/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,40 @@
public class RenderUtils {

public static CompoundShape createBoxyRing(String featureID, String ID,
double alpha, int deltaY, float playerX, float playerY, float playerZ,
float angleMargin, float heightMargin,
int boxyRingInnerColorR, int boxyRingInnerColorG, int boxyRingInnerColorB, int boxyRingInnerColorA, boolean boxyRingInnerVisibleThroughWalls,
int boxyRingOuterCornerColorR, int boxyRingOuterCornerColorG, int boxyRingOuterCornerColorB, int boxyRingOuterCornerColorA, boolean boxyRingOuterCornerVisibleThroughWalls,
int boxyRingOuterPlaneColorR, int boxyRingOuterPlaneColorG, int boxyRingOuterPlaneColorB, int boxyRingOuterPlaneColorA, boolean boxyRingOuterPlaneVisibleThroughWalls) {
float playerX, float playerY, float playerZ,
double alpha, double alpha1, double alpha2,
float deltaY, float deltaY1, float deltaY2,
int boxyRingInnerColor, boolean boxyRingInnerVisibleThroughWalls,
int boxyRingOuterCornerColor, boolean boxyRingOuterCornerVisibleThroughWalls,
int boxyRingOuterPlaneColor, boolean boxyRingOuterPlaneVisibleThroughWalls) {

double margin_rad = Math.toRadians(angleMargin);
float radius1_1 = (float) (Math.min(Math.abs(Math.tan(alpha1) * deltaY1), 2e3));
float radius1_2 = (float) (Math.min(Math.abs(Math.tan(alpha1) * deltaY2), 2e3));

float radius1_1 = (float) (Math.abs(Math.tan(alpha + margin_rad) * (deltaY + heightMargin)));
float radius1_2 = (float) (Math.abs(Math.tan(alpha + margin_rad) * (deltaY - heightMargin)));
float radius2 = (float) (Math.min(Math.abs(Math.tan(alpha) * deltaY), 2e3));

float radius2 = (float) (Math.abs(Math.tan(alpha) * deltaY));

float radius3_1 = (float) (Math.abs(Math.tan(alpha - margin_rad) * (deltaY + heightMargin)));
float radius3_2 = (float) (Math.abs(Math.tan(alpha - margin_rad) * (deltaY - heightMargin)));
float radius3_1 = (float) (Math.abs(Math.tan(alpha2) * deltaY1));
float radius3_2 = (float) (Math.abs(Math.tan(alpha2) * deltaY2));

float segmentLength = 0.5f;
// resampling segmentAmount to draw clean InterCircleStrips
int segmentAmount = max(8, (int) round((PI * max(radius1_1, max(radius1_2, max(radius2, max(radius3_1, radius3_2)))) * 2) / segmentLength));
float lowY = playerY + deltaY - heightMargin;
float highY = playerY + deltaY + heightMargin;
int segmentAmount = (int) round(clamp((PI * max(radius1_1, radius1_2) * 2) / segmentLength, 8, 128));
float highY = playerY + deltaY1;
float lowY = playerY + deltaY2;

return new CompoundShape(featureID, ID,
new CircleXZ(radius1_1, segmentAmount, playerX, highY, playerZ, boxyRingOuterCornerColorR, boxyRingOuterCornerColorG, boxyRingOuterCornerColorB, boxyRingOuterCornerColorA, boxyRingOuterCornerVisibleThroughWalls),
new CircleXZ(radius1_2, segmentAmount, playerX, lowY , playerZ, boxyRingOuterCornerColorR, boxyRingOuterCornerColorG, boxyRingOuterCornerColorB, boxyRingOuterCornerColorA, boxyRingOuterCornerVisibleThroughWalls),
new CircleXZ(radius1_1, segmentAmount, playerX, highY, playerZ, boxyRingOuterCornerColor, boxyRingOuterCornerVisibleThroughWalls),
new CircleXZ(radius1_2, segmentAmount, playerX, lowY , playerZ, boxyRingOuterCornerColor, boxyRingOuterCornerVisibleThroughWalls),

new CircleXZ(radius2, segmentAmount, playerX, playerY + deltaY, playerZ, boxyRingInnerColorR, boxyRingInnerColorG, boxyRingInnerColorB, boxyRingInnerColorA, 2, boxyRingInnerVisibleThroughWalls),
new CircleXZ(radius2, segmentAmount, playerX, playerY + deltaY, playerZ, boxyRingInnerColor, 2, boxyRingInnerVisibleThroughWalls),

new CircleXZ(radius3_1, segmentAmount, playerX, highY, playerZ, boxyRingOuterCornerColorR, boxyRingOuterCornerColorG, boxyRingOuterCornerColorB, boxyRingOuterCornerColorA, boxyRingOuterCornerVisibleThroughWalls),
new CircleXZ(radius3_2, segmentAmount, playerX, lowY , playerZ, boxyRingOuterCornerColorR, boxyRingOuterCornerColorG, boxyRingOuterCornerColorB, boxyRingOuterCornerColorA, boxyRingOuterCornerVisibleThroughWalls),
new CircleXZ(radius3_1, segmentAmount, playerX, highY, playerZ, boxyRingOuterCornerColor, boxyRingOuterCornerVisibleThroughWalls),
new CircleXZ(radius3_2, segmentAmount, playerX, lowY , playerZ, boxyRingOuterCornerColor, boxyRingOuterCornerVisibleThroughWalls),

new InterCircleStrip(radius1_1, radius1_2, segmentAmount, playerX, highY, lowY , playerZ, boxyRingOuterPlaneColorR, boxyRingOuterPlaneColorG, boxyRingOuterPlaneColorB, boxyRingOuterPlaneColorA, boxyRingOuterPlaneVisibleThroughWalls),
new InterCircleStrip(radius3_1, radius3_2, segmentAmount, playerX, highY, lowY , playerZ, boxyRingOuterPlaneColorR, boxyRingOuterPlaneColorG, boxyRingOuterPlaneColorB, boxyRingOuterPlaneColorA, boxyRingOuterPlaneVisibleThroughWalls),
new InterCircleStrip(radius1_1, radius3_1, segmentAmount, playerX, highY, highY, playerZ, boxyRingOuterPlaneColorR, boxyRingOuterPlaneColorG, boxyRingOuterPlaneColorB, boxyRingOuterPlaneColorA, boxyRingOuterPlaneVisibleThroughWalls),
new InterCircleStrip(radius1_2, radius3_2, segmentAmount, playerX, lowY , lowY , playerZ, boxyRingOuterPlaneColorR, boxyRingOuterPlaneColorG, boxyRingOuterPlaneColorB, boxyRingOuterPlaneColorA, boxyRingOuterPlaneVisibleThroughWalls)
new InterCircleStrip(radius1_1, radius1_2, segmentAmount, playerX, highY, lowY , playerZ, boxyRingOuterPlaneColor, boxyRingOuterPlaneVisibleThroughWalls),
new InterCircleStrip(radius3_1, radius3_2, segmentAmount, playerX, highY, lowY , playerZ, boxyRingOuterPlaneColor, boxyRingOuterPlaneVisibleThroughWalls),
new InterCircleStrip(radius1_1, radius3_1, segmentAmount, playerX, highY, highY, playerZ, boxyRingOuterPlaneColor, boxyRingOuterPlaneVisibleThroughWalls),
new InterCircleStrip(radius1_2, radius3_2, segmentAmount, playerX, lowY , lowY , playerZ, boxyRingOuterPlaneColor, boxyRingOuterPlaneVisibleThroughWalls)
);
}
}
Loading