Skip to content

Commit

Permalink
Phase out API for split between API and implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
12awsomeman34 committed Jul 7, 2016
1 parent dfa0acd commit f05e98e
Show file tree
Hide file tree
Showing 55 changed files with 1,168 additions and 2,528 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "XtraAPI"]
path = XtraAPI
url = https://github.com/XtraStudio/XtraAPI.git
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
compile "org.reflections:reflections:0.9.10"
compile "org.javassist:javassist:3.20.0-GA"
compile "com.github.Laxamer:LaxFile:2.0.1"
compile project(':XtraAPI')
}

blossom {
Expand All @@ -49,10 +50,12 @@ shadowJar {
include(dependency('org.reflections:reflections'))
include(dependency('org.javassist:javassist'))
include(dependency('com.github.Laxamer:LaxFile'))
include(dependency(':XtraAPI'))
}
relocate 'org.reflections', 'com.xtra.core.core-dependencies.reflections'
relocate 'javassist', 'com.xtra.core.core-dependencies.javassist'
relocate 'com.laxamer.file', 'com.xtra.core.core-dependencies.laxfile'
relocate 'com.xtra.api', 'com.xtra.core.core-dependencies.api'
classifier = ''
}

Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name=XtraCore
group=com.xtra
url=https://github.com/XtraStudio
organization=XtraStudio
version=1.0
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include 'XtraAPI'
58 changes: 0 additions & 58 deletions src/main/java/com/xtra/core/Core.java

This file was deleted.

