Skip to content

Commit

Permalink
Merge pull request #168 from KevinDaGame/develop
Browse files Browse the repository at this point in the history
v8.8.0
  • Loading branch information
KevinDaGame authored May 17, 2023
2 parents 59a0301 + 8462d6d commit 555d0bb
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class BiomeBrush extends AbstractBrush {

private VoxelBiome selectedBiome = VoxelBiome.PLAINS;



private void biome(final SnipeData v) {
final int brushSize = v.getBrushSize();
final double brushSizeSquared = Math.pow(brushSize, 2);
Expand All @@ -28,8 +26,11 @@ private void biome(final SnipeData v) {
final double xSquared = Math.pow(x, 2);
for (int z = -brushSize; z <= brushSize; z++) {
if ((xSquared + Math.pow(z, 2)) <= brushSizeSquared) {
var location = new BaseLocation(this.getWorld(), this.getTargetBlock().getX() + x, 0, this.getTargetBlock().getZ() + z);
for ( int y = getMinHeight(); y <= getMaxHeight(); y++) {
var location = new BaseLocation(this.getWorld(), this.getTargetBlock().getX() + x, y, this.getTargetBlock().getZ() + z);
this.addOperation(new BiomeOperation(location, getWorld().getBiome(location), this.selectedBiome));

}
}
}
}
Expand All @@ -51,7 +52,7 @@ protected final void powder(final SnipeData v) {
public final void info(final VoxelMessage vm) {
vm.brushName(this.getName());
vm.size();
vm.custom(Messages.SELECTED_BIOME_TYPE.replace("%selectedBiome%", this.selectedBiome.key()));
vm.custom(Messages.SELECTED_BIOME_TYPE.replace("%selectedBiome%", this.selectedBiome.toString()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.kevindagame.command;

import com.github.kevindagame.VoxelBrushManager;
import com.github.kevindagame.VoxelSniper;
import com.github.kevindagame.brush.BrushData;
import com.github.kevindagame.brush.IBrush;
import com.github.kevindagame.brush.perform.IPerformerBrush;
Expand Down Expand Up @@ -58,13 +59,17 @@ public boolean doCommand(IPlayer player, final String[] args) {
try {
int originalSize = snipeData.getBrushSize();
int newSize = Integer.parseInt(args[0]);

var brush = sniper.getBrush(currentToolId);
if (brush == null) {
snipeData.sendMessage(Messages.NO_BRUSH_SELECTED);
return true;
}

if(newSize > VoxelSniper.voxelsniper.getVoxelSniperConfiguration().getMaxBrushSize()){
snipeData.sendMessage(Messages.BRUSH_SIZE_TOO_LARGE.replace("%maxBrushSize%", String.valueOf(VoxelSniper.voxelsniper.getVoxelSniperConfiguration().getMaxBrushSize())));
return true;
}

if (new PlayerBrushSizeChangedEvent(player, currentToolId, brush, originalSize, newSize).callEvent().isCancelled()) {
snipeData.sendMessage(Messages.ACTION_CANCELLED);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.kevindagame.service

import com.github.kevindagame.util.Release
import com.github.kevindagame.util.VersionChecker
import com.google.gson.Gson
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL

class GithubService {
private val releasesApiUrl = "https://api.github.com/repos/${VersionChecker.REPOSITORY_OWNER}/${VersionChecker.REPOSITORY_NAME}/releases"

/**
* Gets the latest version info from the GitHub API.
* @return The latest version info as a list of releases.
*/
fun getReleasesFromApi(): List<Release> {
return try {
val conn = URL(releasesApiUrl).openConnection() as HttpURLConnection
conn.addRequestProperty("User-Agent", "Mozilla/5.0")
val reader = InputStreamReader(conn.inputStream)
val releases = Gson().fromJson(reader, Array<Release>::class.java)
reader.close()
conn.disconnect()
releases.filterNot { it.prerelease }.toList()
} catch (e: Exception) {
System.err.println("Failed to check for a new version of VoxelSniper-Reimagined.")
emptyList()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ public enum Messages implements ComponentLike {
POLY_BRUSH_INFO_LINE,
ACTION_CANCELLED,
UPDATE_AVAILABLE,
BRUSH_SIZE_TOO_LARGE,
;
//</editor-fold>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.github.kevindagame.util

import com.google.gson.Gson
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import com.github.kevindagame.service.GithubService

/**
* @author KevinDaGame
* A class that checks for the latest version of VoxelSniper-Reimagined on GitHub.
*/
class VersionChecker() {
private val releasesApiUrl = "https://api.github.com/repos/$REPOSITORY_OWNER/$REPOSITORY_NAME/releases"
class VersionChecker(private val service: GithubService = GithubService()) {


/**
* Gets the latest version info from the GitHub API.
Expand All @@ -19,7 +16,7 @@ class VersionChecker() {
* @return The latest version info, or null if the current version is the latest.
*/
fun getLatestVersionInfo(currentVersion: String): VersionInfo? {
val releases = getReleasesFromApi()
val releases = service.getReleasesFromApi()
if (releases.isEmpty()) {
return null
}
Expand All @@ -41,24 +38,6 @@ class VersionChecker() {
return null
}

/**
* Gets the latest version info from the GitHub API.
* @return The latest version info as a list of releases.
*/
private fun getReleasesFromApi(): List<Release> {
return try {
val conn = URL(releasesApiUrl).openConnection() as HttpURLConnection
conn.addRequestProperty("User-Agent", "Mozilla/5.0")
val reader = InputStreamReader(conn.inputStream)
val releases = Gson().fromJson(reader, Array<Release>::class.java)
reader.close()
conn.disconnect()
releases.filterNot { it.prerelease }.toList()
} catch (e: Exception) {
System.err.println("Failed to check for a new version of VoxelSniper-Reimagined.")
emptyList()
}
}

/**
* Checks if the current version is outdated.
Expand All @@ -68,9 +47,14 @@ class VersionChecker() {
* @return True if the current version is outdated, false otherwise.
*/
private fun isOutdated(currentVersion: String, latestVersion: String): Boolean {
val current = parseVersion(currentVersion)
val latest = parseVersion(latestVersion)
return current < latest
return try {

val current = parseVersion(currentVersion)
val latest = parseVersion(latestVersion)
current < latest
} catch (e: NumberFormatException) {
false
}
}

/**
Expand All @@ -80,6 +64,7 @@ class VersionChecker() {
* @return The parsed version.
*/
private fun parseVersion(version: String): Version {

val versionParts = version.lowercase().removePrefix("v").split(".")
val major = versionParts.getOrElse(0) { "0" }.toInt()
val minor = versionParts.getOrElse(1) { "0" }.toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ String getName() {
private Version getVersion() {
return version;
}

@Override
public String toString() {
return (!namespace.equals(DEFAULT_NAMESPACE) ? namespace + ":" : "") + key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
public class VoxelSniperConfiguration {

public static final String CONFIG_IDENTIFIER_UNDO_CACHE_SIZE = "undo-cache-size";
public static final String CONFIG_IDENTIFIER_MAX_BRUSH_SIZE = "max-brush-size";
public static final String CONFIG_IDENTIFIER_MESSAGE_ON_LOGIN_ENABLED = "message-on-login-enabled";
public static final String CONFIG_IDENTIFIER_DEFAULT_BRUSH = "default-brush";
public static final String CONFIG_IDENTIFIER_PLOTSQUARED_INTEGRATION_ENABLED = "plotsquared-integration-enabled";
public static final String CONFIG_IDENTIFIER_WORLDGUARD_INTEGRATION_ENABLED = "worldguard-integration-enabled";
public static final String CONFIG_IDENTIFIER_UPDATE_CHECKER_ENABLED = "update-checker-enabled";

public static final int DEFAULT_UNDO_CACHE_SIZE = 20;
public static final int DEFAULT_MAX_BRUSH_SIZE = 20;
public static final boolean DEFAULT_MESSAGE_ON_LOGIN_ENABLED = true;
private static final boolean DEFAULT_USE_PLOTSQUARED = false;
private static final boolean DEFAULT_USE_WORLDGUARD = false;
Expand Down Expand Up @@ -49,6 +51,15 @@ public void setUndoCacheSize(int size) {
configuration.set(CONFIG_IDENTIFIER_UNDO_CACHE_SIZE, size);
}

/**
* Returns the maximum brush size.
*
* @return the maximum brush size
*/
public int getMaxBrushSize() {
return configuration.getInt(CONFIG_IDENTIFIER_MAX_BRUSH_SIZE, DEFAULT_MAX_BRUSH_SIZE);
}

/**
* Returns if the login message is enabled.
*
Expand Down
1 change: 1 addition & 0 deletions VoxelSniperCore/src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

undo-cache-size: 20
max-brush-size: 20
message-on-login-enabled: true

# The default brush to use when a player joins the server. Default: snipe. To have no brush selected on login, set this to none.
Expand Down
2 changes: 1 addition & 1 deletion VoxelSniperCore/src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,4 @@ ACTION_CANCELLED: <red>The action was cancelled
UPDATE_AVAILABLE: |-
<gold>An update is available for VoxelSniper. You are running version <red>%currentVersion%</red> and the latest version is <green>%latestVersion%</green>.
Download the latest version <green><click:OPEN_URL:%downloadUrl%><hover:show_text:Click me!>here</hover></click></green>!</gold>
BRUSH_SIZE_TOO_LARGE: <red>Brush size is too large. Maximum brush size is %maxBrushSize%.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.github.kevindagame.util


import com.github.kevindagame.service.GithubService
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`

class VersionCheckerTest {
@Test
Expand All @@ -29,5 +32,14 @@ class VersionCheckerTest {
assertTrue(v3 != v4)
}

@Test
fun getLatestVersionInfo_invalid_version() {
val githubService = mock(GithubService::class.java)
`when`(githubService.getReleasesFromApi()).thenReturn(listOf(Release("", "v1.0.0", emptyList<Asset>(), false)))
val versionChecker = VersionChecker(githubService)
val latestVersionInfo = versionChecker.getLatestVersionInfo("v1.1.0-SNAPSHOT")
assertEquals(null, latestVersionInfo)
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.kevindagame.voxelsniper.biome;

import com.github.kevindagame.voxelsniper.Version;
import junit.framework.TestCase;

public class VoxelBiomeTest extends TestCase {

public void testTestToString() {
VoxelBiome namespaced = new VoxelBiome("namespace", "key", Version.V1_16);
VoxelBiome onlyKey = new VoxelBiome("key");
assertEquals("namespace:key", namespaced.toString());
assertEquals("key", onlyKey.toString());
}
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/voxel-core.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ java {
}

group = "com.github.kevindagame"
version = "8.7.0"
version = findProperty("voxelsniper.version") ?: "unspecified"
//java.sourceCompatibility = JavaVersion.VERSION_16

tasks.withType<JavaCompile> {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
voxelsniper.version=8.8.0

0 comments on commit 555d0bb

Please sign in to comment.