use of org.spongepowered.api.Game in project SpongeCommon by SpongePowered.
the class SnapshotGenerationTest method init.
@Before
public void init() {
PluginManager manager = Mockito.mock(PluginManager.class);
this.eventManager = new SpongeEventManager(this.logger, manager);
try {
Field field = Timings.class.getDeclaredField("factory");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, Mockito.mock(TimingsFactory.class));
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
this.plugin = new Object();
PluginContainer container = Mockito.mock(PluginContainer.class);
Mockito.when(manager.fromInstance(this.plugin)).thenReturn(Optional.of(container));
Cause cause = Cause.of(EventContext.empty(), this);
this.entity = Mockito.mock(Entity.class, withSettings().defaultAnswer(Mockito.RETURNS_MOCKS));
this.event = SpongeEventFactory.createSpawnEntityEvent(cause, Lists.newArrayList(this.entity));
Game game = mock(Game.class);
CauseStackManager csm = mock(CauseStackManager.class);
Mockito.when(game.getCauseStackManager()).thenReturn(csm);
}
use of org.spongepowered.api.Game in project core by CubeEngine.
the class UserMatcher method match.
public Optional<User> match(String name, boolean searchOffline) {
Game game = Sponge.getGame();
if (name == null) {
return null;
}
// Direct Match Online Players:
Optional<Player> player = game.getServer().getPlayer(name);
if (player.isPresent()) {
return Optional.of(player.get());
}
// Find Online Players with similar name
Map<String, Player> onlinePlayerMap = new HashMap<>();
for (Player onlineUser : game.getServer().getOnlinePlayers()) {
onlinePlayerMap.put(onlineUser.getName(), onlineUser);
}
String foundUser = sm.matchString(name, onlinePlayerMap.keySet());
if (foundUser != null) {
return Optional.of(onlinePlayerMap.get(foundUser));
}
UserStorageService storage = game.getServiceManager().provideUnchecked(UserStorageService.class);
Optional<User> directMatchOffline;
try {
directMatchOffline = storage.get(name);
} catch (IllegalArgumentException ignore) {
return Optional.empty();
}
if (directMatchOffline.isPresent()) {
return directMatchOffline;
}
if (searchOffline) {
String match = sm.matchString(name, storage.getAll().stream().map(GameProfile::getName).filter(Optional::isPresent).map(Optional::get).collect(toList()));
if (match != null) {
return storage.get(match);
}
}
return Optional.empty();
}
use of org.spongepowered.api.Game in project SpongeAPI by SpongePowered.
the class SimpleServiceManagerTest method mockEventManager.
@Before
public void mockEventManager() throws Exception {
this.manager = mock(PluginManager.class);
this.testPluginContainer = mock(PluginContainer.class);
when(this.testPluginContainer.getId()).thenReturn("TestPlugin");
when(this.manager.fromInstance(this.testPlugin)).thenReturn(Optional.of(this.testPluginContainer));
Game game = mock(Game.class);
TestHooks.setInstance("eventManager", mock(EventManager.class));
when(game.getEventManager()).thenReturn(mock(EventManager.class));
CauseStackManager csm = mock(CauseStackManager.class);
when(game.getCauseStackManager()).thenReturn(csm);
when(csm.pushCause(null)).thenReturn(csm);
when(csm.popCause()).thenReturn(null);
when(csm.getCurrentCause()).thenReturn(Cause.of(EventContext.empty(), this));
CauseStackManager.StackFrame sf = mock(CauseStackManager.StackFrame.class);
when(sf.pushCause(null)).thenReturn(sf);
when(sf.addContext(null, null)).thenReturn(sf);
when(sf.popCause()).thenReturn(null);
when(sf.getCurrentCause()).thenReturn(Cause.of(EventContext.empty(), this));
when(csm.pushCauseFrame()).thenReturn(sf);
TestHooks.setGame(game);
TestHooks.setInstance("causeStackManager", csm);
}
use of org.spongepowered.api.Game in project SpongeAPI by SpongePowered.
the class GenericArgumentsTest method getPlayerElement.
private CommandElement getPlayerElement() throws Exception {
// Create two fake players
Player player1 = Mockito.mock(Player.class);
Mockito.when(player1.getName()).thenReturn("test1");
Player player2 = Mockito.mock(Player.class);
Mockito.when(player2.getName()).thenReturn("test2");
Server mock = Mockito.mock(Server.class);
Mockito.when(mock.getOnlinePlayers()).thenReturn(Lists.newArrayList(player1, player2));
Mockito.when(mock.getPlayer(Mockito.anyString())).thenReturn(Optional.empty());
Mockito.when(mock.getPlayer("test1")).thenReturn(Optional.of(player1));
Mockito.when(mock.getPlayer("test2")).thenReturn(Optional.of(player2));
Game gameMock = Mockito.mock(Game.class);
Mockito.when(gameMock.getServer()).thenReturn(mock);
Field field = Sponge.class.getDeclaredField("game");
field.setAccessible(true);
field.set(null, gameMock);
// Create element
return GenericArguments.player(Text.of("player"));
}
use of org.spongepowered.api.Game in project SpongeAPI by SpongePowered.
the class ChildCommandsTest method initialize.
@Before
public void initialize() throws Exception {
TestPlainTextSerializer.inject();
Game game = mock(Game.class);
CauseStackManager csm = mock(CauseStackManager.class);
when(game.getCauseStackManager()).thenReturn(csm);
when(csm.pushCause(null)).thenReturn(csm);
when(csm.popCause()).thenReturn(null);
CommandManager cm = mock(CommandManager.class);
when(game.getCommandManager()).thenReturn(cm);
TestHooks.setGame(game);
TestHooks.setInstance("commandManager", cm);
}
Aggregations