generated from TropheusJ/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AddPackFindersEvent deprecate AddPackFindersCallback
- Loading branch information
Showing
9 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
...in/java/io/github/fabricators_of_create/porting_lib/event/common/AddPackFindersEvent.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,52 @@ | ||
package io.github.fabricators_of_create.porting_lib.event.common; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import io.github.fabricators_of_create.porting_lib.core.event.BaseEvent; | ||
import net.fabricmc.fabric.api.event.Event; | ||
import net.fabricmc.fabric.api.event.EventFactory; | ||
import net.minecraft.server.packs.PackType; | ||
import net.minecraft.server.packs.repository.PackRepository; | ||
import net.minecraft.server.packs.repository.RepositorySource; | ||
|
||
/** | ||
* Fired on {@link PackRepository} creation to allow mods to add new pack finders. | ||
*/ | ||
public class AddPackFindersEvent extends BaseEvent { | ||
public static final Event<Callback> EVENT = EventFactory.createArrayBacked(Callback.class, callbacks -> event -> { | ||
for (Callback c : callbacks) | ||
c.findPacks(event); | ||
}); | ||
private final PackType packType; | ||
private final Consumer<RepositorySource> sources; | ||
|
||
public AddPackFindersEvent(PackType packType, Consumer<RepositorySource> sources) { | ||
this.packType = packType; | ||
this.sources = sources; | ||
} | ||
|
||
/** | ||
* Adds a new source to the list of pack finders. | ||
* | ||
* @param source the pack finder | ||
*/ | ||
public void addRepositorySource(RepositorySource source) { | ||
sources.accept(source); | ||
} | ||
|
||
/** | ||
* @return the {@link PackType} of the pack repository being constructed. | ||
*/ | ||
public PackType getPackType() { | ||
return packType; | ||
} | ||
|
||
@Override | ||
public void sendEvent() { | ||
EVENT.invoker().findPacks(this); | ||
} | ||
|
||
public interface Callback { | ||
void findPacks(AddPackFindersEvent event); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
.../java/io/github/fabricators_of_create/porting_lib/extensions/PackRepositoryExtension.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,9 @@ | ||
package io.github.fabricators_of_create.porting_lib.extensions; | ||
|
||
import net.minecraft.server.packs.repository.RepositorySource; | ||
|
||
public interface PackRepositoryExtension { | ||
default void pl$addPackFinder(RepositorySource packFinder) { | ||
throw new RuntimeException("PackRepository implementation does not support adding sources!"); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...java/io/github/fabricators_of_create/porting_lib/mixin/client/CreateWorldScreenMixin.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,24 @@ | ||
package io.github.fabricators_of_create.porting_lib.mixin.client; | ||
|
||
import com.llamalad7.mixinextras.sugar.Local; | ||
|
||
import io.github.fabricators_of_create.porting_lib.event.common.AddPackFindersEvent; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.gui.screens.Screen; | ||
import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen; | ||
|
||
import net.minecraft.server.packs.PackType; | ||
import net.minecraft.server.packs.repository.PackRepository; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(CreateWorldScreen.class) | ||
public class CreateWorldScreenMixin { | ||
@Inject(method = "openFresh", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/worldselection/CreateWorldScreen;createDefaultLoadConfig(Lnet/minecraft/server/packs/repository/PackRepository;Lnet/minecraft/world/level/WorldDataConfiguration;)Lnet/minecraft/server/WorldLoader$InitConfig;")) | ||
private static void addPacks(Minecraft minecraft, Screen screen, CallbackInfo ci, @Local(index = 2) PackRepository repository) { | ||
new AddPackFindersEvent(PackType.SERVER_DATA, repository::pl$addPackFinder).sendEvent(); | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
...java/io/github/fabricators_of_create/porting_lib/mixin/common/ServerPacksSourceMixin.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,21 @@ | ||
package io.github.fabricators_of_create.porting_lib.mixin.common; | ||
|
||
import io.github.fabricators_of_create.porting_lib.event.common.AddPackFindersEvent; | ||
import net.minecraft.server.packs.PackType; | ||
import net.minecraft.server.packs.repository.PackRepository; | ||
import net.minecraft.server.packs.repository.ServerPacksSource; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.nio.file.Path; | ||
|
||
@Mixin(ServerPacksSource.class) | ||
public class ServerPacksSourceMixin { | ||
@Inject(method = "createPackRepository(Ljava/nio/file/Path;)Lnet/minecraft/server/packs/repository/PackRepository;", at = @At("RETURN")) | ||
private static void firePackFinders(Path path, CallbackInfoReturnable<PackRepository> cir) { | ||
new AddPackFindersEvent(PackType.SERVER_DATA, cir.getReturnValue()::pl$addPackFinder).sendEvent(); | ||
} | ||
} |
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