Skip to content

Commit

Permalink
Restore full Java 8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jul 9, 2022
1 parent 516066a commit 27ba9eb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ abstract class SpoofedLoginResult extends LoginResult {

static {
Class<? extends SpoofedLoginResult> implClass;
try {
// try to use stackwalker if running Java 9 or newer
Class.forName("java.lang.StackWalker");
implClass = Class.forName("me.lucko.bungeeguard.bungee.SpoofedLoginResultJava9").asSubclass(SpoofedLoginResult.class);
} catch (ClassNotFoundException e) {
implClass = SpoofedLoginResultJava8.class;
if (classExists("java.lang.StackWalker")) {
try {
implClass = Class.forName("me.lucko.bungeeguard.bungee.SpoofedLoginResultJava9")
.asSubclass(SpoofedLoginResult.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else if (classExists("jdk.internal.reflect.Reflection")) {
implClass = SpoofedLoginResultJdkInternal.class;
} else {
implClass = SpoofedLoginResultReflection.class;
}

try {
Expand Down Expand Up @@ -89,6 +94,15 @@ static void inject(InitialHandler handler, String token) {
}
}

private static boolean classExists(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

private final Property bungeeGuardToken;
private final Property[] bungeeGuardTokenArray;
private final boolean offline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
import jdk.internal.reflect.Reflection;
import net.md_5.bungee.protocol.Property;

public class SpoofedLoginResultJava8 extends SpoofedLoginResult {
public class SpoofedLoginResultJdkInternal extends SpoofedLoginResult {

// online mode constructor
public SpoofedLoginResultJava8(LoginResult oldProfile, String bungeeGuardToken) {
public SpoofedLoginResultJdkInternal(LoginResult oldProfile, String bungeeGuardToken) {
super(oldProfile, bungeeGuardToken);
}

// offline mode constructor
public SpoofedLoginResultJava8(String bungeeGuardToken) {
public SpoofedLoginResultJdkInternal(String bungeeGuardToken) {
super(bungeeGuardToken);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This file is part of BungeeGuard, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.bungeeguard.bungee;

import net.md_5.bungee.connection.LoginResult;
import net.md_5.bungee.protocol.Property;

public class SpoofedLoginResultReflection extends SpoofedLoginResult {

// online mode constructor
public SpoofedLoginResultReflection(LoginResult oldProfile, String bungeeGuardToken) {
super(oldProfile, bungeeGuardToken);
}

// offline mode constructor
public SpoofedLoginResultReflection(String bungeeGuardToken) {
super(bungeeGuardToken);
}

@Override
public Property[] getProperties() {
StackTraceElement[] trace = new Exception().getStackTrace();

Class<?> caller = null;
if (trace.length >= 2) {
try {
caller = Class.forName(trace[1].getClassName());
} catch (ClassNotFoundException e) {
// ignore
}
}

return getSpoofedProperties(caller);
}
}

0 comments on commit 27ba9eb

Please sign in to comment.