получение World по его имени
Суть в том, что ID бина - это и есть World.name
This commit is contained in:
@@ -12,6 +12,7 @@ import mc.core.player.PlayerManager;
|
|||||||
import mc.core.player.PlayerSettings;
|
import mc.core.player.PlayerSettings;
|
||||||
import mc.core.world.World;
|
import mc.core.world.World;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -26,8 +27,6 @@ public class H2PlayerManager implements PlayerManager {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private H2PlayerService h2PlayerService;
|
private H2PlayerService h2PlayerService;
|
||||||
private List<H2Player> playerList = Collections.synchronizedList(new ArrayList<>());
|
private List<H2Player> playerList = Collections.synchronizedList(new ArrayList<>());
|
||||||
@Autowired
|
|
||||||
private World world; //FIXME
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Player createPlayer(String name, EntityLocation location, World world) {
|
public Player createPlayer(String name, EntityLocation location, World world) {
|
||||||
@@ -91,25 +90,9 @@ public class H2PlayerManager implements PlayerManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Player getOfflinePlayer(String name) {
|
public Player getOfflinePlayer(String name) {
|
||||||
//TODO похоже в попытке где-то оптимизировать/сэконопить я сам себя ******[обманул]
|
return playerList.stream()
|
||||||
//необходимо этот участок кода переписать
|
|
||||||
//потому как похоже на экономию на спичках
|
|
||||||
H2Player h2Player = playerList.stream()
|
|
||||||
.filter(player -> player.getName().equals(name))
|
.filter(player -> player.getName().equals(name))
|
||||||
.filter(player -> !player.isOnline())
|
.filter(player -> !player.isOnline())
|
||||||
.findFirst().orElse(null);
|
.findFirst().orElseGet(() -> h2PlayerService.getByName(name));
|
||||||
|
|
||||||
if (h2Player == null) {
|
|
||||||
h2Player = h2PlayerService.getByName(name);
|
|
||||||
if (h2Player != null) {
|
|
||||||
h2Player.setWorld(world);
|
|
||||||
return h2Player;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
h2Player.setWorld(world);
|
|
||||||
return h2Player;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import mc.core.EntityLocation;
|
import mc.core.EntityLocation;
|
||||||
import mc.core.h2db.H2Player;
|
import mc.core.h2db.H2Player;
|
||||||
|
import mc.core.world.World;
|
||||||
import org.hibernate.annotations.GenericGenerator;
|
import org.hibernate.annotations.GenericGenerator;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -55,11 +57,7 @@ public class H2PlayerEntity {
|
|||||||
this.locationZ = player.getLocation().getZ();
|
this.locationZ = player.getLocation().getZ();
|
||||||
this.locationYaw = player.getLocation().getYaw();
|
this.locationYaw = player.getLocation().getYaw();
|
||||||
this.locationPitch = player.getLocation().getPitch();
|
this.locationPitch = player.getLocation().getPitch();
|
||||||
if (player.getWorld() != null) { //FIXME
|
this.locationWorld = player.getWorld().getName();
|
||||||
this.locationWorld = player.getWorld().getName();
|
|
||||||
} else {
|
|
||||||
this.locationWorld = "null_world";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUuid(String uuid) {
|
public void setUuid(String uuid) {
|
||||||
@@ -78,12 +76,12 @@ public class H2PlayerEntity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public H2Player toPlayer() {
|
public H2Player toPlayer(ApplicationContext context) {
|
||||||
H2Player player = new H2Player();
|
H2Player player = new H2Player();
|
||||||
return toPlayer(player);
|
return toPlayer(player, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public H2Player toPlayer(H2Player player) {
|
public H2Player toPlayer(H2Player player, ApplicationContext context) {
|
||||||
player.setId(this.id.intValue());
|
player.setId(this.id.intValue());
|
||||||
player.setUuid(UUID.fromString(this.uuid));
|
player.setUuid(UUID.fromString(this.uuid));
|
||||||
player.setName(this.name);
|
player.setName(this.name);
|
||||||
@@ -97,7 +95,7 @@ public class H2PlayerEntity {
|
|||||||
player.getLocation().setYawPitch(this.locationYaw, this.locationPitch);
|
player.getLocation().setYawPitch(this.locationYaw, this.locationPitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
player.setWorld(null); //FIXME
|
player.setWorld(context.getBean(this.locationWorld, World.class));
|
||||||
|
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,15 @@ import mc.core.h2db.H2Player;
|
|||||||
import mc.core.h2db.entity.H2PlayerEntity;
|
import mc.core.h2db.entity.H2PlayerEntity;
|
||||||
import mc.core.h2db.repository.H2PlayerEntityRepository;
|
import mc.core.h2db.repository.H2PlayerEntityRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class H2PlayerServiceImpl implements H2PlayerService {
|
public class H2PlayerServiceImpl implements H2PlayerService {
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext context;
|
||||||
@Autowired
|
@Autowired
|
||||||
private H2PlayerEntityRepository h2PlayerEntityRepository;
|
private H2PlayerEntityRepository h2PlayerEntityRepository;
|
||||||
|
|
||||||
@@ -19,7 +22,7 @@ public class H2PlayerServiceImpl implements H2PlayerService {
|
|||||||
//TODO возможно имеет смысл здесь оптимизация
|
//TODO возможно имеет смысл здесь оптимизация
|
||||||
//вместо toPlayer() сделать toPlayer(H2Player) который в существующий
|
//вместо toPlayer() сделать toPlayer(H2Player) который в существующий
|
||||||
//будет дописывать/обновлять данные
|
//будет дописывать/обновлять данные
|
||||||
return h2PlayerEntityRepository.saveAndFlush(entity).toPlayer(player);
|
return h2PlayerEntityRepository.saveAndFlush(entity).toPlayer(player, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -30,12 +33,12 @@ public class H2PlayerServiceImpl implements H2PlayerService {
|
|||||||
@Override
|
@Override
|
||||||
public H2Player getByName(String name) {
|
public H2Player getByName(String name) {
|
||||||
Optional<H2PlayerEntity> optEntity = h2PlayerEntityRepository.findByName(name);
|
Optional<H2PlayerEntity> optEntity = h2PlayerEntityRepository.findByName(name);
|
||||||
return optEntity.map(H2PlayerEntity::toPlayer).orElse(null);
|
return optEntity.map(entiry -> entiry.toPlayer(context)).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public H2Player getById(int id) {
|
public H2Player getById(int id) {
|
||||||
Optional<H2PlayerEntity> optEntity = h2PlayerEntityRepository.findById((long) id);
|
Optional<H2PlayerEntity> optEntity = h2PlayerEntityRepository.findById((long) id);
|
||||||
return optEntity.map(H2PlayerEntity::toPlayer).orElse(null);
|
return optEntity.map(entiry -> entiry.toPlayer(context)).orElse(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,10 +44,10 @@ public class TestSpringConfig {
|
|||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean("mockWorld")
|
||||||
public World mockWorld() {
|
public World mockWorld() {
|
||||||
World mockWorld = mock(World.class);
|
World mockWorld = mock(World.class);
|
||||||
when(mockWorld.getName()).thenReturn("mock_world");
|
when(mockWorld.getName()).thenReturn("mockWorld");
|
||||||
return mockWorld;
|
return mockWorld;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package mc.core.h2db.service;
|
|||||||
import mc.core.EntityLocation;
|
import mc.core.EntityLocation;
|
||||||
import mc.core.h2db.H2Player;
|
import mc.core.h2db.H2Player;
|
||||||
import mc.core.h2db.TestSpringConfig;
|
import mc.core.h2db.TestSpringConfig;
|
||||||
|
import mc.core.world.World;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -21,6 +22,8 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
class H2PlayerServiceTest {
|
class H2PlayerServiceTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private H2PlayerService h2PlayerService;
|
private H2PlayerService h2PlayerService;
|
||||||
|
@Autowired
|
||||||
|
private World world;
|
||||||
|
|
||||||
private H2Player buildPlayer() {
|
private H2Player buildPlayer() {
|
||||||
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
|
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
|
||||||
@@ -38,18 +41,26 @@ class H2PlayerServiceTest {
|
|||||||
rnd.nextFloat() * (maxF - minF) + minF,
|
rnd.nextFloat() * (maxF - minF) + minF,
|
||||||
rnd.nextFloat() * (maxF - minF) + minF
|
rnd.nextFloat() * (maxF - minF) + minF
|
||||||
));
|
));
|
||||||
player.setWorld(null); //FIXME
|
player.setWorld(world);
|
||||||
|
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertPlayers(H2Player expected, H2Player actual) {
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
assertEquals(expected.getName(), actual.getName());
|
||||||
|
assertEquals(expected.getLocation(), actual.getLocation());
|
||||||
|
assertNotNull(actual.getWorld());
|
||||||
|
assertEquals(expected.getWorld(), actual.getWorld());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void save() {
|
void save() {
|
||||||
H2Player player = buildPlayer();
|
H2Player player = buildPlayer();
|
||||||
H2Player savedPlayer = h2PlayerService.save(player);
|
H2Player savedPlayer = h2PlayerService.save(player);
|
||||||
|
|
||||||
player.setId(savedPlayer.getId()); //FIXME костыль, однако
|
player.setId(savedPlayer.getId()); //FIXME костыль, однако
|
||||||
assertEquals(player, savedPlayer);
|
assertPlayers(player, savedPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -111,7 +122,7 @@ class H2PlayerServiceTest {
|
|||||||
H2Player player = h2PlayerService.save(buildPlayer());
|
H2Player player = h2PlayerService.save(buildPlayer());
|
||||||
|
|
||||||
H2Player player2 = h2PlayerService.getByName(player.getName());
|
H2Player player2 = h2PlayerService.getByName(player.getName());
|
||||||
assertEquals(player, player2);
|
assertPlayers(player, player2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -134,7 +145,7 @@ class H2PlayerServiceTest {
|
|||||||
H2Player player = h2PlayerService.save(buildPlayer());
|
H2Player player = h2PlayerService.save(buildPlayer());
|
||||||
|
|
||||||
H2Player player2 = h2PlayerService.getById(player.getId());
|
H2Player player2 = h2PlayerService.getById(player.getId());
|
||||||
assertEquals(player, player2);
|
assertPlayers(player, player2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2018-04-28
|
|
||||||
*/
|
|
||||||
package mc.world.simple;
|
package mc.world.simple;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@@ -12,13 +8,15 @@ import mc.core.world.World;
|
|||||||
import mc.core.world.WorldType;
|
import mc.core.world.WorldType;
|
||||||
import mc.core.world.chunk.Chunk;
|
import mc.core.world.chunk.Chunk;
|
||||||
import mc.core.world.chunk.ChunkProvider;
|
import mc.core.world.chunk.ChunkProvider;
|
||||||
|
import org.springframework.beans.factory.BeanNameAware;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SimpleWorld implements World {
|
public class SimpleWorld implements World, BeanNameAware {
|
||||||
@Getter
|
@Getter
|
||||||
private final String name = "flat";
|
private String name;
|
||||||
@Getter
|
@Getter
|
||||||
private final WorldType worldType = WorldType.FLAT;
|
private final WorldType worldType = WorldType.FLAT;
|
||||||
private EntityLocation spawn;
|
private EntityLocation spawn;
|
||||||
@@ -60,4 +58,9 @@ public class SimpleWorld implements World {
|
|||||||
((FlatChunkProvider)chunkProvider).setLayersBlock(listOfLayers);
|
((FlatChunkProvider)chunkProvider).setLayersBlock(listOfLayers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBeanName(@Nonnull String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user