automatic target selection
This commit is contained in:
@@ -2,76 +2,18 @@ package com.kasetoatz.elytramace;
|
||||
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.option.KeyBinding;
|
||||
import net.minecraft.client.util.InputUtil;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.attribute.EntityAttributes;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.entity.projectile.ProjectileUtil;
|
||||
import net.minecraft.item.*;
|
||||
import net.minecraft.predicate.entity.EntityPredicates;
|
||||
import net.minecraft.registry.tag.ItemTags;
|
||||
import net.minecraft.screen.slot.SlotActionType;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.hit.EntityHitResult;
|
||||
import net.minecraft.util.hit.HitResult;
|
||||
import net.minecraft.util.math.Box;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class ElytraMace implements ClientModInitializer
|
||||
{
|
||||
private MinecraftClient client;
|
||||
private final KeyBinding.Category keybindCategory = KeyBinding.Category.create(Identifier.of("elytramace", "elytramace"));
|
||||
private final KeyBinding selectTargetKeybind = KeyBindingHelper.registerKeyBinding(
|
||||
new KeyBinding(
|
||||
"key.elytramace.select_target",
|
||||
InputUtil.Type.KEYSYM,
|
||||
GLFW.GLFW_KEY_KP_8,
|
||||
keybindCategory
|
||||
)
|
||||
);
|
||||
public static LivingEntity TARGET;
|
||||
private boolean unequipped = false;
|
||||
private boolean lockTarget = false;
|
||||
|
||||
private LivingEntity getTarget()
|
||||
{
|
||||
Entity camera = client.getCameraEntity();
|
||||
if (camera == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
HitResult hitResult = camera.raycast(client.options.getSimulationDistance().getValue() * 16, 0F, false);
|
||||
Vec3d start = camera.getCameraPosVec(0F);
|
||||
double squaredDist = hitResult.getPos().squaredDistanceTo(start);
|
||||
double newDist = Math.sqrt(squaredDist);
|
||||
Vec3d rotation = camera.getRotationVec(0F);
|
||||
Vec3d end = start.add(rotation.multiply(newDist));
|
||||
Box box = camera.getBoundingBox().stretch(rotation.multiply(newDist)).expand(1D);
|
||||
EntityHitResult result = ProjectileUtil.raycast(camera, start, end, box, EntityPredicates.CAN_HIT, squaredDist);
|
||||
if (result == null || !(result.getEntity() instanceof LivingEntity))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return (LivingEntity)result.getEntity();
|
||||
}
|
||||
|
||||
private void updateTarget()
|
||||
{
|
||||
LivingEntity nextTarget = getTarget();
|
||||
if (nextTarget == TARGET)
|
||||
{
|
||||
TARGET = null;
|
||||
}
|
||||
else if (nextTarget != null)
|
||||
{
|
||||
TARGET = nextTarget;
|
||||
}
|
||||
}
|
||||
|
||||
private int getItem(PlayerInventory inventory, Item item)
|
||||
{
|
||||
@@ -105,44 +47,37 @@ public class ElytraMace implements ClientModInitializer
|
||||
@Override
|
||||
public void onInitializeClient()
|
||||
{
|
||||
client = MinecraftClient.getInstance();
|
||||
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||
if (client.player == null || client.interactionManager == null || client.getNetworkHandler() == null)
|
||||
if (client.player == null || client.interactionManager == null || client.world == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
LivingEntity target = client.world.getEntitiesByClass(LivingEntity.class, client.player.getBoundingBox().expand(client.player.getAttributeValue(EntityAttributes.ENTITY_INTERACTION_RANGE) + client.player.getVelocity().length() * 2), e -> e != client.player).stream().min(Comparator.comparingDouble(client.player::squaredDistanceTo)).orElse(null);
|
||||
if (unequipped && !client.player.isGliding())
|
||||
{
|
||||
client.interactionManager.clickSlot(client.player.playerScreenHandler.syncId, 6, 0, SlotActionType.PICKUP, client.player);
|
||||
unequipped = false;
|
||||
PlayerInventory inventory = client.player.getInventory();
|
||||
int axe = getAxe(inventory);
|
||||
int mace = getItem(inventory, Items.MACE);
|
||||
int fireworks = getItem(inventory, Items.FIREWORK_ROCKET);
|
||||
if (TARGET.isBlocking() && axe > -1)
|
||||
if (target != null)
|
||||
{
|
||||
inventory.setSelectedSlot(axe);
|
||||
client.interactionManager.attackEntity(client.player, TARGET);
|
||||
PlayerInventory inventory = client.player.getInventory();
|
||||
int axe = getAxe(inventory);
|
||||
int mace = getItem(inventory, Items.MACE);
|
||||
int fireworks = getItem(inventory, Items.FIREWORK_ROCKET);
|
||||
if (target.isBlocking() && axe > -1)
|
||||
{
|
||||
inventory.setSelectedSlot(axe);
|
||||
client.interactionManager.attackEntity(client.player, target);
|
||||
}
|
||||
inventory.setSelectedSlot(mace);
|
||||
client.interactionManager.attackEntity(client.player, target);
|
||||
inventory.setSelectedSlot(fireworks);
|
||||
}
|
||||
inventory.setSelectedSlot(mace);
|
||||
client.interactionManager.attackEntity(client.player, TARGET);
|
||||
inventory.setSelectedSlot(fireworks);
|
||||
lockTarget = false;
|
||||
}
|
||||
if (TARGET != null && client.player.isGliding() && !unequipped && client.player.distanceTo(TARGET) <= client.player.getAttributeValue(EntityAttributes.ENTITY_INTERACTION_RANGE) + client.player.getVelocity().length() * 2)
|
||||
if (!unequipped && target != null && client.player.isGliding())
|
||||
{
|
||||
lockTarget = true;
|
||||
client.interactionManager.clickSlot(client.player.playerScreenHandler.syncId, 6, 0, SlotActionType.PICKUP, client.player);
|
||||
unequipped = true;
|
||||
}
|
||||
while (selectTargetKeybind.wasPressed() && !lockTarget)
|
||||
{
|
||||
updateTarget();
|
||||
}
|
||||
if (TARGET != null && !TARGET.isAlive())
|
||||
{
|
||||
TARGET = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user