1111import net .minecraft .client .network .ServerInfo ;
1212import net .minecraft .entity .Entity ;
1313import net .minecraft .util .math .Vec3d ;
14+ import net .minecraft .world .World ;
1415
1516public class MiscUtils {
16- public static final boolean hasMoved (Entity entity ){
17+ public static final boolean hasMoved (final Entity entity ){
1718 return entity .prevX != entity .getX () || entity .prevY != entity .getY () || entity .prevZ != entity .getZ ();
1819 }
1920
20- public static final boolean isLookingAt (Entity entity , Entity player ){
21- Vec3d vec3d = player .getRotationVec (1.0F ).normalize ();
21+ public static final boolean isLookingAt (final Entity entity , final Entity player ){
22+ final Vec3d vec3d = player .getRotationVec (1f ).normalize ();
2223 Vec3d vec3d2 = new Vec3d (entity .getX () - player .getX (), entity .getEyeY () - player .getEyeY (), entity .getZ () - player .getZ ());
23- double d = vec3d2 .length ();
24+ final double d = vec3d2 .length ();
2425 vec3d2 = new Vec3d (vec3d2 .x / d , vec3d2 .y / d , vec3d2 .z / d );//normalize
25- double e = vec3d .dotProduct (vec3d2 );
26+ final double e = vec3d .dotProduct (vec3d2 );
2627 return e > 1.0D - 0.03D / d ? /*client.player.canSee(entity)*/ true : false ;
2728 }
2829
30+ public static final byte getDimensionId (final World world ){
31+ if (world == null ) return -1 ;
32+ else if (world .getRegistryKey () == World .OVERWORLD ) return 0 ;
33+ else if (world .getRegistryKey () == World .NETHER ) return 1 ;
34+ else if (world .getRegistryKey () == World .END ) return 2 ;
35+ else return 3 ;
36+ }
37+
2938 private static final String ADDRESS_2B2T = "2b2t.org" ; // TODO: make EvMod more server-independent
3039 public static final int HASHCODE_2B2T = ADDRESS_2B2T .hashCode (); // -437714968;
31- // private static final boolean USE_CANONICAL_IP = false;
32- public static final String getServerAddress (ServerInfo serverInfo , boolean USE_CANONICAL_IP ){
33- if (serverInfo == null ) return null ;
40+ private static final String getServerAddress (final ServerInfo serverInfo , final boolean USE_CANONICAL_IP ){
41+ // if(serverInfo == null) return null;
3442 final String name = Normalizer .normalize (serverInfo .name , Normalizer .Form .NFKD ).toLowerCase ().replaceAll ("[^\\ p{IsAlphabetic}\\ p{IsDigit}]+" , "" );
3543 // TODO: Sync with proxy via some API, and have it tell us what server the backend is connecting to?
36- if (name .contains ("2b2tproxy" )) return "2b2t.org" ;
44+ if (name .contains ("2b2tproxy" )) return ADDRESS_2B2T ;
3745
38- String address = serverInfo .address .toLowerCase ();
46+ final String address = serverInfo .address .toLowerCase ();
3947 switch (address ){
40- case "2b2t.org" :
48+ case ADDRESS_2B2T : // "2b2t.org"
4149 case "connect.2b2t.org" :
4250 return ADDRESS_2B2T ;
4351 default :
@@ -55,22 +63,23 @@ public static final String getServerAddress(ServerInfo serverInfo, boolean USE_C
5563 return address ;
5664 }
5765 }
58- public static final String getServerAddress (){ // Will never return null
59- MinecraftClient client = MinecraftClient .getInstance ();
60- if (client == null ) return "null0" ;
61- if ( client . getCurrentServerEntry () == null ){
62- return client . getServer () == null ? "null1" : client .getServer (). getSaveProperties (). getLevelName ();
63- }
64- return getServerAddress ( client .getCurrentServerEntry (), /*useCanonical=*/ false ) ;
66+ private static final String getServerAddress (final boolean USE_CANONICAL_IP ){
67+ final MinecraftClient client = MinecraftClient .getInstance ();
68+ // if(client == null) return "null0";
69+ assert client != null ;
70+ final ServerInfo serverInfo = client .getCurrentServerEntry ();
71+ return serverInfo != null ? getServerAddress ( serverInfo , USE_CANONICAL_IP )
72+ : client .getServer () != null ? client . getServer (). getSaveProperties (). getLevelName () : null ;
6573 }
74+ public static final String getServerAddress (){return getServerAddress (/*useCanonical=*/ false );}
6675
67- public static final int getServerAddressHashCode (ServerInfo serverInfo ){
68- return serverInfo == null ? 0 : getServerAddress (serverInfo , /*useCanonical=*/ true ). hashCode ( );
69- }
70- public static final int getCurrentServerAddressHashCode (){
71- MinecraftClient client = MinecraftClient . getInstance ();
72- if ( client == null || client . getCurrentServerEntry () == null ) return 0 ;
73- return getServerAddress ( client . getCurrentServerEntry (), /*useCanonical=*/ true ). hashCode () ;
76+ public static final int getServerAddressHashCode (){
77+ final String address = getServerAddress (/*useCanonical=*/ true );
78+ return switch ( address ){
79+ case null -> 0 ;
80+ case ADDRESS_2B2T -> HASHCODE_2B2T ; // Tiny optimization (no need to compute hash), since we store it anyway
81+ default -> address . hashCode () ;
82+ } ;
7483 }
7584
7685 // TODO: on db-side, create a function that can reverse uuid -> username
@@ -83,7 +92,7 @@ private static final UUID encodeAsUUID(final String str){
8392 return new UUID (bb .getLong (), bb .getLong ());
8493 }
8594
86- public static final byte [] getEncodedPlayerIds (MinecraftClient client ){
95+ public static final byte [] getEncodedPlayerIds (final MinecraftClient client ){
8796 final String sessionName = client .getSession ().getUsername (), playerName = client .player .getGameProfile ().getName ();
8897 final UUID sessionUUID = client .getSession ().getUuidOrNull (), playerUUID = client .player .getGameProfile ().getId ();
8998 final UUID usableSessionUUID = sessionUUID != null ? sessionUUID : MiscUtils .encodeAsUUID (sessionName );
0 commit comments