Skip to content

Commit

Permalink
Almost working
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyhedral committed Nov 18, 2024
1 parent a97eb09 commit be976d7
Show file tree
Hide file tree
Showing 23 changed files with 573 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.sweetrpg.crafttracker.CraftTracker;
import com.sweetrpg.crafttracker.client.screen.widget.SmallButton;
import com.sweetrpg.crafttracker.common.network.PacketHandler;
import com.sweetrpg.crafttracker.common.network.packet.data.AddToQueueData;
import com.sweetrpg.crafttracker.common.network.packet.data.DisplayCraftListData;
import com.sweetrpg.crafttracker.common.network.packet.data.DisplayShoppingListData;
import com.sweetrpg.crafttracker.common.network.packet.data.ToggleCraftListData;
import com.sweetrpg.crafttracker.common.network.packet.data.ToggleShoppingListData;
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.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
Expand All @@ -26,6 +28,9 @@

public class ClientEventHandler {

// static boolean craftListDisplayed = true;
// static boolean shoppingListDisplayed = true;

// public static void onModelBakeEvent(final ModelBakeEvent event) {
// Map<ResourceLocation, BakedModel> modelRegistry = event.getModelRegistry();
//
Expand Down Expand Up @@ -54,30 +59,40 @@ public class ClientEventHandler {

// @SubscribeEvent
public static void onKeyInput(final InputEvent.KeyInputEvent event) {
CraftTracker.LOGGER.debug("#onKeyInput: {}", event);
CraftTracker.LOGGER.trace("#onKeyInput: {}", event);

if(ModKeyBindings.ADD_TO_QUEUE_MAPPING.consumeClick()) {
if(ModKeyBindings.ADD_TO_QUEUE_MAPPING.matches(event.getKey(), event.getScanCode())) {
CraftTracker.LOGGER.debug("#onKeyInput: ADD_TO_QUEUE_MAPPING");
PacketHandler.send(PacketDistributor.SERVER.noArg(), new AddToQueueData("TODO"));
}
else if(ModKeyBindings.TOGGLE_CRAFT_LIST_MAPPING.consumeClick()) {
PacketHandler.send(PacketDistributor.SERVER.noArg(), new DisplayCraftListData(true));
else if(ModKeyBindings.TOGGLE_CRAFT_LIST_MAPPING.matches(event.getKey(), event.getScanCode())) {
CraftTracker.LOGGER.debug("#onKeyInput: TOGGLE_CRAFT_LIST_MAPPING");
// craftListDisplayed = !craftListDisplayed;
PacketHandler.send(PacketDistributor.SERVER.noArg(), new ToggleCraftListData());
}
else if(ModKeyBindings.TOGGLE_SHOPPING_LIST_MAPPING.consumeClick()) {
PacketHandler.send(PacketDistributor.SERVER.noArg(), new DisplayShoppingListData(true));
else if(ModKeyBindings.TOGGLE_SHOPPING_LIST_MAPPING.matches(event.getKey(), event.getScanCode())) {
CraftTracker.LOGGER.debug("#onKeyInput: TOGGLE_SHOPPING_LIST_MAPPING");
// shoppingListDisplayed = !shoppingListDisplayed;
PacketHandler.send(PacketDistributor.SERVER.noArg(), new ToggleShoppingListData());
}
}

@SubscribeEvent
public void onInputEvent(final MovementInputUpdateEvent event) {
CraftTracker.LOGGER.trace("#onInputEvent: {}", event);

}

@SubscribeEvent
public void onScreenInit(final ScreenEvent.InitScreenEvent.Post event) {
CraftTracker.LOGGER.trace("#onScreenInit: {}", event);

Screen screen = event.getScreen();
if(screen instanceof InventoryScreen || screen instanceof CreativeModeInventoryScreen) {
boolean creative = screen instanceof CreativeModeInventoryScreen;
// boolean dtLoaded = ModList.get().isLoaded("doggytalents");
// CraftTracker.LOGGER.debug("#onScreenInit: creative {}", creative);

// boolean dtLoaded = ModList.get().isLoaded("doggytalents");
Minecraft mc = Minecraft.getInstance();
int width = mc.getWindow().getGuiScaledWidth();
int height = mc.getWindow().getGuiScaledHeight();
Expand All @@ -89,18 +104,23 @@ public void onScreenInit(final ScreenEvent.InitScreenEvent.Post event) {
int x = guiLeft + (creative ? 36 : sizeX / 2 - 10);
int y = guiTop + (creative ? 7 : 48);

// event.addListener(new Button(x, y, screen, (btn) -> {
// PacketHandler.send(PacketDistributor.SERVER.noArg(), new OpenCatScreenData());
event.addListener(new SmallButton(x, y, new TranslatableComponent("X"), (btn) -> {
CraftTracker.LOGGER.debug("#onScreenInit: SMALL BUTTON PRESSED {}", btn);
PacketHandler.send(PacketDistributor.SERVER.noArg(), new AddToQueueData("TODO"));
//// btn.active = false;
// }));
}));
}
}

@SubscribeEvent
public void onScreenDrawForeground(final ScreenEvent.DrawScreenEvent event) {
CraftTracker.LOGGER.trace("#onScreenDrawForeground: {}", event);

Screen screen = event.getScreen();
if(screen instanceof InventoryScreen || screen instanceof CreativeModeInventoryScreen) {
boolean creative = screen instanceof CreativeModeInventoryScreen;
// CraftTracker.LOGGER.debug("#onScreenInit: creative {}", creative);

// CatInventoryButton btn = null;
//
// //TODO just create a static variable in this class
Expand Down Expand Up @@ -134,6 +154,8 @@ 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public SmallButton(int x, int y, Component text, OnPress onPress) {

@Override
public void renderButton(PoseStack stack, int mouseX, int mouseY, float partialTicks) {
Minecraft mc = Minecraft.getInstance();
Font font = mc.font;
Minecraft mc = Minecraft.getInstance();
Font font = mc.font;
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, this.alpha);
RenderSystem.setShaderTexture(0, Resources.SMALL_WIDGETS);
int i = this.getYImage(this.isHoveredOrFocused());
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
this.blit(stack, this.x, this.y, 0, i * 12, this.width, this.height);
this.renderBg(stack, mc, mouseX, mouseY);
int j = getFGColor();
this.drawCenteredString(stack, font, this.getMessage(), this.x + this.width / 2, this.y + (this.height - 8) / 2, j | Mth.ceil(this.alpha * 255.0F) << 24);
int i = this.getYImage(this.isHoveredOrFocused());
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
this.blit(stack, this.x, this.y, 0, i * 12, this.width, this.height);
this.renderBg(stack, mc, mouseX, mouseY);
int j = getFGColor();
this.drawCenteredString(stack, font, this.getMessage(), this.x + this.width / 2, this.y + (this.height - 8) / 2, j | Mth.ceil(this.alpha * 255.0F) << 24);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/sweetrpg/crafttracker/common/Screens.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ public class Screens {
// }
// }

public static void updateCraftQueue(ServerPlayer player) {
// TODO: display if hidden
// redraw list of items
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package com.sweetrpg.crafttracker.common.addon.jei;

import com.sweetrpg.crafttracker.CraftTracker;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.constants.ModIds;
import mezz.jei.api.registration.IAdvancedRegistration;
import mezz.jei.api.registration.IGuiHandlerRegistration;
import mezz.jei.api.registration.IRecipeRegistration;
import mezz.jei.api.registration.ISubtypeRegistration;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.resources.ResourceLocation;

@JeiPlugin
public class CTPlugin implements IModPlugin {

public static IJeiRuntime jeiRuntime;

@Override
public ResourceLocation getPluginUid() {
return new ResourceLocation(ModIds.JEI_ID, "crafttracker");
}

@Override
public void registerItemSubtypes(ISubtypeRegistration registration) {
CraftTracker.LOGGER.debug("CTPlugin#registerItemSubtypes: {}", registration);

// registration.registerSubtypeInterpreter(ModBlocks.CAT_TREE.get().asItem(), (stack, ctx) -> {
// IColorMaterial colorMaterial = CatTreeUtil.getColorMaterial(stack);
//
Expand All @@ -29,7 +37,29 @@ public void registerItemSubtypes(ISubtypeRegistration registration) {

@Override
public void registerRecipes(IRecipeRegistration registration) {
// registration.addRecipes(CatTreeRecipeMaker.createCatTreeRecipes(), RecipeTypes.CRAFTING.getUid());
CraftTracker.LOGGER.debug("CTPlugin#registerRecipes: {}", registration);

// registration.addRecipes(CatTreeRecipeMaker.createCatTreeRecipes(), RecipeTypes.CRAFTING.getUid());
// registration.addRecipes(PetDoorRecipeMaker.createPetDoorRecipes(), RecipeTypes.CRAFTING.getUid());
}

@Override
public void registerGuiHandlers(IGuiHandlerRegistration registration) {
CraftTracker.LOGGER.debug("CTPlugin#registerGuiHandlers: {}", registration);

}

@Override
public void registerAdvanced(IAdvancedRegistration registration) {
CraftTracker.LOGGER.debug("CTPlugin#registerAdvanced: {}", registration);

registration.getJeiHelpers().getGuiHelper().createCraftingGridHelper(0);
}

@Override
public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
CraftTracker.LOGGER.debug("CTPlugin#onRuntimeAvailable: {}", jeiRuntime);

CTPlugin.jeiRuntime = jeiRuntime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class Constants {
public static final ResourceLocation CHANNEL_NAME = Util.getResource("channel");
public static final String PROTOCOL_VERSION = Integer.toString(1);

// Storage
public static final String STORAGE_CRAFTING_QUEUE = "crafting_queue";
public static final String STORAGE_SHOPPING_LIST = "shopping_list";

// Language
public static final String LOCALE_EN_US = "en_us";
public static final String LOCALE_EN_GB = "en_gb";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.sweetrpg.crafttracker.common.manager;

import com.sweetrpg.crafttracker.CraftTracker;
import com.sweetrpg.crafttracker.common.storage.CraftingQueueStorage;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraftforge.common.crafting.conditions.ICondition;
import org.antlr.v4.misc.OrderedHashMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CraftingQueueManager {

public static CraftingQueueManager INSTANCE = new CraftingQueueManager();

private Map<ResourceLocation, Integer> endProducts = new OrderedHashMap<>();
private Map<ResourceLocation, Integer> intermediateProducts = new OrderedHashMap<>();
private Map<ResourceLocation, Integer> rawMaterials = new HashMap<>();

private CraftingQueueStorage storage;

public CraftingQueueManager() {
this.storage = CraftingQueueStorage.get(Minecraft.getInstance().level);
}

public List<QueueItem> getEndProducts() {
return endProducts.entrySet()
.stream()
.map((e) -> new QueueItem(e.getKey(), e.getValue()))
.collect(Collectors.toUnmodifiableList());
}

public List<QueueItem> getIntermediates() {
return intermediateProducts.entrySet()
.stream()
.map((e) -> new QueueItem(e.getKey(), e.getValue()))
.collect(Collectors.toUnmodifiableList());
}

public List<QueueItem> getRawMaterials() {
return rawMaterials.entrySet()
.stream()
.map((e) -> new QueueItem(e.getKey(), e.getValue()))
.collect(Collectors.toUnmodifiableList());
}

public void addProduct(ResourceLocation itemId, int quantity) {
CraftTracker.LOGGER.debug("#addProduct: {}, quantity: {}", itemId, quantity);

RecipeManager rm = new RecipeManager(ICondition.IContext.EMPTY);

rm.byKey(itemId).ifPresentOrElse(r -> {
this.endProducts.compute(itemId, (k, v) -> {
if(v == null) {
return quantity;
}

return v + quantity;
});

var playerId = Minecraft.getInstance().player.getUUID();
this.storage.getData(playerId).addItem(itemId, quantity);
},
() -> {
// should not have gotten here, since #addProduct should have filtered out the item
// since it had to ingredients
CraftTracker.LOGGER.warn("#computeAll: no recipe found for {}", itemId);
});

computeAll();
}

public void removeProduct(ResourceLocation itemId, int quantity) {

}

public void computeAll() {
Map<ResourceLocation, Integer> intermediateProducts = new OrderedHashMap<>();
Map<ResourceLocation, Integer> rawMaterials = new HashMap<>();

RecipeManager rm = new RecipeManager(ICondition.IContext.EMPTY);

this.endProducts.forEach((k, v) -> {
rm.byKey(k).ifPresentOrElse(r -> {
var ingredients = r.getIngredients();

},
() -> {
// should not have gotten here, since #addProduct should have filtered out the item
// since it had to ingredients
CraftTracker.LOGGER.warn("#computeAll: no recipe found for {}", k);
});

});

this.intermediateProducts = intermediateProducts;
this.rawMaterials = rawMaterials;
}

public void computeIntermediates() {

}

public class QueueItem {
private ResourceLocation itemId;
private int quantity;

public QueueItem(ResourceLocation itemId, int quantity) {
this.itemId = itemId;
this.quantity = quantity;
}

public ResourceLocation getItemId() {
return itemId;
}

public void setItemId(ResourceLocation itemId) {
this.itemId = itemId;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.sweetrpg.crafttracker.common.manager;

public class ShoppingListManager {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.sweetrpg.crafttracker.CraftTracker;
import com.sweetrpg.crafttracker.common.network.packet.AddToQueuePacket;
import com.sweetrpg.crafttracker.common.network.packet.ToggleCraftListPacket;
import com.sweetrpg.crafttracker.common.network.packet.ToggleShoppingListPacket;
import com.sweetrpg.crafttracker.common.network.packet.data.AddToQueueData;
import com.sweetrpg.crafttracker.common.network.packet.data.ToggleCraftListData;
import com.sweetrpg.crafttracker.common.network.packet.data.ToggleShoppingListData;
import net.minecraftforge.network.PacketDistributor;

public final class PacketHandler {
Expand All @@ -11,6 +15,8 @@ public final class PacketHandler {

public static void init() {
registerPacket(new AddToQueuePacket(), AddToQueueData.class);
registerPacket(new ToggleCraftListPacket(), ToggleCraftListData.class);
registerPacket(new ToggleShoppingListPacket(), ToggleShoppingListData.class);
// registerPacket(new CatNamePacket(), CatNameData.class);
// registerPacket(new CatObeyPacket(), CatObeyData.class);
// registerPacket(new CatTalentPacket(), CatTalentData.class);
Expand Down
Loading

0 comments on commit be976d7

Please sign in to comment.