Skip to content

Commit

Permalink
chore: clean up code; make use of var
Browse files Browse the repository at this point in the history
  • Loading branch information
mzdun committed Sep 23, 2024
1 parent 3f38494 commit fc4768b
Show file tree
Hide file tree
Showing 48 changed files with 290 additions and 265 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
build/
run/
bin/
mods/
.vscode/
.idea/
.fabric-cache/
*.pyc
*.log
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Resource locator mod for Fabric.

It helps players to find resources more easily. At start, it only looks for coal, so the player can produce torches and fend-off mobs. At later stages, it can help locate resources near the player that are needed for advancing the session.

**WARNING** - mod is beta quality, not tested with large number of mods, performance impact not known. Please, follow bug tracker to ask questions and report issues.
**WARNING** - mod is alpha quality, not tested with large number of mods, performance impact not known. Please, follow bug tracker to ask questions and report issues.

## Features

Expand Down
14 changes: 11 additions & 3 deletions fabric/any/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id 'jacoco'
}

project.setProperty('mod_version', "${rootProject.mod_version}-${project.minecraft_version}-fabric")
project.setProperty('mod_version', "${rootProject.mod_version}+${project.minecraft_version}-fabric")

version = project.mod_version
group = project.maven_group
Expand All @@ -23,6 +23,14 @@ loom {
}
}

jar {
manifest {
attributes(
'Scanner-Version': rootProject.mod_version,
)
}
}

serviceLoader {
serviceInterface \
'com.midnightbits.scanner.platform.PlatformInterface',
Expand Down Expand Up @@ -181,13 +189,13 @@ tasks.register('install', Exec) {
def pluginJar = project.repack.outputs.files[0]
def baseName = file(pluginJar).name

def outDir = "${installHome}/.fabric/user-mods/${project.minecraft_version}-fabric"
def outDir = "${installHome}/.feather/user-mods/${project.minecraft_version}-fabric"

dependsOn 'repack'
group 'Publishing'

executable 'python'
args = ["${rootDir}/tools/install-fabric.py", "${project.minecraft_version}-fabric", pluginJar]
args = ["${rootDir}/tools/install-feather.py", "${project.minecraft_version}-fabric", pluginJar]
}

// configure the maven publication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,37 @@

import com.midnightbits.scanner.platform.KeyBinder;
import com.midnightbits.scanner.platform.PlatformInterface;
import com.midnightbits.scanner.utils.CacheableValue;
import com.midnightbits.scanner.utils.Manifests;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;

public class FabricPlatform implements PlatformInterface {
private final FabricKeyBinder binder = new FabricKeyBinder();

private final CacheableValue<String> scannerVersion = new CacheableValue<>(
() -> Manifests.getAttribute("Scanner-Version"));
private final CacheableValue<String> minecraftVersion = new CacheableValue<>(
() -> Manifests.getAttribute("Fabric-Minecraft-Version"));

@Override
public String getPlatformName() {
return "Fabric";
}

@Override
public boolean isDevelopmentEnvironment() {
return FabricLoader.getInstance().isDevelopmentEnvironment();
public String getScannerVersion() {
return scannerVersion.get();
}

@Override
public boolean isDedicatedServer() {
return FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER;
public String getMinecraftVersion() {
return minecraftVersion.get();
}

@Override
public Path getGameDir() {
return FabricLoader.getInstance().getGameDir().normalize();
public boolean isDevelopmentEnvironment() {
return FabricLoader.getInstance().isDevelopmentEnvironment();
}

@Override
Expand Down
24 changes: 14 additions & 10 deletions scanner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ java {
}
}

jar {
manifest {
attributes(
'Scanner-Version': project.mod_version,
)
}
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs = ['-Xlint:deprecation']
}
Expand All @@ -46,23 +54,19 @@ jacocoTestReport {
dependsOn test // tests are required to run before generating the report

reports {
xml {
enabled true // coveralls plugin depends on xml format report
}

html {
enabled true
}
xml.required = true
html.required = true
}

afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(
dir: it,
exclude: [
'com/midnightbits/scanner/rt/core/Id*',
'com/midnightbits/scanner/rt/core/InvalidIdentifierException*',
'com/midnightbits/scanner/rt/math/**'
'com/midnightbits/scanner/rt/core/Id.class',
'com/midnightbits/scanner/rt/core/InvalidIdentifierException.class',
'com/midnightbits/scanner/rt/math/**',
'com/midnightbits/scanner/utils/Manifests.class',
])
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.midnightbits.scanner.sonar.BlockEchoes;
import com.midnightbits.scanner.sonar.Sonar;
import com.midnightbits.scanner.utils.ConfigFile;
import com.midnightbits.scanner.utils.Manifests;

public class ResourceScannerMod implements ScannerMod {
public static final String TAG = "resource-scanner";
Expand All @@ -30,7 +31,9 @@ public void onInitializeClient() {
settings.blockDistance(), settings.blockRadius(), settings.interestingIds(), settings.echoesSize());
});

LOGGER.warn("resource-scanner ({}, {})",
LOGGER.warn("resource-scanner ({} for {}, {}, {})",
Manifests.getTagString(Services.PLATFORM.getScannerVersion()),
Manifests.getProductVersion("MC", Services.PLATFORM.getMinecraftVersion()),
Services.PLATFORM.getPlatformName(),
Services.PLATFORM.getEnvironmentName());
Path configDir = Services.PLATFORM.getConfigDir();
Expand Down Expand Up @@ -58,18 +61,17 @@ private void onScanPressed(ClientCore client) {
return;

for (BlockEcho echo : sonar.echoes()) {
LOGGER.info("{} ({}) {}", echo.getPingTime(), echo.getPosition(), echo.getId());
LOGGER.info("{} ({}) {}", echo.pingTime(), echo.position(), echo.id());
}
LOGGER.info("");
}

@Override
public void setSonar(Sonar sonar) {
this.sonar = sonar;
}

@Override
public Iterable<BlockEcho> echoes() {
return sonar.echoes();
}

public void setSonar(Sonar sonar) {
this.sonar = sonar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
public interface PlatformInterface {
String getPlatformName();

String getScannerVersion();

String getMinecraftVersion();

boolean isDevelopmentEnvironment();

default String getEnvironmentName() {
return this.isDevelopmentEnvironment() ? "development" : "production";
}

boolean isDedicatedServer();

Path getGameDir();

Path getConfigDir();

KeyBinder getKeyBinder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.midnightbits.scanner.rt.core;

import com.midnightbits.scanner.sonar.BlockEcho;
import com.midnightbits.scanner.sonar.Sonar;

public interface ScannerMod {
void onInitializeClient();

void setSonar(Sonar sonar);

Iterable<BlockEcho> echoes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static EventEmitter createEmitter() {

@SuppressWarnings("unchecked")
default void apply(Iterable<EventListener<?>> listeners, Event event) {
for (var listener : listeners) {
for (final var listener : listeners) {
if (event.cancelable() && event.cancelled()) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import java.util.List;
import java.util.Map;

public class MapEventEmitter implements EventEmitter {
private Map<Class<?>, List<EventListener<?>>> listeners = new HashMap<>();
public final class MapEventEmitter implements EventEmitter {
private final Map<Class<?>, List<EventListener<?>>> listeners = new HashMap<>();

@Override
public <T extends Event> void addEventListener(Class<T> type, EventListener<T> listener) {
var clazzListeners = listeners.computeIfAbsent(type, k -> new ArrayList<>());
final var clazzListeners = listeners.computeIfAbsent(type, k -> new ArrayList<>());
clazzListeners.add(listener);
}

@Override
public <T extends Event> void removeEventListener(Class<T> type, EventListener<T> listener) {
var clazzListeners = listeners.get(type);
final var clazzListeners = listeners.get(type);
if (clazzListeners == null) {
return;
}
Expand All @@ -26,8 +26,8 @@ public <T extends Event> void removeEventListener(Class<T> type, EventListener<T
@Override
public void dispatchEvent(Event event) {
assert event != null;
var clazz = event.getClass();
var clazzListeners = listeners.get(clazz);
final var clazz = event.getClass();
final var clazzListeners = listeners.get(clazz);
if (clazzListeners == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,16 @@
import com.midnightbits.scanner.rt.core.Id;
import com.midnightbits.scanner.rt.math.V3i;
import com.midnightbits.scanner.utils.Clock;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class BlockEcho implements Comparable<BlockEcho> {
private final V3i position;
private final Id id;
private final long pingTime;

public BlockEcho(V3i position, Id id, long pingTime) {
this.position = position;
this.id = id;
this.pingTime = pingTime;
}
public record BlockEcho(V3i position, Id id, long pingTime) implements Comparable<BlockEcho> {

public static BlockEcho echoFrom(V3i position, Id id) {
return new BlockEcho(position, id, Clock.currentTimeMillis());
}

public Id getId() {
return id;
}

public V3i getPosition() {
return position;
}

public long getPingTime() {
return pingTime;
}

public boolean equals(Object obj) {
if (obj == null) {
throw new NullPointerException();
Expand All @@ -45,7 +25,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
final var builder = new StringBuilder();
builder.append("new BlockEcho(new V3i(")
.append(position.getX()).append(", ")
.append(position.getY()).append(", ")
Expand All @@ -61,11 +41,11 @@ public String toString() {
}

@Override
public int compareTo(BlockEcho other) {
public int compareTo(@Nullable BlockEcho other) {
if (other == null) {
throw new NullPointerException();
}
int result = (int) (pingTime - other.pingTime);
var result = (int) (pingTime - other.pingTime);
if (result != 0) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import com.midnightbits.scanner.rt.core.Id;
import com.midnightbits.scanner.rt.math.V3i;
import org.jetbrains.annotations.NotNull;

public class BlockEchoes implements Iterable<BlockEcho> {
public final class BlockEchoes implements Iterable<BlockEcho> {
private final TreeSet<BlockEcho> echoes = new TreeSet<>();
public static final int MAX_SIZE = 100;

Expand Down Expand Up @@ -38,7 +39,7 @@ public void refresh(int maxSize) {
* otherwise
*/
public boolean echoFrom(V3i position, Id id) {
boolean replaced = evictBlocks(stream().filter(b -> b.getPosition().equals(position)));
boolean replaced = evictBlocks(stream().filter(b -> b.position().equals(position)));
if (echoes.size() >= maxSize) {
evictBlocks(stream().limit(echoes.size() - maxSize + 1));
}
Expand All @@ -61,6 +62,7 @@ private boolean evictBlocks(Stream<BlockEcho> stream) {
return !evictions.isEmpty();
}

@NotNull
@Override
public Iterator<BlockEcho> iterator() {
return echoes.iterator();
Expand Down
Loading

0 comments on commit fc4768b

Please sign in to comment.