Skip to content

Commit

Permalink
Added the Checkstyle configuration and fixed the code according to it
Browse files Browse the repository at this point in the history
  • Loading branch information
bibo38 committed Mar 15, 2016
1 parent b6e2641 commit b18b868
Show file tree
Hide file tree
Showing 28 changed files with 576 additions and 462 deletions.
10 changes: 10 additions & 0 deletions .checkstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>

<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<local-check-config name="Bibo Check" location="config/checkstyle/checkstyle.xml" type="project" description="">
<additional-data name="protect-config-file" value="false"/>
</local-check-config>
<fileset name="all" enabled="true" check-config-name="Bibo Check" local="true">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "config/checkstyle"]
path = config/checkstyle
url = https://github.com/bibo38/Checkstyle.git
9 changes: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
apply plugin: 'java'
apply plugin: 'eclipse'
plugins {
id 'java'
id 'eclipse'
id 'checkstyle'
}

version = 1.2

Expand Down Expand Up @@ -30,3 +33,5 @@ dependencies {
task wrapper(type: Wrapper) {
gradleVersion = '2.4'
}

apply from: 'config/checkstyle/checkstyle.gradle'
1 change: 1 addition & 0 deletions config/checkstyle
Submodule checkstyle added at 05da0d
67 changes: 37 additions & 30 deletions src/me/bibo38/Bibo38Lib/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,83 @@

import java.util.Arrays;

public class Base64
public final class Base64
{
private final static String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static final String SINGLE_PADDING = "=";
private static final String DOUBLE_PADDING = SINGLE_PADDING + SINGLE_PADDING;
private static final int ENCODED_SIZE = 4;
private static final int DECODED_SIZE = 3; // 3 Bytes to 4 characters

public static String encode(byte[] data)

private Base64() {}

public static String encode(byte data[])
{
String ret = "";
int conv[] = new int[4];
int padding = data.length % 3;
int conv[] = new int[ENCODED_SIZE];
int padding = data.length % DECODED_SIZE;

if(padding != 0)
{
padding = 3 - padding; // 1 byte to much means two bytes padding
padding = DECODED_SIZE - padding; // 1 byte to much means two bytes padding
data = Arrays.copyOf(data, data.length + padding);

// Ensure the rest is filled up with zeros
data[data.length-1] = 0;
data[data.length - 1] = 0;
if(padding == 2)
data[data.length-2] = 0;
data[data.length - 2] = 0;
}

for(int i = 0; i < data.length; i += 3)
for(int i = 0; i < data.length; i += DECODED_SIZE)
{
conv[0] = (data[i] & 0xFF) >> 2;
conv[1] = ((data[i] & 0x3) << 4) | ((data[i+1] & 0xFF) >> 4);
conv[2] = ((data[i+1] & 0xF) << 2) | ((data[i+2] & 0xFF) >> 6);
conv[3] = data[i+2] & 0x3F;
conv[1] = ((data[i] & 0x3) << 4) | ((data[i + 1] & 0xFF) >> 4);
conv[2] = ((data[i + 1] & 0xF) << 2) | ((data[i + 2] & 0xFF) >> 6);
conv[3] = data[i + 2] & 0x3F;

for(int j = 0; j < 4; j++)
ret += characters.charAt(conv[j]);
for(int j = 0; j < ENCODED_SIZE; j++)
ret += CHARACTERS.charAt(conv[j]);
}

if(padding != 0)
ret = ret.substring(0, ret.length() - padding) + ((padding == 1)? "=" : "==");
ret = ret.substring(0, ret.length() - padding) + ((padding == 1)? SINGLE_PADDING : DOUBLE_PADDING);

return ret;
}

public static byte[] decode(String encoded)
{
encoded = encoded.replace("=", "");
int padding = encoded.length() % 4;
encoded = encoded.replace(SINGLE_PADDING, "");
int padding = encoded.length() % ENCODED_SIZE;

if(padding != 0)
{
if(padding == 1)
throw new IllegalArgumentException("Single remaining encoded character is not possible");
padding = 4 - padding;
encoded += (padding == 1)? "=" : "==";
padding = ENCODED_SIZE - padding;
encoded += (padding == 1)? SINGLE_PADDING : DOUBLE_PADDING;
}

byte[] ret = new byte[encoded.length() / 4 * 3 - padding];
int[] conv = new int[4];
byte ret[] = new byte[encoded.length() / ENCODED_SIZE * DECODED_SIZE - padding];
int conv[] = new int[ENCODED_SIZE];

for(int i = 0; i < ret.length; i += 3)
for(int i = 0; i < ret.length; i += DECODED_SIZE)
{
char[] aktEnc = encoded.substring(0, 4).toCharArray();
encoded = encoded.substring(4);
for(int j = 0; j < 4; j++)
conv[j] = characters.indexOf(aktEnc[j]);
char aktEnc[] = encoded.substring(0, ENCODED_SIZE).toCharArray();
encoded = encoded.substring(ENCODED_SIZE);
for(int j = 0; j < ENCODED_SIZE; j++)
conv[j] = CHARACTERS.indexOf(aktEnc[j]);

ret[i] = (byte) ((conv[0] << 2) | (conv[1] >> 4));

if(i+1 == ret.length)
if(i + 1 == ret.length)
break;
ret[i+1] = (byte) (((conv[1] & 0xF) << 4) | (conv[2] >> 2));
ret[i + 1] = (byte) (((conv[1] & 0xF) << 4) | (conv[2] >> 2));

if(i+2 == ret.length)
if(i + 2 == ret.length)
break;
ret[i+2] = (byte) (((conv[2] & 0x3) << 6) | conv[3]);
ret[i + 2] = (byte) (((conv[2] & 0x3) << 6) | conv[3]);
}

return ret;
Expand Down
6 changes: 3 additions & 3 deletions src/me/bibo38/Bibo38Lib/Bibo38Lib.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

public class Bibo38Lib extends JavaPlugin
{
private PluginDescriptionFile pdFile;
private Logger log;

public boolean vaultOn = false;
public Language lang;

public String jdbcURL;
public String jdbcUser;
public String jdbcPass;

private PluginDescriptionFile pdFile;
private Logger log;

private WebServer webServ = null;

@Override
Expand Down
67 changes: 39 additions & 28 deletions src/me/bibo38/Bibo38Lib/Compability.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Compability
public final class Compability
{
private static final int magic = 0xCAFEBABE;
private static final int MAGIC = 0xCAFEBABE;
private static final int BUFFER_SIZE = 1024;

public static Class<?> loadCompatible(InputStream is, String name) throws NoSuchMethodException, SecurityException, IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
private Compability() {}

public static Class<?> loadCompatible(InputStream is, String name) throws Exception
{
ClassLoader cl = Compability.class.getClassLoader();
Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
defineClass.setAccessible(true);

Expand All @@ -24,9 +24,9 @@ public static Class<?> loadCompatible(InputStream is, String name) throws NoSuch

String packageVersion = Utils.getPackageVersion();

if(dis.readInt() != magic) // Magic Bytes
if(dis.readInt() != MAGIC) // Magic Bytes
throw new ClassFormatError("Wrong magic Bytes");
dos.writeInt(magic);
dos.writeInt(MAGIC);

dos.writeInt(dis.readInt()); // Major/Minor Version

Expand All @@ -37,31 +37,34 @@ public static Class<?> loadCompatible(InputStream is, String name) throws NoSuch
byte tag = dis.readByte();
dos.writeByte(tag);
int copySize = -1;
switch(tag)

if(tag < 0 || tag >= TagType.values().length)
tag = (byte) TagType.INVALID.ordinal();
switch(TagType.values()[tag])
{
case 1: // Utf8
case UTF8:
String text = dis.readUTF();
text = text.replaceAll("net/minecraft/server/[^/]+", "net/minecraft/server/"+packageVersion).
replaceAll("org/bukkit/craftbukkit/[^/]+", "org/bukkit/craftbukkit/"+packageVersion);
text = text.replaceAll("net/minecraft/server/[^/]+", "net/minecraft/server/" + packageVersion).
replaceAll("org/bukkit/craftbukkit/[^/]+", "org/bukkit/craftbukkit/" + packageVersion);
dos.writeUTF(text);
break;

case 5: // Long
case 6: copySize = 8; // Double
case LONG:
case DOUBLE: copySize = 8;
break;
case 7: // Class
case 8: // String
case 16: copySize = 2; // MethodType
case CLASS:
case STRING:
case METHOD_TYPE: copySize = 2;
break;
case 3: // Integer
case 4: // Float
case 9: // Fieldref
case 10: // Methodref
case 11: // InterfaceMethodref
case 12: // NameAndType
case 18: copySize = 4; // InvokeDynamic
case INTEGER:
case FLOAT:
case FIELDREF:
case METHODREF:
case INTERFACE_METHODREF:
case NAME_AND_TYPE:
case INVOKE_DYNAMIC: copySize = 4;
break;
case 15: copySize = 3; // MethodHandle
case METHOD_HANDLE: copySize = 3;
break;
default:
// throw new ClassFormatError("Unknown Tag "+tag);
Expand All @@ -70,12 +73,20 @@ public static Class<?> loadCompatible(InputStream is, String name) throws NoSuch
os.write(is.read());
}

byte data[] = new byte[1024];
byte data[] = new byte[BUFFER_SIZE];
int len;
while((len = is.read(data)) != -1)
os.write(data, 0, len);

byte[] out = os.toByteArray();
return (Class<?>) defineClass.invoke(cl, name, out, 0, out.length);
byte out[] = os.toByteArray();
return (Class<?>) defineClass.invoke(Compability.class.getClassLoader(), name, out, 0, out.length);
}
}

enum TagType
{
INVALID, UTF8, UNK2, INTEGER, FLOAT,
LONG, DOUBLE, CLASS, STRING, FIELDREF,
METHODREF, INTERFACE_METHODREF, NAME_AND_TYPE, UNK13, UNK14,
METHOD_HANDLE, METHOD_TYPE, UNK17, INVOKE_DYNAMIC;
}
41 changes: 23 additions & 18 deletions src/me/bibo38/Bibo38Lib/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
import java.util.UUID;
import java.util.regex.Pattern;

public class Utils
public final class Utils
{
public static final int MAX_FOOD_LEVEL = 20;
public static final float MAX_SATURATION_LEVEL = MAX_FOOD_LEVEL;

private Utils() {}

// Reflection Utils
public static void setVal(Field f, Object o, Object val) throws IllegalArgumentException, IllegalAccessException
public static void setVal(Field f, Object o, Object val) throws ReflectiveOperationException
{
boolean access = f.isAccessible();
final boolean access = f.isAccessible();
f.setAccessible(true);
if(val.getClass() != f.getType())
val = convert(val, f.getType());
Expand Down Expand Up @@ -89,7 +94,7 @@ public static Object convert(Object o, Class<?> c)
try
{
ret = c.cast(o); // try casting
} catch(Exception e1)
} catch(Exception e)
{
// Try serialisation
if(o instanceof String && Serializable.class.isAssignableFrom(c))
Expand All @@ -98,9 +103,9 @@ public static Object convert(Object o, Class<?> c)
{
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(Base64.decode((String) o)));
ret = ois.readObject();
} catch(Exception e2)
} catch(Exception ex)
{
e2.printStackTrace();
ex.printStackTrace();
}
} else if(c == String.class && o instanceof Serializable)
{
Expand All @@ -111,17 +116,17 @@ public static Object convert(Object o, Class<?> c)
oos.writeObject(o);
oos.flush();
return Base64.encode(bos.toByteArray());
} catch(Exception e2)
} catch(Exception ex)
{
e2.printStackTrace();
ex.printStackTrace();
}
} else
e1.printStackTrace();
e.printStackTrace();
}
return ret;
}

