-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly implement resource loading on Fabric
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
- Loading branch information
1 parent
1a32dc0
commit 44df4b3
Showing
8 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
fabric/src/main/java/com/blamejared/contenttweaker/fabric/mixin/MinecraftMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.blamejared.contenttweaker.fabric.mixin; | ||
|
||
import com.blamejared.contenttweaker.fabric.resource.FabricResourceManager; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.main.GameConfig; | ||
import net.minecraft.server.packs.PackType; | ||
import net.minecraft.server.packs.repository.PackRepository; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(Minecraft.class) | ||
public abstract class MinecraftMixin { | ||
@Shadow | ||
public abstract PackRepository getResourcePackRepository(); | ||
|
||
@Inject( | ||
method = "<init>", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/server/packs/repository/PackRepository;reload()V" | ||
) | ||
) | ||
private void contenttweaker$init$injectCustomRepositorySources(final GameConfig config, final CallbackInfo info) { | ||
FabricResourceManager.inject(PackType.CLIENT_RESOURCES, ((PackRepositoryAccessor) this.getResourcePackRepository()).contenttweaker$sources()::add); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
fabric/src/main/java/com/blamejared/contenttweaker/fabric/mixin/PackRepositoryAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.blamejared.contenttweaker.fabric.mixin; | ||
|
||
import net.minecraft.server.packs.repository.PackRepository; | ||
import net.minecraft.server.packs.repository.RepositorySource; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
import java.util.Set; | ||
|
||
@Mixin(PackRepository.class) | ||
public interface PackRepositoryAccessor { | ||
@Accessor("sources") Set<RepositorySource> contenttweaker$sources(); | ||
} |
46 changes: 46 additions & 0 deletions
46
fabric/src/main/java/com/blamejared/contenttweaker/fabric/mixin/PackRepositoryMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.blamejared.contenttweaker.fabric.mixin; | ||
|
||
import com.blamejared.contenttweaker.fabric.resource.FabricResourceManager; | ||
import net.minecraft.server.packs.PackType; | ||
import net.minecraft.server.packs.repository.Pack; | ||
import net.minecraft.server.packs.repository.PackRepository; | ||
import net.minecraft.server.packs.repository.RepositorySource; | ||
import org.spongepowered.asm.mixin.Debug; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Mutable; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
@Mixin(value = PackRepository.class, priority = 1001) | ||
public abstract class PackRepositoryMixin { | ||
@Final | ||
@Mutable | ||
@Shadow | ||
private Set<RepositorySource> sources; | ||
|
||
@Inject( | ||
method = "<init>(Lnet/minecraft/server/packs/repository/Pack$PackConstructor;[Lnet/minecraft/server/packs/repository/RepositorySource;)V", | ||
at = @At(value = "RETURN") | ||
) | ||
private void contenttweaker$init$allowSourcesMutation(final Pack.PackConstructor packConstructor, final RepositorySource[] sources, final CallbackInfo info) { | ||
final Set<RepositorySource> previous = new HashSet<>(this.sources); | ||
this.sources = new LinkedHashSet<>(Arrays.asList(sources)); | ||
this.sources.addAll(previous); | ||
} | ||
|
||
@Inject( | ||
method = "<init>(Lnet/minecraft/server/packs/PackType;[Lnet/minecraft/server/packs/repository/RepositorySource;)V", | ||
at = @At(value = "RETURN") | ||
) | ||
private void contenttweaker$init$injectCustomRepositorySources(final PackType type, final RepositorySource[] sources, final CallbackInfo info) { | ||
FabricResourceManager.inject(type, this.sources::add); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ic/src/main/java/com/blamejared/contenttweaker/fabric/resource/FabricResourceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.blamejared.contenttweaker.fabric.resource; | ||
|
||
import com.blamejared.contenttweaker.core.resource.RuntimeRepositorySource; | ||
import com.blamejared.contenttweaker.core.resource.UserRepositorySource; | ||
import net.minecraft.server.packs.PackType; | ||
import net.minecraft.server.packs.repository.RepositorySource; | ||
|
||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
|
||
public final class FabricResourceManager { | ||
private FabricResourceManager() {} | ||
|
||
public static void inject(final PackType type, final Consumer<RepositorySource> sourceConsumer) { | ||
Stream.of(RuntimeRepositorySource.of(type), UserRepositorySource.of(type)).forEach(sourceConsumer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
forge/src/main/java/com/blamejared/contenttweaker/forge/mixin/PackRepositoryMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.blamejared.contenttweaker.forge.mixin; | ||
|
||
import net.minecraft.server.packs.repository.Pack; | ||
import net.minecraft.server.packs.repository.PackRepository; | ||
import net.minecraft.server.packs.repository.RepositorySource; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Mutable; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
@Mixin(PackRepository.class) | ||
public abstract class PackRepositoryMixin { | ||
@Final | ||
@Mutable | ||
@Shadow | ||
private Set<RepositorySource> sources; | ||
|
||
@Inject( | ||
method = "<init>(Lnet/minecraft/server/packs/repository/Pack$PackConstructor;[Lnet/minecraft/server/packs/repository/RepositorySource;)V", | ||
at = @At(value = "RETURN") | ||
) | ||
private void contenttweaker$init$keepAdditionOrdering(final Pack.PackConstructor packConstructor, final RepositorySource[] sources, final CallbackInfo info) { | ||
final Set<RepositorySource> previous = new LinkedHashSet<>(this.sources); | ||
this.sources = new LinkedHashSet<>(Arrays.asList(sources)); | ||
this.sources.addAll(previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters