Basic port to fabric - no configs yet

This commit is contained in:
tterrag
2018-12-11 10:54:26 -05:00
parent 41d4823245
commit a75f9f8570
14 changed files with 228 additions and 270 deletions

View File

@@ -1,16 +1,17 @@
buildscript {
repositories {
mavenCentral()
maven { url = "http://files.minecraftforge.net/maven" }
maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies { classpath 'net.minecraftforge.gradle:ForgeGradle:2.2-SNAPSHOT' }
repositories {
jcenter()
maven { url = 'http://maven.modmuss50.me' }
}
dependencies {
classpath 'net.fabricmc:fabric-loom:0.1.0-SNAPSHOT'
}
}
plugins {
id 'com.matthewprenger.cursegradle' version '1.0.9'
}
apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: 'fabric-loom'
boolean dev = System.getenv('RELEASE') == null || System.getenv('RELEASE').equals('false');
ext.buildnumber = System.getenv().BUILD_NUMBER ?: 'custom';
@@ -23,39 +24,17 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
minecraft {
version = "${minecraft_version}-${forge_version}"
mappings = 'stable_29'
runDir = 'run'
clientJvmArgs += '-Dfml.coreMods.load=com.tterrag.blur.BlurPlugin'
replace "@VERSION@", project.version
}
processResources {
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
dependencies {
minecraft "com.mojang:minecraft:18w49a"
mappings "net.fabricmc:yarn:18w49a.10"
modCompile "net.fabricmc:fabric-loader:0.2.0.62"
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include '**/*.info'
include '**/*.properties'
// replace version and mcversion
expand 'version': project.version, 'mcversion': project.minecraft.version
}
// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude '**/*.info'
exclude '**/*.properties'
}
}
jar.manifest {
attributes 'FMLCorePlugin': 'com.tterrag.blur.BlurPlugin'
attributes 'FMLCorePluginContainsFMLMod': 'true'
// Fabric API. This is technically optional, but you probably want it anyway.
modCompile "net.fabricmc:fabric:0.1.0.37"
implementation 'com.google.code.findbugs:jsr305:3.0.2'
}
import org.apache.tools.ant.filters.ReplaceTokens

View File

@@ -1,6 +1,6 @@
minecraft_version=1.10.2
forge_version=12.18.3.2297
mod_version=1.0.4
mod_version=1.0.4-fabric
project_id=268324
release_type=release

View File

@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip

View File

