Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gamergaming.taczweaponblueprints.mixin;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.spongepowered.asm.mixin.Final;
- import org.spongepowered.asm.mixin.Mixin;
- import org.spongepowered.asm.mixin.Shadow;
- import org.spongepowered.asm.mixin.injection.Redirect;
- import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
- import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
- import com.gamergaming.taczweaponblueprints.TaCZWeaponBlueprints;
- import com.tacz.guns.GunMod;
- import com.tacz.guns.api.TimelessAPI;
- import com.tacz.guns.client.gui.GunSmithTableScreen;
- import com.tacz.guns.client.gui.components.smith.ResultButton;
- import com.tacz.guns.client.gui.components.smith.TypeButton;
- import com.tacz.guns.crafting.GunSmithTableRecipe;
- import com.tacz.guns.init.ModCreativeTabs;
- import com.tacz.guns.inventory.GunSmithTableMenu;
- import com.tacz.guns.resource.pojo.AttachmentIndexPOJO;
- import com.tacz.guns.resource.pojo.GunIndexPOJO;
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.gui.Font;
- import net.minecraft.client.gui.GuiGraphics;
- import net.minecraft.client.player.LocalPlayer;
- import net.minecraft.core.registries.BuiltInRegistries;
- import net.minecraft.network.chat.Component;
- import net.minecraft.resources.ResourceLocation;
- import net.minecraft.world.entity.player.Inventory;
- import net.minecraft.world.item.CreativeModeTab;
- import net.minecraft.world.item.ItemStack;
- import net.minecraftforge.registries.RegistryObject;
- import org.spongepowered.asm.mixin.injection.At;
- import org.spongepowered.asm.mixin.injection.Inject;
- @Mixin(GunSmithTableScreen.class)
- public class GunSmithTableScreenMixin {
- @Shadow
- @Final
- private Map<String, List<ResourceLocation>> recipes;
- @Shadow
- @Final
- private List<String> recipeKeys;
- @Shadow
- private List<ResourceLocation> selectedRecipeList;
- @Shadow
- private GunSmithTableRecipe selectedRecipe;
- @Shadow
- private String selectedType;
- @Shadow
- private int typePage;
- @Shadow
- private int indexPage;
- @Shadow
- private void init() {}
- @Shadow
- private void getPlayerIngredientCount(GunSmithTableRecipe recipe) {}
- @Shadow
- private void putRecipeType(RegistryObject<CreativeModeTab> tab) {}
- @Shadow
- private GunSmithTableRecipe getSelectedRecipe(ResourceLocation recipeId) {
- return null;
- }
- @Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Ljava/util/List;get(I)Ljava/lang/Object;", ordinal = 0))
- private Object redirectSelectedRecipeListGetInit(List<ResourceLocation> list, int index) {
- if (list == null) {
- return null;
- } else if (list == this.selectedRecipeList && index == 0) {
- if (list.isEmpty()) {
- return null;
- } else {
- return list.get(index);
- }
- } else {
- return list.get(index);
- }
- }
- @Inject(method = "<init>", at = @At("TAIL"), remap = false)
- private void onInit(GunSmithTableMenu menu, Inventory inventory, Component title, CallbackInfo ci) {
- // Ensure recipes and recipeKeys are initialized
- if (this.recipes == null) {
- this.recipes = new HashMap<>();
- }
- if (this.recipeKeys == null) {
- this.recipeKeys = new ArrayList<>();
- }
- }
- @Inject(method = "classifyRecipes", at = @At("HEAD"), cancellable = true, remap = false)
- private void onClassifyRecipes(CallbackInfo ci) {
- // Initialize recipes and recipeKeys if they are null
- if (this.recipes == null) {
- this.recipes = new HashMap<>();
- }
- if (this.recipeKeys == null) {
- this.recipeKeys = new ArrayList<>();
- }
- putRecipeType(ModCreativeTabs.AMMO_TAB);
- putRecipeType(ModCreativeTabs.ATTACHMENT_EXTENDED_MAG_TAB);
- putRecipeType(ModCreativeTabs.ATTACHMENT_SCOPE_TAB);
- putRecipeType(ModCreativeTabs.ATTACHMENT_MUZZLE_TAB);
- putRecipeType(ModCreativeTabs.ATTACHMENT_STOCK_TAB);
- putRecipeType(ModCreativeTabs.ATTACHMENT_GRIP_TAB);
- putRecipeType(ModCreativeTabs.GUN_PISTOL_TAB);
- putRecipeType(ModCreativeTabs.GUN_SNIPER_TAB);
- putRecipeType(ModCreativeTabs.GUN_RIFLE_TAB);
- putRecipeType(ModCreativeTabs.GUN_SHOTGUN_TAB);
- putRecipeType(ModCreativeTabs.GUN_SMG_TAB);
- putRecipeType(ModCreativeTabs.GUN_RPG_TAB);
- putRecipeType(ModCreativeTabs.GUN_MG_TAB);
- for (String key : this.recipeKeys) {
- this.recipes.putIfAbsent(key, new ArrayList<>());
- }
- TimelessAPI.getAllRecipes().forEach((id, recipe) -> {
- final String[] groupNameHolder = { recipe.getResult().getGroup() };
- LocalPlayer player = Minecraft.getInstance().player;
- if (groupNameHolder[0] == null || groupNameHolder[0].isEmpty()) {
- String kind = id.toString().split(":")[1];
- kind = kind.split("/")[0];
- switch (kind) {
- case "ammo":
- groupNameHolder[0] = "ammo";
- break;
- case "attachment":
- TimelessAPI.getCommonAttachmentIndex(new ResourceLocation(id.toString().split(":")[0] + ":" + id.toString().split("/")[1])).ifPresent(attachmentIndex -> {
- AttachmentIndexPOJO pojo = attachmentIndex.getPojo();
- groupNameHolder[0] = pojo.getType().name();
- });
- break;
- case "gun":
- TimelessAPI.getCommonGunIndex(new ResourceLocation(id.toString().split(":")[0] + ":" + id.toString().split("/")[1])).ifPresent(gunIndex -> {
- GunIndexPOJO pojo = gunIndex.getPojo();
- groupNameHolder[0] = pojo.getType();
- });
- break;
- default:
- break;
- }
- }
- String groupName = groupNameHolder[0];
- if (this.recipeKeys.contains(groupName)) {
- recipes.computeIfAbsent(groupName, g -> new ArrayList<>()).add(id);
- } else {
- TaCZWeaponBlueprints.LOGGER.warn("Group name " + groupName + " not found in recipeKeys: {}", this.recipeKeys);
- }
- });
- ci.cancel();
- }
- @Inject(method = "addIndexButtons", at = @At("HEAD"), cancellable = true, remap = false)
- private void onAddIndexButtons(CallbackInfo ci) {
- if (selectedRecipeList == null || selectedRecipeList.isEmpty()) {
- // No recipes to display, perhaps add a message or simply return
- return;
- }
- for (int i = 0; i < 6; i++) {
- int finalIndex = i + indexPage * 6;
- if (finalIndex >= selectedRecipeList.size()) {
- break;
- }
- int yOffset = ((IAbstractContainerScreenAccessor) this).getTopPos() + 66 + 17 * i;
- ResourceLocation recipeId = selectedRecipeList.get(finalIndex);
- GunSmithTableRecipe recipe = getSelectedRecipe(recipeId);
- if (recipe != null) {
- ResultButton button = new ResultButton(((IAbstractContainerScreenAccessor) this).getLeftPos() + 144, yOffset, recipe.getOutput(), b -> {
- this.selectedRecipe = recipe;
- this.getPlayerIngredientCount(this.selectedRecipe);
- this.init();
- });
- if (this.selectedRecipe != null && recipe.getId().equals(this.selectedRecipe.getId())) {
- button.setSelected(true);
- }
- ((IScreenAccessor) this).addRenderableWidget(button);
- }
- }
- ci.cancel();
- }
- @Inject(method = "addTypeButtons", at = @At("HEAD"), cancellable = true, remap = false)
- private void onAddTypeButtons(CallbackInfo ci) {
- for (int i = 0; i < 7; i++) {
- int typeIndex = typePage * 7 + i;
- if (typeIndex >= recipeKeys.size()) {
- break;
- }
- String type = recipeKeys.get(typeIndex);
- int xOffset = ((IAbstractContainerScreenAccessor) this).getLeftPos() + 157 + 24 * i;
- ItemStack icon = ItemStack.EMPTY;
- ResourceLocation tabId = new ResourceLocation(GunMod.MOD_ID, type);
- CreativeModeTab modTab = BuiltInRegistries.CREATIVE_MODE_TAB.get(tabId);
- if (modTab != null) {
- icon = modTab.getIconItem();
- }
- TypeButton typeButton = new TypeButton(xOffset, ((IAbstractContainerScreenAccessor) this).getTopPos() + 2, icon, b -> {
- this.selectedType = type;
- this.selectedRecipeList = recipes.get(type);
- this.indexPage = 0;
- if (this.selectedRecipeList != null && !this.selectedRecipeList.isEmpty()) {
- this.selectedRecipe = getSelectedRecipe(this.selectedRecipeList.get(0));
- this.getPlayerIngredientCount(this.selectedRecipe);
- } else {
- this.selectedRecipe = null;
- }
- this.init();
- });
- if (this.selectedType.equals(type)) {
- typeButton.setSelected(true);
- }
- ((IScreenAccessor) this).addRenderableWidget(typeButton);
- }
- ci.cancel();
- }
- @Inject(method = "updateIngredientCount", at = @At("TAIL"), remap = false)
- private void onUpdateIngredientCount(CallbackInfo ci) {
- this.init();
- }
- @Inject(method = "getPlayerIngredientCount", at = @At("HEAD"), cancellable = true, remap = false)
- private void onGetPlayerIngredientCount(GunSmithTableRecipe recipe, CallbackInfo ci) {
- if (recipe == null) {
- ci.cancel();
- }
- }
- @Inject(method = "render", at = @At("TAIL"), remap = false)
- private void onRender(GuiGraphics graphics, int mouseX, int mouseY, float partialTick, CallbackInfo ci) {
- if (this.selectedRecipeList == null || this.selectedRecipeList.isEmpty()) {
- int xPos = ((IAbstractContainerScreenAccessor) this).getLeftPos() + 191;
- int yPos = ((IAbstractContainerScreenAccessor) this).getTopPos() + 105;
- Font font = ((IScreenAccessor) this).getFont();
- String line1 = "No recipes"; // Component.translatable("gui.tacz.gun_smith_table.no_recipes");
- String line2 = "available"; // Component.translatable("gui.tacz.gun_smith_table.available");
- graphics.drawCenteredString(font, line1, xPos, yPos, 0xFF5555);
- graphics.drawCenteredString(font, line2, xPos, yPos + font.lineHeight, 0xFF5555);
- }
- }
- @Inject(method = "getSelectedRecipe", at = @At("HEAD"), cancellable = true, remap = false)
- private void onGetSelectedRecipe(ResourceLocation recipeId, CallbackInfoReturnable<GunSmithTableRecipe> cir) {
- if (recipeId == null) {
- cir.setReturnValue(null);
- cir.cancel();
- }
- }
- @Inject(method = "putRecipeType", at = @At("HEAD"), cancellable = true, remap = false)
- private void onPutRecipeType(RegistryObject<CreativeModeTab> tab, CallbackInfo ci) {
- String name = tab.getId().getPath();
- if (!this.recipeKeys.contains(name)) {
- this.recipeKeys.add(name);
- }
- ci.cancel();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement