Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PotionBrewingTest.java
- package net.minecraft.world.item.alchemy;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import java.util.List;
- import javax.swing.*;
- import javax.swing.border.TitledBorder;
- import net.minecraft.world.effect.MobEffectInstance;
- public class PotionBrewingTest extends JFrame {
- private static final long serialVersionUID = 1L;
- private boolean[] bits = new boolean[PotionBrewing.NUM_BITS];
- private JButton[] bitButtons = new JButton[PotionBrewing.NUM_BITS];
- private JLabel potionResult;
- private JLabel potionColor;
- private JLabel[] potionEffects = new JLabel[10];
- private JPanel potionStepsPanel;
- public PotionBrewingTest() {
- super("Potion Tester");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(new Dimension(900, 600));
- setLayout(new FlowLayout());
- JPanel bitGrid = new JPanel(new GridLayout(1, PotionBrewing.NUM_BITS));
- bitGrid.setBorder(new TitledBorder("Potion Bits"));
- for (int bit = PotionBrewing.NUM_BITS - 1; bit >= 0; bit--) {
- bitGrid.add(bitButtons[bit] = new JButton("0"));
- final int myBit = bit;
- bitButtons[bit].addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JButton thisButton = (JButton) e.getSource();
- if (bits[myBit]) {
- thisButton.setText(myBit + ": 0");
- bits[myBit] = false;
- } else {
- thisButton.setText(myBit + ": 1");
- bits[myBit] = true;
- }
- updateResult();
- }
- });
- }
- JPanel potionPanel = new JPanel(new FlowLayout());
- potionPanel.setBorder(new TitledBorder("Potion Result"));
- potionPanel.add(potionResult = new JLabel("0 = 0"));
- potionPanel.add(potionColor = new PotionColorLabel());
- JPanel potionEffectsPanel = new JPanel(new GridLayout(potionEffects.length, 1));
- potionEffectsPanel.setBorder(new TitledBorder("Potion Effects"));
- for (int i = 0; i < potionEffects.length; i++) {
- potionEffectsPanel.add(potionEffects[i] = new JLabel());
- }
- // JButton boilButton;
- // JButton shakeButton;
- int count = (int) Math.sqrt((double) brewSteps.size()) + 1;
- JPanel actionsPanel = new JPanel(new GridLayout(count, count));
- actionsPanel.setBorder(new TitledBorder("Actions"));
- for (final BrewStep step : brewSteps) {
- JButton jButton = new JButton(step.getName());
- jButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- setBrew(step.apply(getBrew()));
- }
- });
- actionsPanel.add(jButton);
- }
- // boilButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent e) {
- // setBrew(PotionBrewing.boil(getBrew()));
- // }
- // });
- // shakeButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent e) {
- // setBrew(PotionBrewing.shake(getBrew()));
- // }
- // });
- potionStepsPanel = new JPanel(new GridLayout(brewSteps.size(), 1));
- potionStepsPanel.setBorder(new TitledBorder("Brewing Steps"));
- add(bitGrid);
- add(actionsPanel);
- add(potionPanel);
- add(potionEffectsPanel);
- add(potionStepsPanel);
- setVisible(true);
- updateResult();
- }
- private int getBrew() {
- int brew = 0;
- for (int bit = PotionBrewing.NUM_BITS - 1; bit >= 0; bit--) {
- brew <<= 1;
- if (bits[bit]) {
- brew |= 1;
- }
- }
- return brew;
- }
- private void setBrew(int brew) {
- for (int bit = PotionBrewing.NUM_BITS - 1; bit >= 0; bit--) {
- if ((brew & (1 << bit)) != 0) {
- bitButtons[bit].setText(bit + ": 1");
- bits[bit] = true;
- } else {
- bitButtons[bit].setText(bit + ": 0");
- bits[bit] = false;
- }
- }
- updateResult();
- }
- private void updateResult() {
- int brew = getBrew();
- potionResult.setText(brew + " = " + PotionBrewing.getAppearanceName(brew));
- potionColor.update(potionColor.getGraphics());
- List<MobEffectInstance> effects = PotionBrewing.getEffects(brew);
- if (effects != null) {
- int i = 0;
- for (; i < potionEffects.length && i < effects.size(); i++) {
- MobEffectInstance potionEffect = effects.get(i);
- if (potionEffect.getAmplifier() > 0) {
- potionEffects[i].setText(potionEffect.getDescriptionId() + " x " + (potionEffect.getAmplifier() + 1) + ", Duration: " + potionEffect.getDuration());
- } else {
- potionEffects[i].setText(potionEffect.getDescriptionId() + ", Duration: " + potionEffect.getDuration());
- }
- }
- for (; i < potionEffects.length; i++) {
- potionEffects[i].setText("-");
- }
- } else {
- for (int i = 0; i < potionEffects.length; i++) {
- potionEffects[i].setText("-");
- }
- }
- updatePaths(brew);
- }
- private HashMap<Integer, Integer> successfulSteps = new HashMap<Integer, Integer>();
- private HashMap<Integer, Integer> visitedSteps = new HashMap<Integer, Integer>();
- private void updatePaths(int brew) {
- successfulSteps.clear();
- potionStepsPanel.removeAll();
- if (brew == 0) {
- return;
- }
- for (BrewStep nextStep : brewSteps) {
- visitedSteps.clear();
- LinkedList<BrewStep> stepResult = findPath(nextStep, 0, brew, 1);
- if (stepResult != null) {
- StringBuilder builder = new StringBuilder();
- boolean first = true;
- for (BrewStep step : stepResult) {
- if (!first) {
- builder.append(" => ");
- }
- first = false;
- builder.append(step.getName());
- }
- potionStepsPanel.add(new JLabel(builder.toString()));
- }
- }
- }
- private LinkedList<BrewStep> findPath(BrewStep currentStep, int currentBrew, int targetBrew, int numSteps) {
- if (numSteps > 6) {
- return null;
- }
- int newBrew = currentStep.apply(currentBrew);
- if (newBrew == 0) {
- return null;
- }
- if (newBrew == targetBrew) {
- LinkedList<BrewStep> result = new LinkedList<BrewStep>();
- result.addFirst(currentStep);
- successfulSteps.put(newBrew, numSteps);
- return result;
- }
- if (!currentStep.isCanContinue()) {
- return null;
- }
- Integer visitedStepCount = visitedSteps.get(newBrew);
- if (visitedStepCount != null && visitedStepCount < numSteps) {
- return null;
- }
- visitedSteps.put(newBrew, numSteps);
- Integer prevSteps = successfulSteps.get(newBrew);
- if (prevSteps != null && prevSteps <= numSteps) {
- return null;
- }
- LinkedList<BrewStep> result = null;
- for (BrewStep nextStep : brewSteps) {
- LinkedList<BrewStep> stepResult = findPath(nextStep, newBrew, targetBrew, numSteps + 1);
- if (stepResult != null && (result == null || result.size() > stepResult.size())) {
- result = stepResult;
- }
- }
- if (result != null) {
- result.addFirst(currentStep);
- successfulSteps.put(newBrew, numSteps);
- }
- return result;
- }
- private class PotionColorLabel extends JLabel {
- private static final long serialVersionUID = 1L;
- public PotionColorLabel() {
- super();
- setSize(new Dimension(32, 32));
- setMinimumSize(new Dimension(32, 32));
- setMaximumSize(new Dimension(32, 32));
- setPreferredSize(new Dimension(32, 32));
- setVisible(true);
- }
- @Override
- protected void paintComponent(Graphics graphics) {
- super.paintComponent(graphics);
- Rectangle bounds = graphics.getClipBounds();
- int brew = getBrew();
- int color = PotionBrewing.getColorValue(brew);
- graphics.setColor(new Color((color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff));
- graphics.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
- }
- }
- private static List<BrewStep> brewSteps = new ArrayList<BrewStep>();
- static {
- // brewSteps.add(new BrewStep("BOIL", "", true) {
- // @Override
- // public int apply(int brew) {
- // return PotionBrewing.boil(brew);
- // }
- // });
- // brewSteps.add(new BrewStep("SHAKE", "") {
- // @Override
- // public int apply(int brew) {
- // return PotionBrewing.shake(brew);
- // }
- // });
- brewSteps.add(new BrewStep("STIRR", "") {
- @Override
- public int apply(int brew) {
- return PotionBrewing.stirr(brew);
- }
- });
- // brewSteps.add(new BrewStep("Y Flower", "+0-5"));
- // brewSteps.add(new BrewStep("R Flower", "+1-8"));
- brewSteps.add(new BrewStep("Water", "-1-3-5-7-9-11-13"));
- brewSteps.add(new BrewStep("Catalyzer", "+0"));
- brewSteps.add(new BrewStep("Charged Caffeine", "+2"));
- brewSteps.add(new BrewStep("Dried Cactus", "+12+6"));
- brewSteps.add(new BrewStep("Slime Essence", "+10+7+1"));
- brewSteps.add(new BrewStep("Moon Mushroom", "+8"));
- brewSteps.add(new BrewStep("Unicorn Tears", "+13"));
- brewSteps.add(new BrewStep("Spider Eggs", "+10+7+5"));
- brewSteps.add(new BrewStep("Crushed Wolf Fang", "+9+3"));
- brewSteps.add(new BrewStep("Crystalized Blood", "+11"));
- brewSteps.add(new BrewStep("Fermented Eyes", "+14+9"));
- brewSteps.add(new BrewStep("Turtle Soul", "+10+4-7"));
- brewSteps.add(new BrewStep("Magma Cream", "+14+6+1"));
- brewSteps.add(new BrewStep("Fish Gylls", "+12+10"));
- }
- private static class BrewStep {
- private final String name;
- private final String effect;
- private final boolean canContinue;
- public BrewStep(String name, String effect) {
- this(name, effect, true);
- }
- public BrewStep(String name, String effect, boolean canContinue) {
- this.name = name;
- this.effect = effect;
- this.canContinue = canContinue;
- }
- public int apply(int brew) {
- return PotionBrewing.applyBrew(brew, effect);
- }
- public String getName() {
- return name;
- }
- public boolean isCanContinue() {
- return canContinue;
- }
- }
- public static void main(String[] args) {
- new PotionBrewingTest();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement