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