Advertisement
tahg

Untitled

Nov 27th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.46 KB | None | 0 0
  1. PotionBrewingTest.java
  2.  
  3.  
  4. package net.minecraft.world.item.alchemy;
  5.  
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.util.*;
  9. import java.util.List;
  10.  
  11. import javax.swing.*;
  12. import javax.swing.border.TitledBorder;
  13.  
  14. import net.minecraft.world.effect.MobEffectInstance;
  15.  
  16. public class PotionBrewingTest extends JFrame {
  17.  
  18. private static final long serialVersionUID = 1L;
  19.  
  20. private boolean[] bits = new boolean[PotionBrewing.NUM_BITS];
  21. private JButton[] bitButtons = new JButton[PotionBrewing.NUM_BITS];
  22. private JLabel potionResult;
  23. private JLabel potionColor;
  24. private JLabel[] potionEffects = new JLabel[10];
  25. private JPanel potionStepsPanel;
  26.  
  27.  
  28. public PotionBrewingTest() {
  29. super("Potion Tester");
  30.  
  31. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.  
  33. setSize(new Dimension(900, 600));
  34. setLayout(new FlowLayout());
  35.  
  36. JPanel bitGrid = new JPanel(new GridLayout(1, PotionBrewing.NUM_BITS));
  37. bitGrid.setBorder(new TitledBorder("Potion Bits"));
  38. for (int bit = PotionBrewing.NUM_BITS - 1; bit >= 0; bit--) {
  39. bitGrid.add(bitButtons[bit] = new JButton("0"));
  40.  
  41. final int myBit = bit;
  42. bitButtons[bit].addActionListener(new ActionListener() {
  43. public void actionPerformed(ActionEvent e) {
  44. JButton thisButton = (JButton) e.getSource();
  45. if (bits[myBit]) {
  46. thisButton.setText(myBit + ": 0");
  47. bits[myBit] = false;
  48. } else {
  49. thisButton.setText(myBit + ": 1");
  50. bits[myBit] = true;
  51. }
  52. updateResult();
  53. }
  54. });
  55. }
  56.  
  57. JPanel potionPanel = new JPanel(new FlowLayout());
  58. potionPanel.setBorder(new TitledBorder("Potion Result"));
  59. potionPanel.add(potionResult = new JLabel("0 = 0"));
  60. potionPanel.add(potionColor = new PotionColorLabel());
  61.  
  62. JPanel potionEffectsPanel = new JPanel(new GridLayout(potionEffects.length, 1));
  63. potionEffectsPanel.setBorder(new TitledBorder("Potion Effects"));
  64. for (int i = 0; i < potionEffects.length; i++) {
  65. potionEffectsPanel.add(potionEffects[i] = new JLabel());
  66. }
  67.  
  68. // JButton boilButton;
  69. // JButton shakeButton;
  70. int count = (int) Math.sqrt((double) brewSteps.size()) + 1;
  71. JPanel actionsPanel = new JPanel(new GridLayout(count, count));
  72. actionsPanel.setBorder(new TitledBorder("Actions"));
  73. for (final BrewStep step : brewSteps) {
  74. JButton jButton = new JButton(step.getName());
  75. jButton.addActionListener(new ActionListener() {
  76. public void actionPerformed(ActionEvent e) {
  77. setBrew(step.apply(getBrew()));
  78. }
  79. });
  80. actionsPanel.add(jButton);
  81. }
  82.  
  83. // boilButton.addActionListener(new ActionListener() {
  84. // public void actionPerformed(ActionEvent e) {
  85. // setBrew(PotionBrewing.boil(getBrew()));
  86. // }
  87. // });
  88. // shakeButton.addActionListener(new ActionListener() {
  89. // public void actionPerformed(ActionEvent e) {
  90. // setBrew(PotionBrewing.shake(getBrew()));
  91. // }
  92. // });
  93.  
  94. potionStepsPanel = new JPanel(new GridLayout(brewSteps.size(), 1));
  95. potionStepsPanel.setBorder(new TitledBorder("Brewing Steps"));
  96.  
  97. add(bitGrid);
  98. add(actionsPanel);
  99. add(potionPanel);
  100. add(potionEffectsPanel);
  101. add(potionStepsPanel);
  102.  
  103. setVisible(true);
  104.  
  105. updateResult();
  106. }
  107.  
  108. private int getBrew() {
  109. int brew = 0;
  110. for (int bit = PotionBrewing.NUM_BITS - 1; bit >= 0; bit--) {
  111. brew <<= 1;
  112. if (bits[bit]) {
  113. brew |= 1;
  114. }
  115. }
  116. return brew;
  117. }
  118.  
  119. private void setBrew(int brew) {
  120. for (int bit = PotionBrewing.NUM_BITS - 1; bit >= 0; bit--) {
  121. if ((brew & (1 << bit)) != 0) {
  122. bitButtons[bit].setText(bit + ": 1");
  123. bits[bit] = true;
  124. } else {
  125. bitButtons[bit].setText(bit + ": 0");
  126. bits[bit] = false;
  127. }
  128. }
  129. updateResult();
  130. }
  131.  
  132. private void updateResult() {
  133. int brew = getBrew();
  134. potionResult.setText(brew + " = " + PotionBrewing.getAppearanceName(brew));
  135. potionColor.update(potionColor.getGraphics());
  136.  
  137. List<MobEffectInstance> effects = PotionBrewing.getEffects(brew);
  138. if (effects != null) {
  139. int i = 0;
  140. for (; i < potionEffects.length && i < effects.size(); i++) {
  141. MobEffectInstance potionEffect = effects.get(i);
  142. if (potionEffect.getAmplifier() > 0) {
  143. potionEffects[i].setText(potionEffect.getDescriptionId() + " x " + (potionEffect.getAmplifier() + 1) + ", Duration: " + potionEffect.getDuration());
  144. } else {
  145. potionEffects[i].setText(potionEffect.getDescriptionId() + ", Duration: " + potionEffect.getDuration());
  146. }
  147. }
  148. for (; i < potionEffects.length; i++) {
  149. potionEffects[i].setText("-");
  150. }
  151. } else {
  152. for (int i = 0; i < potionEffects.length; i++) {
  153. potionEffects[i].setText("-");
  154. }
  155. }
  156.  
  157. updatePaths(brew);
  158. }
  159.  
  160. private HashMap<Integer, Integer> successfulSteps = new HashMap<Integer, Integer>();
  161. private HashMap<Integer, Integer> visitedSteps = new HashMap<Integer, Integer>();
  162.  
  163. private void updatePaths(int brew) {
  164.  
  165.  
  166. successfulSteps.clear();
  167. potionStepsPanel.removeAll();
  168.  
  169. if (brew == 0) {
  170. return;
  171. }
  172.  
  173. for (BrewStep nextStep : brewSteps) {
  174. visitedSteps.clear();
  175. LinkedList<BrewStep> stepResult = findPath(nextStep, 0, brew, 1);
  176. if (stepResult != null) {
  177. StringBuilder builder = new StringBuilder();
  178. boolean first = true;
  179. for (BrewStep step : stepResult) {
  180. if (!first) {
  181. builder.append(" => ");
  182. }
  183. first = false;
  184. builder.append(step.getName());
  185. }
  186.  
  187. potionStepsPanel.add(new JLabel(builder.toString()));
  188. }
  189. }
  190. }
  191.  
  192. private LinkedList<BrewStep> findPath(BrewStep currentStep, int currentBrew, int targetBrew, int numSteps) {
  193.  
  194. if (numSteps > 6) {
  195. return null;
  196. }
  197.  
  198. int newBrew = currentStep.apply(currentBrew);
  199. if (newBrew == 0) {
  200. return null;
  201. }
  202.  
  203.  
  204. if (newBrew == targetBrew) {
  205. LinkedList<BrewStep> result = new LinkedList<BrewStep>();
  206. result.addFirst(currentStep);
  207. successfulSteps.put(newBrew, numSteps);
  208. return result;
  209. }
  210.  
  211. if (!currentStep.isCanContinue()) {
  212. return null;
  213. }
  214.  
  215. Integer visitedStepCount = visitedSteps.get(newBrew);
  216. if (visitedStepCount != null && visitedStepCount < numSteps) {
  217. return null;
  218. }
  219. visitedSteps.put(newBrew, numSteps);
  220.  
  221. Integer prevSteps = successfulSteps.get(newBrew);
  222. if (prevSteps != null && prevSteps <= numSteps) {
  223. return null;
  224. }
  225.  
  226. LinkedList<BrewStep> result = null;
  227.  
  228. for (BrewStep nextStep : brewSteps) {
  229. LinkedList<BrewStep> stepResult = findPath(nextStep, newBrew, targetBrew, numSteps + 1);
  230. if (stepResult != null && (result == null || result.size() > stepResult.size())) {
  231. result = stepResult;
  232. }
  233. }
  234.  
  235. if (result != null) {
  236. result.addFirst(currentStep);
  237. successfulSteps.put(newBrew, numSteps);
  238. }
  239. return result;
  240. }
  241.  
  242. private class PotionColorLabel extends JLabel {
  243.  
  244. private static final long serialVersionUID = 1L;
  245.  
  246. public PotionColorLabel() {
  247. super();
  248.  
  249. setSize(new Dimension(32, 32));
  250. setMinimumSize(new Dimension(32, 32));
  251. setMaximumSize(new Dimension(32, 32));
  252. setPreferredSize(new Dimension(32, 32));
  253. setVisible(true);
  254. }
  255.  
  256. @Override
  257. protected void paintComponent(Graphics graphics) {
  258. super.paintComponent(graphics);
  259.  
  260. Rectangle bounds = graphics.getClipBounds();
  261.  
  262. int brew = getBrew();
  263. int color = PotionBrewing.getColorValue(brew);
  264.  
  265. graphics.setColor(new Color((color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff));
  266. graphics.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
  267. }
  268. }
  269.  
  270. private static List<BrewStep> brewSteps = new ArrayList<BrewStep>();
  271. static {
  272. // brewSteps.add(new BrewStep("BOIL", "", true) {
  273. // @Override
  274. // public int apply(int brew) {
  275. // return PotionBrewing.boil(brew);
  276. // }
  277. // });
  278. // brewSteps.add(new BrewStep("SHAKE", "") {
  279. // @Override
  280. // public int apply(int brew) {
  281. // return PotionBrewing.shake(brew);
  282. // }
  283. // });
  284. brewSteps.add(new BrewStep("STIRR", "") {
  285. @Override
  286. public int apply(int brew) {
  287. return PotionBrewing.stirr(brew);
  288. }
  289. });
  290. // brewSteps.add(new BrewStep("Y Flower", "+0-5"));
  291. // brewSteps.add(new BrewStep("R Flower", "+1-8"));
  292. brewSteps.add(new BrewStep("Water", "-1-3-5-7-9-11-13"));
  293. brewSteps.add(new BrewStep("Catalyzer", "+0"));
  294. brewSteps.add(new BrewStep("Charged Caffeine", "+2"));
  295. brewSteps.add(new BrewStep("Dried Cactus", "+12+6"));
  296. brewSteps.add(new BrewStep("Slime Essence", "+10+7+1"));
  297. brewSteps.add(new BrewStep("Moon Mushroom", "+8"));
  298. brewSteps.add(new BrewStep("Unicorn Tears", "+13"));
  299. brewSteps.add(new BrewStep("Spider Eggs", "+10+7+5"));
  300. brewSteps.add(new BrewStep("Crushed Wolf Fang", "+9+3"));
  301. brewSteps.add(new BrewStep("Crystalized Blood", "+11"));
  302. brewSteps.add(new BrewStep("Fermented Eyes", "+14+9"));
  303. brewSteps.add(new BrewStep("Turtle Soul", "+10+4-7"));
  304. brewSteps.add(new BrewStep("Magma Cream", "+14+6+1"));
  305. brewSteps.add(new BrewStep("Fish Gylls", "+12+10"));
  306. }
  307.  
  308. private static class BrewStep {
  309.  
  310. private final String name;
  311. private final String effect;
  312. private final boolean canContinue;
  313.  
  314. public BrewStep(String name, String effect) {
  315. this(name, effect, true);
  316. }
  317.  
  318. public BrewStep(String name, String effect, boolean canContinue) {
  319. this.name = name;
  320. this.effect = effect;
  321. this.canContinue = canContinue;
  322. }
  323.  
  324. public int apply(int brew) {
  325. return PotionBrewing.applyBrew(brew, effect);
  326. }
  327.  
  328. public String getName() {
  329. return name;
  330. }
  331.  
  332. public boolean isCanContinue() {
  333. return canContinue;
  334. }
  335.  
  336. }
  337.  
  338. public static void main(String[] args) {
  339. new PotionBrewingTest();
  340. }
  341.  
  342.  
  343. }
  344.  
  345.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement