Merge pull request #193 from cabaletta/service-loader

Service loader
This commit is contained in:
Leijurv 2018-10-01 14:37:15 -07:00 committed by GitHub
commit 810b92fbad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 56 deletions

View File

@ -20,6 +20,9 @@ package baritone.api;
import baritone.api.behavior.*; import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider; import baritone.api.cache.IWorldProvider;
import java.util.Iterator;
import java.util.ServiceLoader;
/** /**
* API exposure for various things implemented in Baritone. * API exposure for various things implemented in Baritone.
* <p> * <p>
@ -28,37 +31,37 @@ import baritone.api.cache.IWorldProvider;
* @author Brady * @author Brady
* @since 9/23/2018 * @since 9/23/2018
*/ */
public class BaritoneAPI { public final class BaritoneAPI {
// General private final static IBaritoneProvider baritone;
private static final Settings settings = new Settings(); private final static Settings settings;
private static IWorldProvider worldProvider;
// Behaviors static {
private static IFollowBehavior followBehavior; ServiceLoader<IBaritoneProvider> baritoneLoader = ServiceLoader.load(IBaritoneProvider.class);
private static ILookBehavior lookBehavior; Iterator<IBaritoneProvider> instances = baritoneLoader.iterator();
private static IMemoryBehavior memoryBehavior; baritone = instances.next();
private static IMineBehavior mineBehavior;
private static IPathingBehavior pathingBehavior; settings = new Settings();
}
public static IFollowBehavior getFollowBehavior() { public static IFollowBehavior getFollowBehavior() {
return followBehavior; return baritone.getFollowBehavior();
} }
public static ILookBehavior getLookBehavior() { public static ILookBehavior getLookBehavior() {
return lookBehavior; return baritone.getLookBehavior();
} }
public static IMemoryBehavior getMemoryBehavior() { public static IMemoryBehavior getMemoryBehavior() {
return memoryBehavior; return baritone.getMemoryBehavior();
} }
public static IMineBehavior getMineBehavior() { public static IMineBehavior getMineBehavior() {
return mineBehavior; return baritone.getMineBehavior();
} }
public static IPathingBehavior getPathingBehavior() { public static IPathingBehavior getPathingBehavior() {
return pathingBehavior; return baritone.getPathingBehavior();
} }
public static Settings getSettings() { public static Settings getSettings() {
@ -66,34 +69,6 @@ public class BaritoneAPI {
} }
public static IWorldProvider getWorldProvider() { public static IWorldProvider getWorldProvider() {
return worldProvider; return baritone.getWorldProvider();
} }
/**
* FOR INTERNAL USE ONLY
*/
public static void registerProviders(
IWorldProvider worldProvider
) {
BaritoneAPI.worldProvider = worldProvider;
}
/**
* FOR INTERNAL USE ONLY
*/
// @formatter:off
public static void registerDefaultBehaviors(
IFollowBehavior followBehavior,
ILookBehavior lookBehavior,
IMemoryBehavior memoryBehavior,
IMineBehavior mineBehavior,
IPathingBehavior pathingBehavior
) {
BaritoneAPI.followBehavior = followBehavior;
BaritoneAPI.lookBehavior = lookBehavior;
BaritoneAPI.memoryBehavior = memoryBehavior;
BaritoneAPI.mineBehavior = mineBehavior;
BaritoneAPI.pathingBehavior = pathingBehavior;
}
// @formatter:on
} }

View File

@ -0,0 +1,40 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api;
import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider;
/**
* @author Brady
* @since 9/29/2018
*/
public interface IBaritoneProvider {
IFollowBehavior getFollowBehavior();
ILookBehavior getLookBehavior();
IMemoryBehavior getMemoryBehavior();
IMineBehavior getMineBehavior();
IPathingBehavior getPathingBehavior();
IWorldProvider getWorldProvider();
}

View File

@ -88,8 +88,6 @@ public enum Baritone {
// We might want to change this... // We might want to change this...
this.settings = BaritoneAPI.getSettings(); this.settings = BaritoneAPI.getSettings();
BaritoneAPI.registerProviders(WorldProvider.INSTANCE);
this.behaviors = new ArrayList<>(); this.behaviors = new ArrayList<>();
{ {
registerBehavior(PathingBehavior.INSTANCE); registerBehavior(PathingBehavior.INSTANCE);
@ -98,16 +96,6 @@ public enum Baritone {
registerBehavior(LocationTrackingBehavior.INSTANCE); registerBehavior(LocationTrackingBehavior.INSTANCE);
registerBehavior(FollowBehavior.INSTANCE); registerBehavior(FollowBehavior.INSTANCE);
registerBehavior(MineBehavior.INSTANCE); registerBehavior(MineBehavior.INSTANCE);
// TODO: Clean this up
// Maybe combine this call in someway with the registerBehavior calls?
BaritoneAPI.registerDefaultBehaviors(
FollowBehavior.INSTANCE,
LookBehavior.INSTANCE,
MemoryBehavior.INSTANCE,
MineBehavior.INSTANCE,
PathingBehavior.INSTANCE
);
} }
if (BaritoneAutoTest.ENABLE_AUTO_TEST) { if (BaritoneAutoTest.ENABLE_AUTO_TEST) {
registerEventListener(BaritoneAutoTest.INSTANCE); registerEventListener(BaritoneAutoTest.INSTANCE);

View File

@ -0,0 +1,61 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone;
import baritone.api.IBaritoneProvider;
import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider;
import baritone.behavior.*;
import baritone.cache.WorldProvider;
/**
* @author Brady
* @since 9/29/2018
*/
public final class BaritoneProvider implements IBaritoneProvider {
@Override
public IFollowBehavior getFollowBehavior() {
return FollowBehavior.INSTANCE;
}
@Override
public ILookBehavior getLookBehavior() {
return LookBehavior.INSTANCE;
}
@Override
public IMemoryBehavior getMemoryBehavior() {
return MemoryBehavior.INSTANCE;
}
@Override
public IMineBehavior getMineBehavior() {
return MineBehavior.INSTANCE;
}
@Override
public IPathingBehavior getPathingBehavior() {
return PathingBehavior.INSTANCE;
}
@Override
public IWorldProvider getWorldProvider() {
return WorldProvider.INSTANCE;
}
}

View File

@ -0,0 +1 @@
baritone.BaritoneProvider