-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaimmod.java
More file actions
358 lines (298 loc) · 14.7 KB
/
aimmod.java
File metadata and controls
358 lines (298 loc) · 14.7 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package com.example.aimmod;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.BlockPos;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCarpet;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent.KeyInputEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.lwjgl.input.Keyboard;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Vec3;
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
@Mod(modid = "aimmod", version = "2.7")
public class AimMod {
private static final Set<String> excludedBlocks = new HashSet<>();
private static final Set<String> priorityBlocks = new HashSet<>();
private boolean isEnabled = true;
private float normalTurnSpeed = 48.68f;
private float fastTurnSpeed = 32.0f;
private BlockPos lastTargetedBlock = null;
private int ticksSinceLastTarget = 0;
private static final int MAX_TICKS_WITHOUT_TARGET = 5;
private Configuration config;
private boolean isBreakingEnabled = false;
private int breakingCooldown = 0;
private static final int BREAKING_COOLDOWN_TICKS = 5;
private boolean isBreaking = false;
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event) {
config = new Configuration(event.getSuggestedConfigurationFile());
loadConfig();
}
@Mod.EventHandler
public void init(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(this);
ClientRegistry.registerKeyBinding(new KeyBinding("Open AimMod Menu", Keyboard.KEY_M, "AimMod"));
}
private void loadConfig() {
config.load();
String[] defaultExcludedBlocks = {"minecraft:carpet", "minecraft:bedrock"};
String[] excludedBlocksArray = config.getStringList("ExcludedBlocks", Configuration.CATEGORY_GENERAL, defaultExcludedBlocks, "Blocks excluded from AimMod targeting");
excludedBlocks.clear();
excludedBlocks.addAll(Arrays.asList(excludedBlocksArray));
String[] defaultPriorityBlocks = {"minecraft:diamond_ore", "minecraft:gold_ore"};
String[] priorityBlocksArray = config.getStringList("PriorityBlocks", Configuration.CATEGORY_GENERAL, defaultPriorityBlocks, "Blocks prioritized by AimMod");
priorityBlocks.clear();
priorityBlocks.addAll(Arrays.asList(priorityBlocksArray));
isEnabled = config.getBoolean("ModEnabled", Configuration.CATEGORY_GENERAL, true, "Is AimMod enabled?");
normalTurnSpeed = config.getFloat("NormalTurnSpeed", Configuration.CATEGORY_GENERAL, 0.5f, 0.1f, 2.0f, "AimMod normal turn speed");
fastTurnSpeed = config.getFloat("FastTurnSpeed", Configuration.CATEGORY_GENERAL, 1.0f, 0.5f, 4.0f, "AimMod fast turn speed for quick target switching");
if (config.hasChanged()) {
config.save();
}
}
private void saveConfig() {
config.get(Configuration.CATEGORY_GENERAL, "ExcludedBlocks", new String[]{}).set(excludedBlocks.toArray(new String[0]));
config.get(Configuration.CATEGORY_GENERAL, "PriorityBlocks", new String[]{}).set(priorityBlocks.toArray(new String[0]));
config.get(Configuration.CATEGORY_GENERAL, "ModEnabled", true).set(isEnabled);
config.get(Configuration.CATEGORY_GENERAL, "NormalTurnSpeed", 1.2f).set(normalTurnSpeed);
config.get(Configuration.CATEGORY_GENERAL, "FastTurnSpeed", 6.0f).set(fastTurnSpeed);
config.save();
}
private int frameCounter = 0;
private static final int UPDATE_FREQUENCY = 3; // Обновлять каждый 3-й кадр
@SubscribeEvent
public void onRenderGameOverlay(RenderGameOverlayEvent.Text event) {
if (!isEnabled) return;
frameCounter++;
if (frameCounter % UPDATE_FREQUENCY != 0) return;
Minecraft mc = Minecraft.func_71410_x();
BlockPos targetBlock = findNewTarget(mc);
if (targetBlock != null) {
aimAtBlock(targetBlock, ticksSinceLastTarget >= MAX_TICKS_WITHOUT_TARGET);
lastTargetedBlock = targetBlock;
ticksSinceLastTarget = 0;
} else {
ticksSinceLastTarget++;
}
}
private boolean isValidTarget(BlockPos pos) {
Minecraft mc = Minecraft.func_71410_x();
Block block = mc.field_71441_e.func_180495_p(pos).func_177230_c();
ResourceLocation blockRL = Block.field_149771_c.func_177774_c(block);
String blockName = blockRL != null ? blockRL.toString() : "";
// Проверяем, может ли игрок сломать блок
boolean canBreak = mc.field_71439_g.func_146099_a(block);
return !excludedBlocks.contains(blockName) &&
!(block instanceof BlockCarpet) &&
!mc.field_71441_e.func_175623_d(pos) &&
block.func_176195_g(mc.field_71441_e, pos) >= 0 &&
canBreak;
}
private boolean hasLineOfSight(BlockPos pos) {
Minecraft mc = Minecraft.func_71410_x();
Vec3 start = mc.field_71439_g.func_174824_e(1.0F);
Vec3 end = new Vec3(pos.func_177958_n() + 0.5, pos.func_177956_o() + 0.5, pos.func_177952_p() + 0.5);
MovingObjectPosition result = mc.field_71441_e.func_147447_a(start, end, false, true, false);
return result == null || result.func_178782_a().equals(pos);
}
private boolean isPriorityBlock(BlockPos pos) {
Minecraft mc = Minecraft.func_71410_x();
Block block = mc.field_71441_e.func_180495_p(pos).func_177230_c();
ResourceLocation blockRL = Block.field_149771_c.func_177774_c(block);
String blockName = blockRL != null ? blockRL.toString() : "";
return priorityBlocks.contains(blockName);
}
private void aimAtBlock(BlockPos blockPos, boolean fastAim) {
Minecraft mc = Minecraft.func_71410_x();
double dx = blockPos.func_177958_n() + 0.5 - mc.field_71439_g.field_70165_t;
double dy = blockPos.func_177956_o() + 0.5 - (mc.field_71439_g.field_70163_u + mc.field_71439_g.func_70047_e());
double dz = blockPos.func_177952_p() + 0.5 - mc.field_71439_g.field_70161_v;
double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
float targetYaw = (float) Math.toDegrees(Math.atan2(dz, dx)) - 90;
float targetPitch = (float) -Math.toDegrees(Math.atan2(dy, Math.sqrt(dx * dx + dz * dz)));
float yawDiff = angleDifference(targetYaw, mc.field_71439_g.field_70177_z);
float pitchDiff = angleDifference(targetPitch, mc.field_71439_g.field_70125_A);
float turnSpeed = fastAim ? fastTurnSpeed : normalTurnSpeed;
float yawStep = (float) (yawDiff * 0.15 * turnSpeed);
float pitchStep = (float) (pitchDiff * 0.15 * turnSpeed);
mc.field_71439_g.field_70177_z += yawStep;
mc.field_71439_g.field_70125_A += pitchStep;
mc.field_71439_g.field_70125_A = Math.max(-90, Math.min(90, mc.field_71439_g.field_70125_A));
}
private BlockPos findNewTarget(Minecraft mc) {
int searchRadius = 5;
BlockPos playerPos = mc.field_71439_g.func_180425_c();
BlockPos bestTarget = null;
double bestScore = Double.MAX_VALUE;
// Get the player's look vector
Vec3 lookVec = mc.field_71439_g.func_70040_Z();
double maxReachDistance = mc.field_71442_b.func_78757_d();
double maxReachDistanceSq = maxReachDistance * maxReachDistance;
for (int x = -searchRadius; x <= searchRadius; x++) {
for (int y = -searchRadius; y <= searchRadius; y++) {
for (int z = -searchRadius; z <= searchRadius; z++) {
BlockPos pos = playerPos.func_177982_a(x, y, z);
if (isValidTarget(pos) && hasLineOfSight(pos)) {
// Calculate the vector from the player to the block
Vec3 toBlock = new Vec3(
pos.func_177958_n() + 0.5 - mc.field_71439_g.field_70165_t,
pos.func_177956_o() + 0.5 - (mc.field_71439_g.field_70163_u + mc.field_71439_g.func_70047_e()),
pos.func_177952_p() + 0.5 - mc.field_71439_g.field_70161_v
);
// Check if the block is within reach distance
double distanceSq = toBlock.func_72433_c() * toBlock.func_72433_c();
if (distanceSq > maxReachDistanceSq) {
continue; // Skip this block if it's too far
}
// Normalize the vector
double length = toBlock.func_72433_c();
Vec3 normalizedToBlock = new Vec3(
toBlock.field_72450_a / length,
toBlock.field_72448_b / length,
toBlock.field_72449_c / length
);
// Calculate the dot product between the look vector and the normalized vector to the block
double dotProduct = lookVec.func_72430_b(normalizedToBlock);
// Convert dot product to an angle
double angle = Math.acos(dotProduct);
// Calculate score based on angle (lower is better) and distance
double score = angle + (length / 10); // We divide length by 10 to balance its impact
// Apply priority block bonus
if (isPriorityBlock(pos)) {
score -= Math.PI / 4; // Reduce score by 45 degrees for priority blocks
}
if (score < bestScore) {
bestScore = score;
bestTarget = pos;
}
}
}
}
}
return bestTarget;
}
private float angleDifference(float angle1, float angle2) {
float diff = (angle1 - angle2 + 180) % 360 - 180;
return diff < -180 ? diff + 360 : diff;
}
@SubscribeEvent
public void onKeyInput(KeyInputEvent event) {
if (Keyboard.isKeyDown(Keyboard.KEY_M)) {
Minecraft.func_71410_x().func_147108_a(new AimModGui(this));
}
}
@SubscribeEvent
public void onClientTick(TickEvent.ClientTickEvent event) {
if (event.phase != TickEvent.Phase.START || !isEnabled || !isBreakingEnabled) return;
Minecraft mc = Minecraft.func_71410_x();
if (mc.field_71439_g == null || mc.field_71441_e == null) return;
if (breakingCooldown > 0) {
breakingCooldown--;
return;
}
MovingObjectPosition mop = mc.field_71476_x;
if (mop != null && mop.field_72313_a == MovingObjectPosition.MovingObjectType.BLOCK) {
BlockPos pos = mop.func_178782_a();
if (isValidTarget(pos)) {
if (!isBreaking) {
// Simulate left mouse button press
mc.field_71474_y.field_74312_F.func_74510_a(mc.field_71474_y.field_74312_F.func_151463_i(), true);
isBreaking = true;
}
breakingCooldown = BREAKING_COOLDOWN_TICKS;
} else {
stopBreaking(mc);
}
} else {
stopBreaking(mc);
}
}
private void stopBreaking(Minecraft mc) {
if (isBreaking) {
// Simulate left mouse button release
mc.field_71474_y.field_74312_F.func_74510_a(mc.field_71474_y.field_74312_F.func_151463_i(), false);
isBreaking = false;
}
}
public void toggleMod() {
isEnabled = !isEnabled;
Minecraft.func_71410_x().field_71439_g.func_145747_a(new ChatComponentText("AimMod " + (isEnabled ? "enabled" : "disabled")));
saveConfig();
}
public void toggleBreaking() {
isBreakingEnabled = !isBreakingEnabled;
Minecraft.func_71410_x().field_71439_g.func_145747_a(new ChatComponentText("AimMod auto-breaking " + (isBreakingEnabled ? "enabled" : "disabled")));
}
public void toggleBlock(String blockName) {
if (excludedBlocks.contains(blockName)) {
excludedBlocks.remove(blockName);
Minecraft.func_71410_x().field_71439_g.func_145747_a(new ChatComponentText(blockName + " removed from exclusion list"));
} else {
excludedBlocks.add(blockName);
Minecraft.func_71410_x().field_71439_g.func_145747_a(new ChatComponentText(blockName + " added to exclusion list"));
}
saveConfig();
}
public void togglePriorityBlock(String blockName) {
if (priorityBlocks.contains(blockName)) {
priorityBlocks.remove(blockName);
Minecraft.func_71410_x().field_71439_g.func_145747_a(new ChatComponentText(blockName + " removed from priority list"));
} else {
priorityBlocks.add(blockName);
Minecraft.func_71410_x().field_71439_g.func_145747_a(new ChatComponentText(blockName + " added to priority list"));
}
saveConfig();
}
public boolean isBlockExcluded(String blockName) {
return excludedBlocks.contains(blockName);
}
public boolean isBlockPriority(String blockName) {
return priorityBlocks.contains(blockName);
}
public void setNormalTurnSpeed(float speed) {
this.normalTurnSpeed = speed;
Minecraft.func_71410_x().field_71439_g.func_145747_a(new ChatComponentText("Normal turn speed set to " + speed));
saveConfig();
}
public void setFastTurnSpeed(float speed) {
this.fastTurnSpeed = speed;
Minecraft.func_71410_x().field_71439_g.func_145747_a(new ChatComponentText("Fast turn speed set to " + speed));
saveConfig();
}
public float getNormalTurnSpeed() {
return normalTurnSpeed;
}
public float getFastTurnSpeed() {
return fastTurnSpeed;
}
public boolean isEnabled() {
return isEnabled;
}
public boolean isBreakingEnabled() {
return isBreakingEnabled;
}
public String getTargetedBlockName() {
Minecraft mc = Minecraft.func_71410_x();
MovingObjectPosition mop = mc.field_71476_x;
if (mop != null && mop.field_72313_a == MovingObjectPosition.MovingObjectType.BLOCK) {
Block block = mc.field_71441_e.func_180495_p(mop.func_178782_a()).func_177230_c();
ResourceLocation blockRL = Block.field_149771_c.func_177774_c(block);
return blockRL != null ? blockRL.toString() : "Unknown";
}
return "No block targeted";
}
}