Spring Cleaning

Clisprail is so bad.
This commit is contained in:
Dane
2013-06-20 16:30:36 -04:00
parent 244588f5b6
commit 9e99eea72c
3 changed files with 109 additions and 83 deletions
+32 -20
View File
@@ -5,7 +5,6 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.TimerTask; import java.util.TimerTask;
import org.parabot.core.asm.ASMClassLoader; import org.parabot.core.asm.ASMClassLoader;
import org.parabot.core.bot.loader.BotLoader; import org.parabot.core.bot.loader.BotLoader;
import org.parabot.core.classpath.ClassPath; import org.parabot.core.classpath.ClassPath;
@@ -17,8 +16,9 @@ import org.parabot.environment.servers.ServerProvider;
/** /**
* Game context * Game context
*
* @author Clisprail * @author Clisprail
* *
*/ */
public class Context { public class Context {
private static HashMap<ThreadGroup, Context> threadGroups = new HashMap<ThreadGroup, Context>(); private static HashMap<ThreadGroup, Context> threadGroups = new HashMap<ThreadGroup, Context>();
@@ -29,16 +29,14 @@ public class Context {
private ServerProvider serverProvider = null; private ServerProvider serverProvider = null;
private int tab = 0; private int tab = 0;
private Applet gameApplet = null; private Applet gameApplet = null;
private Object clientInstance = null; private Object clientInstance = null;
private static ArrayList<Paintable> paintables = new ArrayList<Paintable>(); private static ArrayList<Paintable> paintables = new ArrayList<Paintable>();
private PaintDebugger paintDebugger = new PaintDebugger(); private PaintDebugger paintDebugger = new PaintDebugger();
public boolean added = false; public boolean added = false;
public Context(final ServerProvider serverProvider) { public Context(final ServerProvider serverProvider) {
threadGroups.put(Thread.currentThread().getThreadGroup(), this); threadGroups.put(Thread.currentThread().getThreadGroup(), this);
@@ -47,40 +45,44 @@ public class Context {
id++; id++;
this.classPath = new ClassPath(); this.classPath = new ClassPath();
} }
/** /**
* Sets the ServerProvider class loader * Sets the ServerProvider class loader
*
* @param serverEnvironment * @param serverEnvironment
*/ */
public void setEnvironment(ASMClassLoader serverEnvironment) { public void setEnvironment(ASMClassLoader serverEnvironment) {
classLoader = new BotLoader(classPath, serverEnvironment); classLoader = new BotLoader(classPath, serverEnvironment);
} }
/** /**
* Sets the main client instance * Sets the main client instance
*/ */
public void setClientInstance(Object object) { public void setClientInstance(Object object) {
this.clientInstance = object; this.clientInstance = object;
} }
/** /**
* ClassPath * ClassPath
*
* @return classpath * @return classpath
*/ */
public ClassPath getClassPath() { public ClassPath getClassPath() {
return classPath; return classPath;
} }
/** /**
* Determines if applet has been set * Determines if applet has been set
*
* @return <b>true</b> if set * @return <b>true</b> if set
*/ */
public boolean appletSet() { public boolean appletSet() {
return gameApplet != null; return gameApplet != null;
} }
/** /**
* Gets game applet * Gets game applet
*
* @return applet * @return applet
*/ */
public Applet getApplet() { public Applet getApplet() {
@@ -89,6 +91,7 @@ public class Context {
/** /**
* Resolves the context from threadgroup * Resolves the context from threadgroup
*
* @return context * @return context
*/ */
public static Context resolve() { public static Context resolve() {
@@ -102,10 +105,10 @@ public class Context {
serverProvider.parseJar(); serverProvider.parseJar();
serverProvider.injectHooks(); serverProvider.injectHooks();
gameApplet = serverProvider.fetchApplet(); gameApplet = serverProvider.fetchApplet();
if(getClient() == null) { if (getClient() == null) {
setClientInstance(gameApplet); setClientInstance(gameApplet);
} }
serverProvider.addMenuItems(BotUI.getInstance().getBar()); serverProvider.addMenuItems(BotUI.getInstance().getJMenuBar());
BotUI.getInstance().validate(); BotUI.getInstance().validate();
final GamePanel panel = GamePanel.getInstance(); final GamePanel panel = GamePanel.getInstance();
panel.removeLoader(); panel.removeLoader();
@@ -122,6 +125,7 @@ public class Context {
/** /**
* Gets the server prodiver belonging to this context * Gets the server prodiver belonging to this context
*
* @return server provider * @return server provider
*/ */
public ServerProvider getServerProvider() { public ServerProvider getServerProvider() {
@@ -130,6 +134,7 @@ public class Context {
/** /**
* Gets class loader from this context * Gets class loader from this context
*
* @return class loader * @return class loader
*/ */
public ASMClassLoader getASMClassLoader() { public ASMClassLoader getASMClassLoader() {
@@ -138,6 +143,7 @@ public class Context {
/** /**
* Gets the id of this context * Gets the id of this context
*
* @return id context * @return id context
*/ */
public static int getID() { public static int getID() {
@@ -146,46 +152,52 @@ public class Context {
/** /**
* Tab id of this context * Tab id of this context
*
* @return tab id of this context * @return tab id of this context
*/ */
public int getTab() { public int getTab() {
return tab; return tab;
} }
/** /**
* Adds a paintable instance to the paintables * Adds a paintable instance to the paintables
*
* @param paintable * @param paintable
*/ */
public void addPaintable(Paintable paintable) { public void addPaintable(Paintable paintable) {
paintables.add(paintable); paintables.add(paintable);
} }
/** /**
* Removes a paintable instance from the paintables * Removes a paintable instance from the paintables
*
* @param paintable * @param paintable
*/ */
public void removePaintable(Paintable paintable) { public void removePaintable(Paintable paintable) {
paintables.remove(paintable); paintables.remove(paintable);
} }
/** /**
* Gets the paintable instances * Gets the paintable instances
*
* @return array of paintable instances * @return array of paintable instances
*/ */
public Paintable[] getPaintables() { public Paintable[] getPaintables() {
return paintables.toArray(new Paintable[paintables.size()]); return paintables.toArray(new Paintable[paintables.size()]);
} }
/** /**
* The client debug painter * The client debug painter
*
* @return debug painter * @return debug painter
*/ */
public PaintDebugger getPaintDebugger() { public PaintDebugger getPaintDebugger() {
return paintDebugger; return paintDebugger;
} }
/** /**
* Gets the main/client instance * Gets the main/client instance
*
* @return instance of the the client * @return instance of the the client
*/ */
public Object getClient() { public Object getClient() {
+69 -44
View File
@@ -1,80 +1,105 @@
package org.parabot.core.ui; package org.parabot.core.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JPopupMenu; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JToolBar; import javax.swing.JToolBar;
import javax.swing.WindowConstants;
import org.parabot.core.ui.components.BotToolbar; import org.parabot.core.ui.components.BotToolbar;
import org.parabot.core.ui.components.GamePanel; import org.parabot.core.ui.components.GamePanel;
import org.parabot.core.ui.components.LogArea; import org.parabot.core.ui.components.LogArea;
import org.parabot.core.ui.images.Images; import org.parabot.core.ui.images.Images;
import org.parabot.core.ui.utils.AwtUtil;
import org.parabot.core.ui.utils.SwingUtil; import org.parabot.core.ui.utils.SwingUtil;
/** /**
* Bot frame
* *
* @author Clisprail * @author Dane
* *
*/ */
public class BotUI extends JFrame { public class BotUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = -2126184292879805519L;
private static BotUI instance = null; private static BotUI instance = null;
private JMenuBar bar = null;
public static BotUI getInstance() { public static BotUI getInstance() {
return instance == null ? instance = new BotUI() : instance; return instance == null ? instance = new BotUI() : instance;
} }
public BotUI() { public BotUI() {
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
setDefaultLookAndFeelDecorated(false);
bar = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem screenshot = new JMenuItem("Screenshot");
screenshot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new Thread(new Runnable() {
@Override
public void run() {
} this.setTitle("Parabot");
}).start(); this.setResizable(false);
} this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}); this.setIconImage(Images.getResource("/org/parabot/core/ui/images/icon.png"));
file.add(screenshot); this.setLayout(new BorderLayout());
bar.add(file);
JScrollPane textPane = LogArea.getInstance(); int iToolbarHeight = 24;
GroupLayout layout = new GroupLayout(getContentPane()); int iGameHeight = 503;
JToolBar tool = BotToolbar.getInstance(); int iLogHeight = 128;
GamePanel pane = GamePanel.getInstance();
pane.addLoader(); JPanel panel = new JPanel();
getContentPane().setLayout(layout); panel.setLocation(0, 0);
setJMenuBar(bar); panel.setPreferredSize(new Dimension(765, iToolbarHeight + iGameHeight + iLogHeight));
setTitle("Parabot");
setIconImage(Images.getResource("/org/parabot/core/ui/images/icon.png")); JMenuBar menubar = new JMenuBar();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setResizable(false); JMenu mnuFile = new JMenu("File");
SwingUtil.center(this); JMenuItem itmScreenshot = new JMenuItem("Screenshot");
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))).addComponent(tool, 768, 768, 768).addComponent(pane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(textPane, 768, 768, 768)); itmScreenshot.addActionListener(this);
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(tool, 30, 30, 30).addComponent(pane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(textPane, 100, 100, 100).addContainerGap(58, Short.MAX_VALUE)));
LogArea.log("Welcome to Parabot v2"); mnuFile.add(itmScreenshot);
menubar.add(mnuFile);
this.setJMenuBar(menubar);
int x = 0;
int y = 0;
JToolBar toolbar = BotToolbar.getInstance();
toolbar.setPreferredSize(new Dimension(765, iToolbarHeight));
toolbar.setLocation(x, y);
y += iToolbarHeight;
GamePanel gamePanel = GamePanel.getInstance();
gamePanel.setPreferredSize(new Dimension(765, iGameHeight));
toolbar.setLocation(x, y);
y += iGameHeight;
JScrollPane scrlConsole = LogArea.getInstance();
scrlConsole.setPreferredSize(new Dimension(765, iLogHeight));
toolbar.setLocation(x, y);
panel.add(toolbar);
panel.add(gamePanel);
panel.add(scrlConsole);
this.add(panel, BorderLayout.CENTER);
SwingUtil.finalize(this);
LogArea.log("Welcome to Parabot!");
} }
public JMenuBar getBar() { @Override
return bar; public void actionPerformed(ActionEvent e) {
} String command = e.getActionCommand();
switch (command) {
case "Screenshot":
LogArea.log("Boop");
break;
default:
System.out.println("Invalid command: ");
}
}
} }
@@ -1,7 +1,5 @@
package org.parabot.core.ui.components; package org.parabot.core.ui.components;
import java.awt.Dimension;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Properties; import java.util.Properties;
@@ -21,22 +19,17 @@ public class LogArea extends JScrollPane {
private static final long serialVersionUID = 6571141103751675714L; private static final long serialVersionUID = 6571141103751675714L;
private static LogArea instance = null; private static LogArea instance = null;
private static LogTextArea logArea = new LogTextArea(); private static LogTextArea logArea = new LogTextArea();
private LogArea() { private LogArea() {
super(TextAreaLogHandler.TEXT_AREA, super(TextAreaLogHandler.TEXT_AREA, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
logArea.setPreferredSize(new Dimension(775, 80));
setPreferredSize(new Dimension(776, 100));
setSize(775, 100);
setVisible(true); setVisible(true);
registerLogging(); registerLogging();
} }
public static LogArea getInstance() { public static LogArea getInstance() {
return instance == null ? instance = new LogArea() : instance; return instance == null ? instance = new LogArea() : instance;
} }
private static final Logger log = Logger.getLogger("Bot"); private static final Logger log = Logger.getLogger("Bot");
public static void log(String s) { public static void log(String s) {
@@ -63,19 +56,15 @@ public class LogArea extends JScrollPane {
final Properties logging = new Properties(); final Properties logging = new Properties();
final String logFormatter = LogFormatter.class.getCanonicalName(); final String logFormatter = LogFormatter.class.getCanonicalName();
final String fileHandler = FileHandler.class.getCanonicalName(); final String fileHandler = FileHandler.class.getCanonicalName();
logging.setProperty("handlers", logging.setProperty("handlers", TextAreaLogHandler.class.getCanonicalName() + "," + fileHandler);
TextAreaLogHandler.class.getCanonicalName() + "," + fileHandler);
logging.setProperty(".level", "INFO"); logging.setProperty(".level", "INFO");
logging.setProperty(SystemConsoleHandler.class.getCanonicalName() logging.setProperty(SystemConsoleHandler.class.getCanonicalName() + ".formatter", logFormatter);
+ ".formatter", logFormatter);
logging.setProperty(fileHandler + ".formatter", logFormatter); logging.setProperty(fileHandler + ".formatter", logFormatter);
logging.setProperty(TextAreaLogHandler.class.getCanonicalName() logging.setProperty(TextAreaLogHandler.class.getCanonicalName() + ".formatter", logFormatter);
+ ".formatter", logFormatter);
final ByteArrayOutputStream logout = new ByteArrayOutputStream(); final ByteArrayOutputStream logout = new ByteArrayOutputStream();
try { try {
logging.store(logout, ""); logging.store(logout, "");
LogManager.getLogManager().readConfiguration( LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(logout.toByteArray()));
new ByteArrayInputStream(logout.toByteArray()));
} catch (final Exception ignored) { } catch (final Exception ignored) {
} }
} }