Skip to content

Instantly share code, notes, and snippets.

@EDDxample
Created July 27, 2019 00:00
Show Gist options
  • Save EDDxample/b01bbe308fa367ea8cc26d360dfd353a to your computer and use it in GitHub Desktop.
Save EDDxample/b01bbe308fa367ea8cc26d360dfd353a to your computer and use it in GitHub Desktop.
that chunk thing
package eddxample;
import net.minecraft.world.chunk.ChunkStatus;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import javax.swing.*;
public class Minimap {
private static JFrame minimap;
public static void init() {
System.setProperty("java.awt.headless", "false");
javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { initMinimap(); } });
}
public static void initMinimap() {
minimap = new JFrame("Chunk Minimap");
minimap.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
minimap.setSize(600, 600);
MinimapPanel mp = new MinimapPanel();
minimap.getContentPane().addMouseListener(mp);
minimap.getContentPane().addMouseMotionListener(mp);
minimap.getContentPane().addMouseWheelListener(mp);
minimap.add(mp);
minimap.setVisible(true);
System.setProperty("java.awt.headless", "true");
}
class MinimapPanel extends JPanel implements ActionListener, MouseListener, MouseMotionListener, MouseWheelListener {
static int origenX, origenY, clickX, clickY, chunkSize = 40, centerChunkX, centerChunkY;
static boolean shouldDrag = false;
static int[] getRadius(Rectangle bounds) {
int midX = bounds.width/2,
midY = bounds.height/2;
centerChunkX = midX - ((midX - origenX) % chunkSize);
centerChunkY = midY - ((midY - origenY) % chunkSize);
int radiusX = (int) (Math.floor((double)(centerChunkX + (chunkSize*3))/chunkSize))*chunkSize;
int radiusY = (int) (Math.floor((double)(centerChunkY + (chunkSize*4))/chunkSize))*chunkSize;
return new int[] {radiusX, radiusY};
}
public void paint(Graphics g1) {
super.paint(g1);
Graphics2D g = (Graphics2D) g1;
Rectangle bounds = g.getClipBounds();
int[] radius = getRadius(bounds);
//DRAW CHUNKS
for (int j = centerChunkY - radius[1]; j < centerChunkY + radius[1]; j += chunkSize)
{
for (int i = centerChunkX - radius[0]; i < centerChunkX + radius[0]; i += chunkSize)
{
//ChunkPos
int x = (i - origenX) / chunkSize;
int y = (j - origenY) / chunkSize;
g.setColor((x+y)%2==0? new Color(0xA0A0A0) : new Color(0x646464));
g.fillRect(i, j, chunkSize, chunkSize);
g.setColor(Color.black);
String text = x+", "+y;
int textWidth = g.getFontMetrics().stringWidth(text);
if (textWidth < chunkSize && chunkSize > 10) g.drawString(text, i + chunkSize/2 - textWidth/2, j + 5 + chunkSize/2);
}
}
//0,0 LINES
g.setColor(new Color(0xFF0000));
g.drawLine(0, origenY, bounds.width, origenY);
g.drawLine(0, origenY+1, bounds.width, origenY+1);
g.setColor(new Color(0x0000FF));
g.drawLine(origenX, 0, origenX, bounds.height);
g.drawLine(origenX+1, 0, origenX+1, bounds.height);
}
//EVENTS
public void mousePressed(MouseEvent e) {
shouldDrag = false;
if (e.getButton() == 1)
{
shouldDrag = true;
clickX = e.getX();
clickY = e.getY();
}
else if (e.getButton() == 3)
{
int x = e.getX() - (e.getX() - origenX)%chunkSize;
int y = e.getY() - (e.getY() - origenY)%chunkSize;
System.out.println(x+" , "+y);
}
}
public void mouseDragged(MouseEvent e) {
if (shouldDrag)
{
origenX += e.getX() - clickX;
origenY += e.getY() - clickY;
clickX = e.getX();
clickY = e.getY();
repaint();
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
double i = -e.getPreciseWheelRotation();
int save = chunkSize;
if (i < 0) chunkSize += chunkSize > 6 ? i : 0;
if (i > 0) chunkSize += chunkSize < 60 ? i : 0;
if (chunkSize != save)
{
origenX += (i > 0 ? -1 : 1)*((e.getX() - origenX)/chunkSize + 1);
origenY += (i > 0 ? -1 : 1)*((e.getY() - origenY)/chunkSize + 1);
repaint();
}
}
public void actionPerformed(ActionEvent e){}
public void mouseClicked (MouseEvent e) {}
public void mouseReleased (MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mouseMoved (MouseEvent e) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment