Skip to content

Commit

Permalink
General Improvements
Browse files Browse the repository at this point in the history
---

- Add an DisplayTest ExtensionPoint
- Insure the mod won't break if installed on a Server

Signed-off-by: AterAnimAvis <AterAnimAvis@gmail.com>
  • Loading branch information
AterAnimAvis committed Sep 20, 2020
1 parent 52d3401 commit 3309f35
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 197 deletions.
100 changes: 50 additions & 50 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
170 changes: 26 additions & 144 deletions src/main/java/com/unascribed/blockrenderer/BlockRenderer.java
Original file line number Diff line number Diff line change
@@ -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<String> versionProvider = () -> FMLNetworkConstants.IGNORESERVERONLY;
BiPredicate<String, Boolean> versionChecker = (version, isNetwork) -> true;
Pair<Supplier<String>, BiPredicate<String, Boolean>> extension = Pair.of(versionProvider, versionChecker);

context.registerExtensionPoint(ExtensionPoint.DISPLAYTEST, () -> extension);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand Down
Loading

0 comments on commit 3309f35

Please sign in to comment.