Skip to content

Commit

Permalink
Pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Jan 20, 2024
1 parent e930c6d commit 00f12f9
Show file tree
Hide file tree
Showing 35 changed files with 116 additions and 93 deletions.
11 changes: 3 additions & 8 deletions src/main/java/org/sgrewritten/stargate/Stargate.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.sgrewritten.stargate.thread.ThreadHelper;
import org.sgrewritten.stargate.thread.task.StargateAsyncTask;
import org.sgrewritten.stargate.thread.task.StargateRegionTask;
import org.sgrewritten.stargate.thread.task.StargateTask;
import org.sgrewritten.stargate.util.BStatsHelper;
import org.sgrewritten.stargate.util.BungeeHelper;
import org.sgrewritten.stargate.util.FileHelper;
Expand Down Expand Up @@ -128,8 +129,6 @@ public class Stargate extends JavaPlugin implements StargateAPI, ConfigurationAP
private StorageAPI storageAPI;
private LanguageManager languageManager;
private BungeeManager bungeeManager;
private final SynchronousPopulator synchronousTickPopulator = new SynchronousPopulator();
private final SynchronousPopulator syncSecPopulator = new SynchronousPopulator();
private static final int MAX_TEXT_LENGTH = 13;

private StargateEconomyAPI economyManager;
Expand Down Expand Up @@ -566,12 +565,8 @@ public void onDisable() {
//Close networked always-on Stargates as they have no destination on next start
registry.getNetworkRegistry(StorageType.LOCAL).closeAllPortals();
registry.getNetworkRegistry(StorageType.INTER_SERVER).closeAllPortals();
/*
* Replacement for legacy, which used:
* methodPortal.closeAllGates(this); Portal.clearGates(); managedWorlds.clear();
*/
synchronousTickPopulator.clear();
syncSecPopulator.clear();
StargateTask.forceRunAllTasks();

ThreadHelper.setAsyncQueueEnabled(false);
if (ConfigurationHelper.getBoolean(ConfigurationOption.USING_BUNGEE)) {
Messenger messenger = Bukkit.getMessenger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void generateStructure(VectorOperation converter, Location topLeft) {
// (Clear all blocks that are in the portal iris)
Material[] irisClosedList = irisClosed.toArray(new Material[0]);
for(BlockVector blockVector : this.blocks){
int target = RANDOM.nextInt(irisClosedList.length)-1;
int target = RANDOM.nextInt(irisClosedList.length);
Material chosenType = irisClosedList[target];
Location location = VectorUtils.getLocation(topLeft,converter,blockVector);
new StargateRegionTask(location, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public int getConfigVersion() {

@Override
protected TwoTuple<String, Object> getNewConfigPair(TwoTuple<String, Object> oldPair) {
if (!configConversions.contains(oldPair.getFirstValue())) {
if (!configConversions.containsKey(oldPair.getFirstValue())) {
return oldPair;
}
String newKey = configConversions.getProperty(oldPair.getFirstValue());
Expand Down Expand Up @@ -91,8 +91,8 @@ private static Properties loadConfigConversions(String file) {
private void runChangeWorldNameToUUID(SQLDatabaseAPI database, TableNameConfiguration nameConfiguration,
StorageType type) throws SQLException, IOException {
String directory = "/migration/database/v-9/";
String fileNameUpdate = directory + (type == StorageType.LOCAL ? "update_world_local" : "update_world_inter_server");
String fileNameGet = directory + (type == StorageType.LOCAL ? "get_world_local" : "get_world_inter_server");
String fileNameUpdate = directory + (type == StorageType.LOCAL ? "update_world_local.sql" : "update_world_inter_server.sql");
String fileNameGet = directory + (type == StorageType.LOCAL ? "get_world_local.sql" : "get_world_inter_server.sql");
try (Connection connection = database.getConnection()) {
Set<String> worldNames = new HashSet<>();
try (InputStream stream = Stargate.class.getResourceAsStream(fileNameGet)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ public void renameNetwork(String newId, String oldId, StorageType storageType) t
int zMod = corner1Chunk.getZ() < corner2Chunk.getZ() ? 1 : -1;

Set<Chunk> chunks = new HashSet<>();
for (int x = corner1Chunk.getX(); shouldStop(corner2Chunk.getX(), x, xMod); x += xMod) {
for (int z = corner1Chunk.getZ(); shouldStop(corner2Chunk.getZ(), z, zMod); z += zMod) {
for (int x = corner1Chunk.getX(); !shouldStop(corner2Chunk.getX(), x, xMod); x += xMod) {
for (int z = corner1Chunk.getZ(); !shouldStop(corner2Chunk.getZ(), z, zMod); z += zMod) {
chunks.add(world.getChunkAt(x, z));
}
}
Expand All @@ -402,9 +402,9 @@ public void renameNetwork(String newId, String oldId, StorageType storageType) t

private boolean shouldStop(int target, int currentPosition, int modifier) {
if (modifier > 0) {
return currentPosition >= target;
return currentPosition > target;
} else {
return currentPosition <= target;
return currentPosition < target;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public StargateRegionTask(Location location, Runnable runnable, boolean bungee)
this.plugin = Stargate.getInstance();
this.bungee = bungee;
}

public StargateRegionTask(Location location, Runnable runnable) {
this(location, runnable, false);
}
Expand All @@ -41,19 +42,23 @@ public void runDelayed(long delay) {
ScheduledTask theTask = Bukkit.getServer().getRegionScheduler().runDelayed(plugin, location, super::runTask, delay);
super.registerFoliaTask(theTask);
} else {
super.registerBukkitTask(new StargateBukkitRunnable(this::runPopulatorTask )).runTaskLater(plugin,delay);
super.registerBukkitTask(new StargateBukkitRunnable(this::runPopulatorTask)).runTaskLater(plugin, delay);
}
}

private void runPopulatorTask(){
private void runPopulatorTask() {
populator.addAction(super::runTask, bungee);
super.registerTask();
}

public static void startPopulator(Plugin plugin){
if(USING_FOLIA){
public static void startPopulator(Plugin plugin) {
if (USING_FOLIA) {
return;
}
new StargateBukkitRunnable(populator).runTaskTimer(plugin,0,1);
new StargateBukkitRunnable(populator).runTaskTimer(plugin, 0, 1);
}

public static void clearPopulator() {
populator.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.scheduler.BukkitRunnable;
import org.sgrewritten.stargate.property.NonLegacyMethod;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

Expand Down Expand Up @@ -61,12 +62,15 @@ private void cancelIfTaskHasBeenScheduled(boolean bukkit) {
}

/**
* Runns all tasks
* Runs all tasks
*/
public static void forceRunAllTasks() {
int counter = 0;
while(!tasks.isEmpty() && counter > MAXIMUM_SHUTDOWN_CYCLES) {
tasks.forEach(Runnable::run);
while(!tasks.isEmpty() && counter < MAXIMUM_SHUTDOWN_CYCLES) {
Queue<Runnable> scheduledTasks = new LinkedList<>(tasks);
scheduledTasks.forEach(Runnable::run);
tasks.removeAll(scheduledTasks);
counter++;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/sgrewritten/stargate/util/ButtonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public static Material getButtonMaterial(Location locationToGenerateButton) {
}

private static Material getOverWaterButtonMaterial(){
return BUTTONS.get(RANDOM.nextInt(BUTTONS.size())-1);
return BUTTONS.get(RANDOM.nextInt(BUTTONS.size()));
}

private static Material getUnderWaterButtonMaterial(){
return CORAL_WALL_FANS.get(RANDOM.nextInt(CORAL_WALL_FANS.size())-1);
return CORAL_WALL_FANS.get(RANDOM.nextInt(CORAL_WALL_FANS.size()));
}

private static Stream<Material> getDeadWallCoralWallFans(){
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/org/sgrewritten/stargate/StargateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.sgrewritten.stargate.network.portal.PortalBlockGenerator;
import org.sgrewritten.stargate.network.portal.PortalFactory;
import org.sgrewritten.stargate.thread.ThreadHelper;
import org.sgrewritten.stargate.util.StargateTestHelper;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -47,7 +48,7 @@ class StargateTest {

@BeforeEach
void setup() throws TranslatableException, NoFormatFoundException, GateConflictException {
server = MockBukkit.mock();
server = StargateTestHelper.setup();
scheduler = server.getScheduler();
WorldMock world = server.addSimpleWorld("world");
System.setProperty("bstats.relocatecheck", "false");
Expand All @@ -70,7 +71,7 @@ void setup() throws TranslatableException, NoFormatFoundException, GateConflictE

@AfterEach
void tearDown() {
MockBukkit.unmock();
StargateTestHelper.tearDown();
}

@Test
Expand Down Expand Up @@ -119,7 +120,7 @@ void reload_StupidDefaultNetworkNameUUID() {

@ParameterizedTest
@ValueSource(strings = {"thisNameIsWayTooLong", "", "Test1\nTest2"})
void reload_StupidDefaultNetworkNameTooLong(String name) {
void reload_StupidDefaultNetworkName(String name) {
Stargate.setLogLevel(Level.OFF);
plugin.setConfigurationOptionValue(ConfigurationOption.DEFAULT_NETWORK, name);
plugin.reload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public class MaterialHandlerResolverTest {

@BeforeEach
void setUp() throws InvalidNameException, UnimplementedFlagException, NameLengthException, NameConflictException {
this.server = MockBukkit.mock();
StargateTestHelper.setup();
this.server = StargateTestHelper.setup();
this.storage = new StorageMock();
this.blockHandlerResolver = new BlockHandlerResolver(storage);
this.registry = new RegistryMock(storage, blockHandlerResolver);
Expand All @@ -62,7 +61,7 @@ void setUp() throws InvalidNameException, UnimplementedFlagException, NameLength

@AfterEach
void tearDown() {
MockBukkit.unmock();
StargateTestHelper.tearDown();
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.sgrewritten.stargate.database.property.PropertiesDatabase;
import org.sgrewritten.stargate.database.property.StoredProperty;
import org.sgrewritten.stargate.util.LanguageManagerMock;
import org.sgrewritten.stargate.util.StargateTestHelper;

import java.io.File;
import java.io.IOException;
Expand All @@ -30,7 +31,7 @@ class CommandParityTest {

@BeforeEach
void setUp() throws IOException {
@NotNull ServerMock server = MockBukkit.mock();
@NotNull ServerMock server = StargateTestHelper.setup();
this.plugin = MockBukkit.createMockPlugin();
console = server.getConsoleSender();
player = server.addPlayer();
Expand All @@ -42,7 +43,7 @@ void setUp() throws IOException {

@AfterEach
void tearDown() {
MockBukkit.unmock();
StargateTestHelper.tearDown();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sgrewritten.stargate.Stargate;
import org.sgrewritten.stargate.util.StargateTestHelper;

class CommandTraceTest {

Expand All @@ -21,7 +22,7 @@ class CommandTraceTest {

@BeforeEach
void setUp() {
@NotNull ServerMock server = MockBukkit.mock();
@NotNull ServerMock server = StargateTestHelper.setup();
sender = server.addPlayer();
System.setProperty("bstats.relocatecheck", "false");
plugin = MockBukkit.load(Stargate.class);
Expand All @@ -31,7 +32,7 @@ void setUp() {

@AfterEach
void tearDown() {
MockBukkit.unmock();
StargateTestHelper.tearDown();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.sgrewritten.stargate.network.portal.PortalFactory;
import org.sgrewritten.stargate.network.portal.portaldata.PortalData;
import org.sgrewritten.stargate.util.SQLTestHelper;
import org.sgrewritten.stargate.util.StargateTestHelper;
import org.sgrewritten.stargate.util.database.DatabaseHelper;
import org.sgrewritten.stargate.util.database.PortalStorageHelper;

Expand Down Expand Up @@ -90,7 +91,7 @@ public DatabaseTester(SQLDatabaseAPI database, TableNameConfiguration nameConfig
DatabaseTester.isMySQL = isMySQL;
DatabaseTester.nameConfig = nameConfig;

ServerMock server = MockBukkit.mock();
ServerMock server = StargateTestHelper.setup();
world = new WorldMock(Material.DIRT, 5);
server.addWorld(world);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.sgrewritten.stargate.exception.TranslatableException;
import org.sgrewritten.stargate.exception.database.StorageWriteException;
import org.sgrewritten.stargate.network.StorageType;
import org.sgrewritten.stargate.util.StargateTestHelper;

import java.sql.Connection;
import java.sql.SQLException;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static void setUp() throws SQLException, InvalidStructureException, Starg

@AfterAll
public static void tearDown() throws SQLException {
MockBukkit.unmock();
StargateTestHelper.tearDown();

try (Connection connection = database.getConnection()) {
connection.prepareStatement("DROP DATABASE Stargate;").execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.sgrewritten.stargate.exception.TranslatableException;
import org.sgrewritten.stargate.exception.database.StorageWriteException;
import org.sgrewritten.stargate.network.StorageType;
import org.sgrewritten.stargate.util.StargateTestHelper;

import java.io.File;
import java.sql.SQLException;
Expand All @@ -37,7 +38,7 @@ public static void setUp() throws SQLException, InvalidStructureException, Trans

@AfterAll
public static void tearDown() throws SQLException {
MockBukkit.unmock();
StargateTestHelper.tearDown();
try {
DatabaseTester.deleteAllTables(nameConfig);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sgrewritten.stargate.util.StargateTestHelper;

import java.io.File;
import java.io.IOException;
Expand All @@ -17,15 +18,15 @@ class PropertiesDatabaseTest {

@BeforeEach
void setUp() throws IOException {
MockBukkit.mock();
StargateTestHelper.setup();
MockPlugin plugin = MockBukkit.createMockPlugin();
fileLocation = new File(plugin.getDataFolder(), "test.properties");
propertiesDatabase = new PropertiesDatabase(fileLocation);
}

@AfterEach
void tearDown() {
MockBukkit.unmock();
StargateTestHelper.tearDown();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ class GateFormatTest {

@BeforeEach
void setUp() {
MockBukkit.mock();
StargateTestHelper.setup();
this.gateFormat = GateFormatRegistry.getFormat("nether.gate");
}

@AfterEach
void tearDown() {
MockBukkit.unmock();
StargateTestHelper.tearDown();
}

@Test
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/sgrewritten/stargate/gate/GateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.sgrewritten.stargate.network.RegistryMock;
import org.sgrewritten.stargate.network.portal.PortalBlockGenerator;
import org.sgrewritten.stargate.network.portal.portaldata.GateData;
import org.sgrewritten.stargate.util.StargateTestHelper;

import java.io.File;
import java.util.List;
Expand All @@ -37,7 +38,7 @@ class GateTest {

@BeforeEach
void setUp() {
ServerMock server = MockBukkit.mock();
ServerMock server = StargateTestHelper.setup();
this.world = server.addSimpleWorld("world");
Location topLeft = new Location(world, 0, 6, 0);
BlockFace facing = BlockFace.SOUTH;
Expand All @@ -53,7 +54,7 @@ void setUp() {

@AfterEach
void tearDown() {
MockBukkit.unmock();
StargateTestHelper.tearDown();
}

@Test
Expand Down
Loading

0 comments on commit 00f12f9

Please sign in to comment.