From 3309f35054f84edd7d488ad68c1806a58c4ffdbc Mon Sep 17 00:00:00 2001 From: AterAnimAvis Date: Sun, 20 Sep 2020 15:32:40 +0100 Subject: [PATCH] General Improvements --- - Add an DisplayTest ExtensionPoint - Insure the mod won't break if installed on a Server Signed-off-by: AterAnimAvis --- build.gradle | 100 +++++------ .../blockrenderer/BlockRenderer.java | 170 +++--------------- .../blockrenderer/init/Keybindings.java | 3 +- .../blockrenderer/proxy/ClientProxy.java | 159 ++++++++++++++++ .../blockrenderer/proxy/CommonProxy.java | 13 ++ .../blockrenderer/proxy/DedicatedProxy.java | 18 ++ .../blockrenderer/proxy/package-info.java | 7 + .../screens/EnterNamespaceScreen.java | 2 +- .../screens/EnterSizeScreen.java | 2 +- 9 files changed, 277 insertions(+), 197 deletions(-) create mode 100644 src/main/java/com/unascribed/blockrenderer/proxy/ClientProxy.java create mode 100644 src/main/java/com/unascribed/blockrenderer/proxy/CommonProxy.java create mode 100644 src/main/java/com/unascribed/blockrenderer/proxy/DedicatedProxy.java create mode 100644 src/main/java/com/unascribed/blockrenderer/proxy/package-info.java diff --git a/build.gradle b/build.gradle index 41614bc..d38c837 100644 --- a/build.gradle +++ b/build.gradle @@ -1,87 +1,87 @@ import java.time.Instant buildscript { - repositories { - maven { url = 'https://files.minecraftforge.net/maven' } - jcenter() - mavenCentral() - } - dependencies { - classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true - } + repositories { + maven { url = 'https://files.minecraftforge.net/maven' } + jcenter() + mavenCentral() + } + dependencies { + classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true + } } apply plugin: 'net.minecraftforge.gradle' group = "com.unascribed" archivesBaseName = "BlockRenderer" -version = "1.16.3-1.2.1" +version = "1.16.3-1.3.0" sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = JavaVersion.VERSION_1_8 minecraft { - mappings channel: 'snapshot', version: '20200723-1.16.1' + mappings channel: 'snapshot', version: '20200723-1.16.1' - // accessTransformer = file('src/main/resources/META-INF/block_renderer_at.cfg') + // accessTransformer = file('src/main/resources/META-INF/block_renderer_at.cfg') - runs { - client { - workingDirectory project.file('run') + runs { + client { + workingDirectory project.file('run') - property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' - property 'forge.logging.console.level', 'debug' + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + property 'forge.logging.console.level', 'debug' - mods { - blockrenderer { - source sourceSets.main - } - } - } + mods { + blockrenderer { + source sourceSets.main + } + } + } - server { - workingDirectory project.file('run') + server { + workingDirectory project.file('run') - property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' - property 'forge.logging.console.level', 'debug' + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + property 'forge.logging.console.level', 'debug' - mods { - blockrenderer { - source sourceSets.main - } - } - } - } + mods { + blockrenderer { + source sourceSets.main + } + } + } + } } dependencies { - minecraft 'net.minecraftforge:forge:1.16.2-33.0.19' + minecraft 'net.minecraftforge:forge:1.16.2-33.0.19' - // Javax Annotations (N.B. ForgeGradle currently provides this) - implementation 'com.google.code.findbugs:jsr305:3+' + // Javax Annotations (N.B. ForgeGradle currently provides this) + implementation 'com.google.code.findbugs:jsr305:3+' } jar { - manifest { - attributes([ - 'Specification-Title' : rootProject.name, - 'Specification-Vendor' : project.group, - 'Specification-Version' : '1', - 'Implementation-Title' : rootProject.name, - 'Implementation-Version' : project.version, - 'Implementation-Vendor' : project.group, - 'Implementation-Timestamp': Instant.now() - ]) - } + manifest { + attributes([ + 'Specification-Title' : rootProject.name, + 'Specification-Vendor' : project.group, + 'Specification-Version' : '1', + 'Implementation-Title' : rootProject.name, + 'Implementation-Version' : project.version, + 'Implementation-Vendor' : project.group, + 'Implementation-Timestamp': Instant.now() + ]) + } } jar.finalizedBy('reobfJar') task sourcesJar(type: Jar, dependsOn: classes) { - classifier = 'sources' - from sourceSets.main.allSource + classifier = 'sources' + from sourceSets.main.allSource } build.dependsOn sourcesJar artifacts { - archives sourcesJar + archives sourcesJar } diff --git a/src/main/java/com/unascribed/blockrenderer/BlockRenderer.java b/src/main/java/com/unascribed/blockrenderer/BlockRenderer.java index c4bafce..8b53eff 100644 --- a/src/main/java/com/unascribed/blockrenderer/BlockRenderer.java +++ b/src/main/java/com/unascribed/blockrenderer/BlockRenderer.java @@ -1,161 +1,43 @@ package com.unascribed.blockrenderer; -import com.unascribed.blockrenderer.init.Keybindings; -import com.unascribed.blockrenderer.render.SingleRenderer; -import com.unascribed.blockrenderer.render.impl.ItemStackRenderer; -import com.unascribed.blockrenderer.render.request.IRequest; -import com.unascribed.blockrenderer.screens.EnterNamespaceScreen; -import com.unascribed.blockrenderer.screens.EnterSizeScreen; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.recipebook.IRecipeShownListener; -import net.minecraft.client.gui.recipebook.RecipeBookGui; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.client.gui.screen.inventory.ContainerScreen; -import net.minecraft.client.gui.widget.TextFieldWidget; -import net.minecraft.client.util.InputMappings; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.inventory.container.Slot; -import net.minecraft.item.ItemStack; -import net.minecraft.util.ResourceLocation; -import net.minecraft.util.text.TranslationTextComponent; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.TickEvent; -import net.minecraftforge.eventbus.api.EventPriority; -import net.minecraftforge.eventbus.api.SubscribeEvent; +import com.unascribed.blockrenderer.proxy.ClientProxy; +import com.unascribed.blockrenderer.proxy.CommonProxy; +import com.unascribed.blockrenderer.proxy.DedicatedProxy; +import net.minecraftforge.fml.DistExecutor; +import net.minecraftforge.fml.ExtensionPoint; +import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.registries.ForgeRegistries; +import net.minecraftforge.fml.network.FMLNetworkConstants; +import org.apache.commons.lang3.tuple.Pair; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.lwjgl.glfw.GLFW; -import static com.unascribed.blockrenderer.utils.StringUtils.addMessage; +import java.util.function.BiPredicate; +import java.util.function.Supplier; @Mod(Reference.MOD_ID) public class BlockRenderer { - public static final Logger LOGGER = LogManager.getLogger("BlockRenderer"); + public static final Logger LOGGER = LogManager.getLogger("BlockRenderer"); - private boolean down = false; + public static CommonProxy proxy; - public static IRequest pendingRequest; + public BlockRenderer() { + proxy = DistExecutor.safeRunForDist(() -> ClientProxy::new, () -> DedicatedProxy::new); + proxy.init(); - public BlockRenderer() { - MinecraftForge.EVENT_BUS.register(this); - } + registerDisplayTest(ModLoadingContext.get()); + } - @SubscribeEvent(priority= EventPriority.HIGHEST) - public void onFrameStart(TickEvent.RenderTickEvent e) { - if (e.phase != TickEvent.Phase.START) return; - - if (pendingRequest != null) { - pendingRequest.render(); - pendingRequest = null; - } - - if (!isKeyDown()) { - down = false; - return; - } - - if (down) return; - down = true; - - Minecraft client = Minecraft.getInstance(); - Slot hovered = null; - Screen currentScreen = client.currentScreen; - boolean isContainerScreen = currentScreen instanceof ContainerScreen; - - if (isContainerScreen) hovered = ((ContainerScreen) currentScreen).getSlotUnderMouse(); - - if (Screen.hasControlDown()) { - String namespace = ""; - if (hovered != null && hovered.getHasStack()) { - ResourceLocation identifier = ForgeRegistries.ITEMS.getKey(hovered.getStack().getItem()); - if (identifier != null) namespace = identifier.getNamespace(); - } - - PlayerEntity player = client.player; - if (!isContainerScreen && player != null && !player.getHeldItemMainhand().isEmpty()) { - ResourceLocation identifier = ForgeRegistries.ITEMS.getKey(player.getHeldItemMainhand().getItem()); - if (identifier != null) namespace = identifier.getNamespace(); - } - - client.displayGuiScreen(new EnterNamespaceScreen(client.currentScreen, namespace.trim())); - return; - } - - if (!isContainerScreen) { - PlayerEntity player = client.player; - - if (player != null && !player.getHeldItemMainhand().isEmpty()) { - renderStack(player.getHeldItemMainhand()); - return; - } - addMessage(new TranslationTextComponent("msg.block_renderer.notContainer")); - return; - } - - if (hovered == null) { - addMessage(new TranslationTextComponent("msg.block_renderer.slot.absent")); - return; - } - - ItemStack stack = hovered.getStack(); - - if (stack.isEmpty()) { - addMessage(new TranslationTextComponent("msg.block_renderer.slot.empty")); - return; - } - - renderStack(stack); - } - - private static void renderStack(ItemStack stack) { - Minecraft client = Minecraft.getInstance(); - - if (Screen.hasShiftDown()) { - client.displayGuiScreen(new EnterSizeScreen(client.currentScreen, stack)); - return; - } - - SingleRenderer.render(new ItemStackRenderer(), stack, 512, false, false); - } - - private static boolean isKeyDown() { - Minecraft client = Minecraft.getInstance(); - Screen currentScreen = client.currentScreen; - - /* Unbound key */ - if (Keybindings.render.isInvalid()) return false; - - /* Has the Keybinding been triggered? */ - if (Keybindings.render.isPressed()) return true; - - /* Not in Screen so we should be ok */ - if (currentScreen == null) return false; - - /* Non Containers seem to behave ok */ - boolean hasSlots = currentScreen instanceof ContainerScreen; - if (!hasSlots) return false; - - /* TextFieldWidgets */ - if (currentScreen.getListener() instanceof TextFieldWidget) return false; - - /* Recipe Books */ - if (currentScreen instanceof IRecipeShownListener) { - RecipeBookGui recipeBook = ((IRecipeShownListener) currentScreen).getRecipeGui(); - if (recipeBook.isVisible()) return false; - } - - /* Actually Check to see if the key is down */ - InputMappings.Input key = Keybindings.render.getKey(); - - if (key.getType() == InputMappings.Type.MOUSE) { - return GLFW.glfwGetMouseButton(client.getMainWindow().getHandle(), key.getKeyCode()) == GLFW.GLFW_PRESS; - } - - return InputMappings.isKeyDown(client.getMainWindow().getHandle(), key.getKeyCode()); - } + /** + * Ensure that we don't cause the client to show a server as incompatible and vice-versa + */ + private void registerDisplayTest(ModLoadingContext context) { + Supplier versionProvider = () -> FMLNetworkConstants.IGNORESERVERONLY; + BiPredicate versionChecker = (version, isNetwork) -> true; + Pair, BiPredicate> extension = Pair.of(versionProvider, versionChecker); + context.registerExtensionPoint(ExtensionPoint.DISPLAYTEST, () -> extension); + } } diff --git a/src/main/java/com/unascribed/blockrenderer/init/Keybindings.java b/src/main/java/com/unascribed/blockrenderer/init/Keybindings.java index f449fe2..24e8a2d 100644 --- a/src/main/java/com/unascribed/blockrenderer/init/Keybindings.java +++ b/src/main/java/com/unascribed/blockrenderer/init/Keybindings.java @@ -1,6 +1,7 @@ package com.unascribed.blockrenderer.init; import net.minecraft.client.settings.KeyBinding; +import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.Mod; @@ -10,7 +11,7 @@ import static com.unascribed.blockrenderer.Reference.MOD_ID; -@Mod.EventBusSubscriber(modid = MOD_ID, bus = Bus.MOD) +@Mod.EventBusSubscriber(modid = MOD_ID, bus = Bus.MOD, value = Dist.CLIENT) public interface Keybindings { KeyBinding render = new KeyBinding("key.block_renderer.render", GLFW.GLFW_KEY_GRAVE_ACCENT, "key.categories.block_renderer"); diff --git a/src/main/java/com/unascribed/blockrenderer/proxy/ClientProxy.java b/src/main/java/com/unascribed/blockrenderer/proxy/ClientProxy.java new file mode 100644 index 0000000..d589d60 --- /dev/null +++ b/src/main/java/com/unascribed/blockrenderer/proxy/ClientProxy.java @@ -0,0 +1,159 @@ +package com.unascribed.blockrenderer.proxy; + +import com.unascribed.blockrenderer.init.Keybindings; +import com.unascribed.blockrenderer.render.SingleRenderer; +import com.unascribed.blockrenderer.render.impl.ItemStackRenderer; +import com.unascribed.blockrenderer.render.request.IRequest; +import com.unascribed.blockrenderer.screens.EnterNamespaceScreen; +import com.unascribed.blockrenderer.screens.EnterSizeScreen; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.recipebook.IRecipeShownListener; +import net.minecraft.client.gui.recipebook.RecipeBookGui; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.inventory.ContainerScreen; +import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.client.util.InputMappings; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.container.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.text.TranslationTextComponent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.eventbus.api.EventPriority; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.registries.ForgeRegistries; +import org.lwjgl.glfw.GLFW; + +import java.util.Deque; +import java.util.LinkedList; + +import static com.unascribed.blockrenderer.utils.StringUtils.addMessage; + +public class ClientProxy extends CommonProxy { + + private boolean down = false; + private final Deque pendingRequests = new LinkedList<>(); + + @Override + public void init() { + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public void render(IRequest request) { + pendingRequests.add(request); + } + + @SubscribeEvent(priority = EventPriority.HIGHEST) + public void onFrameStart(TickEvent.RenderTickEvent e) { + if (e.phase != TickEvent.Phase.START) return; + + if (pendingRequests.size() > 0) pendingRequests.poll().render(); + + if (!isKeyDown()) { + down = false; + return; + } + + if (down) return; + down = true; + + Minecraft client = Minecraft.getInstance(); + Slot hovered = null; + Screen currentScreen = client.currentScreen; + boolean isContainerScreen = currentScreen instanceof ContainerScreen; + + if (isContainerScreen) hovered = ((ContainerScreen) currentScreen).getSlotUnderMouse(); + + if (Screen.hasControlDown()) { + String namespace = ""; + if (hovered != null && hovered.getHasStack()) { + ResourceLocation identifier = ForgeRegistries.ITEMS.getKey(hovered.getStack().getItem()); + if (identifier != null) namespace = identifier.getNamespace(); + } + + PlayerEntity player = client.player; + if (!isContainerScreen && player != null && !player.getHeldItemMainhand().isEmpty()) { + ResourceLocation identifier = ForgeRegistries.ITEMS.getKey(player.getHeldItemMainhand().getItem()); + if (identifier != null) namespace = identifier.getNamespace(); + } + + client.displayGuiScreen(new EnterNamespaceScreen(client.currentScreen, namespace.trim())); + return; + } + + if (!isContainerScreen) { + PlayerEntity player = client.player; + + if (player != null && !player.getHeldItemMainhand().isEmpty()) { + renderStack(player.getHeldItemMainhand()); + return; + } + addMessage(new TranslationTextComponent("msg.block_renderer.notContainer")); + return; + } + + if (hovered == null) { + addMessage(new TranslationTextComponent("msg.block_renderer.slot.absent")); + return; + } + + ItemStack stack = hovered.getStack(); + + if (stack.isEmpty()) { + addMessage(new TranslationTextComponent("msg.block_renderer.slot.empty")); + return; + } + + renderStack(stack); + } + + private static void renderStack(ItemStack stack) { + Minecraft client = Minecraft.getInstance(); + + if (Screen.hasShiftDown()) { + client.displayGuiScreen(new EnterSizeScreen(client.currentScreen, stack)); + return; + } + + SingleRenderer.render(new ItemStackRenderer(), stack, 512, false, false); + } + + private static boolean isKeyDown() { + Minecraft client = Minecraft.getInstance(); + Screen currentScreen = client.currentScreen; + + /* Unbound key */ + if (Keybindings.render.isInvalid()) return false; + + /* Has the Keybinding been triggered? */ + if (Keybindings.render.isPressed()) return true; + + /* Not in Screen so we should be ok */ + if (currentScreen == null) return false; + + /* Non Containers seem to behave ok */ + boolean hasSlots = currentScreen instanceof ContainerScreen; + if (!hasSlots) return false; + + /* TextFieldWidgets */ + if (currentScreen.getListener() instanceof TextFieldWidget) return false; + + /* Recipe Books */ + if (currentScreen instanceof IRecipeShownListener) { + RecipeBookGui recipeBook = ((IRecipeShownListener) currentScreen).getRecipeGui(); + if (recipeBook.isVisible()) return false; + } + + /* Actually Check to see if the key is down */ + InputMappings.Input key = Keybindings.render.getKey(); + + if (key.getType() == InputMappings.Type.MOUSE) { + return GLFW.glfwGetMouseButton(client.getMainWindow().getHandle(), key.getKeyCode()) == GLFW.GLFW_PRESS; + } + + return InputMappings.isKeyDown(client.getMainWindow().getHandle(), key.getKeyCode()); + } + +} diff --git a/src/main/java/com/unascribed/blockrenderer/proxy/CommonProxy.java b/src/main/java/com/unascribed/blockrenderer/proxy/CommonProxy.java new file mode 100644 index 0000000..7592e8e --- /dev/null +++ b/src/main/java/com/unascribed/blockrenderer/proxy/CommonProxy.java @@ -0,0 +1,13 @@ +package com.unascribed.blockrenderer.proxy; + +import com.unascribed.blockrenderer.render.request.IRequest; + +public abstract class CommonProxy { + + public abstract void init(); + + public void render(IRequest request) { + // NO-OP + } + +} diff --git a/src/main/java/com/unascribed/blockrenderer/proxy/DedicatedProxy.java b/src/main/java/com/unascribed/blockrenderer/proxy/DedicatedProxy.java new file mode 100644 index 0000000..7df50b7 --- /dev/null +++ b/src/main/java/com/unascribed/blockrenderer/proxy/DedicatedProxy.java @@ -0,0 +1,18 @@ +package com.unascribed.blockrenderer.proxy; + +import com.unascribed.blockrenderer.BlockRenderer; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.event.server.FMLServerStartedEvent; + +public class DedicatedProxy extends CommonProxy { + + @Override + public void init() { + MinecraftForge.EVENT_BUS.addListener(this::onServerStarted); + } + + public void onServerStarted(FMLServerStartedEvent event) { + BlockRenderer.LOGGER.error("Running a Client only Mod on a Dedicated Server"); + } + +} diff --git a/src/main/java/com/unascribed/blockrenderer/proxy/package-info.java b/src/main/java/com/unascribed/blockrenderer/proxy/package-info.java new file mode 100644 index 0000000..5c13dd2 --- /dev/null +++ b/src/main/java/com/unascribed/blockrenderer/proxy/package-info.java @@ -0,0 +1,7 @@ +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +package com.unascribed.blockrenderer.proxy; + +import com.unascribed.blockrenderer.annotation.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/java/com/unascribed/blockrenderer/screens/EnterNamespaceScreen.java b/src/main/java/com/unascribed/blockrenderer/screens/EnterNamespaceScreen.java index f2b009e..7a56584 100644 --- a/src/main/java/com/unascribed/blockrenderer/screens/EnterNamespaceScreen.java +++ b/src/main/java/com/unascribed/blockrenderer/screens/EnterNamespaceScreen.java @@ -91,6 +91,6 @@ void onRender(Button button) { minecraft.displayGuiScreen(old); if (minecraft.world == null) return; - BlockRenderer.pendingRequest = new BulkItemRequest(round(size), text.getText(), useId.isChecked(), addSize.isChecked()); + BlockRenderer.proxy.render(new BulkItemRequest(round(size), text.getText(), useId.isChecked(), addSize.isChecked())); } } diff --git a/src/main/java/com/unascribed/blockrenderer/screens/EnterSizeScreen.java b/src/main/java/com/unascribed/blockrenderer/screens/EnterSizeScreen.java index 934c602..58124ca 100644 --- a/src/main/java/com/unascribed/blockrenderer/screens/EnterSizeScreen.java +++ b/src/main/java/com/unascribed/blockrenderer/screens/EnterSizeScreen.java @@ -34,6 +34,6 @@ void onRender(Button button) { minecraft.displayGuiScreen(old); if (minecraft.world == null) return; - BlockRenderer.pendingRequest = new ItemRequest(round(size), stack, useId.isChecked(), addSize.isChecked()); + BlockRenderer.proxy.render(new ItemRequest(round(size), stack, useId.isChecked(), addSize.isChecked())); } }