Skip to content

Commit

Permalink
Handle toggle key bindings
Browse files Browse the repository at this point in the history
Add populate shopping list key binding
  • Loading branch information
paulyhedral committed Nov 25, 2024
1 parent 0e7f618 commit b339b94
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package com.sweetrpg.crafttracker.client.event;

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.sweetrpg.crafttracker.CraftTracker;
import com.sweetrpg.crafttracker.client.screen.QueueManagementScreen;
import com.sweetrpg.crafttracker.common.addon.jei.CTPlugin;
import com.sweetrpg.crafttracker.common.lib.CTRuntime;
import com.sweetrpg.crafttracker.common.lib.Constants;
import com.sweetrpg.crafttracker.common.manager.CraftingQueueManager;
import com.sweetrpg.crafttracker.common.registry.ModKeyBindings;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.event.MovementInputUpdateEvent;
import net.minecraftforge.client.event.ScreenEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

import java.util.UUID;

public class ClientEventHandler {

// static boolean craftListDisplayed = true;
Expand All @@ -32,26 +29,27 @@ public class ClientEventHandler {
// public static void onModelBakeEvent(final ModelBakeEvent event) {
// Map<ResourceLocation, BakedModel> modelRegistry = event.getModelRegistry();
//
//// // cat tree
//// try {
//// ResourceLocation resourceLocation = ForgeRegistries.BLOCKS.getKey(ModBlocks.CAT_TREE.get());
//// ResourceLocation unbakedModelLoc = new ResourceLocation(resourceLocation.getNamespace(), "block/" + resourceLocation.getPath());
////
//// BlockModel model = (BlockModel) event.getModelLoader().getModel(unbakedModelLoc);
//// BakedModel customModel = new CatTreeModel(event.getModelLoader(), model, model.bake(event.getModelLoader(), model, ForgeModelBakery.defaultTextureGetter(), BlockModelRotation.X180_Y180, unbakedModelLoc, true));
////
//// // Replace all valid block states
//// ModBlocks.CAT_TREE.get().getStateDefinition().getPossibleStates().forEach(state -> {
//// modelRegistry.put(BlockModelShaper.stateToModelLocation(state), customModel);
//// });
////
//// // Replace inventory model
//// modelRegistry.put(new ModelResourceLocation(resourceLocation, "inventory"), customModel);
//// }
//// catch(Exception e) {
//// CraftTracker.LOGGER.warn("Could not get base Cat Tree model. Reverting to default textures...");
//// e.printStackTrace();
//// }

/// / // cat tree
/// / try {
/// / ResourceLocation resourceLocation = ForgeRegistries.BLOCKS.getKey(ModBlocks.CAT_TREE.get());
/// / ResourceLocation unbakedModelLoc = new ResourceLocation(resourceLocation.getNamespace(), "block/" + resourceLocation.getPath());
/// /
/// / BlockModel model = (BlockModel) event.getModelLoader().getModel(unbakedModelLoc);
/// / BakedModel customModel = new CatTreeModel(event.getModelLoader(), model, model.bake(event.getModelLoader(), model, ForgeModelBakery.defaultTextureGetter(), BlockModelRotation.X180_Y180, unbakedModelLoc, true));
/// /
/// / // Replace all valid block states
/// / ModBlocks.CAT_TREE.get().getStateDefinition().getPossibleStates().forEach(state -> {
/// / modelRegistry.put(BlockModelShaper.stateToModelLocation(state), customModel);
/// / });
/// /
/// / // Replace inventory model
/// / modelRegistry.put(new ModelResourceLocation(resourceLocation, "inventory"), customModel);
/// / }
/// / catch(Exception e) {
/// / CraftTracker.LOGGER.warn("Could not get base Cat Tree model. Reverting to default textures...");
/// / e.printStackTrace();
/// / }
//
// }

Expand All @@ -62,32 +60,17 @@ public static void onKeyInput(final InputEvent.KeyInputEvent event) {
if(ModKeyBindings.ADD_TO_QUEUE_MAPPING.matches(event.getKey(), event.getScanCode())) {
CraftTracker.LOGGER.debug("#onKeyInput: ADD_TO_QUEUE_MAPPING");

CTPlugin.jeiRuntime.getIngredientListOverlay().getIngredientUnderMouse()
.ifPresent(ingredient -> {
CraftTracker.LOGGER.debug("AddToQueuePacket#handle: type {}", ingredient.getType());
CraftTracker.LOGGER.debug("AddToQueuePacket#handle: ingredient {}", ingredient.getIngredient());

if(ingredient.getIngredient() instanceof ItemStack itemStack) {
ResourceLocation res = itemStack.getItem().getRegistryName();
CraftTracker.LOGGER.debug("AddToQueuePacket#handle: res {}", res);

var player = Minecraft.getInstance().player;
CraftingQueueManager.INSTANCE.addProduct(player, res, 1);
// PacketHandler.sendToServer(new AddToQueueData(res, 1));
}
});


handleAddToQueue();
}
else if(ModKeyBindings.TOGGLE_CRAFT_LIST_MAPPING.matches(event.getKey(), event.getScanCode())) {
CraftTracker.LOGGER.debug("#onKeyInput: TOGGLE_CRAFT_LIST_MAPPING");
// craftListDisplayed = !craftListDisplayed;
// PacketHandler.sendToServer(new ToggleCraftListData());

handleToggleCraftList();
}
else if(ModKeyBindings.TOGGLE_SHOPPING_LIST_MAPPING.matches(event.getKey(), event.getScanCode())) {
CraftTracker.LOGGER.debug("#onKeyInput: TOGGLE_SHOPPING_LIST_MAPPING");
// shoppingListDisplayed = !shoppingListDisplayed;
// PacketHandler.sendToServer(new ToggleShoppingListData());

handleToggleShoppingList();
}
else if(ModKeyBindings.OPEN_QUEUE_MANAGER_MAPPING.matches(event.getKey(), event.getScanCode())) {
CraftTracker.LOGGER.debug("#onKeyInput: OPEN_QUEUE_MANAGER_MAPPING");
Expand All @@ -96,6 +79,54 @@ else if(ModKeyBindings.OPEN_QUEUE_MANAGER_MAPPING.matches(event.getKey(), event.
}
}

private static void handleToggleCraftList() {
CraftTracker.LOGGER.debug("#handleToggleCraftList");

TranslatableComponent msg;
switch(CTRuntime.INSTANCE.queueOverlayRequestedState) {
case SHOW:
CTRuntime.INSTANCE.queueOverlayRequestedState = CTRuntime.OverlayState.HIDE;
msg = new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_MSG_QUEUE_OVERLAY_MODE_HIDE);
Minecraft.getInstance().player.sendMessage(msg, UUID.randomUUID());
break;

case HIDE:
CTRuntime.INSTANCE.queueOverlayRequestedState = CTRuntime.OverlayState.DYNAMIC;
msg = new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_MSG_QUEUE_OVERLAY_MODE_DYNAMIC);
Minecraft.getInstance().player.sendMessage(msg, UUID.randomUUID());
break;

case DYNAMIC:
CTRuntime.INSTANCE.queueOverlayRequestedState = CTRuntime.OverlayState.SHOW;
msg = new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_MSG_QUEUE_OVERLAY_MODE_SHOW);
Minecraft.getInstance().player.sendMessage(msg, UUID.randomUUID());
break;
}
}

private static void handleToggleShoppingList() {
CraftTracker.LOGGER.debug("#handleToggleShoppingList");
}

private static void handleAddToQueue() {
CraftTracker.LOGGER.debug("#handleToggleCraftList");

CTPlugin.jeiRuntime.getIngredientListOverlay().getIngredientUnderMouse()
.ifPresent(ingredient -> {
CraftTracker.LOGGER.debug("AddToQueuePacket#handle: type {}", ingredient.getType());
CraftTracker.LOGGER.debug("AddToQueuePacket#handle: ingredient {}", ingredient.getIngredient());

if(ingredient.getIngredient() instanceof ItemStack itemStack) {
ResourceLocation res = itemStack.getItem().getRegistryName();
CraftTracker.LOGGER.debug("AddToQueuePacket#handle: res {}", res);

var player = Minecraft.getInstance().player;
CraftingQueueManager.INSTANCE.addProduct(player, res, 1);
// PacketHandler.sendToServer(new AddToQueueData(res, 1));
}
});
}

@SubscribeEvent
public void onInputEvent(final MovementInputUpdateEvent event) {
CraftTracker.LOGGER.trace("#onInputEvent: {}", event);
Expand Down Expand Up @@ -172,36 +203,36 @@ public void onScreenDrawForeground(final ScreenEvent.DrawScreenEvent event) {
}
}

public void drawSelectionBox(PoseStack matrixStackIn, Player player, float particleTicks, AABB boundingBox) {
CraftTracker.LOGGER.debug("#drawSelectionBox: {}, player: {}", matrixStackIn, player);

RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
// RenderSystem.disableAlphaTest();
RenderSystem.depthMask(false);
RenderSystem.enableBlend();
RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
RenderSystem.setShaderColor(0.0F, 0.0F, 0.0F, 0.7F);
//TODO Used when drawing outline of bounding box
RenderSystem.lineWidth(2.0F);


RenderSystem.disableTexture();
Vec3 vec3d = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();
double d0 = vec3d.x();
double d1 = vec3d.y();
double d2 = vec3d.z();

BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder();
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
LevelRenderer.renderLineBox(matrixStackIn, bufferbuilder, boundingBox.move(-d0, -d1, -d2), 1F, 1F, 0F, 0.8F);
Tesselator.getInstance().end();
RenderSystem.setShaderColor(0.0F, 0.0F, 0.0F, 0.3F);
RenderSystem.depthMask(true);
RenderSystem.enableTexture();
RenderSystem.disableBlend();
//RenderSystem.enableAlphaTest();
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
}
// public void drawSelectionBox(PoseStack matrixStackIn, Player player, float particleTicks, AABB boundingBox) {
// CraftTracker.LOGGER.debug("#drawSelectionBox: {}, player: {}", matrixStackIn, player);
//
// RenderSystem.setShader(GameRenderer::getPositionTexShader);
// RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
// // RenderSystem.disableAlphaTest();
// RenderSystem.depthMask(false);
// RenderSystem.enableBlend();
// RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
// RenderSystem.setShaderColor(0.0F, 0.0F, 0.0F, 0.7F);
// //TODO Used when drawing outline of bounding box
// RenderSystem.lineWidth(2.0F);
//
//
// RenderSystem.disableTexture();
// Vec3 vec3d = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();
// double d0 = vec3d.x();
// double d1 = vec3d.y();
// double d2 = vec3d.z();
//
// BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder();
// bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
// LevelRenderer.renderLineBox(matrixStackIn, bufferbuilder, boundingBox.move(-d0, -d1, -d2), 1F, 1F, 0F, 0.8F);
// Tesselator.getInstance().end();
// RenderSystem.setShaderColor(0.0F, 0.0F, 0.0F, 0.3F);
// RenderSystem.depthMask(true);
// RenderSystem.enableTexture();
// RenderSystem.disableBlend();
// //RenderSystem.enableAlphaTest();
// RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ public static void removeProduct(ResourceLocation itemId, int quantity) {

CraftingQueueManager.INSTANCE.removeProduct(player, itemId, quantity);
}

