aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/rosegoldaddons/features/EntityReach.java
blob: f59c37ae08dff4dc6d46a717f590169c10be42b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package rosegoldaddons.features;

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.entity.Entity;
import net.minecraft.entity.boss.EntityWither;
import net.minecraft.entity.item.EntityArmorStand;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.item.ItemSkull;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Vec3;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import rosegoldaddons.Main;
import rosegoldaddons.utils.RenderUtils;

import java.awt.*;
import java.util.ArrayList;

public class EntityReach {
    private static Entity toInteract;
    private static final ArrayList<Entity> solved = new ArrayList<>();

    @SubscribeEvent
    public void onInteract(PlayerInteractEvent event) {
        if (!Main.configFile.entityReach) return;
        if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR) {
            if (toInteract != null) {
                if (toInteract instanceof EntityArmorStand) {
                    interactWithEntity2(toInteract);
                }
                interactWithEntity(toInteract);
                toInteract = null;
            }
        } else if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {
            if (toInteract != null) {
                if (toInteract instanceof EntityArmorStand) {
                    interactWithEntity2(toInteract);
                    solved.add(toInteract);
                }
                toInteract = null;
            }
        }
    }

    @SubscribeEvent
    public void clear(WorldEvent.Load event) {
        solved.clear();
    }

    @SubscribeEvent
    public void renderWorld(RenderWorldLastEvent event) {
        if (!Main.configFile.entityReach) return;
        if (toInteract != null) {
            Entity stand = getClosestArmorStand(toInteract);
            String entityName = "Null";
            if(stand != null) {
                entityName = stand.getCustomNameTag();
            }
            if(entityName.equals("") && stand != null) {
                entityName = stand.getName();
            }
            RenderUtils.drawEntityBox(toInteract, Color.RED, true, event.partialTicks);
            RenderUtils.renderWaypointText(entityName, toInteract.posX, toInteract.posY + toInteract.height, toInteract.posZ, event.partialTicks);
        }
        boolean found = false;
        ArrayList<Entity> entities = getAllEntitiesInRange();
        for (Entity entity : entities) {
            if (isLookingAtAABB(entity.getEntityBoundingBox(), event)) {
                toInteract = entity;
                found = true;
            }
            if(entity instanceof EntityArmorStand) {
                ItemStack itemStack = ((EntityArmorStand) entity).getCurrentArmor(3);
                if (itemStack != null && itemStack.getItem() instanceof ItemSkull) {
                    if(itemStack.serializeNBT().getCompoundTag("tag").getCompoundTag("SkullOwner").getCompoundTag("Properties").toString().contains("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYjk2OTIzYWQyNDczMTAwMDdmNmFlNWQzMjZkODQ3YWQ1Mzg2NGNmMTZjMzU2NWExODFkYzhlNmIyMGJlMjM4NyJ9fX0=")) {
                        if(solved.contains(entity)) {
                            RenderUtils.drawEntityBox(entity, Color.YELLOW, true, event.partialTicks);
                        } else {
                            RenderUtils.drawEntityBox(entity, Color.MAGENTA, true, event.partialTicks);
                        }
                    }
                }
            }
        }
        if (!Main.configFile.sticky && !found) {
            toInteract = null;
        }
    }

    private static Entity getClosestArmorStand(Entity entity) {
        Entity closest = null;
        double smallest = 9999;
        for (Entity entity1 : (Minecraft.getMinecraft().theWorld.loadedEntityList)) {
            if (entity1 instanceof EntityArmorStand) {
                double dist = entity.getDistanceToEntity(entity1);
                if(dist < smallest) {
                    smallest = dist;
                    closest = entity1;
                }
            }
        }
        return closest;
    }

    private static boolean isLookingAtAABB(AxisAlignedBB aabb, RenderWorldLastEvent event) {
        Vec3 position = new Vec3(Minecraft.getMinecraft().thePlayer.posX, (Minecraft.getMinecraft().thePlayer.posY + Minecraft.getMinecraft().thePlayer.getEyeHeight()), Minecraft.getMinecraft().thePlayer.posZ);
        Vec3 look = Minecraft.getMinecraft().thePlayer.getLook(event.partialTicks);
        look = scaleVec(look, 0.2F);
        for (int i = 0; i < 320; i++) {
            if (aabb.minX <= position.xCoord && aabb.maxX >= position.xCoord && aabb.minY <= position.yCoord && aabb.maxY >= position.yCoord && aabb.minZ <= position.zCoord && aabb.maxZ >= position.zCoord) {
                return true;
            }
            position = position.add(look);
        }

        return false;
    }

    private static ArrayList<Entity> getAllEntitiesInRange() {
        ArrayList<Entity> entities = new ArrayList<>();
        for (Entity entity1 : (Minecraft.getMinecraft().theWorld.loadedEntityList)) {
            if (!(entity1 instanceof EntityItem) && !(entity1 instanceof EntityXPOrb) &&!(entity1 instanceof EntityWither) && !(entity1 instanceof EntityPlayerSP)) {
                entities.add(entity1);
            }
        }
        return entities;
    }

    private static void interactWithEntity(Entity entity) {
        PlayerControllerMP playerControllerMP = Minecraft.getMinecraft().playerController;
        playerControllerMP.interactWithEntitySendPacket(Minecraft.getMinecraft().thePlayer, entity);
    }

    private static void interactWithEntity2(Entity entity) {
        PlayerControllerMP playerControllerMP = Minecraft.getMinecraft().playerController;
        playerControllerMP.isPlayerRightClickingOnEntity(Minecraft.getMinecraft().thePlayer, entity, Minecraft.getMinecraft().objectMouseOver);
    }


    private static Vec3 scaleVec(Vec3 vec, float f) {
        return new Vec3(vec.xCoord * (double) f, vec.yCoord * (double) f, vec.zCoord * (double) f);
    }

}