Thanks to gonglinyuan
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.ArrayList;
|
||||
import org.jackhuang.hellominecraft.util.func.Consumer;
|
||||
|
||||
/**
|
||||
@@ -27,9 +27,7 @@ import org.jackhuang.hellominecraft.util.func.Consumer;
|
||||
*/
|
||||
public class EventHandler<T> {
|
||||
|
||||
HashSet<Event<T>> handlers = new HashSet<>();
|
||||
HashSet<Consumer<T>> consumers = new HashSet<>();
|
||||
HashSet<Runnable> runnables = new HashSet<>();
|
||||
ArrayList<Object> events = new ArrayList<>();
|
||||
Object sender;
|
||||
|
||||
public EventHandler(Object sender) {
|
||||
@@ -37,38 +35,42 @@ public class EventHandler<T> {
|
||||
}
|
||||
|
||||
public void register(Event<T> t) {
|
||||
handlers.add(t);
|
||||
if (!events.contains(t))
|
||||
events.add(t);
|
||||
}
|
||||
|
||||
public void register(Consumer<T> t) {
|
||||
consumers.add(t);
|
||||
if (!events.contains(t))
|
||||
events.add(t);
|
||||
}
|
||||
|
||||
public void register(Runnable t) {
|
||||
runnables.add(t);
|
||||
if (!events.contains(t))
|
||||
events.add(t);
|
||||
}
|
||||
|
||||
public void unregister(Event<T> t) {
|
||||
handlers.remove(t);
|
||||
events.remove(t);
|
||||
}
|
||||
|
||||
public void unregister(Consumer<T> t) {
|
||||
consumers.remove(t);
|
||||
events.remove(t);
|
||||
}
|
||||
|
||||
public void unregister(Runnable t) {
|
||||
runnables.remove(t);
|
||||
events.remove(t);
|
||||
}
|
||||
|
||||
public boolean execute(T x) {
|
||||
boolean flag = true;
|
||||
for (Event<T> t : handlers)
|
||||
if (!t.call(sender, x))
|
||||
flag = false;
|
||||
for (Consumer<T> t : consumers)
|
||||
t.accept(x);
|
||||
for (Runnable t : runnables)
|
||||
t.run();
|
||||
for (Object t : events)
|
||||
if (t instanceof Event) {
|
||||
if (!((Event) t).call(sender, x))
|
||||
flag = false;
|
||||
} else if (t instanceof Consumer)
|
||||
((Consumer) t).accept(x);
|
||||
else if (t instanceof Runnable)
|
||||
((Runnable) t).run();
|
||||
return flag;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util.func;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface CallbackIO<T> {
|
||||
|
||||
void call(T t) throws IOException;
|
||||
}
|
||||
@@ -28,7 +28,7 @@ import org.jackhuang.hellominecraft.util.StrUtils;
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public final class JdkVersion {
|
||||
public final class JdkVersion implements Cloneable {
|
||||
|
||||
private String ver;
|
||||
|
||||
@@ -69,6 +69,15 @@ public final class JdkVersion {
|
||||
return location == null ? 0 : new File(location).hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() {
|
||||
try {
|
||||
return super.clone();
|
||||
} catch (CloneNotSupportedException ex) {
|
||||
throw new Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public JdkVersion(String location) {
|
||||
File f = new File(location);
|
||||
if (f.exists() && f.isFile())
|
||||
@@ -103,13 +112,13 @@ public final class JdkVersion {
|
||||
*/
|
||||
public static final int JAVA_19 = 6;
|
||||
|
||||
private static final String javaVersion;
|
||||
private static final int majorJavaVersion;
|
||||
private static final String JAVA_VER;
|
||||
private static final int MAJOR_JAVA_VER;
|
||||
|
||||
static {
|
||||
javaVersion = System.getProperty("java.version");
|
||||
JAVA_VER = System.getProperty("java.version");
|
||||
// version String should look like "1.4.2_10"
|
||||
majorJavaVersion = parseVersion(javaVersion);
|
||||
MAJOR_JAVA_VER = parseVersion(JAVA_VER);
|
||||
}
|
||||
|
||||
private static int parseVersion(String javaVersion) {
|
||||
@@ -136,7 +145,7 @@ public final class JdkVersion {
|
||||
* @see System#getProperty(String)
|
||||
*/
|
||||
public static String getJavaVersion() {
|
||||
return javaVersion;
|
||||
return JAVA_VER;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,7 +164,7 @@ public final class JdkVersion {
|
||||
* @see #JAVA_17
|
||||
*/
|
||||
public static int getMajorJavaVersion() {
|
||||
return majorJavaVersion;
|
||||
return MAJOR_JAVA_VER;
|
||||
}
|
||||
|
||||
public static boolean isJava64Bit() {
|
||||
|
||||
@@ -115,10 +115,11 @@ public class ZipEngine {
|
||||
|
||||
public void putStream(InputStream is, String pathName) throws IOException {
|
||||
int length;
|
||||
BufferedInputStream bis = new BufferedInputStream(is);
|
||||
zos.putNextEntry(new ZipEntry(pathName));
|
||||
while ((length = bis.read(buf)) > 0)
|
||||
zos.write(buf, 0, length);
|
||||
try (BufferedInputStream bis = new BufferedInputStream(is)) {
|
||||
zos.putNextEntry(new ZipEntry(pathName));
|
||||
while ((length = bis.read(buf)) > 0)
|
||||
zos.write(buf, 0, length);
|
||||
}
|
||||
}
|
||||
|
||||
public void putTextFile(String text, String pathName) throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user