mirror of
https://github.com/cabaletta/baritone
synced 2024-12-18 13:15:13 +00:00
Initial Behavior system commit.
Still needs some work, just getting this in the repo!
This commit is contained in:
parent
a36b223aab
commit
42fd04e329
@ -48,6 +48,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.reflections:reflections:0.9.11'
|
||||
implementation ('org.spongepowered:mixin:0.7.8-SNAPSHOT') {
|
||||
// Mixin includes a lot of dependencies that are too up-to-date
|
||||
exclude module: 'launchwrapper'
|
||||
|
@ -1,5 +1,10 @@
|
||||
package baritone.bot;
|
||||
|
||||
import baritone.bot.behavior.Behavior;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 7/31/2018 10:50 PM
|
||||
@ -21,6 +26,7 @@ public enum Baritone {
|
||||
private GameActionHandler actionHandler;
|
||||
private GameEventHandler gameEventHandler;
|
||||
private InputOverrideHandler inputOverrideHandler;
|
||||
private List<Behavior> behaviors;
|
||||
|
||||
/**
|
||||
* Whether or not Baritone is active
|
||||
@ -36,6 +42,14 @@ public enum Baritone {
|
||||
|
||||
this.active = true;
|
||||
this.initialized = true;
|
||||
|
||||
new Reflections("baritone.bot.behavior.impl").getSubTypesOf(Behavior.class).forEach(c -> {
|
||||
try {
|
||||
behaviors.add(c.newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public final boolean isInitialized() {
|
||||
@ -62,6 +76,10 @@ public enum Baritone {
|
||||
return this.inputOverrideHandler;
|
||||
}
|
||||
|
||||
public final List<Behavior> getBehaviors() {
|
||||
return this.behaviors;
|
||||
}
|
||||
|
||||
public final boolean isActive() {
|
||||
return this.active;
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package baritone.bot;
|
||||
|
||||
import baritone.bot.behavior.Behavior;
|
||||
import baritone.bot.event.IGameEventListener;
|
||||
import baritone.bot.event.events.ChatEvent;
|
||||
import net.minecraft.client.settings.KeyBinding;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 7/31/2018 11:04 PM
|
||||
@ -14,7 +17,9 @@ public final class GameEventHandler implements IGameEventListener {
|
||||
GameEventHandler() {}
|
||||
|
||||
@Override
|
||||
public final void onTick() {}
|
||||
public final void onTick() {
|
||||
dispatchEventToBehaviors(behavior -> onTick());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProcessKeyBinds() {
|
||||
@ -31,8 +36,16 @@ public final class GameEventHandler implements IGameEventListener {
|
||||
KeyBinding.onTick(keyCode < 0 ? keyCode + 100 : keyCode);
|
||||
}
|
||||
}
|
||||
|
||||
dispatchEventToBehaviors(behavior -> onProcessKeyBinds());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSendChatMessage(ChatEvent event) {}
|
||||
public void onSendChatMessage(ChatEvent event) {
|
||||
dispatchEventToBehaviors(behavior -> onSendChatMessage(event));
|
||||
}
|
||||
|
||||
private void dispatchEventToBehaviors(Consumer<Behavior> dispatchFunction) {
|
||||
Baritone.INSTANCE.getBehaviors().stream().filter(Behavior::isEnabled).forEach(dispatchFunction);
|
||||
}
|
||||
}
|
78
src/main/java/baritone/bot/behavior/Behavior.java
Normal file
78
src/main/java/baritone/bot/behavior/Behavior.java
Normal file
@ -0,0 +1,78 @@
|
||||
package baritone.bot.behavior;
|
||||
|
||||
import baritone.bot.event.AbstractGameEventListener;
|
||||
import baritone.bot.utils.Helper;
|
||||
|
||||
/**
|
||||
* A generic bot behavior.
|
||||
*
|
||||
* @author Brady
|
||||
* @since 8/1/2018 6:29 PM
|
||||
*/
|
||||
public class Behavior implements AbstractGameEventListener, Helper {
|
||||
|
||||
/**
|
||||
* Whether or not this behavior is enabled
|
||||
*/
|
||||
private boolean enabled;
|
||||
|
||||
/**
|
||||
* Toggles the enabled state of this {@link Behavior}.
|
||||
*
|
||||
* @return The new state.
|
||||
*/
|
||||
public final boolean toggle() {
|
||||
return this.setEnabled(!this.enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the enabled state of this {@link Behavior}.
|
||||
*
|
||||
* @return The new state.
|
||||
*/
|
||||
public final boolean setEnabled(boolean enabled) {
|
||||
boolean newState = getNewState(this.enabled, enabled);
|
||||
if (newState == this.enabled)
|
||||
return this.enabled;
|
||||
|
||||
if (this.enabled = newState) {
|
||||
onStart();
|
||||
} else {
|
||||
onCancel();
|
||||
}
|
||||
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to determine what the new enabled state of this
|
||||
* {@link Behavior} should be given the old state, and the
|
||||
* proposed state. Intended to be overriden by behaviors
|
||||
* that should always be active, given that the bot itself is
|
||||
* active.
|
||||
*
|
||||
* @param oldState The old state
|
||||
* @param proposedState The proposed state
|
||||
* @return The new state
|
||||
*/
|
||||
public boolean getNewState(boolean oldState, boolean proposedState) {
|
||||
return proposedState;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Whether or not this {@link Behavior} is active.
|
||||
*/
|
||||
public final boolean isEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the state changes from disabled to enabled
|
||||
*/
|
||||
public void onStart() {}
|
||||
|
||||
/**
|
||||
* Called when the state changes from enabled to disabled
|
||||
*/
|
||||
public void onCancel() {}
|
||||
}
|
8
src/main/java/baritone/bot/behavior/PathFinding.java
Normal file
8
src/main/java/baritone/bot/behavior/PathFinding.java
Normal file
@ -0,0 +1,8 @@
|
||||
package baritone.bot.behavior;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 8/1/2018 5:38 PM
|
||||
*/
|
||||
public class PathFinding {
|
||||
}
|
Loading…
Reference in New Issue
Block a user