Skip to content

Commit

Permalink
The default sounds and configs are now a built in resource pack that …
Browse files Browse the repository at this point in the history
…can be toggled on and off
  • Loading branch information
Sollace committed Oct 6, 2023
1 parent e48ef1a commit 67584a7
Show file tree
Hide file tree
Showing 923 changed files with 27 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/main/java/eu/ha3/presencefootsteps/PresenceFootsteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.client.toast.ToastManager;
import net.minecraft.client.util.InputUtil;
import net.minecraft.resource.ResourceType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class PresenceFootsteps implements ClientModInitializer {
public static final Logger logger = LogManager.getLogger("PFSolver");
Expand Down Expand Up @@ -85,6 +88,10 @@ public void onInitializeClient() {

ClientTickEvents.END_CLIENT_TICK.register(this::onTick);
ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(engine);

FabricLoader.getInstance().getModContainer("presencefootsteps").ifPresent(container -> {
ResourceManagerHelper.registerBuiltinResourcePack(new Identifier("presencefootsteps", "default_sound_pack"), container, Text.translatable("pf.default_sounds.name"), ResourcePackActivationType.DEFAULT_ENABLED);
});
}

private void onTick(MinecraftClient client) {
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/eu/ha3/presencefootsteps/sound/SoundEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class SoundEngine implements IdentifiableResourceReloadListener {

private final PFConfig config;

private boolean hasConfigurations;

public SoundEngine(PFConfig config) {
this.config = config;
}
Expand Down Expand Up @@ -88,7 +90,7 @@ public void reload() {
}

public boolean isRunning(MinecraftClient client) {
return config.getEnabled() && (client.isInSingleplayer() || config.getEnabledMP());
return hasConfigurations && config.getEnabled() && (client.isInSingleplayer() || config.getEnabledMP());
}

private Stream<? extends Entity> getTargets(final Entity cameraEntity) {
Expand Down Expand Up @@ -199,17 +201,19 @@ public CompletableFuture<Void> reload(Synchronizer sync, ResourceManager sender,

public void reloadEverything(ResourceManager manager) {
isolator = new PFIsolator(this);

ResourceUtils.forEachReverse(BLOCK_MAP, manager, isolator.getBlockMap()::load);
ResourceUtils.forEach(GOLEM_MAP, manager, isolator.getGolemMap()::load);
ResourceUtils.forEach(PRIMITIVE_MAP, manager, isolator.getPrimitiveMap()::load);
ResourceUtils.forEach(LOCOMOTION_MAP, manager, isolator.getLocomotionMap()::load);
ResourceUtils.forEach(ACOUSTICS, manager, new AcousticsJsonParser(isolator.getAcoustics())::parse);
ResourceUtils.forEach(VARIATOR, manager, isolator.getVariator()::load);
hasConfigurations = false;

hasConfigurations |= ResourceUtils.forEachReverse(BLOCK_MAP, manager, isolator.getBlockMap()::load);
hasConfigurations |= ResourceUtils.forEach(GOLEM_MAP, manager, isolator.getGolemMap()::load);
hasConfigurations |= ResourceUtils.forEach(PRIMITIVE_MAP, manager, isolator.getPrimitiveMap()::load);
hasConfigurations |= ResourceUtils.forEach(LOCOMOTION_MAP, manager, isolator.getLocomotionMap()::load);
hasConfigurations |= ResourceUtils.forEach(ACOUSTICS, manager, new AcousticsJsonParser(isolator.getAcoustics())::parse);
hasConfigurations |= ResourceUtils.forEach(VARIATOR, manager, isolator.getVariator()::load);
}

public void shutdown() {
isolator = new PFIsolator(this);
hasConfigurations = false;

PlayerEntity player = MinecraftClient.getInstance().player;

Expand Down
11 changes: 7 additions & 4 deletions src/main/java/eu/ha3/presencefootsteps/util/ResourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
import net.minecraft.util.Identifier;

public interface ResourceUtils {
static void forEach(Identifier id, ResourceManager manager, Consumer<Reader> consumer) {
manager.getAllResources(id).forEach(res -> {
static boolean forEach(Identifier id, ResourceManager manager, Consumer<Reader> consumer) {
return manager.getAllResources(id).stream().mapToInt(res -> {
try (Reader stream = new InputStreamReader(res.getInputStream())) {
consumer.accept(stream);
return 1;
} catch (Exception e) {
PresenceFootsteps.logger.error("Error encountered loading resource " + id + " from pack" + res.getResourcePackName(), e);
return 0;
}
});
}).sum() > 0;
}

static void forEachReverse(Identifier id, ResourceManager manager, Consumer<Reader> consumer) {
static boolean forEachReverse(Identifier id, ResourceManager manager, Consumer<Reader> consumer) {
List<Resource> resources = manager.getAllResources(id);
for (int i = resources.size() - 1; i >= 0; i--) {
Resource res = resources.get(i);
Expand All @@ -31,5 +33,6 @@ static void forEachReverse(Identifier id, ResourceManager manager, Consumer<Read
PresenceFootsteps.logger.error("Error encountered loading resource " + id + " from pack" + res.getResourcePackName(), e);
}
}
return !resources.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.presencefootsteps.settings": "Presence Footsteps",
"pf.default_sounds.name": "Default Sound Pack",
"menu.pf.title": "Presence Footsteps Options",
"menu.pf.multiplayer.true": "Single + Multiplayer",
"menu.pf.multiplayer.false": "Singleplayer Only",
Expand Down
Empty file.
Loading

0 comments on commit 67584a7

Please sign in to comment.