Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Title #90

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions OptiGUI/src/main/java/opekope2/optigui/inter/IScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package opekope2.optigui.inter;

import net.minecraft.text.Text;

public interface IScreen {

void setTitle(Text title);
}
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ abstract class MinecraftClientMixin {
@Nullable
public Screen currentScreen;

@Shadow public abstract void setScreen(@Nullable Screen screen);

@Inject(method = "setScreen(Lnet/minecraft/client/gui/screen/Screen;)V", at = @At("TAIL"))
private void setScreenMixin(Screen screen, CallbackInfo ci) {
if (player != null && player.getEntityWorld() != null && currentScreen != null) {
15 changes: 14 additions & 1 deletion OptiGUI/src/main/java/opekope2/optigui/mixin/ScreenMixin.java
Original file line number Diff line number Diff line change
@@ -2,21 +2,29 @@

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import opekope2.optigui.inter.IScreen;
import opekope2.optigui.internal.InitializerKt;
import opekope2.optigui.registry.RetexturableScreenRegistry;
import opekope2.optigui.toast.InspectorToast;
import opekope2.optigui.util.InteractionUtil;
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.CallbackInfoReturnable;

@Mixin(Screen.class)
public abstract class ScreenMixin {
public class ScreenMixin implements IScreen {
@Shadow
protected MinecraftClient client;

@Mutable
@Shadow @Final
protected Text title;//TODO title

@Inject(method = "keyPressed", at = @At("TAIL"))
void handleKeyPress(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
Screen thiz = (Screen) (Object) this;
@@ -30,4 +38,9 @@ void handleKeyPress(int keyCode, int scanCode, int modifiers, CallbackInfoReturn
client.keyboard.setClipboard(inspection);
client.getToastManager().add(new InspectorToast());
}

@Override
public void setTitle(Text title) {//TODO title
this.title = title;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package opekope2.optigui.filter

import net.minecraft.text.TextContent
import opekope2.optigui.interaction.Interaction

/**
* A filter, which returns the first non-null evaluation from [filters].
* It returns `null`, if none of [filters] return a non-null value, or [filters] is empty.
@@ -20,7 +23,20 @@ open class FirstMatchFilter<TInput, TResult : Any>(private val filters: Collecti
override fun evaluate(input: TInput): TResult? {
for (filter in filters) {
val result = filter.evaluate(input)
if (result != null) return result
if (result != null) {
if (filter is PostProcessorFilter<*, *, *, *>) {//TODO title
val title = filter.getTitle()
if (input is Interaction) {
if (title.content != TextContent.EMPTY) {
input.screen.setTitle(title)
}
else if (!title.style.isEmpty) {
input.screen.setTitle(input.screen.title.copyContentOnly().setStyle(title.style))
}
}
}
return result
}
}
return null
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package opekope2.optigui.filter

import net.minecraft.text.Text

/**
* A post-processor filter, which changes the output of the given sub-filter.
*
@@ -13,8 +15,9 @@ package opekope2.optigui.filter
*
* @see PreProcessorFilter
*/
class PostProcessorFilter<TInput, TSubFilterResult : Any, TResult : Any>(
class PostProcessorFilter<TInput, TTitle : Text, TSubFilterResult : Any, TResult : Any>(
private val filter: IFilter<TInput, out TSubFilterResult>,
private val title: TTitle,//TODO title
private val transformDescription: String,
private val transform: (input: TInput, result: TSubFilterResult?) -> TResult?
) : IFilter<TInput, TResult>, Iterable<IFilter<TInput, out TSubFilterResult>> {
@@ -24,8 +27,9 @@ class PostProcessorFilter<TInput, TSubFilterResult : Any, TResult : Any>(
* @param filter The sub-filter to evaluate
* @param result The (constant) return value of the [transform] function
*/
constructor(filter: IFilter<TInput, out TSubFilterResult>, result: TResult) : this(
constructor(filter: IFilter<TInput, out TSubFilterResult>, title: TTitle, result: TResult) : this(
filter,
title,//TODO title
"Set result to `$result`",
{ _, subFilterResult -> result.takeIf { subFilterResult != null } }
)
@@ -35,4 +39,6 @@ class PostProcessorFilter<TInput, TSubFilterResult : Any, TResult : Any>(
override fun iterator() = iterator { yield(filter) }

override fun toString() = "${javaClass.name}, transform: $transformDescription"

fun getTitle(): Text = this.title//TODO title
}
Original file line number Diff line number Diff line change
@@ -80,6 +80,7 @@ internal object FilterLoader : IdentifiableResourceReloadListener, ClientModInit

rawFilterData to PostProcessorFilter(
ConjunctionFilter(selectorFilters),
rawFilterData.title,//TODO title
rawFilterData.replacementTexture
)
}
Original file line number Diff line number Diff line change
@@ -78,5 +78,5 @@ internal class VillagerTypeSelector : AbstractListSelector<Identifier>() {
)

override fun transformInteraction(interaction: Interaction) =
(interaction.data.entity as? VillagerEntity)?.villagerData?.type
Registries.VILLAGER_TYPE.getId((interaction.data.entity as? VillagerEntity)?.villagerData?.type)//TODO fix
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package opekope2.optigui.resource

import net.minecraft.text.Text
import net.minecraft.util.Identifier

/**
@@ -33,6 +34,11 @@ interface IRawFilterData {
*/
val replacementTexture: Identifier

/**
* The replacement title of the filter.//TODO title
*/
val title: Text

/**
* The raw selector data as key-value pairs.
*/
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package opekope2.optigui.resource

import net.minecraft.resource.ResourceManager
import net.minecraft.text.Text
import net.minecraft.util.DyeColor
import net.minecraft.util.Identifier
import opekope2.optigui.internal.util.delimiters
@@ -66,8 +67,9 @@ private fun createFilterData(
)
continue
}
yield(
OptiFineFilterData(resourcePath, null, resolvedReplacement, properties, false).apply {
yield(//TODO title
OptiFineFilterData(resourcePath, null, resolvedReplacement, Text.empty(),
properties, false).apply {
originalTexture = original
replaceableTextures = setOf(original)
}
@@ -156,7 +158,8 @@ private fun createFilterData(
}

"creative" -> {}
"inventory" -> yield(OptiFineFilterData(resource, null, replacementTexture, properties, false))
"inventory" -> yield(OptiFineFilterData(resource, null, replacementTexture, Text.empty(),
properties, false))//TODO title
}
}

@@ -179,6 +182,7 @@ private open class OptiFineFilterData(
override val resource: Identifier,
final override val container: Identifier?,
final override var replacementTexture: Identifier,
override val title: Text,//TODO title
protected val properties: Options,
private val filterName: Boolean
) : IRawFilterData {
@@ -191,7 +195,8 @@ private open class OptiFineFilterData(
replacementTexture: Identifier,
properties: Options,
filterName: Boolean
) : this(resource, Identifier(container), replacementTexture, properties, filterName)
) : this(resource, Identifier(container), replacementTexture, Text.empty(),
properties, filterName)

var originalTexture: Identifier? = container?.let(ContainerDefaultGuiTextureRegistry::get)

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package opekope2.optigui.resource

import com.google.gson.JsonSyntaxException
import net.minecraft.resource.ResourceManager
import net.minecraft.text.Style
import net.minecraft.text.Text
import net.minecraft.text.TextColor
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import opekope2.optigui.internal.util.assertNotEmpty
import opekope2.optigui.internal.util.delimiters
@@ -13,6 +18,7 @@ import org.ini4j.Profile
import org.slf4j.Logger
import org.slf4j.event.Level.ERROR
import org.slf4j.event.Level.WARN
import java.util.regex.Pattern

/**
* OptiGUI INI filter loader.
@@ -64,8 +70,60 @@ class OptiGuiFilterLoader : IFilterLoader {
}
section -= "load.priority"
}

var title = Text.empty()//TODO title
if ("title.json" in section) {
val titleJson = section["title.json"]!!
try {
title = Text.Serializer.fromJson(titleJson)
} catch (e: JsonSyntaxException) {
logger.eventBuilder(WARN, id, sectionName).log(
"Ignoring section [{}] in `{}`, because title json `{}` is malformed",
sectionName, id, titleJson
)
return@loadSection setOf()
}

if(titleJson.contains("\"text\":\"this_name\"")) { //if player want to use current container name
title = Text.empty().setStyle(title.style)
}

section -= "title.json"
section -= "title.name"
section -= "title.style"
} else {
if ("title.name" in section) {
val titleName = section["title.name"]!!
title = Text.translatable(titleName)
section -= "title.name"
}

if ("title.style" in section) {
val titleStyle = section["title.style"]!!
val styles = titleStyle.split(Pattern.compile(" "))
var style = Style.EMPTY
for (st in styles) {
val format = Formatting.byName(st.lowercase())
style = if (format != null) style.withFormatting(format)
else {
val color = TextColor.parse(st)
if (color != null) {
style.withColor(color)
} else {
logger.eventBuilder(WARN, id, sectionName).log(
"Ignoring section [{}] in `{}`, because title style `{}` is malformed in `{}`",
sectionName, id, titleStyle, st
)
return@loadSection setOf()
}
}
}
title = title.setStyle(style)
section -= "title.style"
}
}

containers.map { FilterData(priority, id, it, replacement, section) }
containers.map { FilterData(priority, id, it, replacement, title, section) }
}
}
}
@@ -75,6 +133,7 @@ class OptiGuiFilterLoader : IFilterLoader {
override val resource: Identifier,
override val container: Identifier?,
override val replacementTexture: Identifier,
override val title: Text,//TODO title
private val section: Profile.Section
) : IRawFilterData {
override var replaceableTextures: Set<Identifier> = (
3 changes: 3 additions & 0 deletions OptiGUI/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -75,6 +75,9 @@
"fabric-api(required)"
]
},
"loom:injected_interfaces": {
"net/minecraft/class_437": ["opekope2/optigui/inter/IScreen"]
},
"optigui:container_default_gui_textures": {
"minecraft:anvil": "minecraft:textures/gui/container/anvil.png",
"minecraft:chipped_anvil": "minecraft:textures/gui/container/anvil.png",
3 changes: 3 additions & 0 deletions OptiGUI/src/main/resources/optigui.accesswidener
Original file line number Diff line number Diff line change
@@ -32,3 +32,6 @@ accessible field net/minecraft/screen/HopperScreenHandler

# ShulkerBoxScreenHandler
accessible field net/minecraft/screen/ShulkerBoxScreenHandler inventory Lnet/minecraft/inventory/Inventory;

#Fork
accessible field net/minecraft/client/gui/screen/Screen title Lnet/minecraft/text/Text;