@@ -1,81 +1,85 @@
package com.tterrag.blur;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.ArrayUtils;
import com.google.common.base.Throwables;
import com.tterrag.blur.mixin.MixinWorldRenderer;
import com.tterrag.blur.util.ShaderResourcePack;
import static com.tterrag.blur.Blur.*;
import net.fabricmc.api.ModInitializer;
import net.minecraft.class_279;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.GlUniform;
import net.minecraft.client.gl.Shader;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.resource.ClientResourcePackContainer;
import net.minecraft.resource.ResourcePackCompatibility;
import net.minecraft.resource.ResourcePackContainer;
import net.minecraft.resource.ResourcePackContainer.Factory;
import net.minecraft.resource.ResourcePackContainer.SortingDirection;
import net.minecraft.resource.ResourcePackContainerManager;
import net.minecraft.resource.ResourcePackCreator;
import net.minecraft.text.StringTextComponent;
import net.minecraft.util.Identifier;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.resources.IResourcePack;
import net.minecraft.client.resources.SimpleReloadableResourceManager;
import net.minecraft.client.shader.Shader;
import net.minecraft.client.shader.ShaderGroup;
import net.minecraft.client.shader.ShaderUniform;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.RenderTickEvent;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
@Mod(modid = MODID, name = MOD_NAME, version = VERSION, acceptedMinecraftVersions = "[1.9, 1.13)", clientSideOnly = true, guiFactory = "com.tterrag.blur.config.BlurGuiFactory")
public class Blur {
public class Blur implements ModInitializer {
public static final String MODID = "blur";
public static final String MOD_NAME = "Blur";
public static final String VERSION = "@VERSION@";
@Instance
public static Blur instance;
public Configuration config;
private String[] blurExclusions;
private String[] blurExclusions = new String[] {
"net.minecraft.client.gui.ingame.ChatGui"
};
private Field _listShaders;
private long start;
private int fadeTime;
private int fadeTime = 250;
public int radius; // Store default so we don't trigger an extra reload
private int colorFirst, colorSecond;
public int radius = 10; // Store default so we don't trigger an extra reload
public int colorFirst, colorSecond;
@Nonnull
private ShaderResourcePack dummyPack = new ShaderResourcePack();
@SuppressWarnings("unchecked")
public Blur() {
((List<IResourcePack>)ReflectionHelper.getPrivateValue(Minecraft.class, Minecraft.getMinecraft(), "field_110449_ao", "defaultResourcePacks")).add(dummyPack);
}
public static Blur instance;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(this);
public Blur() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
Field _rps = MinecraftClient.class.getDeclaredField("resourcePackContainerManager");
_rps.setAccessible(true);
ResourcePackContainerManager<ClientResourcePackContainer> rps = (ResourcePackContainerManager<ClientResourcePackContainer>)_rps.get(MinecraftClient.getInstance());
rps.addCreator(new ResourcePackCreator() {
@Override
public <T extends ResourcePackContainer> void registerContainer(Map<String, T> var1, Factory<T> factory) {
T var3 = (T) new ClientResourcePackContainer("blur", true, () -> dummyPack, new StringTextComponent(dummyPack.getName()), new StringTextComponent(dummyPack.getName()), ResourcePackCompatibility.COMPATIBLE, SortingDirection.BOTTOM, true, null);
if (var3 != null) {
var1.put("blur", var3);
}
}
});
instance = this;
}
@Override
public void onInitialize() {
// Add our dummy resourcepack
((SimpleReloadableResourceManager)Minecraft.getMinecraft().getResourceManager()).registerReloadListener(dummyPack);
// ((ReloadableResourceManager)MinecraftClient.getInstance().getResourceManager()).addListener(dummyPack);
config = new Configuration(new File(event.getModConfigurationDirectory(), "blur.cfg"));
saveConfig();
// config = new Configuration(new File(event.getModConfigurationDirectory(), "blur.cfg"));
// saveConfig();
}
private void saveConfig() {
/* private void saveConfig() {
blurExclusions = config.getStringList("guiExclusions", Configuration.CATEGORY_GENERAL, new String[] {
GuiChat.class.getName(),
@@ -103,28 +107,32 @@ public class Blur {
);
config.save();
}
}*/
@SubscribeEvent
/* @SubscribeEvent
public void onConfigChanged(OnConfigChangedEvent event) {
if (event.getModID().equals(MODID)) {
saveConfig();
}
}
@SubscribeEvent
public void onGuiChange(GuiOpenEvent event) {
*/
public void onGuiChange(Gui newGui) {
if (_listShaders == null) {
_listShaders = ReflectionHelper.findField(ShaderGroup.class, "field_148031_d", "listShaders");
try {
_listShaders = class_279.class.getDeclaredField("field_1497");
_listShaders.setAccessible(true);
} catch (NoSuchFieldException | SecurityException e) {
throw new RuntimeException(e);
}
}
if (Minecraft.getMinecraft().world != null) {
EntityRenderer er = Minecraft.getMinecraft().entityRenderer;
boolean excluded = event.getGui() == null || ArrayUtils.contains(blurExclusions, event.getGui().getClass().getName());
if (!er.isShaderActive() && !excluded) {
er.loadShader(new ResourceLocation("shaders/post/fade_in_blur.json"));
if (MinecraftClient.getInstance().world != null) {
WorldRenderer er = MinecraftClient.getInstance().worldRenderer;
boolean excluded = newGui == null || ArrayUtils.contains(blurExclusions, newGui.getClass().getName());
if (!er.method_3175() && !excluded) {
((MixinWorldRenderer)er).invokeLoadShader(new Identifier("shaders/post/fade_in_blur.json"));
start = System.currentTimeMillis();
} else if (er.isShaderActive() && excluded) {
er.stopUseShader();
} else if (er.method_3175() && excluded) {
er.method_3207();
}
}
}
@@ -133,17 +141,16 @@ public class Blur {
return Math.min((System.currentTimeMillis() - start) / (float) fadeTime, 1);
}
@SubscribeEvent
public void onRenderTick(RenderTickEvent event) {
if (event.phase == Phase.END && Minecraft.getMinecraft().currentScreen != null && Minecraft.getMinecraft().entityRenderer.isShaderActive()) {
ShaderGroup sg = Minecraft.getMinecraft().entityRenderer.getShaderGroup();
public void onPostRenderTick() {
if (MinecraftClient.getInstance().currentGui != null && MinecraftClient.getInstance().worldRenderer.method_3175()) {
class_279 sg = MinecraftClient.getInstance().worldRenderer.method_3183();
try {
@SuppressWarnings("unchecked")
List<Shader> shaders = (List<Shader>) _listShaders.get(sg);
for (Shader s : shaders) {
ShaderUniform su = s.getShaderManager().getShaderUniform("Progress");
GlUniform su = s.method_1295().getUniformByName("Progress");
if (su != null) {
su.set(getProgress());
su.method_1251(getProgress());
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
@@ -152,8 +159,8 @@ public class Blur {
}
}
public static int getBackgroundColor(boolean second) {
int color = second ? instance.colorSecond : instance.colorFirst;
public int getBackgroundColor(boolean second) {
int color = second ? colorSecond : colorFirst;
int a = color >>> 24;
int r = (color >> 16) & 0xFF;
int b = (color >> 8) & 0xFF;

View File

@@ -1,33 +0,0 @@
package com.tterrag.blur;
import java.util.Map;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
@IFMLLoadingPlugin.SortingIndex(Integer.MAX_VALUE)
public class BlurPlugin implements IFMLLoadingPlugin {
@Override
public String[] getASMTransformerClass() {
return new String[] { "com.tterrag.blur.BlurTransformer" };
}
@Override
public String getModContainerClass() {
return null;
}
@Override
public String getSetupClass() {
return null;
}
@Override
public void injectData(Map<String, Object> data) {
}
@Override
public String getAccessTransformerClass() {
return null;
}
}

View File

@@ -1,43 +0,0 @@
package com.tterrag.blur.config;
import javax.annotation.Nonnull;
import com.tterrag.blur.Blur;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.config.GuiConfig;
import net.minecraftforge.fml.client.config.GuiConfigEntries;
public class BlurConfigGui extends GuiConfig {
public BlurConfigGui(GuiScreen parentScreen) {
super(parentScreen, new ConfigElement(Blur.instance.config.getCategory(Configuration.CATEGORY_GENERAL)).getChildElements(), Blur.MODID, false, false, I18n.format(Blur.MODID + ".config.title"));
}
@Override
public void initGui() {
if (this.entryList == null || this.needsRefresh)
{
this.entryList = new GuiConfigEntries(this, mc) {
@SuppressWarnings({ "unused", "null" })
@Override
protected void drawContainerBackground(@Nonnull Tessellator tessellator) {
if (mc.world == null) {
super.drawContainerBackground(tessellator);
}
}
};
this.needsRefresh = false;
}
super.initGui();
}
@Override
public void drawDefaultBackground() {
drawWorldBackground(0);
}
}

View File

@@ -1,41 +0,0 @@
package com.tterrag.blur.config;
import java.util.Set;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.fml.client.IModGuiFactory;
public class BlurGuiFactory implements IModGuiFactory {
@Override
public void initialize(Minecraft minecraftInstance) {}
@Override
public Class<? extends GuiScreen> mainConfigGuiClass() {
return BlurConfigGui.class;
}
@Override
public Set<RuntimeOptionCategoryElement> runtimeGuiCategories() {
return null;
}
@Override
@Deprecated
public RuntimeOptionGuiHandler getHandlerFor(RuntimeOptionCategoryElement element) {
return null;
}
// 1.12
// @Override
public boolean hasConfigGui() {
return true;
}
// 1.12
// @Override
public GuiScreen createConfigGui(GuiScreen parentScreen) {
return new BlurConfigGui(parentScreen);
}
}

View File

@@ -0,0 +1,27 @@
package com.tterrag.blur.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import com.tterrag.blur.Blur;
import net.minecraft.client.gui.Gui;
@Mixin(Gui.class)
public class MixinGui {
@ModifyConstant(
method = "drawBackground(I)V",
constant = @Constant(intValue = -1072689136))
public int getFirstBackgroundColor(int color) {
return Blur.instance.colorFirst;
}
@ModifyConstant(
method = "drawBackground(I)V",
constant = @Constant(intValue = -804253680))
public int getSecondBackgroundColor(int color) {
return Blur.instance.colorSecond;
}
}

View File

@@ -0,0 +1,30 @@
package com.tterrag.blur.mixin;
import org.objectweb.asm.Opcodes;
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;
import com.tterrag.blur.Blur;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Gui;
@Mixin(MinecraftClient.class)
public class MixinMinecraftClient {
@Inject(method = "openGui(Lnet/minecraft/client/gui/Gui;)V",
at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;currentGui:Lnet/minecraft/client/gui/Gui;", opcode = Opcodes.PUTFIELD, ordinal = 0))
public void onGuiOpen(Gui newGui, CallbackInfo info) {
Blur.instance.onGuiChange(newGui);
}
@Inject(method = "method_1523(Z)V",
at = @At(value = "INVOKE", target = "net/minecraft/client/toast/ToastManager.draw()V"),
require = 1)
public void onPostRenderTick(CallbackInfo info) {
Blur.instance.onPostRenderTick();
}
}

View File

@@ -0,0 +1,15 @@
package com.tterrag.blur.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.util.Identifier;
@Mixin(WorldRenderer.class)
public interface MixinWorldRenderer {
@Invoker
void invokeLoadShader(Identifier loc);
}

View File

@@ -1,40 +1,42 @@
package com.tterrag.blur.util;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.function.Predicate;
import com.google.common.collect.ImmutableSet;
import com.tterrag.blur.Blur;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.client.resources.IResourcePack;
import net.minecraft.client.resources.data.IMetadataSection;
import net.minecraft.client.resources.data.MetadataSerializer;
import net.minecraft.client.resources.data.PackMetadataSection;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceReloadListener;
import net.minecraft.resource.ResourceType;
import net.minecraft.resource.metadata.PackResourceMetadata;
import net.minecraft.resource.metadata.ResourceMetadataReader;
import net.minecraft.text.StringTextComponent;
import net.minecraft.util.Identifier;
public class ShaderResourcePack implements IResourcePack, IResourceManagerReloadListener {
public class ShaderResourcePack implements ResourcePack, ResourceReloadListener {
protected boolean validPath(ResourceLocation location) {
return location.getResourceDomain().equals("minecraft") && location.getResourcePath().startsWith("shaders/");
protected boolean validPath(Identifier location) {
return location.getNamespace().equals("minecraft") && location.getPath().startsWith("shaders/");
}
private final Map<ResourceLocation, String> loadedData = new HashMap<>();
private final Map<Identifier, String> loadedData = new HashMap<>();
@Override
public InputStream getInputStream(ResourceLocation location) throws IOException {
if (validPath(location)) {
public InputStream open(ResourceType type, Identifier location) throws IOException {
if (type == ResourceType.ASSETS && validPath(location)) {
String s = loadedData.computeIfAbsent(location, loc -> {
InputStream in = Blur.class.getResourceAsStream("/" + location.getResourcePath());
InputStream in = Blur.class.getResourceAsStream("/" + location.getPath());
StringBuilder data = new StringBuilder();
Scanner scan = new Scanner(in);
try {
@@ -53,37 +55,45 @@ public class ShaderResourcePack implements IResourcePack, IResourceManagerReload
}
@Override
public boolean resourceExists(ResourceLocation location) {
return validPath(location) && Blur.class.getResource("/" + location.getResourcePath()) != null;
public boolean contains(ResourceType type, Identifier location) {
return type == ResourceType.ASSETS && validPath(location) && Blur.class.getResource("/" + location.getPath()) != null;
}
@Override
public Set<String> getResourceDomains() {
public Set<String> getNamespaces(ResourceType type) {
return ImmutableSet.of("minecraft");
}
@SuppressWarnings({ "unchecked", "null" })
@Override
public <T extends IMetadataSection> T getPackMetadata(MetadataSerializer metadataSerializer, String metadataSectionName) throws IOException {
if ("pack".equals(metadataSectionName)) {
return (T) new PackMetadataSection(new TextComponentString("Blur's default shaders"), 3);
public <T> T parseMetadata(ResourceMetadataReader<T> var1) throws IOException {
if ("pack".equals(var1.getKey())) {
return (T) new PackResourceMetadata(new StringTextComponent("Blur's default shaders"), 4);
}
return null;
}
@Override
public BufferedImage getPackImage() throws IOException {
throw new FileNotFoundException("pack.png");
}
@Override
public String getPackName() {
public String getName() {
return "Blur dummy resource pack";
}
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
public void onResourceReload(ResourceManager resourceManager) {
loadedData.clear();
}
@Override
public void close() throws IOException {}
@Override
public InputStream openRoot(String var1) throws IOException {
return open(ResourceType.ASSETS, new Identifier(var1));
}
@Override
public Collection<Identifier> findResources(ResourceType var1, String var2, int var3, Predicate<String> var4) {
return Collections.emptyList();
}
}

View File

@@ -0,0 +1,13 @@
{
"required": true,
"package": "com.tterrag.blur.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinGui",
"MixinMinecraftClient",
"MixinWorldRenderer"
],
"injectors": {
"defaultRequire": 1
}
}

View File

@@ -0,0 +1,10 @@
{
"id": "blur",
"name": "Blur",
"version": "1.0.0",
"side": "client",
"initializer": "com.tterrag.blur.Blur",
"mixins": {
"client": "blur.client.json"
}
}

View File

@@ -1,16 +0,0 @@
[
{
"modid": "blur",
"name": "Blur",
"description": "Adds a blurry background when viewing GUIs",
"version": "${version}",
"mcversion": "${mcversion}",
"url": "http://tterrag.com",
"updateUrl": "http://ci.tterrag.com/job/BlurBG",
"authorList": ["tterrag"],
"credits": "",
"logoFile": "",
"screenshots": [],
"dependencies": []
}
]