Skip to content

Commit

Permalink
simplify build scripts
Browse files Browse the repository at this point in the history
and reduce call stack (a bit)
  • Loading branch information
lonefelidae16 committed Jun 11, 2024
1 parent 8f5148c commit e9cd7cd
Show file tree
Hide file tree
Showing 27 changed files with 84 additions and 203 deletions.
39 changes: 19 additions & 20 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,20 @@ base {
archivesName = project.archives_base_name
}

var versionTitle = "$project.mod_version for $project.minecraft_version"

allprojects {
apply plugin: 'java'

java {
withSourcesJar()
sourceCompatibility = JavaVersion.toVersion(targetJdk)
targetCompatibility = JavaVersion.toVersion(targetJdk)
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
it.options.release.set(targetJdk)
}

project.ext['moduleId'] = 'unnamed'
project.ext['moduleName'] = ''
project.ext['mod_license'] = project.mod_license
Expand All @@ -52,10 +63,10 @@ allprojects {
}

props.putAll([
'moduleId': project.ext['moduleId'],
'moduleName': project.ext['moduleName'],
'moduleId' : project.ext['moduleId'],
'moduleName' : project.ext['moduleName'],
'mod_license': project.ext['mod_license'],
'version': project.version
'version' : project.version
])

filesMatching("fabric.mod.json") {
Expand All @@ -65,10 +76,10 @@ allprojects {

doLast {
fileTree(dir: outputs.files.asPath, include: "**/fabric.mod.json").each { file ->
var json = JsonParser.parseString(file.text).asJsonObject
var fabricModJson = JsonParser.parseString(file.text).asJsonObject
if (!project.ext['moduleAccessWidener'].isEmpty()) {
json.add("accessWidener", new JsonPrimitive(project.ext['moduleAccessWidener']))
file.text = json.toString()
fabricModJson.add("accessWidener", new JsonPrimitive(project.ext['moduleAccessWidener']))
file.text = fabricModJson.toString()
}
}
}
Expand Down Expand Up @@ -124,18 +135,6 @@ dependencies {
implementation project(path: 'sound-categories:versioned:sc1.18', configuration: 'namedElements')
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
it.options.release.set(targetJdk)
}

java {
withSourcesJar()

sourceCompatibility = JavaVersion.toVersion(targetJdk)
targetCompatibility = JavaVersion.toVersion(targetJdk)
}

jar {
from("LICENSE") {
rename { "${it}_${project.archives_base_name}" }
Expand Down
8 changes: 0 additions & 8 deletions logics/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
id 'fabric-loom'
}

var targetJdk = Integer.parseInt(rootProject.java_compiler_version)
var extraSounds = project.parent

base {
Expand All @@ -23,10 +22,3 @@ dependencies {
loom {
accessWidenerPath = file("src/main/resources/extrasounds.accesswidener")
}

java {
withSourcesJar()

sourceCompatibility = JavaVersion.toVersion(targetJdk)
targetCompatibility = JavaVersion.toVersion(targetJdk)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static Identifier getClickId(Identifier id, SoundType type) {
}

public static SoundEvent createEvent(String path) {
return createEvent(Objects.requireNonNull(generateIdentifier(path)));
return createEvent(Objects.requireNonNull(generateIdentifier(MODID, path)));
}

public static SoundEvent createEvent(Identifier id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import me.lonefelidae16.groominglib.Util;
import me.lonefelidae16.groominglib.api.PrefixableMessageFactory;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
Expand Down Expand Up @@ -199,15 +198,15 @@ public void blockInteract(SoundEvent snd, BlockPos position) {
}

public void blockInteract(Item item, BlockPos position) {
this.blockInteract(getSoundByItem(item, SoundType.PICKUP), position);
this.blockInteract(this.getSoundByItem(item, SoundType.PICKUP), position);
}

public void playSound(SoundEvent snd, SoundType type) {
this.playSound(snd, type.pitch, type.category);
}

public void playSound(Item item, SoundType type) {
this.playSound(getSoundByItem(item, type), type.pitch, type.category);
this.playSound(this.getSoundByItem(item, type), type.pitch, type.category);
}

/**
Expand All @@ -230,24 +229,43 @@ private void handleQuickMoveSound(Item item) {
}

public void playSound(SoundEvent snd, float pitch, SoundCategory category, SoundCategory... optionalVolumes) {
float volume = getSoundVolume(Mixers.MASTER);
float volume = this.getSoundVolume(Mixers.MASTER);
if (optionalVolumes != null) {
for (SoundCategory cat : optionalVolumes) {
volume = Math.min(getSoundVolume(cat), volume);
volume = Math.min(this.getSoundVolume(cat), volume);
}
}
if (volume == 0) {
// skip reflection when volume is zero.
if (DebugUtils.DEBUG) {
this.logZeroVolume(snd);
}
return;
}
final var soundInstance = VersionedPositionedSoundInstanceWrapper.newInstance(
snd.getId(), category, volume, pitch, false, 0, SoundInstance.AttenuationType.NONE,
0.0D, 0.0D, 0.0D, true
);
this.playSound((PositionedSoundInstance) Objects.requireNonNull(soundInstance));
this.playSound(Objects.requireNonNull(soundInstance));
}

public void playSound(SoundEvent snd, SoundType type, float volume, float pitch, BlockPos position) {
volume *= getSoundVolume(Mixers.MASTER);
if (volume == 0) {
// skip reflection when volume is zero.
if (DebugUtils.DEBUG) {
this.logZeroVolume(snd);
}
return;
}
final var soundInstance = VersionedPositionedSoundInstanceWrapper.newInstance(
snd, type.category, getSoundVolume(Mixers.MASTER) * volume, pitch, position
snd, type.category, volume, pitch, position
);
this.playSound((PositionedSoundInstance) Objects.requireNonNull(soundInstance));
this.playSound(Objects.requireNonNull(soundInstance));
}

private void logZeroVolume(SoundEvent snd) {
LOGGER.warn("Sound suppressed due to zero volume, was '{}'.", snd.getId());
}

private void playSound(SoundInstance instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public class SoundPackLoader {
private static final Path CACHE_PATH = Path.of(System.getProperty("java.io.tmpdir"), ".minecraft_fabric", CACHE_FNAME);

public static final Map<Identifier, SoundEvent> CUSTOM_SOUND_EVENT = new HashMap<>();
public static final VersionedClientResource EXTRA_SOUNDS_RESOURCE = Objects.requireNonNull(VersionedClientResource.newInstance(ExtraSounds.MODID, "%s Runtime Resources".formatted(ExtraSounds.class.getSimpleName())));
public static final VersionedClientResource EXTRA_SOUNDS_RESOURCE = Objects.requireNonNull(
VersionedClientResource.newInstance(ExtraSounds.MODID, "%s Runtime Resources".formatted(ExtraSounds.class.getSimpleName()))
);
public static final Logger LOGGER = LogManager.getLogger(
SoundPackLoader.class,
new PrefixableMessageFactory("%s/%s".formatted(
Expand Down Expand Up @@ -113,7 +115,7 @@ public static void init() {
}

final JsonObject jsonObject = cacheData.asJsonObject();
jsonObject.keySet().forEach(key -> putSoundEvent(ExtraSounds.generateIdentifier(key)));
jsonObject.keySet().forEach(key -> putSoundEvent(ExtraSounds.generateIdentifier(ExtraSounds.MODID, key)));
} catch (Exception ex) {
// If there is an exception, regenerate and write the cache.
if (DebugUtils.DEBUG) {
Expand Down Expand Up @@ -297,7 +299,7 @@ private static String sanitize(String in) {
/**
* Shows the cache data that include {@link CacheInfo} and Json String.
*/
protected static class CacheData {
static class CacheData {
/**
* The cache info.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.lang.reflect.Method;
import java.util.Objects;

public interface VersionedPositionedSoundInstanceWrapper {
public interface VersionedPositionedSoundInstanceWrapper extends SoundInstance {
String METHOD_KEY_INIT = VersionedPositionedSoundInstanceWrapper.class.getCanonicalName() + "#init";

static VersionedPositionedSoundInstanceWrapper newInstance(Identifier id, SoundCategory category, float volume, float pitch, boolean repeat, int repeatDelay, SoundInstance.AttenuationType attenuationType, double x, double y, double z, boolean relative) {
Expand All @@ -30,7 +30,7 @@ static VersionedPositionedSoundInstanceWrapper newInstance(Identifier id, SoundC
try {
return (VersionedPositionedSoundInstanceWrapper) Objects.requireNonNull(init).invoke(null, id, category, volume, pitch, repeat, repeatDelay, attenuationType, x, y, z, relative);
} catch (Exception ex) {
ExtraSounds.LOGGER.error("Cannot initialize 'PositionedSoundInstance'", ex);
ExtraSounds.LOGGER.error("Cannot instantiate 'PositionedSoundInstance'", ex);
}

return null;
Expand Down
13 changes: 0 additions & 13 deletions versioned/es1.18.2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
id 'fabric-loom'
}

var targetJdk = Integer.parseInt(rootProject.java_compiler_version)
var extraSounds = project.parent.parent

base {
Expand All @@ -25,18 +24,6 @@ tasks.withType(ProcessResources).configureEach {
project.ext['moduleAccessWidener'] = 'extrasounds.accesswidener'
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
it.options.release.set(targetJdk)
}

loom {
accessWidenerPath = file("src/main/resources/extrasounds.accesswidener")
}

java {
withSourcesJar()

sourceCompatibility = JavaVersion.toVersion(targetJdk)
targetCompatibility = JavaVersion.toVersion(targetJdk)
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack

@Inject(method = "interactBlock", at = @At(value = "HEAD"))
private void extrasounds$storeState(ClientPlayerEntity player, ClientWorld world, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
if (world == null) {
if (player == null || world == null) {
return;
}

Expand All @@ -91,7 +91,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack

@Inject(method = "interactBlock", at = @At(value = "RETURN", ordinal = 2))
private void extrasounds$afterOnUse(ClientPlayerEntity player, ClientWorld world, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
if (world == null || player.isSpectator()) {
if (player == null || world == null || player.isSpectator()) {
return;
}

Expand All @@ -108,7 +108,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack
), locals = LocalCapture.CAPTURE_FAILSOFT
)
private void extrasounds$interactEntityAt(PlayerEntity player, Entity entity, EntityHitResult hitResult, Hand hand, CallbackInfoReturnable<ActionResult> cir, Vec3d target) {
if (player == null || hitResult == null || player.isSpectator()) {
if (player == null || entity == null || hitResult == null || player.isSpectator()) {
return;
}

Expand Down
13 changes: 0 additions & 13 deletions versioned/es1.18/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
id 'fabric-loom'
}

var targetJdk = Integer.parseInt(rootProject.java_compiler_version)
var extraSounds = project.parent.parent

base {
Expand All @@ -25,18 +24,6 @@ tasks.withType(ProcessResources).configureEach {
project.ext['moduleAccessWidener'] = 'extrasounds.accesswidener'
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
it.options.release.set(targetJdk)
}

loom {
accessWidenerPath = file("src/main/resources/extrasounds.accesswidener")
}

java {
withSourcesJar()

sourceCompatibility = JavaVersion.toVersion(targetJdk)
targetCompatibility = JavaVersion.toVersion(targetJdk)
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack

@Inject(method = "interactBlock", at = @At(value = "HEAD"))
private void extrasounds$storeState(ClientPlayerEntity player, ClientWorld world, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
if (world == null) {
if (player == null || world == null) {
return;
}

Expand All @@ -91,7 +91,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack

@Inject(method = "interactBlock", at = @At(value = "RETURN", ordinal = 2))
private void extrasounds$afterOnUse(ClientPlayerEntity player, ClientWorld world, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
if (world == null || player.isSpectator()) {
if (player == null || world == null || player.isSpectator()) {
return;
}

Expand All @@ -108,7 +108,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack
), locals = LocalCapture.CAPTURE_FAILSOFT
)
private void extrasounds$interactEntityAt(PlayerEntity player, Entity entity, EntityHitResult hitResult, Hand hand, CallbackInfoReturnable<ActionResult> cir, Vec3d target) {
if (player == null || hitResult == null || player.isSpectator()) {
if (player == null || entity == null || hitResult == null || player.isSpectator()) {
return;
}

Expand Down
13 changes: 0 additions & 13 deletions versioned/es1.19.1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
id 'fabric-loom'
}

var targetJdk = Integer.parseInt(rootProject.java_compiler_version)
var extraSounds = project.parent.parent

base {
Expand All @@ -25,18 +24,6 @@ tasks.withType(ProcessResources).configureEach {
project.ext['moduleAccessWidener'] = 'extrasounds.accesswidener'
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
it.options.release.set(targetJdk)
}

loom {
accessWidenerPath = file("src/main/resources/extrasounds.accesswidener")
}

java {
withSourcesJar()

sourceCompatibility = JavaVersion.toVersion(targetJdk)
targetCompatibility = JavaVersion.toVersion(targetJdk)
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack
@Inject(method = "interactBlockInternal", at = @At(value = "HEAD"))
private void extrasounds$storeState(ClientPlayerEntity player, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
final World world = this.client.world;
if (world == null) {
if (player == null || world == null) {
return;
}

Expand All @@ -102,7 +102,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack
)
private void extrasounds$afterOnUse(ClientPlayerEntity player, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir, MutableObject<ActionResult> mutableObject) {
final World world = this.client.world;
if (world == null || player.isSpectator()) {
if (player == null || world == null || player.isSpectator()) {
return;
}

Expand All @@ -119,7 +119,7 @@ protected boolean canSoundArmorStandPreferred(ItemStack currentStack, ItemStack
), locals = LocalCapture.CAPTURE_FAILSOFT
)
private void extrasounds$interactEntityAt(PlayerEntity player, Entity entity, EntityHitResult hitResult, Hand hand, CallbackInfoReturnable<ActionResult> cir, Vec3d target) {
if (player == null || hitResult == null || player.isSpectator()) {
if (player == null || entity == null || hitResult == null || player.isSpectator()) {
return;
}

Expand Down
Loading

0 comments on commit e9cd7cd

Please sign in to comment.