public static Object getVal(Field f, Object o) throws IllegalArgumentException, IllegalAccessException
public static Object getVal(Field f, Object o) throws ReflectiveOperationException
{
Object ret;
boolean access = f.isAccessible();
Expand Down Expand Up @@ -169,9 +174,7 @@ public static void normalizePlayer(Player p, GameMode gm)
clearInventory(p);
p.setFlying(false);
p.setAllowFlight(false);
p.setFoodLevel(20);
p.setSaturation(20);
p.setExhaustion(0);
heal(p);
p.setLevel(0);
p.setTotalExperience(0);
p.setExp(0);
Expand Down Expand Up @@ -218,8 +221,8 @@ public static ItemStack getSkullWithName(String name)
public static void heal(Player p)
{
p.setHealthScale(1D);
p.setFoodLevel(20);
p.setSaturation(4.0F);
p.setFoodLevel(MAX_FOOD_LEVEL);
p.setSaturation(MAX_SATURATION_LEVEL);
p.setExhaustion(0);
}

Expand All @@ -232,12 +235,14 @@ public static void setItemName(ItemStack i, String name)
i.setItemMeta(im);
}

public static UUID getUUID(String s)
public static UUID getID(String s)
{
try
{
return UUID.fromString(s);
} catch(Exception e) {}
} catch(Exception e)
{
}
return null;
}

Expand All @@ -246,7 +251,7 @@ public static Player getPlayer(String name)
for(Player akt : Bukkit.getOnlinePlayers())
if(akt.getName().equals(name))
return akt;
return Bukkit.getPlayer(getUUID(name));
return Bukkit.getPlayer(getID(name));
}

public static OfflinePlayer[] getOfflinePlayers(String name)
Expand Down
Loading

0 comments on commit b18b868

Please sign in to comment.