Skip to content

Commit

Permalink
Remove tickhookcontext
Browse files Browse the repository at this point in the history
It was just a huge mess and tbh events work fine...
  • Loading branch information
AlexProgrammerDE committed Nov 11, 2024
1 parent 3d3dbe9 commit c234ce7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public <T> void set(MetadataKey<T> key, T value) {
public <T> void remove(MetadataKey<T> key) {
this.metadata.remove(key.key());
}

public <T> T getAndRemove(MetadataKey<T> key) {
return key.cast(this.metadata.remove(key.key()));
}
}
75 changes: 43 additions & 32 deletions server/src/main/java/com/soulfiremc/server/plugins/KillAura.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import com.soulfiremc.server.api.InternalPlugin;
import com.soulfiremc.server.api.PluginInfo;
import com.soulfiremc.server.api.event.bot.BotPostEntityTickEvent;
import com.soulfiremc.server.api.event.bot.BotPreEntityTickEvent;
import com.soulfiremc.server.api.event.lifecycle.InstanceSettingsRegistryInitEvent;
import com.soulfiremc.server.protocol.bot.state.TickHookContext;
import com.soulfiremc.server.api.metadata.MetadataKey;
import com.soulfiremc.server.protocol.bot.state.entity.Entity;
import com.soulfiremc.server.settings.lib.SettingsObject;
import com.soulfiremc.server.settings.property.*;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
Expand All @@ -33,6 +35,8 @@

@Slf4j
public class KillAura extends InternalPlugin {
private static final MetadataKey<AttackEntity> ATTACK_ENTITY = MetadataKey.of("kill_aura", "attack_entity", AttackEntity.class);

public KillAura() {
super(new PluginInfo(
"kill-aura",
Expand Down Expand Up @@ -72,17 +76,10 @@ public static void onPreEntityTick(BotPreEntityTickEvent event) {
return;
}

double distance;
var bestVisiblePoint = control.getEntityVisiblePoint(target);
if (bestVisiblePoint != null) {
distance =
bestVisiblePoint.distance(bot.dataManager().clientEntity().eyePosition());
} else {
distance =
target
.eyePosition()
.distance(bot.dataManager().clientEntity().eyePosition());
}
var distance = bestVisiblePoint != null ?
bestVisiblePoint.distance(bot.dataManager().clientEntity().eyePosition())
: target.eyePosition().distance(bot.dataManager().clientEntity().eyePosition());

if (distance > lookRange) {
return;
Expand All @@ -96,27 +93,39 @@ public static void onPreEntityTick(BotPreEntityTickEvent event) {
.lookAt(RotationOrigin.EYES, target.originPosition(RotationOrigin.EYES));
}

TickHookContext.INSTANCE
.get()
.addHook(
TickHookContext.HookType.POST_ENTITY_TICK,
() -> {
if (control.attackCooldownTicks() > 0) {
return;
}

var swing = distance <= swingRange;
if (distance <= hitRange) {
control.attack(target, swing);
} else if (swing) {
control.swingArm();
}

if (bot.protocolVersion().olderThan(ProtocolVersion.v1_9)
|| bot.settingsSource().get(KillAuraSettings.IGNORE_COOLDOWN)) {
control.attackCooldownTicks(bot.settingsSource().getRandom(KillAuraSettings.ATTACK_DELAY_TICKS).getAsInt());
}
});
bot.metadata().set(ATTACK_ENTITY, new AttackEntity(target, distance));
}

@EventHandler
public static void onPostEntityTick(BotPostEntityTickEvent event) {
var bot = event.connection();
if (!bot.settingsSource().get(KillAuraSettings.ENABLE)) {
return;
}

var control = bot.botControl();
if (control.attackCooldownTicks() > 0) {
return;
}

var attackEntity = bot.metadata().getAndRemove(ATTACK_ENTITY);
if (attackEntity == null) {
return;
}

var hitRange = bot.settingsSource().get(KillAuraSettings.HIT_RANGE);
var swingRange = bot.settingsSource().get(KillAuraSettings.SWING_RANGE);
var swing = attackEntity.distance() <= swingRange;
if (attackEntity.distance() <= hitRange) {
control.attack(attackEntity.attackEntity(), swing);
} else if (swing) {
control.swingArm();
}

if (bot.protocolVersion().olderThan(ProtocolVersion.v1_9)
|| bot.settingsSource().get(KillAuraSettings.IGNORE_COOLDOWN)) {
control.attackCooldownTicks(bot.settingsSource().getRandom(KillAuraSettings.ATTACK_DELAY_TICKS).getAsInt());
}
}

@EventHandler
Expand Down Expand Up @@ -194,4 +203,6 @@ private static class KillAuraSettings implements SettingsObject {
20,
1));
}

private record AttackEntity(Entity attackEntity, double distance) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.soulfiremc.server.protocol.bot.SessionDataManager;
import com.soulfiremc.server.protocol.bot.container.InventoryManager;
import com.soulfiremc.server.protocol.bot.state.ControlState;
import com.soulfiremc.server.protocol.bot.state.TickHookContext;
import com.soulfiremc.server.protocol.netty.ResolveUtil;
import com.soulfiremc.server.protocol.netty.ViaClientSession;
import com.soulfiremc.server.proxy.SFProxy;
Expand Down Expand Up @@ -177,11 +176,7 @@ public void tick(int ticks) {
}

for (var i = 0L; i < Math.min(ticks, 10); i++) {
var tickHookState = TickHookContext.INSTANCE.get();
tickHookState.clear();

SoulFireAPI.postEvent(new BotPreTickEvent(this));
tickHookState.callHooks(TickHookContext.HookType.PRE_TICK);

dataManager.tick();
botControl.tick();
Expand All @@ -191,7 +186,6 @@ public void tick(int ticks) {
}

SoulFireAPI.postEvent(new BotPostTickEvent(this));
tickHookState.callHooks(TickHookContext.HookType.POST_TICK);
}
} catch (Throwable t) {
logger.error("Error while ticking bot!", t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1114,16 +1114,12 @@ public void tick() {
// Tick cooldowns
tickCooldowns();

var tickHookState = TickHookContext.INSTANCE.get();

SoulFireAPI.postEvent(new BotPreEntityTickEvent(connection));
tickHookState.callHooks(TickHookContext.HookType.PRE_ENTITY_TICK);

// Tick entities
entityTrackerState.tick();

SoulFireAPI.postEvent(new BotPostEntityTickEvent(connection));
tickHookState.callHooks(TickHookContext.HookType.POST_ENTITY_TICK);
}

private void tickCooldowns() {
Expand Down

This file was deleted.

0 comments on commit c234ce7

Please sign in to comment.