@SuppressWarnings("unchecked") @Override public int loop() { synchronized (lock) { try { if (!ui.isVisible()) handler.stopMod(getName()); World world = minecraft.getWorld(); JTable list = ui.getList(); DefaultTableModel model = (DefaultTableModel) list.getModel(); TableRowSorter<TableModel> sorter = (TableRowSorter<TableModel>) list.getRowSorter(); if (world != null) { Player currentPlayer = minecraft.getPlayer(); int playerX = (int) java.lang.Math.round(currentPlayer.getX()); int playerY = (int) java.lang.Math.round(currentPlayer.getY()); int playerZ = (int) java.lang.Math.round(currentPlayer.getZ()); ArrayList<Block> blocks = new ArrayList<Block>(); for (int x = playerX - radius; x < playerX + radius; x++) for (int y = playerY - radius; y < playerY + radius; y++) for (int z = playerZ - radius; z < playerZ + radius; z++) if (world.getBlockIDAt(x, y, z) == blockID) blocks.add(new Block(x, y, z)); label: for (int row = 0; row < model.getRowCount(); row++) { int x = (Integer) model.getValueAt(row, 1); int y = (Integer) model.getValueAt(row, 2); int z = (Integer) model.getValueAt(row, 3); for (Block block : blocks) { if (x == block.getX() && y == block.getY() && z == block.getZ()) { model.setValueAt(getDistanceTo(x, y, z, currentPlayer), row, 0); continue label; } } model.removeRow(row); } label: for (Block block : blocks) { for (int row = 0; row < model.getRowCount(); row++) { int x = (Integer) model.getValueAt(row, 1); int y = (Integer) model.getValueAt(row, 2); int z = (Integer) model.getValueAt(row, 3); if (x == block.getX() && y == block.getY() && z == block.getZ()) continue label; } model.addRow( new Object[] { getDistanceTo(block.getX(), block.getY(), block.getZ(), minecraft.getPlayer()), block.getX(), block.getY(), block.getZ() }); } sorter.sort(); list.repaint(); } else { for (int i = model.getRowCount() - 1; i >= 0; i--) model.removeRow(i); } } catch (Exception exception) { } } return 500; }
private double getDistanceTo(double x, double y, double z, Player player) { double x2 = player.getX(); double y2 = player.getY(); double z2 = player.getZ(); double xResult = java.lang.Math.pow(java.lang.Math.max(x, x2) - java.lang.Math.min(x, x2), 2); double yResult = java.lang.Math.pow(java.lang.Math.max(y, y2) - java.lang.Math.min(y, y2), 2); double zResult = java.lang.Math.pow(java.lang.Math.max(z, z2) - java.lang.Math.min(z, z2), 2); return java.lang.Math.sqrt(xResult + yResult + zResult); }
/** * Pushes a new Construct onto the end of the array. If the index is specified, this works like a * "insert" operation, in that all values are shifted to the right, starting with the value at * that index. If the array is associative though, you MUST send null, otherwise an {@link * IllegalArgumentException} is thrown. Ideally, you should use {@link #set} anyways for an * associative array. * * @param c The Construct to add to the array * @throws IllegalArgumentException If index is not null, and this is an associative array. * @throws IndexOutOfBoundsException If the index is not null, and the index specified is out of * range. */ public void push(Construct c, Integer index) throws IllegalArgumentException, IndexOutOfBoundsException { if (!associative_mode) { if (index != null) { array.add(index, c); } else { array.add(c); } next_index++; } else { if (index != null) { throw new IllegalArgumentException("Cannot insert into an associative array"); } int max = 0; for (String key : associative_array.keySet()) { try { int i = Integer.parseInt(key); max = java.lang.Math.max(max, i); } catch (NumberFormatException e) { } } if (c instanceof CEntry) { associative_array.put(Integer.toString(max + 1), ((CEntry) c).construct()); } else { associative_array.put(Integer.toString(max + 1), c); } } if (c instanceof CArray) { ((CArray) c).parent = this; } regenValue(new HashSet<CArray>()); }
// 计算用户组权限 public String parseRight(String s_right) { int i_Right, i_RightNum; String s_rtnRight; i_Right = 0; i_RightNum = Integer.parseInt(s_right); if (i_RightNum != 0) i_Right = i_Right + (int) java.lang.Math.pow(2, i_RightNum - 1); s_rtnRight = Integer.toString(i_Right); return s_rtnRight; }
// 计算用户和菜单权限 public String parseRight(String[] s_right) { int i_Right, i_RightNum; String s_rtnRight; i_Right = 0; for (int i = 0; i < s_right.length; i++) { i_RightNum = Integer.parseInt(s_right[i]); if (i_RightNum != 0) i_Right = i_Right + (int) java.lang.Math.pow(2, i_RightNum - 1); } s_rtnRight = Integer.toString(i_Right); return s_rtnRight; }
public CArray(Target t, Construct... items) { super("{}", ConstructType.ARRAY, t); if (items != null) { for (Construct item : items) { if (item instanceof CEntry) { // it's an associative array associative_mode = true; break; } } } associative_array = new TreeMap<String, Construct>(comparator); array = new ArrayList<Construct>(); if (associative_mode) { if (items != null) { for (Construct item : items) { if (item instanceof CEntry) { associative_array.put( normalizeConstruct(((CEntry) item).ckey), ((CEntry) item).construct); } else { int max = Integer.MIN_VALUE; for (String key : associative_array.keySet()) { try { int i = Integer.parseInt(key); max = java.lang.Math.max(max, i); } catch (NumberFormatException e) { } } if (max == Integer.MIN_VALUE) { max = -1; // Special case, there are no integer indexes in here yet. } associative_array.put(Integer.toString(max + 1), item); if (item instanceof CArray) { ((CArray) item).parent = this; } } } } } else { if (items != null) { for (Construct item : items) { array.add(item); if (item instanceof CArray) { ((CArray) item).parent = this; } } } this.next_index = array.size(); } regenValue(new HashSet<CArray>()); }
private static int readInputStreamWithTimeout(InputStream is, byte[] b, int timeoutMillis) throws IOException { try { int bufferOffset = 0; long maxTimeMillis = System.currentTimeMillis() + timeoutMillis; while (System.currentTimeMillis() < maxTimeMillis && bufferOffset < b.length) { int readLength = java.lang.Math.min(is.available(), b.length - bufferOffset); int readResult = is.read(b, bufferOffset, readLength); if (readResult == -1) break; bufferOffset += readResult; } return bufferOffset; } catch (IOException ex) { Loggers.SERVER.error("Error reading stream. Stream closed: " + ex); return -1; } }
/** calculate the difference between two basic emotions in Plutchik's theory */ public static double offsetEmotion( PlutchikBasicEmotion e1, PlutchikBasicEmotion e2, double v1, double v2) { assert (e1 == PlutchikInverseBasicEmotion(e2)); double val = java.lang.Math.abs(v1 - v2); return val; }
// Here some legacy code makes use of generics. They are tested, so there // is no risk of an actual error, but Java issues a warning. @SuppressWarnings("unchecked") public DialogParameters( JFrame parent, Vector<ParameterDescription> vec, boolean strict, Vector<LayerDesc> layers) { super(parent, Globals.messages.getString("Param_opt"), true); keyb1 = new OSKeybPanel(KEYBMODES.GREEK); keyb2 = new OSKeybPanel(KEYBMODES.MISC); keyb1.setField(this); keyb2.setField(this); keyb.addTab("Greek", keyb1); keyb.addTab("Misc", keyb2); keyb.setVisible(false); v = vec; int ycount = 0; // We create dynamically all the needed elements. // For this reason, we work on arrays of the potentially useful Swing // objects. jtf = new JTextField[MAX_ELEMENTS]; jcb = new JCheckBox[MAX_ELEMENTS]; jco = new JComboBox[MAX_ELEMENTS]; active = false; addComponentListener(this); GridBagLayout bgl = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); Container contentPane = getContentPane(); contentPane.setLayout(bgl); boolean extStrict = strict; ParameterDescription pd; int top = 0; JLabel lab; tc = 0; cc = 0; co = 0; // We process all parameter passed. Depending on its type, a // corresponding interface element will be created. // A symmetrical operation is done when validating parameters. for (ycount = 0; ycount < v.size(); ++ycount) { if (ycount > MAX) break; pd = (ParameterDescription) v.elementAt(ycount); // We do not need to store label objects, since we do not need // to retrieve data from them. lab = new JLabel(pd.description); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 1; constraints.gridy = ycount; constraints.gridwidth = 1; constraints.gridheight = 1; // The first element needs a little bit more space at the top. if (ycount == 0) top = 10; else top = 0; // Here we configure the grid layout constraints.insets = new Insets(top, 20, 0, 6); constraints.fill = GridBagConstraints.VERTICAL; constraints.anchor = GridBagConstraints.EAST; lab.setEnabled(!(pd.isExtension && extStrict)); if (!(pd.parameter instanceof Boolean)) contentPane.add(lab, constraints); constraints.anchor = GridBagConstraints.WEST; constraints.insets = new Insets(top, 0, 0, 0); constraints.fill = GridBagConstraints.HORIZONTAL; // Now, depending on the type of parameter we create interface // elements and we populate the dialog. if (pd.parameter instanceof PointG) { jtf[tc] = new JTextField(10); jtf[tc].setText("" + ((PointG) (pd.parameter)).x); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 1; constraints.gridheight = 1; // Disable FidoCadJ extensions in the strict compatibility mode jtf[tc].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jtf[tc++], constraints); jtf[tc] = new JTextField(10); jtf[tc].setText("" + ((PointG) (pd.parameter)).y); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 3; constraints.gridy = ycount; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.insets = new Insets(top, 6, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jtf[tc].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jtf[tc++], constraints); } else if (pd.parameter instanceof String) { jtf[tc] = new JTextField(24); jtf[tc].setText((String) (pd.parameter)); // If we have a String text field in the first position, its // contents should be evidenced, since it is supposed to be // the most important field (e.g. for the AdvText primitive) if (ycount == 0) jtf[tc].selectAll(); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.insets = new Insets(top, 0, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jtf[tc].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jtf[tc++], constraints); } else if (pd.parameter instanceof Boolean) { jcb[cc] = new JCheckBox(pd.description); jcb[cc].setSelected(((Boolean) (pd.parameter)).booleanValue()); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.insets = new Insets(top, 0, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jcb[cc].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jcb[cc++], constraints); } else if (pd.parameter instanceof Integer) { jtf[tc] = new JTextField(24); jtf[tc].setText(((Integer) pd.parameter).toString()); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.insets = new Insets(top, 0, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jtf[tc].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jtf[tc++], constraints); } else if (pd.parameter instanceof Float) { // TODO. // WARNING: (DB) this is supposed to be temporary. In fact, I // am planning to upgrade some of the parameters from int // to float. But for a few months, the users should not be // aware of that, even if the internal representation is // slowing being adapted. jtf[tc] = new JTextField(24); int dummy = java.lang.Math.round((Float) pd.parameter); jtf[tc].setText("" + dummy); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.insets = new Insets(top, 0, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jtf[tc].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jtf[tc++], constraints); } else if (pd.parameter instanceof FontG) { GraphicsEnvironment gE; gE = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] s = gE.getAvailableFontFamilyNames(); jco[co] = new JComboBox(); for (int i = 0; i < s.length; ++i) { jco[co].addItem(s[i]); if (s[i].equals(((FontG) pd.parameter).getFamily())) jco[co].setSelectedIndex(i); } constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.insets = new Insets(top, 0, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jco[co].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jco[co++], constraints); } else if (pd.parameter instanceof LayerInfo) { jco[co] = new JComboBox(new Vector<LayerDesc>(layers)); jco[co].setSelectedIndex(((LayerInfo) pd.parameter).layer); jco[co].setRenderer(new LayerCellRenderer()); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.insets = new Insets(top, 0, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jco[co].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jco[co++], constraints); } else if (pd.parameter instanceof ArrowInfo) { jco[co] = new JComboBox<ArrowInfo>(); jco[co].addItem(new ArrowInfo(0)); jco[co].addItem(new ArrowInfo(1)); jco[co].addItem(new ArrowInfo(2)); jco[co].addItem(new ArrowInfo(3)); jco[co].setSelectedIndex(((ArrowInfo) pd.parameter).style); jco[co].setRenderer(new ArrowCellRenderer()); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.insets = new Insets(top, 0, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jco[co].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jco[co++], constraints); } else if (pd.parameter instanceof DashInfo) { jco[co] = new JComboBox<DashInfo>(); for (int k = 0; k < Globals.dashNumber; ++k) { jco[co].addItem(new DashInfo(k)); } jco[co].setSelectedIndex(((DashInfo) pd.parameter).style); jco[co].setRenderer(new DashCellRenderer()); constraints.weightx = 100; constraints.weighty = 100; constraints.gridx = 2; constraints.gridy = ycount; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.insets = new Insets(top, 0, 0, 20); constraints.fill = GridBagConstraints.HORIZONTAL; jco[co].setEnabled(!(pd.isExtension && extStrict)); contentPane.add(jco[co++], constraints); } } // Put the OK and Cancel buttons and make them active. JButton ok = new JButton(Globals.messages.getString("Ok_btn")); JButton cancel = new JButton(Globals.messages.getString("Cancel_btn")); JButton keybd = new JButton("\u00B6\u2211\u221A"); // phylum keybd.setFocusable(false); keybd.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // If at this point, the keyboard is not visible, this means // that it will become visible in a while. It is better to // resize first and then show up the keyboard. if (keyb.isVisible()) { MIN_WIDTH = 400; MIN_HEIGHT = 350; } else { MIN_WIDTH = 400; MIN_HEIGHT = 500; } // setSize(MIN_WIDTH, MIN_HEIGHT); keyb.setVisible(!keyb.isVisible()); pack(); } }); constraints.gridx = 0; constraints.gridy = ycount++; constraints.gridwidth = 4; constraints.gridheight = 1; constraints.anchor = GridBagConstraints.EAST; constraints.insets = new Insets(6, 20, 20, 20); // Put the OK and Cancel buttons and make them active. Box b = Box.createHorizontalBox(); b.add(keybd); // phylum b.add(Box.createHorizontalGlue()); ok.setPreferredSize(cancel.getPreferredSize()); if (Globals.okCancelWinOrder) { b.add(ok); b.add(Box.createHorizontalStrut(12)); b.add(cancel); } else { b.add(cancel); b.add(Box.createHorizontalStrut(12)); b.add(ok); } // b.add(Box.createHorizontalStrut(12)); contentPane.add(b, constraints); constraints.gridx = 0; constraints.gridy = ycount; constraints.gridwidth = 4; constraints.gridheight = 1; constraints.anchor = GridBagConstraints.EAST; constraints.insets = new Insets(6, 20, 20, 20); contentPane.add(keyb, constraints); ok.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { try { int ycount; ParameterDescription pd; tc = 0; cc = 0; co = 0; // Here we read all the contents of the interface and we // update the contents of the parameter description array. for (ycount = 0; ycount < v.size(); ++ycount) { if (ycount > MAX) break; pd = (ParameterDescription) v.elementAt(ycount); if (pd.parameter instanceof PointG) { ((PointG) (pd.parameter)).x = Integer.parseInt(jtf[tc++].getText()); ((PointG) (pd.parameter)).y = Integer.parseInt(jtf[tc++].getText()); } else if (pd.parameter instanceof String) { pd.parameter = jtf[tc++].getText(); } else if (pd.parameter instanceof Boolean) { pd.parameter = Boolean.valueOf(jcb[cc++].isSelected()); } else if (pd.parameter instanceof Integer) { pd.parameter = Integer.valueOf(Integer.parseInt(jtf[tc++].getText())); } else if (pd.parameter instanceof Float) { pd.parameter = Float.valueOf(Float.parseFloat(jtf[tc++].getText())); } else if (pd.parameter instanceof FontG) { pd.parameter = new FontG((String) jco[co++].getSelectedItem()); } else if (pd.parameter instanceof LayerInfo) { pd.parameter = new LayerInfo(jco[co++].getSelectedIndex()); } else if (pd.parameter instanceof ArrowInfo) { pd.parameter = new ArrowInfo(jco[co++].getSelectedIndex()); } else if (pd.parameter instanceof DashInfo) { pd.parameter = new DashInfo(jco[co++].getSelectedIndex()); } } } catch (NumberFormatException E) { // Error detected. Probably, the user has entered an // invalid string when FidoCadJ was expecting a numerical // input. JOptionPane.showMessageDialog( null, Globals.messages.getString("Format_invalid"), "", JOptionPane.INFORMATION_MESSAGE); return; } active = true; // Globals.activeWindow.setEnabled(true); setVisible(false); keyb.setVisible(false); } }); cancel.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { // Globals.activeWindow.setEnabled(true); setVisible(false); keyb.setVisible(false); } }); // Here is an action in which the dialog is closed AbstractAction cancelAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { // Globals.activeWindow.setEnabled(true); setVisible(false); keyb.setVisible(false); } }; DialogUtil.addCancelEscape(this, cancelAction); this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { // Globals.activeWindow.setEnabled(true); keyb.setVisible(false); } }); pack(); DialogUtil.center(this); getRootPane().setDefaultButton(ok); }