Skip to content

Commit

Permalink
Reverted platform mod name setting code
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 18, 2023
1 parent 6e7a840 commit 1be5ab8
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public void registerTypeWrappers(ScriptType type, TypeWrappers typeWrappers) {
typeWrappers.registerSimple(EntitySelector.class, UtilsJS::entitySelector);
typeWrappers.registerSimple(ReplacementMatch.class, ReplacementMatch::of);
typeWrappers.registerSimple(Stat.class, PlayerStatsJS::statOf);
typeWrappers.registerSimple(NotificationBuilder.class, NotificationBuilder::of);
typeWrappers.register(NotificationBuilder.class, NotificationBuilder::of);

// components //
typeWrappers.registerSimple(Component.class, TextWrapper::of);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public Toast.Visibility render(PoseStack poseStack, ToastComponent toastComponen
for (var i = 0; i < text.size(); i++) {
var line = text.get(i);

if (notification.titleShadow) {
if (notification.textShadow) {
mc.font.drawShadow(poseStack, line, th, tv + i * 10, 0xFFFFFF);
} else {
mc.font.draw(poseStack, line, th, tv + i * 10, 0xFFFFFF);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.mods.kubejs.core.mixin.common.mod;

import dev.latvian.mods.kubejs.util.CustomModNames;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -12,10 +12,10 @@
public abstract class ModNameTooltipMixin {
@Inject(method = "getModName(Ljava/lang/String;)Ljava/lang/String;", at = @At("HEAD"), cancellable = true, remap = false)
private static void kjs$modId(String modId, CallbackInfoReturnable<String> cir) {
var r = CustomModNames.get(modId);
var r = PlatformWrapper.getMods().get(modId);

if (!r.isEmpty()) {
cir.setReturnValue(r);
if (r != null && !r.getCustomName().isEmpty()) {
cir.setReturnValue(r.getCustomName());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.mods.kubejs.core.mixin.common.mod;

import dev.latvian.mods.kubejs.util.CustomModNames;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -12,10 +12,10 @@
public abstract class REITooltipMixin {
@Inject(method = "getModFromModId(Ljava/lang/String;)Ljava/lang/String;", at = @At("HEAD"), cancellable = true, remap = false)
private void kjs$modId(String modId, CallbackInfoReturnable<String> cir) {
var r = CustomModNames.get(modId);
var r = PlatformWrapper.getMods().get(modId);

if (!r.isEmpty()) {
cir.setReturnValue(r);
if (r != null && !r.getCustomName().isEmpty()) {
cir.setReturnValue(r.getCustomName());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.platform;

import dev.latvian.mods.kubejs.script.PlatformWrapper;
import dev.latvian.mods.kubejs.util.Lazy;
import net.minecraft.world.entity.MobCategory;

Expand All @@ -10,6 +11,8 @@ static MiscPlatformHelper get() {
return INSTANCE.get();
}

void setModName(PlatformWrapper.ModInfo info, String name);

MobCategory getMobCategory(String name);

boolean isDataGen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import dev.architectury.platform.Platform;
import dev.architectury.utils.Env;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.kubejs.util.CustomModNames;
import net.minecraft.SharedConstants;
import dev.latvian.mods.kubejs.platform.MiscPlatformHelper;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

Expand All @@ -18,11 +15,13 @@ public static class ModInfo {
private final String id;
private String name;
private String version;
private String customName;

public ModInfo(String i) {
id = i;
name = id;
version = "0.0.0";
customName = "";
}

public String getId() {
Expand All @@ -33,28 +32,23 @@ public String getName() {
return name;
}

public void setName(String ignored) {
ConsoleJS.STARTUP.error("Setting mod name has moved to Platform.setModName('mod_id', 'Mod Name')!");
public void setName(String n) {
name = n;
customName = n;
MiscPlatformHelper.get().setModName(this, name);
}

public String getVersion() {
return version;
}
}

private static final Set<String> MOD_LIST = new LinkedHashSet<>();
private static final Map<String, ModInfo> MOD_MAP = new LinkedHashMap<>();

static {
for (var mod : Platform.getMods()) {
var info = new ModInfo(mod.getModId());
info.name = mod.getName();
info.version = mod.getVersion();
MOD_LIST.add(info.id);
MOD_MAP.put(info.id, info);
public String getCustomName() {
return customName;
}
}

private static Map<String, ModInfo> allMods;

public static String getName() {
return ArchitecturyTarget.getCurrentTarget();
}
Expand All @@ -68,27 +62,38 @@ public static boolean isFabric() {
}

public static String getMcVersion() {
return SharedConstants.getCurrentVersion().getName();
return KubeJS.MC_VERSION_STRING;
}

public static Set<String> getList() {
return MOD_LIST;
return getMods().keySet();
}

public static String getModVersion() {
return getInfo(KubeJS.MOD_ID).version;
return KubeJS.thisMod.getVersion();
}

public static boolean isLoaded(String modId) {
return MOD_MAP.containsKey(modId);
return getMods().containsKey(modId);
}

public static ModInfo getInfo(String modID) {
return MOD_MAP.computeIfAbsent(modID, ModInfo::new);
return getMods().computeIfAbsent(modID, ModInfo::new);
}

public static Map<String, ModInfo> getMods() {
return MOD_MAP;
if (allMods == null) {
allMods = new LinkedHashMap<>();

for (var mod : Platform.getMods()) {
var info = new ModInfo(mod.getModId());
info.name = mod.getName();
info.version = mod.getVersion();
allMods.put(info.id, info);
}
}

return allMods;
}

public static boolean isDevelopmentEnvironment() {
Expand All @@ -100,7 +105,7 @@ public static boolean isClientEnvironment() {
}

public static void setModName(String modId, String name) {
CustomModNames.set(modId, name);
getInfo(modId).setName(name);
}

public static int getMinecraftVersion() {
Expand All @@ -110,4 +115,8 @@ public static int getMinecraftVersion() {
public static String getMinecraftVersionString() {
return KubeJS.MC_VERSION_STRING;
}

public static boolean isGeneratingData() {
return MiscPlatformHelper.get().isDataGen();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import dev.latvian.mods.kubejs.bindings.TextWrapper;
import dev.latvian.mods.kubejs.client.NotificationToast;
import dev.latvian.mods.rhino.BaseFunction;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.NativeJavaObject;
import dev.latvian.mods.rhino.mod.util.color.Color;
import dev.latvian.mods.rhino.mod.util.color.SimpleColor;
import net.fabricmc.api.EnvType;
Expand All @@ -12,6 +15,7 @@
import net.minecraft.world.item.ItemStack;

import java.time.Duration;
import java.util.Map;
import java.util.function.Consumer;

public class NotificationBuilder {
Expand All @@ -20,9 +24,20 @@ public class NotificationBuilder {
public static final Color DEFAULT_BORDER_COLOR = new SimpleColor(0x472954);
public static final Color DEFAULT_BACKGROUND_COLOR = new SimpleColor(0x241335);

public static NotificationBuilder of(Object object) {
private static final int FLAG_ICON = 1;
private static final int FLAG_TEXT_SHADOW = 2;
private static final int FLAG_DURATION = 4;

public static NotificationBuilder of(Context cx, Object object) {
if (object instanceof NotificationBuilder b) {
return b;
} else if (object instanceof Map<?, ?> map) {
return null; // FIXME
} else if (object instanceof BaseFunction func) {
Consumer consumer = (Consumer) NativeJavaObject.createInterfaceAdapter(cx, Consumer.class, func);
var b = new NotificationBuilder();
consumer.accept(b);
return b;
} else {
var b = new NotificationBuilder();
b.text = TextWrapper.of(object);
Expand All @@ -44,7 +59,7 @@ public static NotificationBuilder make(Consumer<NotificationBuilder> consumer) {
public Color outlineColor;
public Color borderColor;
public Color backgroundColor;
public boolean titleShadow;
public boolean textShadow;

public NotificationBuilder() {
duration = DEFAULT_DURATION;
Expand All @@ -55,16 +70,16 @@ public NotificationBuilder() {
outlineColor = SimpleColor.BLACK;
borderColor = DEFAULT_BORDER_COLOR;
backgroundColor = DEFAULT_BACKGROUND_COLOR;
titleShadow = true;
textShadow = true;
}

public NotificationBuilder(FriendlyByteBuf buf) {
int flags = buf.readVarInt();
text = buf.readComponent();

duration = ((flags & 4) != 0) ? Duration.ofMillis(buf.readVarLong()) : DEFAULT_DURATION;
duration = ((flags & FLAG_DURATION) != 0) ? Duration.ofMillis(buf.readVarLong()) : DEFAULT_DURATION;

if ((flags & 1) != 0) {
if ((flags & FLAG_ICON) != 0) {
iconType = buf.readVarInt();
icon = buf.readUtf();
iconSize = buf.readByte();
Expand All @@ -77,22 +92,22 @@ public NotificationBuilder(FriendlyByteBuf buf) {
outlineColor = UtilsJS.readColor(buf);
borderColor = UtilsJS.readColor(buf);
backgroundColor = UtilsJS.readColor(buf);
titleShadow = (flags & 2) != 0;
textShadow = (flags & FLAG_TEXT_SHADOW) != 0;
}

public void write(FriendlyByteBuf buf) {
int flags = 0;

if (iconType != 0) {
flags |= 1;
flags |= FLAG_ICON;
}

if (titleShadow) {
flags |= 2;
if (textShadow) {
flags |= FLAG_TEXT_SHADOW;
}

if (duration != DEFAULT_DURATION) {
flags |= 4;
flags |= FLAG_DURATION;
}

buf.writeVarInt(flags);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
package dev.latvian.mods.kubejs.platform.fabric;

import dev.latvian.mods.kubejs.platform.MiscPlatformHelper;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.entity.MobCategory;

@SuppressWarnings("UnstableApiUsage")
public class MiscFabricHelper implements MiscPlatformHelper {
private Boolean dataGen;

@Override
public void setModName(PlatformWrapper.ModInfo info, String name) {
try {
var mc = FabricLoader.getInstance().getModContainer(info.getId());

if (mc.isPresent()) {
var meta = mc.get().getMetadata();
var field = meta.getClass().getDeclaredField("name");
field.setAccessible(true);
field.set(meta, name);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
@SuppressWarnings("deprecation")
public MobCategory getMobCategory(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
package dev.latvian.mods.kubejs.platform.forge;

import dev.latvian.mods.kubejs.platform.MiscPlatformHelper;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import net.minecraft.world.entity.MobCategory;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.ModLoader;
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;

public class MiscForgeHelper implements MiscPlatformHelper {
@Override
public void setModName(PlatformWrapper.ModInfo info, String name) {
try {
var mc = ModList.get().getModContainerById(info.getId());

if (mc.isPresent() && mc.get().getModInfo() instanceof ModInfo i) {
var field = ModInfo.class.getDeclaredField("displayName");
field.setAccessible(true);
field.set(i, name);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
public MobCategory getMobCategory(String name) {
return MobCategory.byName(name);
Expand Down

0 comments on commit 1be5ab8

Please sign in to comment.