Advertisement
Guest User

Untitled

a guest
Jan 6th, 2025
34
0
22 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.61 KB | None | 0 0
  1. package com.gamergaming.taczweaponblueprints.mixin;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import org.spongepowered.asm.mixin.Final;
  9. import org.spongepowered.asm.mixin.Mixin;
  10. import org.spongepowered.asm.mixin.Shadow;
  11. import org.spongepowered.asm.mixin.injection.Redirect;
  12. import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
  13. import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
  14.  
  15. import com.gamergaming.taczweaponblueprints.TaCZWeaponBlueprints;
  16. import com.tacz.guns.GunMod;
  17. import com.tacz.guns.api.TimelessAPI;
  18. import com.tacz.guns.client.gui.GunSmithTableScreen;
  19. import com.tacz.guns.client.gui.components.smith.ResultButton;
  20. import com.tacz.guns.client.gui.components.smith.TypeButton;
  21. import com.tacz.guns.crafting.GunSmithTableRecipe;
  22. import com.tacz.guns.init.ModCreativeTabs;
  23. import com.tacz.guns.inventory.GunSmithTableMenu;
  24. import com.tacz.guns.resource.pojo.AttachmentIndexPOJO;
  25. import com.tacz.guns.resource.pojo.GunIndexPOJO;
  26.  
  27. import net.minecraft.client.Minecraft;
  28. import net.minecraft.client.gui.Font;
  29. import net.minecraft.client.gui.GuiGraphics;
  30. import net.minecraft.client.player.LocalPlayer;
  31. import net.minecraft.core.registries.BuiltInRegistries;
  32. import net.minecraft.network.chat.Component;
  33. import net.minecraft.resources.ResourceLocation;
  34. import net.minecraft.world.entity.player.Inventory;
  35. import net.minecraft.world.item.CreativeModeTab;
  36. import net.minecraft.world.item.ItemStack;
  37. import net.minecraftforge.registries.RegistryObject;
  38.  
  39. import org.spongepowered.asm.mixin.injection.At;
  40. import org.spongepowered.asm.mixin.injection.Inject;
  41.  
  42. @Mixin(GunSmithTableScreen.class)
  43. public class GunSmithTableScreenMixin {
  44.  
  45. @Shadow
  46. @Final
  47. private Map<String, List<ResourceLocation>> recipes;
  48.  
  49. @Shadow
  50. @Final
  51. private List<String> recipeKeys;
  52.  
  53. @Shadow
  54. private List<ResourceLocation> selectedRecipeList;
  55.  
  56. @Shadow
  57. private GunSmithTableRecipe selectedRecipe;
  58.  
  59. @Shadow
  60. private String selectedType;
  61.  
  62. @Shadow
  63. private int typePage;
  64.  
  65. @Shadow
  66. private int indexPage;
  67.  
  68. @Shadow
  69. private void init() {}
  70.  
  71. @Shadow
  72. private void getPlayerIngredientCount(GunSmithTableRecipe recipe) {}
  73.  
  74. @Shadow
  75. private void putRecipeType(RegistryObject<CreativeModeTab> tab) {}
  76.  
  77. @Shadow
  78. private GunSmithTableRecipe getSelectedRecipe(ResourceLocation recipeId) {
  79. return null;
  80. }
  81.  
  82.  
  83. @Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Ljava/util/List;get(I)Ljava/lang/Object;", ordinal = 0))
  84. private Object redirectSelectedRecipeListGetInit(List<ResourceLocation> list, int index) {
  85. if (list == null) {
  86. return null;
  87. } else if (list == this.selectedRecipeList && index == 0) {
  88. if (list.isEmpty()) {
  89. return null;
  90. } else {
  91. return list.get(index);
  92. }
  93. } else {
  94. return list.get(index);
  95. }
  96. }
  97.  
  98. @Inject(method = "<init>", at = @At("TAIL"), remap = false)
  99. private void onInit(GunSmithTableMenu menu, Inventory inventory, Component title, CallbackInfo ci) {
  100. // Ensure recipes and recipeKeys are initialized
  101. if (this.recipes == null) {
  102. this.recipes = new HashMap<>();
  103. }
  104. if (this.recipeKeys == null) {
  105. this.recipeKeys = new ArrayList<>();
  106. }
  107. }
  108.  
  109. @Inject(method = "classifyRecipes", at = @At("HEAD"), cancellable = true, remap = false)
  110. private void onClassifyRecipes(CallbackInfo ci) {
  111. // Initialize recipes and recipeKeys if they are null
  112. if (this.recipes == null) {
  113. this.recipes = new HashMap<>();
  114. }
  115. if (this.recipeKeys == null) {
  116. this.recipeKeys = new ArrayList<>();
  117. }
  118.  
  119. putRecipeType(ModCreativeTabs.AMMO_TAB);
  120. putRecipeType(ModCreativeTabs.ATTACHMENT_EXTENDED_MAG_TAB);
  121. putRecipeType(ModCreativeTabs.ATTACHMENT_SCOPE_TAB);
  122. putRecipeType(ModCreativeTabs.ATTACHMENT_MUZZLE_TAB);
  123. putRecipeType(ModCreativeTabs.ATTACHMENT_STOCK_TAB);
  124. putRecipeType(ModCreativeTabs.ATTACHMENT_GRIP_TAB);
  125. putRecipeType(ModCreativeTabs.GUN_PISTOL_TAB);
  126. putRecipeType(ModCreativeTabs.GUN_SNIPER_TAB);
  127. putRecipeType(ModCreativeTabs.GUN_RIFLE_TAB);
  128. putRecipeType(ModCreativeTabs.GUN_SHOTGUN_TAB);
  129. putRecipeType(ModCreativeTabs.GUN_SMG_TAB);
  130. putRecipeType(ModCreativeTabs.GUN_RPG_TAB);
  131. putRecipeType(ModCreativeTabs.GUN_MG_TAB);
  132.  
  133. for (String key : this.recipeKeys) {
  134. this.recipes.putIfAbsent(key, new ArrayList<>());
  135. }
  136.  
  137. TimelessAPI.getAllRecipes().forEach((id, recipe) -> {
  138. final String[] groupNameHolder = { recipe.getResult().getGroup() };
  139. LocalPlayer player = Minecraft.getInstance().player;
  140. if (groupNameHolder[0] == null || groupNameHolder[0].isEmpty()) {
  141. String kind = id.toString().split(":")[1];
  142. kind = kind.split("/")[0];
  143. switch (kind) {
  144. case "ammo":
  145. groupNameHolder[0] = "ammo";
  146. break;
  147. case "attachment":
  148. TimelessAPI.getCommonAttachmentIndex(new ResourceLocation(id.toString().split(":")[0] + ":" + id.toString().split("/")[1])).ifPresent(attachmentIndex -> {
  149. AttachmentIndexPOJO pojo = attachmentIndex.getPojo();
  150. groupNameHolder[0] = pojo.getType().name();
  151. });
  152. break;
  153. case "gun":
  154. TimelessAPI.getCommonGunIndex(new ResourceLocation(id.toString().split(":")[0] + ":" + id.toString().split("/")[1])).ifPresent(gunIndex -> {
  155. GunIndexPOJO pojo = gunIndex.getPojo();
  156. groupNameHolder[0] = pojo.getType();
  157. });
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. String groupName = groupNameHolder[0];
  164.  
  165. if (this.recipeKeys.contains(groupName)) {
  166. recipes.computeIfAbsent(groupName, g -> new ArrayList<>()).add(id);
  167. } else {
  168. TaCZWeaponBlueprints.LOGGER.warn("Group name " + groupName + " not found in recipeKeys: {}", this.recipeKeys);
  169. }
  170. });
  171.  
  172. ci.cancel();
  173. }
  174.  
  175. @Inject(method = "addIndexButtons", at = @At("HEAD"), cancellable = true, remap = false)
  176. private void onAddIndexButtons(CallbackInfo ci) {
  177. if (selectedRecipeList == null || selectedRecipeList.isEmpty()) {
  178. // No recipes to display, perhaps add a message or simply return
  179. return;
  180. }
  181. for (int i = 0; i < 6; i++) {
  182. int finalIndex = i + indexPage * 6;
  183. if (finalIndex >= selectedRecipeList.size()) {
  184. break;
  185. }
  186. int yOffset = ((IAbstractContainerScreenAccessor) this).getTopPos() + 66 + 17 * i;
  187. ResourceLocation recipeId = selectedRecipeList.get(finalIndex);
  188. GunSmithTableRecipe recipe = getSelectedRecipe(recipeId);
  189. if (recipe != null) {
  190. ResultButton button = new ResultButton(((IAbstractContainerScreenAccessor) this).getLeftPos() + 144, yOffset, recipe.getOutput(), b -> {
  191. this.selectedRecipe = recipe;
  192. this.getPlayerIngredientCount(this.selectedRecipe);
  193. this.init();
  194. });
  195. if (this.selectedRecipe != null && recipe.getId().equals(this.selectedRecipe.getId())) {
  196. button.setSelected(true);
  197. }
  198. ((IScreenAccessor) this).addRenderableWidget(button);
  199. }
  200. }
  201. ci.cancel();
  202. }
  203.  
  204. @Inject(method = "addTypeButtons", at = @At("HEAD"), cancellable = true, remap = false)
  205. private void onAddTypeButtons(CallbackInfo ci) {
  206. for (int i = 0; i < 7; i++) {
  207. int typeIndex = typePage * 7 + i;
  208. if (typeIndex >= recipeKeys.size()) {
  209. break;
  210. }
  211. String type = recipeKeys.get(typeIndex);
  212. int xOffset = ((IAbstractContainerScreenAccessor) this).getLeftPos() + 157 + 24 * i;
  213.  
  214. ItemStack icon = ItemStack.EMPTY;
  215. ResourceLocation tabId = new ResourceLocation(GunMod.MOD_ID, type);
  216. CreativeModeTab modTab = BuiltInRegistries.CREATIVE_MODE_TAB.get(tabId);
  217. if (modTab != null) {
  218. icon = modTab.getIconItem();
  219. }
  220.  
  221. TypeButton typeButton = new TypeButton(xOffset, ((IAbstractContainerScreenAccessor) this).getTopPos() + 2, icon, b -> {
  222. this.selectedType = type;
  223. this.selectedRecipeList = recipes.get(type);
  224. this.indexPage = 0;
  225. if (this.selectedRecipeList != null && !this.selectedRecipeList.isEmpty()) {
  226. this.selectedRecipe = getSelectedRecipe(this.selectedRecipeList.get(0));
  227. this.getPlayerIngredientCount(this.selectedRecipe);
  228. } else {
  229. this.selectedRecipe = null;
  230. }
  231. this.init();
  232. });
  233. if (this.selectedType.equals(type)) {
  234. typeButton.setSelected(true);
  235. }
  236. ((IScreenAccessor) this).addRenderableWidget(typeButton);
  237. }
  238. ci.cancel();
  239. }
  240.  
  241. @Inject(method = "updateIngredientCount", at = @At("TAIL"), remap = false)
  242. private void onUpdateIngredientCount(CallbackInfo ci) {
  243. this.init();
  244. }
  245.  
  246. @Inject(method = "getPlayerIngredientCount", at = @At("HEAD"), cancellable = true, remap = false)
  247. private void onGetPlayerIngredientCount(GunSmithTableRecipe recipe, CallbackInfo ci) {
  248. if (recipe == null) {
  249. ci.cancel();
  250. }
  251. }
  252.  
  253. @Inject(method = "render", at = @At("TAIL"), remap = false)
  254. private void onRender(GuiGraphics graphics, int mouseX, int mouseY, float partialTick, CallbackInfo ci) {
  255. if (this.selectedRecipeList == null || this.selectedRecipeList.isEmpty()) {
  256. int xPos = ((IAbstractContainerScreenAccessor) this).getLeftPos() + 191;
  257. int yPos = ((IAbstractContainerScreenAccessor) this).getTopPos() + 105;
  258. Font font = ((IScreenAccessor) this).getFont();
  259. String line1 = "No recipes"; // Component.translatable("gui.tacz.gun_smith_table.no_recipes");
  260. String line2 = "available"; // Component.translatable("gui.tacz.gun_smith_table.available");
  261. graphics.drawCenteredString(font, line1, xPos, yPos, 0xFF5555);
  262. graphics.drawCenteredString(font, line2, xPos, yPos + font.lineHeight, 0xFF5555);
  263.  
  264. }
  265. }
  266.  
  267. @Inject(method = "getSelectedRecipe", at = @At("HEAD"), cancellable = true, remap = false)
  268. private void onGetSelectedRecipe(ResourceLocation recipeId, CallbackInfoReturnable<GunSmithTableRecipe> cir) {
  269. if (recipeId == null) {
  270. cir.setReturnValue(null);
  271. cir.cancel();
  272. }
  273. }
  274.  
  275. @Inject(method = "putRecipeType", at = @At("HEAD"), cancellable = true, remap = false)
  276. private void onPutRecipeType(RegistryObject<CreativeModeTab> tab, CallbackInfo ci) {
  277. String name = tab.getId().getPath();
  278. if (!this.recipeKeys.contains(name)) {
  279. this.recipeKeys.add(name);
  280. }
  281. ci.cancel();
  282. }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement