Skip to content

Commit

Permalink
MidnightLib 1.1.0 - Port to 1.19.3
Browse files Browse the repository at this point in the history
- Port to 1.19.3 (temporarily disabled forge and quilt until they're available)
- Removed custom button tooltip system in favor of the new vanilla one
  • Loading branch information
Motschen committed Dec 3, 2022
1 parent c751859 commit eaa2b15
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 78 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ subprojects {
// The following line declares the mojmap mappings, you may use other mappings as well
//mappings loom.officialMojangMappings()
// The following line declares the yarn mappings you may select this one as well.
mappings "net.fabricmc:yarn:1.19.2+build.3:v2"
mappings "net.fabricmc:yarn:1.19.3-rc1+build.2:v2"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eu.midnightdust.core.screen;

import eu.midnightdust.core.MidnightLibClient;
import eu.midnightdust.core.config.MidnightLibConfig;
import eu.midnightdust.lib.config.MidnightConfig;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -28,7 +27,7 @@ public MidnightConfigOverviewScreen(Screen parent) {

@Override
protected void init() {
this.addDrawableChild(new ButtonWidget(this.width / 2 - 100, this.height - 28, 200, 20, ScreenTexts.DONE, (button) -> Objects.requireNonNull(client).setScreen(parent)));
this.addDrawableChild(ButtonWidget.builder(ScreenTexts.DONE, (button) -> Objects.requireNonNull(client).setScreen(parent)).dimensions(this.width / 2 - 100, this.height - 28, 200, 20).build());

this.list = new MidnightOverviewListWidget(this.client, this.width, this.height, 32, this.height - 32, 25);
if (this.client != null && this.client.world != null) this.list.setRenderBackground(false);
Expand All @@ -37,8 +36,8 @@ protected void init() {
Collections.sort(sortedMods);
sortedMods.forEach((modid) -> {
if (!MidnightLibClient.hiddenMods.contains(modid)) {
list.addButton(new ButtonWidget(this.width / 2 - 100, this.height - 28, 200, 20, Text.translatable(modid +".midnightconfig.title"), (button) ->
Objects.requireNonNull(client).setScreen(MidnightConfig.getScreen(this,modid))));
list.addButton(ButtonWidget.builder(Text.translatable(modid +".midnightconfig.title"), (button) ->
Objects.requireNonNull(client).setScreen(MidnightConfig.getScreen(this,modid))).dimensions(this.width / 2 - 100, this.height - 28, 200, 20).build());
}
});
super.init();
Expand Down Expand Up @@ -78,7 +77,7 @@ private OverviewButtonEntry(ClickableWidget button) {
}
public static OverviewButtonEntry create(ClickableWidget button) {return new OverviewButtonEntry(button);}
public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
button.y = y;
button.setY(y);
button.render(matrices, mouseX, mouseY, tickDelta);
}
public List<? extends Element> children() {return buttonList;}
Expand Down
49 changes: 18 additions & 31 deletions common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.*;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.math.MatrixStack;
Expand All @@ -37,7 +38,7 @@
import java.util.function.Predicate;
import java.util.regex.Pattern;

/** MidnightConfig v2.3.0 by TeamMidnightDust & Motschen
/** MidnightConfig v2.4.0 by TeamMidnightDust & Motschen
* Single class config library - feel free to copy!
* Based on https://github.com/Minenash/TinyConfig
Expand Down Expand Up @@ -230,17 +231,20 @@ public void loadValues() {
} catch (IllegalAccessException ignored) {}
}
}
public Tooltip getTooltip(EntryInfo info) {
return Tooltip.of(I18n.hasTranslation(translationPrefix+info.field.getName()+".tooltip") ? Text.translatable(translationPrefix+info.field.getName()+".tooltip") : Text.empty());
}
@Override
public void init() {
super.init();
if (!reload) loadValues();

this.addDrawableChild(new ButtonWidget(this.width / 2 - 154, this.height - 28, 150, 20, ScreenTexts.CANCEL, button -> {
this.addDrawableChild(ButtonWidget.builder(ScreenTexts.CANCEL, button -> {
loadValues();
Objects.requireNonNull(client).setScreen(parent);
}));
}).dimensions(this.width / 2 - 154, this.height - 28, 150, 20).build());

ButtonWidget done = this.addDrawableChild(new ButtonWidget(this.width / 2 + 4, this.height - 28, 150, 20, ScreenTexts.DONE, (button) -> {
ButtonWidget done = this.addDrawableChild(ButtonWidget.builder(ScreenTexts.DONE, (button) -> {
for (EntryInfo info : entries)
if (info.id.equals(modid)) {
try {
Expand All @@ -249,28 +253,28 @@ public void init() {
}
write(modid);
Objects.requireNonNull(client).setScreen(parent);
}));
}).dimensions(this.width / 2 + 4, this.height - 28, 150, 20).build());

this.list = new MidnightConfigListWidget(this.client, this.width, this.height, 32, this.height - 32, 25);
if (this.client != null && this.client.world != null) this.list.setRenderBackground(false);
this.addSelectableChild(this.list);
for (EntryInfo info : entries) {
if (info.id.equals(modid)) {
Text name = Objects.requireNonNullElseGet(info.name, () -> Text.translatable(translationPrefix + info.field.getName()));
ButtonWidget resetButton = new ButtonWidget(width - 205, 0, 40, 20, Text.literal("Reset").formatted(Formatting.RED), (button -> {
ButtonWidget resetButton = ButtonWidget.builder(Text.literal("Reset").formatted(Formatting.RED), (button -> {
info.value = info.defaultValue;
info.tempValue = info.defaultValue.toString();
info.index = 0;
double scrollAmount = list.getScrollAmount();
this.reload = true;
Objects.requireNonNull(client).setScreen(this);
list.setScrollAmount(scrollAmount);
}));
})).dimensions(width - 205, 0, 40, 20).build();

if (info.widget instanceof Map.Entry) {
Map.Entry<ButtonWidget.PressAction, Function<Object, Text>> widget = (Map.Entry<ButtonWidget.PressAction, Function<Object, Text>>) info.widget;
if (info.field.getType().isEnum()) widget.setValue(value -> Text.translatable(translationPrefix + "enum." + info.field.getType().getSimpleName() + "." + info.value.toString()));
this.list.addButton(List.of(new ButtonWidget(width - 160, 0,150, 20, widget.getValue().apply(info.value), widget.getKey()),resetButton), name, info);
this.list.addButton(List.of(ButtonWidget.builder(widget.getValue().apply(info.value), widget.getKey()).dimensions(width - 160, 0,150, 20).tooltip(getTooltip(info)).build(),resetButton), name, info);
} else if (info.field.getType() == List.class) {
if (!reload) info.index = 0;
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, Text.empty());
Expand All @@ -280,15 +284,16 @@ public void init() {
widget.setTextPredicate(processor);
resetButton.setWidth(20);
resetButton.setMessage(Text.literal("R").formatted(Formatting.RED));
ButtonWidget cycleButton = new ButtonWidget(width - 185, 0, 20, 20, Text.literal(String.valueOf(info.index)).formatted(Formatting.GOLD), (button -> {
ButtonWidget cycleButton = ButtonWidget.builder(Text.literal(String.valueOf(info.index)).formatted(Formatting.GOLD), (button -> {
((List<String>)info.value).remove("");
double scrollAmount = list.getScrollAmount();
this.reload = true;
info.index = info.index + 1;
if (info.index > ((List<String>)info.value).size()) info.index = 0;
Objects.requireNonNull(client).setScreen(this);
list.setScrollAmount(scrollAmount);
}));
})).dimensions(width - 185, 0, 20, 20).build();
widget.setTooltip(getTooltip(info));
this.list.addButton(List.of(widget, resetButton, cycleButton), name, info);
} else if (info.widget != null) {
ClickableWidget widget;
Expand All @@ -301,10 +306,11 @@ public void init() {
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.widget).apply(textField, done);
textField.setTextPredicate(processor);
}
widget.setTooltip(getTooltip(info));
if (e.isColor()) {
resetButton.setWidth(20);
resetButton.setMessage(Text.literal("R").formatted(Formatting.RED));
ButtonWidget colorButton = new ButtonWidget(width - 185, 0, 20, 20, Text.literal("⬛"), (button -> {}));
ButtonWidget colorButton = ButtonWidget.builder(Text.literal("⬛"), (button -> {})).dimensions(width - 185, 0, 20, 20).build();
try {colorButton.setMessage(Text.literal("⬛").setStyle(Style.EMPTY.withColor(Color.decode(info.tempValue).getRGB())));} catch (Exception ignored) {}
info.colorButton = colorButton;
colorButton.active = false;
Expand All @@ -324,25 +330,6 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
this.renderBackground(matrices);
this.list.render(matrices, mouseX, mouseY, delta);
drawCenteredText(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF);

for (EntryInfo info : entries) {
if (info.id.equals(modid)) {
if (list.getHoveredButton(mouseX,mouseY).isPresent()) {
ClickableWidget buttonWidget = list.getHoveredButton(mouseX,mouseY).get();
Text text = ButtonEntry.buttonsWithText.get(buttonWidget);
Text name = Text.translatable(this.translationPrefix + info.field.getName());
String key = translationPrefix + info.field.getName() + ".tooltip";

if (info.error != null && text.equals(name)) renderTooltip(matrices, info.error.getValue(), mouseX, mouseY);
else if (I18n.hasTranslation(key) && text.equals(name)) {
List<Text> list = new ArrayList<>();
for (String str : I18n.translate(key).split("\n"))
list.add(Text.literal(str));
renderTooltip(matrices, list, mouseX, mouseY);
}
}
}
}
super.render(matrices,mouseX,mouseY,delta);
}
}
Expand Down Expand Up @@ -388,7 +375,7 @@ private ButtonEntry(List<ClickableWidget> buttons, Text text, EntryInfo info) {
children.addAll(buttons);
}
public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
buttons.forEach(b -> { b.y = y; b.render(matrices, mouseX, mouseY, tickDelta); });
buttons.forEach(b -> { b.setY(y); b.render(matrices, mouseX, mouseY, tickDelta); });
if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) {
if (info.centered) textRenderer.drawWithShadow(matrices, text, MinecraftClient.getInstance().getWindow().getScaledWidth() / 2f - (textRenderer.getWidth(text) / 2f), y + 5, 0xFFFFFF);
else DrawableHelper.drawTextWithShadow(matrices, textRenderer, text, 12, y + 5, 0xFFFFFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
import net.minecraft.util.Identifier;

public class TexturedOverlayButtonWidget extends TexturedButtonWidget {

public TexturedOverlayButtonWidget(int x, int y, int width, int height, int u, int v, Identifier texture, PressAction pressAction) {
super(x, y, width, height, u, v, texture, pressAction);
}

public TexturedOverlayButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, Identifier texture, PressAction pressAction) {
super(x, y, width, height, u, v, hoveredVOffset, texture, pressAction);
}

public TexturedOverlayButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, Identifier texture, int textureWidth, int textureHeight, PressAction pressAction) {
super(x, y, width, height, u, v, hoveredVOffset, texture, textureWidth, textureHeight, pressAction);
}
Expand All @@ -19,21 +25,17 @@ public TexturedOverlayButtonWidget(int x, int y, int width, int height, int u, i
super(x, y, width, height, u, v, hoveredVOffset, texture, textureWidth, textureHeight, pressAction, text);
}

public TexturedOverlayButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, Identifier texture, int textureWidth, int textureHeight, PressAction pressAction, TooltipSupplier tooltipSupplier, Text text) {
super(x,y,width,height, u,v,hoveredVOffset,texture,textureWidth,textureHeight,pressAction,tooltipSupplier,text);
}

@Override
public void renderButton(MatrixStack matrices, int mouseX, int mouseY, float delta) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShader(GameRenderer::getPositionTexProgram);
RenderSystem.setShaderTexture(0, WIDGETS_TEXTURE);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, this.alpha);
int i = this.getYImage(this.isHovered());
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
this.drawTexture(matrices, this.x, this.y, 0, 46 + i * 20, this.width / 2, this.height);
this.drawTexture(matrices, this.x + this.width / 2, this.y, 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);
this.drawTexture(matrices, this.getX(), this.getY(), 0, 46 + i * 20, this.width / 2, this.height);
this.drawTexture(matrices, this.getX() + this.width / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);

super.renderButton(matrices, mouseX, mouseY, delta);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package eu.midnightdust.forge;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import eu.midnightdust.hats.witch.WitchHatFeatureRenderer;
import eu.midnightdust.lib.config.AutoCommand;
import eu.midnightdust.lib.config.MidnightConfig;
import eu.midnightdust.lib.util.MidnightColorUtil;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent;

@Mod.EventBusSubscriber(modid = "midnightlib", bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class MidnightLibClientEvents {
Expand All @@ -22,4 +22,16 @@ public static void registerLayerDefinition(EntityRenderersEvent.RegisterLayerDef
public void registerClientTick(TickEvent.ClientTickEvent event) {
MidnightColorUtil.tick();
}
@SubscribeEvent
public void onPostInit(FMLLoadCompleteEvent event) {
ModList.get().applyForEachModContainer(modContainer -> {
System.out.println(modContainer.getModId() + " yes");
if (MidnightConfig.configClass.containsKey(modContainer.getModId())) {

modContainer.registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class, () ->
new ConfigScreenHandler.ConfigScreenFactory((client, parent) -> MidnightConfig.getScreen(parent, modContainer.getModId())));
}
return true;
});
}
}
19 changes: 2 additions & 17 deletions forge/src/main/java/eu/midnightdust/forge/MidnightLibForge.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,22 @@

import eu.midnightdust.core.MidnightLibClient;
import eu.midnightdust.core.MidnightLibServer;
import eu.midnightdust.lib.config.MidnightConfig;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.IExtensionPoint;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.NetworkConstants;

@Mod("midnightlib")
public class MidnightLibForge {
public MidnightLibForge() {
// Submit our event bus to let architectury register our content on the right time
//EventBuses.registerModEventBus("midnightlib", FMLJavaModLoadingContext.get().getModEventBus());
ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> NetworkConstants.IGNORESERVERONLY, (remote, server) -> true));
DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> MidnightLibClient::onInitializeClient);
DistExecutor.safeRunWhenOn(Dist.DEDICATED_SERVER, () -> MidnightLibServer::onInitializeServer);
ModList.get().applyForEachModContainer(modContainer -> {
if (MidnightConfig.configClass.containsKey(modContainer.getModId())) {
ModLoadingContext.get().registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class, () ->
new ConfigScreenHandler.ConfigScreenFactory((client, parent) -> MidnightConfig.getScreen(parent, modContainer.getModId())));
}
System.out.println(modContainer.getModId());
System.out.println("motschen");
return true;
}
);
ModLoadingContext.get().registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class, () ->
new ConfigScreenHandler.ConfigScreenFactory((client, parent) -> MidnightConfig.getScreen(parent, "midnightlib")));
//ModLoadingContext.get().registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class, () ->
// new ConfigScreenHandler.ConfigScreenFactory((client, parent) -> MidnightConfig.getScreen(parent, "midnightlib")));
MinecraftForge.EVENT_BUS.register(new MidnightLibClientEvents());
MinecraftForge.EVENT_BUS.register(new MidnightLibServerEvents());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package eu.midnightdust.forge;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import eu.midnightdust.hats.witch.WitchHatFeatureRenderer;
import eu.midnightdust.lib.config.AutoCommand;
import eu.midnightdust.lib.util.MidnightColorUtil;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

Expand Down
Loading

0 comments on commit eaa2b15

Please sign in to comment.