Skip to content

Commit

Permalink
add config option for loading menu asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
ToCraft committed Sep 25, 2024
1 parent 31fac6b commit a91ab50
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 71 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
remorphed 4.2
================

- add config option for loading menu asynchronously

remorphed 4.1
================

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tocraft.remorphed.config;

import tocraft.craftedcore.config.Config;
import tocraft.craftedcore.config.annotions.Comment;
import tocraft.craftedcore.config.annotions.Synchronize;
import tocraft.remorphed.Remorphed;

Expand All @@ -23,6 +24,8 @@ public class RemorphedConfig implements Config {
put("minecraft:wither", 2);
}
};
@Comment("Whether the entities that should be rendered can be loaded asynchronously")
public boolean loadMenuAsynchronous = true;

@Override
public String getName() {
Expand Down
142 changes: 74 additions & 68 deletions common/src/main/java/tocraft/remorphed/screen/RemorphedScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,65 @@ public void init() {
addRenderableWidget(specialShapeButton);
}

CompletableFuture.runAsync(() -> {
populateUnlockedRenderEntities(minecraft.player);
ShapeType<? extends LivingEntity> currentShape = ShapeType.from(PlayerShape.getCurrentShape(minecraft.player));
if (Remorphed.CONFIG.loadMenuAsynchronous) {
CompletableFuture.runAsync(this::instantiate);
} else {
instantiate();
}
}

private void instantiate() {
populateUnlockedRenderEntities(minecraft.player);
ShapeType<? extends LivingEntity> currentShape = ShapeType.from(PlayerShape.getCurrentShape(minecraft.player));

// handle favorites
unlockedShapes.sort((first, second) -> {
if (Objects.equals(first, currentShape)) {
return -1;
} else if (Objects.equals(second, currentShape)) {
return 1;
} else {
boolean firstIsFav = PlayerMorph.getFavoriteShapes(minecraft.player).contains(first);
boolean secondIsFav = PlayerMorph.getFavoriteShapes(minecraft.player).contains(second);
if (firstIsFav == secondIsFav) {
return 0;
}
else if (firstIsFav) {
return -1;
}
else {
return 1;
}
}
});

// filter unlocked
if (!Remorphed.displayVariantsInMenu) {
List<ShapeType<?>> newUnlocked = new ArrayList<>();
for (ShapeType<?> shapeType : unlockedShapes) {
if (shapeType.equals(currentShape) || !newUnlocked.stream().map(ShapeType::getEntityType).toList().contains(shapeType.getEntityType())) {
newUnlocked.add(shapeType);
}
}

unlockedShapes.clear();
unlockedShapes.addAll(newUnlocked);
}

// handle favorites
unlockedShapes.sort((first, second) -> {
if (Objects.equals(first, currentShape)) {
if (Remorphed.foundSkinShifter) {
populateUnlockedRenderPlayers(minecraft.player);
UUID currentSkin = SkinShifter.getCurrentSkin(minecraft.player);

unlockedSkins.sort((first, second) -> {
if (Objects.equals(first.id(), currentSkin) && currentShape != null) {
return -1;
} else if (Objects.equals(second, currentShape)) {
} else if (Objects.equals(second.id(), currentSkin) && currentShape != null) {
return 1;
} else {
boolean firstIsFav = PlayerMorph.getFavoriteShapes(minecraft.player).contains(first);
boolean secondIsFav = PlayerMorph.getFavoriteShapes(minecraft.player).contains(second);
boolean firstIsFav = PlayerMorph.getFavoriteSkinIds(minecraft.player).contains(first.id());
boolean secondIsFav = PlayerMorph.getFavoriteSkinIds(minecraft.player).contains(second.id());
if (firstIsFav == secondIsFav) {
return 0;
return first.name().compareTo(second.name());
}
else if (firstIsFav) {
return -1;
Expand All @@ -102,70 +146,32 @@ else if (firstIsFav) {
}
}
});
}

// filter unlocked
if (!Remorphed.displayVariantsInMenu) {
List<ShapeType<?>> newUnlocked = new ArrayList<>();
for (ShapeType<?> shapeType : unlockedShapes) {
if (shapeType.equals(currentShape) || !newUnlocked.stream().map(ShapeType::getEntityType).toList().contains(shapeType.getEntityType())) {
newUnlocked.add(shapeType);
}
}

unlockedShapes.clear();
unlockedShapes.addAll(newUnlocked);
}

if (Remorphed.foundSkinShifter) {
populateUnlockedRenderPlayers(minecraft.player);
UUID currentSkin = SkinShifter.getCurrentSkin(minecraft.player);

unlockedSkins.sort((first, second) -> {
if (Objects.equals(first.id(), currentSkin) && currentShape != null) {
return -1;
} else if (Objects.equals(second.id(), currentSkin) && currentShape != null) {
return 1;
} else {
boolean firstIsFav = PlayerMorph.getFavoriteSkinIds(minecraft.player).contains(first.id());
boolean secondIsFav = PlayerMorph.getFavoriteSkinIds(minecraft.player).contains(second.id());
if (firstIsFav == secondIsFav) {
return first.name().compareTo(second.name());
}
else if (firstIsFav) {
return -1;
}
else {
return 1;
}
}
});
}

populateShapeWidgets(unlockedShapes, unlockedSkins);
populateShapeWidgets(unlockedShapes, unlockedSkins);

// implement search handler
searchBar.setResponder(text -> {
setFocused(searchBar);
// implement search handler
searchBar.setResponder(text -> {
setFocused(searchBar);

// Only re-filter if the text contents changed
if (!lastSearchContents.equals(text)) {
((ScreenAccessor) this).getSelectables().removeIf(button -> button instanceof EntityWidget);
children().removeIf(button -> button instanceof EntityWidget);
// Only re-filter if the text contents changed
if (!lastSearchContents.equals(text)) {
((ScreenAccessor) this).getSelectables().removeIf(button -> button instanceof EntityWidget);
children().removeIf(button -> button instanceof EntityWidget);

List<ShapeType<?>> filteredShapes = unlockedShapes
.stream()
.filter(type -> text.isEmpty() || ShapeType.createTooltipText(renderEntities.get(type)).getString().toUpperCase().contains(text.toUpperCase()) || EntityType.getKey(type.getEntityType()).toString().toUpperCase().contains(text.toUpperCase()))
.toList();
List<PlayerProfile> filteredSkins = unlockedSkins
.stream()
.filter(skin -> text.isEmpty() || skin.name().toUpperCase().contains(text.toUpperCase()) || skin.id().toString().contains(text.toUpperCase()))
.toList();
List<ShapeType<?>> filteredShapes = unlockedShapes
.stream()
.filter(type -> text.isEmpty() || ShapeType.createTooltipText(renderEntities.get(type)).getString().toUpperCase().contains(text.toUpperCase()) || EntityType.getKey(type.getEntityType()).toString().toUpperCase().contains(text.toUpperCase()))
.toList();
List<PlayerProfile> filteredSkins = unlockedSkins
.stream()
.filter(skin -> text.isEmpty() || skin.name().toUpperCase().contains(text.toUpperCase()) || skin.id().toString().contains(text.toUpperCase()))
.toList();

populateShapeWidgets(filteredShapes, filteredSkins);
}
populateShapeWidgets(filteredShapes, filteredSkins);
}

lastSearchContents = text;
});
lastSearchContents = text;
});
}

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ org.gradle.jvmargs=-Xmx4G
# Base Versions
archives_base_name=remorphed
artifact_type=release
mod_version=4.1
mod_version=4.2
maven_group=dev.tocraft
# Loader Versions
fabric_loader=0.15.11
# Dependency Versions
craftedcore_version=5.3
woodwalkers_version=5
craftedcore_version=5.5
woodwalkers_version=5.2
skinshifter_version=1.1
# Upload data
curseforge_id=950721
Expand Down

0 comments on commit a91ab50

Please sign in to comment.