public static void handleAddToQueue() {

}

public static void handleToggleCraftOverlay() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class CraftQueueOverlay {
case SUPPRESS:
return;

case DO_NOT_CARE:
case DYNAMIC:
if(ConfigHandler.CLIENT.CRAFT_QUEUE_OVERLAY_HIDE_EMPTY.get() &&
products.isEmpty()) {
return;
Expand Down Expand Up @@ -252,7 +252,7 @@ public class CraftQueueOverlay {
int playerHasQuantity = 0;
if(inventory.contains(stack)) {
playerHasQuantity = inventory.items.stream()
.filter(inv -> inv.getItem().getRegistryName().equals(m.getItemId()))
.filter(inv -> inv.getItem().getRegistryName().equals(f.getItemId()))
.map(inv -> inv.getCount())
.findFirst()
.orElse(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.sweetrpg.crafttracker.common.lib;

import com.sweetrpg.crafttracker.common.config.ConfigHandler;

public class CTRuntime {

public static CTRuntime INSTANCE = new CTRuntime();
Expand All @@ -10,7 +8,7 @@ public enum OverlayState {
SHOW,
HIDE,
SUPPRESS,
DO_NOT_CARE,
DYNAMIC,
}

public OverlayState queueOverlayRequestedState;
Expand All @@ -21,7 +19,7 @@ public CTRuntime() {
}

private void init() {
this.queueOverlayRequestedState = OverlayState.DO_NOT_CARE;
this.shoppingOverlayRequestedState = OverlayState.DO_NOT_CARE;
this.queueOverlayRequestedState = OverlayState.DYNAMIC;
this.shoppingOverlayRequestedState = OverlayState.DYNAMIC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ public class ModKeyBindings {
public static final String KEY_TOGGLE_CRAFT_LIST = "key.toggleCraftList";
public static final String KEY_TOGGLE_SHOPPING_LIST = "key.toggleShoppingList";
public static final String KEY_OPEN_QUEUE_MANAGER = "key.openQueueManager";
public static final String KEY_POPULATE_SHOPPING_LIST = "key.populateShoppingList";

public static final KeyMapping ADD_TO_QUEUE_MAPPING = new KeyMapping(KEY_ADD_TO_QUEUE, KeyConflictContext.GUI, InputConstants.Type.KEYSYM, InputConstants.KEY_Q, KEY_CATEGORIES_CRAFT_TRACKER);
public static final KeyMapping TOGGLE_CRAFT_LIST_MAPPING = new KeyMapping(KEY_TOGGLE_CRAFT_LIST, KeyConflictContext.GUI, InputConstants.Type.KEYSYM, InputConstants.KEY_RSHIFT | InputConstants.KEY_L, KEY_CATEGORIES_CRAFT_TRACKER);
public static final KeyMapping TOGGLE_SHOPPING_LIST_MAPPING = new KeyMapping(KEY_TOGGLE_SHOPPING_LIST, KeyConflictContext.GUI, InputConstants.Type.KEYSYM, InputConstants.KEY_RSHIFT | InputConstants.KEY_S, KEY_CATEGORIES_CRAFT_TRACKER);
public static final KeyMapping OPEN_QUEUE_MANAGER_MAPPING = new KeyMapping(KEY_OPEN_QUEUE_MANAGER, KeyConflictContext.GUI, InputConstants.Type.KEYSYM, InputConstants.KEY_RSHIFT | InputConstants.KEY_M, KEY_CATEGORIES_CRAFT_TRACKER);
public static final KeyMapping POPULATE_SHOPPING_LIST_MAPPING = new KeyMapping(KEY_POPULATE_SHOPPING_LIST, KeyConflictContext.GUI, InputConstants.Type.KEYSYM, InputConstants.KEY_RSHIFT | InputConstants.KEY_P, KEY_CATEGORIES_CRAFT_TRACKER);

public static void init() {
ClientRegistry.registerKeyBinding(ADD_TO_QUEUE_MAPPING);
ClientRegistry.registerKeyBinding(TOGGLE_CRAFT_LIST_MAPPING);
ClientRegistry.registerKeyBinding(TOGGLE_SHOPPING_LIST_MAPPING);
ClientRegistry.registerKeyBinding(OPEN_QUEUE_MANAGER_MAPPING);
ClientRegistry.registerKeyBinding(POPULATE_SHOPPING_LIST_MAPPING);
}
}

0 comments on commit b339b94

Please sign in to comment.