194 changes: 194 additions & 0 deletions src/main/java/com/xtra/core/CoreImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/**
* This file is part of XtraCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2016 - 2016 XtraStudio <https://github.com/XtraStudio>
* Copyright (c) Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.xtra.core;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.xtra.api.ICore;
import com.xtra.api.ban.BanHandler;
import com.xtra.api.command.CommandHandler;
import com.xtra.api.command.annotation.CommandAnnotationHelper;
import com.xtra.api.config.ConfigHandler;
import com.xtra.api.entity.EntityHandler;
import com.xtra.api.listener.ListenerHandler;
import com.xtra.api.logger.LogHandler;
import com.xtra.api.logger.Logger;
import com.xtra.api.plugin.XtraCorePluginContainer;
import com.xtra.api.plugin.XtraCorePluginHandler;
import com.xtra.api.registry.CommandRegistry;
import com.xtra.api.registry.ConfigRegistry;
import com.xtra.api.text.HelpPaginationHandler;
import com.xtra.api.world.direction.DirectionHandler;
import com.xtra.core.ban.BanHandlerImpl;
import com.xtra.core.command.CommandHandlerImpl;
import com.xtra.core.command.annotation.CommandAnnotationHelperImpl;
import com.xtra.core.config.ConfigHandlerImpl;
import com.xtra.core.entity.EntityHandlerImpl;
import com.xtra.core.internal.Internals;
import com.xtra.core.listener.ListenerHandlerImpl;
import com.xtra.core.logger.LogHandlerImpl;
import com.xtra.core.plugin.XtraCorePluginContainerImpl;
import com.xtra.core.plugin.XtraCorePluginHandlerImpl;
import com.xtra.core.registry.CommandRegistryImpl;
import com.xtra.core.registry.ConfigRegistryImpl;
import com.xtra.core.text.HelpPaginationHandlerImpl;
import com.xtra.core.util.ReflectionScanner;
import com.xtra.core.world.direction.DirectionHandlerImpl;

public class CoreImpl implements ICore {

public static CoreImpl instance;
public Map<XtraCorePluginContainer, HelpPaginationHandler> paginationHandlers = new HashMap<>();
private BanHandler banHandler = new BanHandlerImpl();
private CommandAnnotationHelper annotationHelper = new CommandAnnotationHelperImpl();
private EntityHandler entityHandler = new EntityHandlerImpl();
private Map<XtraCorePluginContainer, ListenerHandler> listenerHandlers = new HashMap<>();
private LogHandler logHandler = new LogHandlerImpl();
private XtraCorePluginHandler pluginHandler = new XtraCorePluginHandlerImpl();
private CommandRegistry commandRegistry = new CommandRegistryImpl();
private ConfigRegistry configRegistry = new ConfigRegistryImpl();
private DirectionHandler directionHandler = new DirectionHandlerImpl();

public CoreImpl() {
instance = this;
}

public XtraCorePluginContainer initialize(Object plugin) {
// Create a plugin container
XtraCorePluginContainerImpl containerImpl = (XtraCorePluginContainerImpl) this.pluginHandler.add(plugin);
LogHandlerImpl log = (LogHandlerImpl) this.logHandler;
// Create a logger for the plugin
Logger logger = log.create(containerImpl);
logger.log("======================================================");
logger.log("Initializing with XtraCore version " + Internals.VERSION + "!");

Internals.globalLogger.log("======================================================");
Internals.globalLogger.log("Initializing plugin class " + plugin.getClass().getName());

containerImpl.scanner = ReflectionScanner.create(containerImpl);
return containerImpl;
}

public CommandHandler provideCommandHandler(Object plugin) {
XtraCorePluginContainer container = this.pluginHandler.getContainer(plugin.getClass()).get();
if (container.getCommandHandler().isPresent()) {
return container.getCommandHandler().get();
}
return CommandHandlerImpl.create(plugin);
}

public Optional<CommandHandler> provideCommandHandler(Class<?> clazz) {
XtraCorePluginContainer container = this.pluginHandler.getContainer(clazz).get();
if (container.getCommandHandler().isPresent()) {
return Optional.of(container.getCommandHandler().get());
}
return Optional.empty();
}

public ConfigHandler provideConfigHandler(Object plugin) {
XtraCorePluginContainer container = this.pluginHandler.getContainer(plugin.getClass()).get();
if (container.getConfigHandler().isPresent()) {
return container.getConfigHandler().get();
}
return ConfigHandlerImpl.create(plugin);
}

public Optional<ConfigHandler> provideConfigHandler(Class<?> clazz) {
XtraCorePluginContainer container = this.pluginHandler.getContainer(clazz).get();
if (container.getConfigHandler().isPresent()) {
return Optional.of(container.getConfigHandler().get());
}
return Optional.empty();
}

public ListenerHandler provideListenerHandler(Object plugin) {
for (Map.Entry<XtraCorePluginContainer, ListenerHandler> listenerHandler : this.listenerHandlers.entrySet()) {
if (listenerHandler.getKey().getPlugin().equals(plugin)) {
return listenerHandler.getValue();
}
}
ListenerHandlerImpl listenerHandler = new ListenerHandlerImpl();
listenerHandler.registerListeners(plugin);
return listenerHandler;
}

public Optional<ListenerHandler> provideListenerHandler(Class<?> clazz) {
for (Map.Entry<XtraCorePluginContainer, ListenerHandler> listenerHandler : this.listenerHandlers.entrySet()) {
if (listenerHandler.getKey().getPlugin().getClass().equals(clazz)) {
return Optional.of(listenerHandler.getValue());
}
}
return Optional.empty();
}

public HelpPaginationHandler.Builder createHelpPaginationBuilder(Object plugin) {
HelpPaginationHandlerImpl impl = new HelpPaginationHandlerImpl();
return impl.new Builder(impl, plugin);
}

public Optional<HelpPaginationHandler> getHelpPaginationHandler(Class<?> clazz) {
for (Map.Entry<XtraCorePluginContainer, HelpPaginationHandler> entry : this.paginationHandlers.entrySet()) {
if (entry.getKey().getPlugin().getClass().equals(clazz)) {
return Optional.of(entry.getValue());
}
}
return Optional.empty();
}

public BanHandler getBanHandler() {
return this.banHandler;
}

public CommandAnnotationHelper getCommandAnnotationHelper() {
return this.annotationHelper;
}

public EntityHandler getEntityHandler() {
return this.entityHandler;
}

public LogHandler getLogHandler() {
return this.logHandler;
}

public XtraCorePluginHandler getPluginHandler() {
return this.pluginHandler;
}

public CommandRegistry getCommandRegistry() {
return this.commandRegistry;
}

public ConfigRegistry getConfigRegistry() {
return this.configRegistry;
}

public DirectionHandler getDirectionHandler() {
return this.directionHandler;
}
}
32 changes: 26 additions & 6 deletions src/main/java/com/xtra/core/XtraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,37 @@

package com.xtra.core;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.Plugin;

import com.xtra.core.config.Config;
import com.xtra.core.config.annotation.DoNotReload;
import com.xtra.api.Core;
import com.xtra.api.command.base.CommandBase;
import com.xtra.api.command.base.CommandBaseLite;
import com.xtra.api.config.Config;
import com.xtra.api.config.annotation.DoNotReload;
import com.xtra.api.config.base.ConfigBase;
import com.xtra.core.command.base.CommandBaseImpl;
import com.xtra.core.command.base.CommandBaseLiteImpl;
import com.xtra.core.config.base.ConfigBaseImpl;
import com.xtra.core.internal.InternalCommands;
import com.xtra.core.internal.Internals;
import com.xtra.core.registry.ConfigRegistry;
import com.xtra.core.util.log.Logger;
import com.xtra.core.logger.LoggerImpl;

@Plugin(name = "XtraCore", id = "xtracore", version = Internals.VERSION, authors = {"12AwesomeMan34"}, description = Internals.DESCRIPTION)
public class XtraCore {

@Listener(order = Order.FIRST)
public void onPreInit(GamePreInitializationEvent event) {
Internals.globalLogger = new Logger();
Internals.globalLogger = new LoggerImpl();
Internals.globalLogger.log("======================================================");
Internals.globalLogger.log("Initializing XtraCore version " + Internals.VERSION);

this.provideImplementations();
}

@Listener
Expand All @@ -56,11 +65,22 @@ public void onInit(GameInitializationEvent event) {

@Listener
public void onReload(GameReloadEvent event) {
for (Config config : ConfigRegistry.getAllConfigs()) {
for (Config config : CoreImpl.instance.getConfigRegistry().getAllConfigs()) {
// If there is no DoNotReload annotation, then reload.
if (config.getClass().getAnnotation(DoNotReload.class) == null) {
config.load();
}
}
}

private void provideImplementations() {
try {
FieldUtils.writeStaticField(Core.class, "CORE", new CoreImpl(), true);
FieldUtils.writeStaticField(CommandBase.class, "BASE", new CommandBaseImpl() {}, true);
FieldUtils.writeStaticField(CommandBaseLite.class, "BASE", new CommandBaseLiteImpl() {}, true);
FieldUtils.writeStaticField(ConfigBase.class, "BASE", new ConfigBaseImpl() {}, true);
} catch (Exception e) {
Internals.globalLogger.log(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@
import org.spongepowered.api.text.Text;
import org.spongepowered.api.util.ban.Ban;

public class BanHandler {
import com.xtra.api.ban.BanHandler;

/**
* Gets the ban reason for the specified profile. Will return
* {@link Optional#empty()} if one does not exist.
*
* @param profile The profile to get the ban reason from
* @return The reason, or {@link Optional#empty()} if one is not available
*/
public static Optional<Text> getBanReason(GameProfile profile) {
public class BanHandlerImpl implements BanHandler {

@Override
public Optional<Text> getBanReason(GameProfile profile) {
BanService service = Sponge.getServiceManager().provide(BanService.class).get();
Optional<Ban.Profile> optionalBan = service.getBanFor(profile);
if (optionalBan.isPresent()) {
Expand Down
Loading

0 comments on commit f05e98e

Please sign in to comment.