aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/xmod/gregtech/loaders/misc/AssLineAchievements.java
blob: 351a4cc5461639ce8fba30179211a512aa53a1d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package gtPlusPlus.xmod.gregtech.loaders.misc;

import java.util.concurrent.ConcurrentHashMap;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import gregtech.GT_Mod;
import gregtech.api.util.GT_Log;
import gregtech.api.util.GT_Recipe;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.lib.CORE;
import gtPlusPlus.core.util.Utils;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import gtPlusPlus.xmod.gregtech.common.StaticFields59;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.Achievement;
import net.minecraft.stats.AchievementList;
import net.minecraft.stats.StatBase;
import net.minecraftforge.common.AchievementPage;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;

public class AssLineAchievements {

	public static int assReg = -1;
	public static ConcurrentHashMap<String, Achievement> mAchievementMap;
	public static ConcurrentHashMap<String, Boolean> mIssuedAchievementMap;
	public static int adjX = 5;
	public static int adjY = 9;
	private static boolean active = true;

	public AssLineAchievements() {
		Logger.INFO(active ? "Loading custom achievement page for Assembly Line recipes."
				: "Achievements are disabled.");	
		Utils.registerEvent(this);
	}
	
	private static boolean ready = false;
	private static int recipeTotal = 0;
	private static int recipeCount = 0;
	private static void init() {		
		if (!ready) {
			active = GT_Mod.gregtechproxy.mAchievements;
			try {
				recipeTotal = ((GT_Recipe.GT_Recipe_Map) StaticFields59.mAssLineVisualMapNEI.get(null)).mRecipeList.size();
			} catch (IllegalArgumentException | IllegalAccessException e) {
				recipeTotal = 0;
			}
			mAchievementMap = new ConcurrentHashMap<String, Achievement>();
			mIssuedAchievementMap = new ConcurrentHashMap<String, Boolean>();	
			ready = true;
		}		
		
	}
	
	public static void registerAchievements() {		
		if (active && mAchievementMap.size() > 0) {
			AchievementPage.registerAchievementPage(new AchievementPage("GT Assembly Line",
					(Achievement[]) mAchievementMap.values().toArray(new Achievement[mAchievementMap.size()])));			
		}
		else if (active) {
			Logger.INFO("Unable to register custom achievement page for Assembly Line recipes.");
		}
	}

	public static Achievement registerAssAchievement(GT_Recipe recipe) {
		init();
		String aSafeUnlocalName;
		// Debugging
		if (recipe == null) {
			Logger.INFO(
					"Someone tried to register an achievement for an invalid recipe. Please report this to Alkalus.");
			return null;
		}
		if (recipe.getOutput(0) == null) {
			Logger.INFO(
					"Someone tried to register an achievement for a recipe with null output. Please report this to Alkalus.");
			return null;
		}
		ItemStack aStack = recipe.getOutput(0);
		try {
			aSafeUnlocalName = aStack.getUnlocalizedName();
		} catch (Throwable t) {
			aSafeUnlocalName = ItemUtils.getUnlocalizedItemName(aStack);
		}
		
		Achievement aYouDidSomethingInGT;
		if (mAchievementMap.get(aSafeUnlocalName) == null) {
			assReg++;
			recipeCount++;
			aYouDidSomethingInGT = registerAchievement(aSafeUnlocalName, -(11 + assReg % 5), ((assReg) / 5) - 8,
					recipe.getOutput(0), AchievementList.openInventory, false);
		}
		else {
			aYouDidSomethingInGT = null;
		}
		if (recipeCount >= recipeTotal) {
			Logger.INFO("Critical mass achieved. ["+recipeCount+"]");
			registerAchievements();
		}
		
		return aYouDidSomethingInGT;
	}

	public static Achievement registerAchievement(String textId, int x, int y, ItemStack icon,
			Achievement requirement, boolean special) {
		if (!GT_Mod.gregtechproxy.mAchievements) {
			return null;
		}
		Achievement achievement = new Achievement(textId, textId, adjX + x, adjY + y, icon, requirement);
		if (special) {
			achievement.setSpecial();
		}
		achievement.registerStat();
		if (CORE.DEVENV) {
			GT_Log.out.println("achievement." + textId + "=");
			GT_Log.out.println("achievement." + textId + ".desc=");
		}
		mAchievementMap.put(textId, achievement);
		return achievement;
	}

	public static void issueAchievement(EntityPlayer entityplayer, String textId) {
		if (entityplayer == null || !GT_Mod.gregtechproxy.mAchievements) {
			return;
		}
		
		entityplayer.triggerAchievement((StatBase) getAchievement(textId));
	}

	public static Achievement getAchievement(String textId) {
		if (mAchievementMap.containsKey(textId)) {
			Logger.INFO("Found Achivement: "+textId);
			return (Achievement) mAchievementMap.get(textId);
		}
		return null;
	}

	@SubscribeEvent
	public void onItemPickup(EntityItemPickupEvent event) {
		EntityPlayer player = event.entityPlayer;
		ItemStack stack = event.item.getEntityItem();
		String aPickupUnlocalSafe = ItemUtils.getUnlocalizedItemName(stack);
		if (player == null || stack == null) {
			return;
		}
		
		if (StaticFields59.sAssemblylineVisualRecipes == null) {
			return;
		}
		
		Logger.INFO("Trying to check for achievements");
		// Debug scanner unlocks all AL recipes in creative
		if (player.capabilities.isCreativeMode && aPickupUnlocalSafe.equals("gt.metaitem.01.32761")) {
			for (GT_Recipe recipe : StaticFields59.sAssemblylineVisualRecipes.mRecipeList) {
				issueAchievement(player, recipe.getOutput(0).getUnlocalizedName());
				recipe.mHidden = false;
			}
		}
		for (GT_Recipe recipe : StaticFields59.sAssemblylineVisualRecipes.mRecipeList) {
			
			String aSafeUnlocalName;
			if (recipe.getOutput(0) == null) {
				Logger.INFO(
						"Someone tried to register an achievement for a recipe with null output. Please report this to Alkalus.");
				continue;
			}
			ItemStack aStack = recipe.getOutput(0);
			aSafeUnlocalName = ItemUtils.getUnlocalizedItemName(aStack);			
			if (aSafeUnlocalName.equals(aPickupUnlocalSafe)) {
				issueAchievement(player, aSafeUnlocalName);
				recipe.mHidden = false;
				Logger.INFO("FOUND: " + aSafeUnlocalName + " | " + aPickupUnlocalSafe);
			}
			else {
				//Logger.INFO(aSafeUnlocalName + " | " + aPickupUnlocalSafe);
			}
		}
	}

}