Guest User

Untitled

a guest
Aug 14th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.76 KB | None | 0 0
  1. package com.minecolonies.coremod.client.gui;
  2.  
  3. import com.minecolonies.api.util.LanguageHandler;
  4. import com.minecolonies.api.util.constant.Constants;
  5. import com.minecolonies.blockout.Alignment;
  6. import com.minecolonies.blockout.controls.Button;
  7. import com.minecolonies.blockout.controls.ButtonHandler;
  8. import com.minecolonies.blockout.controls.Image;
  9. import com.minecolonies.blockout.controls.Label;
  10. import com.minecolonies.blockout.views.View;
  11. import com.minecolonies.blockout.views.Window;
  12. import com.minecolonies.coremod.MineColonies;
  13. import com.minecolonies.coremod.colony.CitizenData;
  14. import com.minecolonies.coremod.colony.CitizenDataView;
  15. import com.minecolonies.coremod.network.messages.OpenInventoryMessage;
  16. import com.minecolonies.coremod.util.ExperienceUtils;
  17. import net.minecraft.client.gui.Gui;
  18. import net.minecraft.client.gui.ScaledResolution;
  19. import org.jetbrains.annotations.NotNull;
  20.  
  21. /**
  22. * Window for the citizen.
  23. */
  24. public class WindowCitizen extends Window implements ButtonHandler
  25. {
  26. /**
  27. * The label to find the inventory button.
  28. */
  29. private static final String INVENTORY_BUTTON_ID = "inventory";
  30. /**
  31. * The label to find the gui of the citizen.
  32. */
  33. private static final String CITIZEN_RESOURCE_SUFFIX = ":gui/windowcitizen.xml";
  34. /**
  35. * The label to find strength in the gui.
  36. */
  37. private static final String STRENGTH = "strength";
  38. /**
  39. * The label to find endurance in the gui.
  40. */
  41. private static final String ENDURANCE = "endurance";
  42. /**
  43. * The label to find charisma in the gui.
  44. */
  45. private static final String CHARISMA = "charisma";
  46. /**
  47. * The label to find intelligence in the gui.
  48. */
  49. private static final String INTELLIGENCE = "intelligence";
  50. /**
  51. * The label to find dexterity in the gui.
  52. */
  53. private static final String DEXTERITY = "dexterity";
  54.  
  55. /**
  56. * Xp-bar height.
  57. */
  58. private static final int XP_HEIGHT = 5;
  59. /**
  60. * The x-distance to the left border of the gui of the xpBar.
  61. */
  62. private static final int LEFT_BORDER_X = 10;
  63. /**
  64. * The y-distance to the top-left border of the gui of the xpBar.
  65. */
  66. private static final int LEFT_BORDER_Y = 10;
  67. /**
  68. * The column in which the icon starts.
  69. */
  70. private static final int XP_BAR_ICON_COLUMN = 0;
  71. /**
  72. * The column where the icon ends.
  73. */
  74. private static final int XP_BAR_ICON_COLUMN_END = 172;
  75. /**
  76. * The width of the end piece of the xpBar.
  77. */
  78. private static final int XP_BAR_ICON_COLUMN_END_WIDTH = 10;
  79. /**
  80. * The offset where the end should be placed in the GUI.
  81. */
  82. private static final int XP_BAR_ICON_END_OFFSET = 90;
  83. /**
  84. * The width of the xpBar (Original width is halved to fit in the gui).
  85. */
  86. private static final int XP_BAR_WIDTH = 182 / 2;
  87. /**
  88. * The row where the empty xpBar starts.
  89. */
  90. private static final int XP_BAR_EMPTY_ROW = 64;
  91. /**
  92. * The row where the full xpBar starts.
  93. */
  94. private static final int XP_BAR_FULL_ROW = 69;
  95. /**
  96. * X position of the xpLabel.
  97. */
  98. private static final int XP_LABEL_X = -20;
  99. /**
  100. * Y position of the xpLabel.
  101. */
  102. private static final int XP_LABEL_Y = 38;
  103.  
  104. /**
  105. * Row position of the empty heart icon.
  106. */
  107. private static final int EMPTY_HEART_ICON_ROW_POS = 16;
  108. /**
  109. * Row position of the full heart icon.
  110. */
  111. private static final int FULL_HEART_ICON_ROW_POS = 53;
  112. /**
  113. * Row position of the half/full heart icon.
  114. */
  115. private static final int HALF_HEART_ICON_ROW_POS = 62;
  116. /**
  117. * Column position of the heart icons.
  118. */
  119. private static final int HEART_ICON_COLUMN = 0;
  120. /**
  121. * Dimension of the hearts.
  122. */
  123. private static final int HEART_ICON_HEIGHT_WIDTH = 9;
  124. /**
  125. * The position x where the heart is placed.
  126. */
  127. private static final int HEART_ICON_POS_X = 10;
  128. /**
  129. * The offset x where the next heart should be placed.
  130. */
  131. private static final int HEART_ICON_OFFSET_X = 10;
  132. /**
  133. * The position y where the heart is placed.
  134. */
  135. private static final int HEART_ICON_POS_Y = 10;
  136.  
  137. /**
  138. * The position y where the saturation is placed.
  139. */
  140. private static final int SATURATION_ICON_POS_Y = 10;
  141.  
  142. /**
  143. * Column of the saturation icon.
  144. */
  145. private static final int SATURATION_ICON_COLUMN = 27;
  146.  
  147. /**
  148. * Dimension of the hearts.
  149. */
  150. private static final int SATURATION_ICON_HEIGHT_WIDTH = 9;
  151.  
  152. /**
  153. * Saturation icon x position.
  154. */
  155. private static final int SATURATION_ICON_POS_X = 10;
  156.  
  157. /**
  158. * Saturation item x offset.
  159. */
  160. private static final int SATURATION_ICON_OFFSET_X = 10;
  161.  
  162. /**
  163. * The label to find name in the gui.
  164. */
  165. private static final String WINDOW_ID_NAME = "name";
  166. /**
  167. * The label to find xpLabel in the gui.
  168. */
  169. private static final String WINDOW_ID_XP = "xpLabel";
  170. /**
  171. * The label to find xpBar in the gui.
  172. */
  173. private static final String WINDOW_ID_XPBAR = "xpBar";
  174. /**
  175. * The label to find healthBar in the gui.
  176. */
  177. private static final String WINDOW_ID_HEALTHBAR = "healthBar";
  178.  
  179. /**
  180. * The position of the empty saturation icon.
  181. */
  182. private static final int EMPTY_SATURATION_ITEM_ROW_POS = 16;
  183.  
  184. /**
  185. * The position of the full saturation icon.
  186. */
  187. private static final int FULL_SATURATION_ITEM_ROW_POS = 16 + 36;
  188.  
  189. /**
  190. * The position of the half saturation icon.
  191. */
  192. private static final int HALF_SATURATION_ITEM_ROW_POS = 16 + 45;
  193.  
  194. /**
  195. * The saturation bar of the citizen.
  196. */
  197. private static final String WINDOW_ID_SATURATION_BAR = "saturationBar";
  198.  
  199. /**
  200. * The citizenData.View object.
  201. */
  202. private final CitizenDataView citizen;
  203.  
  204. /**
  205. * Constructor to initiate the citizen windows.
  206. *
  207. * @param citizen citizen to bind the window to.
  208. */
  209. public WindowCitizen(final CitizenDataView citizen)
  210. {
  211. super(Constants.MOD_ID + CITIZEN_RESOURCE_SUFFIX);
  212. this.citizen = citizen;
  213. }
  214.  
  215. /**
  216. * Called when the gui is opened by an player.
  217. */
  218. @Override
  219. public void onOpened()
  220. {
  221. findPaneOfTypeByID(WINDOW_ID_NAME, Label.class).setLabelText(citizen.getName());
  222.  
  223.  
  224. createHealthBar();
  225. createSaturationBar();
  226. createXpBar();
  227. createSkillContent();
  228. }
  229.  
  230. /**
  231. * Creates an health bar according to the citizen maxHealth and currentHealth.
  232. */
  233. private void createHealthBar()
  234. {
  235. findPaneOfTypeByID(WINDOW_ID_HEALTHBAR, View.class).setAlignment(Alignment.MIDDLE_RIGHT);
  236. final ScaledResolution scaledresolution = new ScaledResolution(this.mc);
  237. int width = scaledresolution.getScaledWidth();
  238. int height = scaledresolution.getScaledHeight();
  239.  
  240. //MaxHealth (Black hearts).
  241. for (int i = 0; i < citizen.getMaxHealth() / 2; i++)
  242. {
  243. @NotNull final Image heart = new Image();
  244. heart.setImage(Gui.ICONS, EMPTY_HEART_ICON_ROW_POS, HEART_ICON_COLUMN, HEART_ICON_HEIGHT_WIDTH, HEART_ICON_HEIGHT_WIDTH);
  245. heart.setPosition(i * HEART_ICON_POS_X + HEART_ICON_OFFSET_X, HEART_ICON_POS_Y);
  246. findPaneOfTypeByID(WINDOW_ID_HEALTHBAR, View.class).addChild(heart);
  247. }
  248.  
  249. //Current health (Red hearts).
  250. int heartPos;
  251. for (heartPos = 0; heartPos < ((int) citizen.getHealth() / 2); heartPos++)
  252. {
  253. @NotNull final Image heart = new Image();
  254. heart.setImage(Gui.ICONS, FULL_HEART_ICON_ROW_POS, HEART_ICON_COLUMN, HEART_ICON_HEIGHT_WIDTH, HEART_ICON_HEIGHT_WIDTH);
  255. heart.setPosition(heartPos * HEART_ICON_POS_X + HEART_ICON_OFFSET_X, HEART_ICON_POS_Y);
  256. findPaneOfTypeByID(WINDOW_ID_HEALTHBAR, View.class).addChild(heart);
  257. }
  258.  
  259. //Half hearts.
  260. if (citizen.getHealth() / 2 % 1 > 0)
  261. {
  262. @NotNull final Image heart = new Image();
  263. heart.setImage(Gui.ICONS, HALF_HEART_ICON_ROW_POS, HEART_ICON_COLUMN, HEART_ICON_HEIGHT_WIDTH, HEART_ICON_HEIGHT_WIDTH);
  264. heart.setPosition(heartPos * HEART_ICON_POS_X + HEART_ICON_OFFSET_X, HEART_ICON_POS_Y);
  265. findPaneOfTypeByID(WINDOW_ID_HEALTHBAR, View.class).addChild(heart);
  266. }
  267. }
  268.  
  269. /**
  270. * Creates an health bar according to the citizen maxHealth and currentHealth.
  271. */
  272. private void createSaturationBar()
  273. {
  274. findPaneOfTypeByID(WINDOW_ID_SATURATION_BAR, View.class).setAlignment(Alignment.MIDDLE_RIGHT);
  275.  
  276. //Max saturation (Black food items).
  277. for (int i = 0; i < CitizenData.MAX_SATURATION; i++)
  278. {
  279. @NotNull final Image saturation = new Image();
  280. saturation.setImage(Gui.ICONS, EMPTY_SATURATION_ITEM_ROW_POS, SATURATION_ICON_COLUMN, SATURATION_ICON_HEIGHT_WIDTH, SATURATION_ICON_HEIGHT_WIDTH);
  281.  
  282. saturation.setPosition(i * SATURATION_ICON_POS_X + SATURATION_ICON_OFFSET_X, SATURATION_ICON_POS_Y);
  283. findPaneOfTypeByID(WINDOW_ID_SATURATION_BAR, View.class).addChild(saturation);
  284. }
  285.  
  286. //Current saturation (Full food hearts).
  287. int saturationPos;
  288. for (saturationPos = 0; saturationPos < ((int) citizen.getSaturation()); saturationPos++)
  289. {
  290. @NotNull final Image saturation = new Image();
  291. saturation.setImage(Gui.ICONS, FULL_SATURATION_ITEM_ROW_POS, SATURATION_ICON_COLUMN, SATURATION_ICON_HEIGHT_WIDTH, SATURATION_ICON_HEIGHT_WIDTH);
  292. saturation.setPosition(saturationPos * SATURATION_ICON_POS_X + SATURATION_ICON_OFFSET_X, SATURATION_ICON_POS_Y);
  293. findPaneOfTypeByID(WINDOW_ID_SATURATION_BAR, View.class).addChild(saturation);
  294. }
  295.  
  296. //Half food items.
  297. if (citizen.getSaturation() / 2 % 1 > 0)
  298. {
  299. @NotNull final Image saturation = new Image();
  300. saturation.setImage(Gui.ICONS, HALF_SATURATION_ITEM_ROW_POS, SATURATION_ICON_COLUMN, SATURATION_ICON_HEIGHT_WIDTH, SATURATION_ICON_HEIGHT_WIDTH);
  301. saturation.setPosition(saturationPos * SATURATION_ICON_POS_X + SATURATION_ICON_OFFSET_X, SATURATION_ICON_POS_Y);
  302. findPaneOfTypeByID(WINDOW_ID_SATURATION_BAR, View.class).addChild(saturation);
  303. }
  304. }
  305.  
  306. /**
  307. * Creates the xp bar for each citizen.
  308. * Calculates an xpBarCap which is the maximum of xp to fit into the bar.
  309. * Then creates an xp bar and fills it up with the available xp.
  310. */
  311. private void createXpBar()
  312. {
  313. //Calculates how much percent of the next level has been completed.
  314. final double experienceRatio = ExperienceUtils.getPercentOfLevelCompleted(citizen.getExperience(), citizen.getLevel());
  315.  
  316. findPaneOfTypeByID(WINDOW_ID_XP, Label.class).setLabelText(Integer.toString(citizen.getLevel()));
  317. findPaneOfTypeByID(WINDOW_ID_XP, Label.class).setPosition(XP_LABEL_X, XP_LABEL_Y);
  318.  
  319. @NotNull final Image xpBar = new Image();
  320. xpBar.setImage(Gui.ICONS, XP_BAR_ICON_COLUMN, XP_BAR_EMPTY_ROW, XP_BAR_WIDTH, XP_HEIGHT);
  321. xpBar.setPosition(LEFT_BORDER_X, LEFT_BORDER_Y);
  322.  
  323. @NotNull final Image xpBar2 = new Image();
  324. xpBar2.setImage(Gui.ICONS, XP_BAR_ICON_COLUMN_END, XP_BAR_EMPTY_ROW, XP_BAR_ICON_COLUMN_END_WIDTH, XP_HEIGHT);
  325. xpBar2.setPosition(XP_BAR_ICON_END_OFFSET + LEFT_BORDER_X, LEFT_BORDER_Y);
  326.  
  327. findPaneOfTypeByID(WINDOW_ID_XPBAR, View.class).addChild(xpBar);
  328. findPaneOfTypeByID(WINDOW_ID_XPBAR, View.class).addChild(xpBar2);
  329.  
  330. if (experienceRatio > 0)
  331. {
  332. @NotNull final Image xpBarFull = new Image();
  333. xpBarFull.setImage(Gui.ICONS, XP_BAR_ICON_COLUMN, XP_BAR_FULL_ROW, (int) experienceRatio, XP_HEIGHT);
  334. xpBarFull.setPosition(LEFT_BORDER_X, LEFT_BORDER_Y);
  335. findPaneOfTypeByID(WINDOW_ID_XPBAR, View.class).addChild(xpBarFull);
  336. }
  337. }
  338.  
  339. /**
  340. * Fills the citizen gui with it's skill values.
  341. */
  342. private void createSkillContent()
  343. {
  344. findPaneOfTypeByID(STRENGTH, Label.class).setLabelText(
  345. LanguageHandler.format("com.minecolonies.coremod.gui.citizen.skills.strength", citizen.getStrength()));
  346. findPaneOfTypeByID(ENDURANCE, Label.class).setLabelText(
  347. LanguageHandler.format("com.minecolonies.coremod.gui.citizen.skills.endurance", citizen.getEndurance()));
  348. findPaneOfTypeByID(CHARISMA, Label.class).setLabelText(
  349. LanguageHandler.format("com.minecolonies.coremod.gui.citizen.skills.charisma", citizen.getCharisma()));
  350. findPaneOfTypeByID(INTELLIGENCE, Label.class).setLabelText(
  351. LanguageHandler.format("com.minecolonies.coremod.gui.citizen.skills.intelligence", citizen.getIntelligence()));
  352. findPaneOfTypeByID(DEXTERITY, Label.class).setLabelText(
  353. LanguageHandler.format("com.minecolonies.coremod.gui.citizen.skills.dexterity", citizen.getDexterity()));
  354. }
  355.  
  356. /**
  357. * Called when a button in the citizen has been clicked.
  358. *
  359. * @param button the clicked button.
  360. */
  361. @Override
  362. public void onButtonClicked(@NotNull final Button button)
  363. {
  364. if (button.getID().equals(INVENTORY_BUTTON_ID))
  365. {
  366. MineColonies.getNetwork().sendToServer(new OpenInventoryMessage(citizen));
  367. }
  368. }
  369. }
Add Comment
Please, Sign In to add comment