You are not logged in.

  • Login
Bitte besucht unser neues Forum unter https://forum.nitrado.net | Please visit our new Forum at https://forum.nitrado.net

Dear visitor, welcome to Nitrado.net Prepaid Gameserver Community-Support - Archiv. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

BlvckBytes

User / Kunde

  • "BlvckBytes" is male
  • "BlvckBytes" started this thread

Posts: 797

Location: (*blvckbytes).home

Occupation: Schüler -> HTL für Informationstechnologie und Netzwerktechnik

  • Send private message

1

Friday, December 29th 2017, 1:01am

TitleBuilder Klasse -> Title - ActionBar - Header&Footer

Hey!

Ich habe mir gerade eben einen TitleBuilder geschrieben, da ich den für ein neues Projekt brauche. Ich möchte den hier mal mit euch teilen und würde mich freuen,
wenn er auch benutzt wird und mir Fehler gemeldet werden, damit ich diese beheben kann. Eigentlich müsste der ganze Code von der 1.8 bis zur 1.11 problemlos laufen.

Das ganze besteht aus 3 Klassen, wobei 2 nur "Hilfsklassen" sind und die dritte die eigentliche Klasse ist. Wem das nicht gefällt, der kann es gerne in eine Klasse kopieren ;).

TitleType:
Ein Enum, mit welchem man dann angeben kann, was die Klasse machen soll.

Spoiler Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
package me.blvckbytes.lobbysystem.utils;

/**
 * Created by BlvckBytes on 20.05.2017.
 */
public enum TitleType {

    TITLE, ACTION_BAR, TAB

}


ReflectUtil:
Eine Klasse, welche mir die gängigen Methoden bietet, welche ich beim Arbeiten mit Reflect immer brauche.

Spoiler Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package me.blvckbytes.lobbysystem.utils;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * Created by BlvckBytes on 20.05.2017.
 */
public class ReflectUtil {

    public static Field modifiers = getField( Field.class, "modifiers" );

    /**
     * Searches for an NMS class with the given name. Autodetects the current
     * Server-Version.
     * @param name Name of the searched class
     * @return Found class or null
     */
    public static Class< ? > getNMSClass( String name ) {
        String version = Bukkit.getServer().getClass().getPackage().getName().split( "\\." )[ 3 ];
        try {
            return Class.forName( "net.minecraft.server." + version + "." + name );
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * Searches for an NMS class with the given name. Autodetects the current
     * Server-Version.
     * @param name Name of the searched class
     * @return Found class or null
     */
    public static Class< ? > getNMSClassE( String name ) throws Exception {
        String version = Bukkit.getServer().getClass().getPackage().getName().split( "\\." )[ 3 ];
        return Class.forName( "net.minecraft.server." + version + "." + name );
    }

    /**
     * Sends a Packet-Object to a player
     * @param to Receiver-Player
     * @param packet Packet to send
     */
    public static void sendPacket( Player to, Object packet ) {
        try {
            Object playerHandle = to.getClass().getMethod( "getHandle" ).invoke( to );
            Object playerConnection = playerHandle.getClass().getField( "playerConnection" ).get( playerHandle );
            playerConnection.getClass().getMethod( "sendPacket", getNMSClass( "Packet" ) ).invoke( playerConnection, packet );
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }

    /**
     * Tries to set the field with a certain name of an object
     * to the given value.
     * @param change Object which contains the field
     * @param name Name of the field to change
     * @param to New value for that field
     */
    public static void setField( Object change, String name, Object to ) {
        try {
            Field field = change.getClass().getDeclaredField( name );
            field.setAccessible( true );
            field.set( change, to );
            field.setAccessible( false );
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
    }

    /**
     * Tries to get a field from a class
     * @param clazz Class to get the field from
     * @param name Name of the field
     * @return Found field or null
     */
    public static Field getField( Class< ? > clazz, String name ) {
        try {
            Field field = clazz.getDeclaredField( name );
            field.setAccessible( true );
            if( Modifier.isFinal( field.getModifiers() ) ) {
                modifiers.set( field, field.getModifiers() & ~Modifier.FINAL );
            }
            return field;
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Gets the Version of the Server
     * @return Version as String
     */
    public String getVersion() {
        return Bukkit.getServer().getClass().getPackage().getName().split( "\\." )[ 3 ];
    }
}


TitleBuilder:
Die Klasse, welche (fast) die ganze Arbeit übernimmt.

Spoiler Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package me.blvckbytes.lobbysystem.utils;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by BlvckBytes on 20.05.2017.
 */
public class TitleBuilder extends ReflectUtil {

    private int fadeDur;
    private int stayDur;
    private String superTitle;
    private String subTitle;
    private TitleType type;
    private String text;
    private String header;
    private String footer;

    /**
     * Constructor of class which sets some default values
     */
    public TitleBuilder() {
        fadeDur = 10;
        stayDur = 20;
        superTitle = "§cSuper-Title text not defined!";
        subTitle = "§cSub-Title text not defined!";
        text = "§cAction-Bar text not defined!";
        header = "§cTab-Header text not defined!";
        footer = "§cTab-Footer text is not defined!";
        type = TitleType.TITLE;
    }

    /**
     * Sets the Super-Title of the object
     * @param text Input-String
     * @return Current object of builder
     */
    public TitleBuilder setSuperTitle( String text ) {
        this.superTitle = text;
        return this;
    }

    /**
     * Sets the Sub-Title of the object
     * @param text Input-String
     * @return Current object of builder
     */
    public TitleBuilder setSubTitle( String text ) {
        this.subTitle = text;
        return this;
    }

    /**
     * Sets the duration for how long the title will be displayed
     * @param seconds Time in seconds
     * @return Current object of builder
     */
    public TitleBuilder setStayDuration( int seconds ) {
        this.stayDur = seconds;
        return this;
    }

    /**
     * Sets the duration for how long the fade-in aswell as fade-out
     * animations will take
     * @param seconds Time in seconds
     * @return Current object of builder
     */
    public TitleBuilder setFadeDuration( int seconds ) {
        this.fadeDur = seconds;
        return this;
    }

    /**
     * Sets the text that will get displayed in the action-bar
     * @param text Text to display
     * @return Current object of builder
     */
    public TitleBuilder setText( String text ) {
        this.text = text;
        return this;
    }

    /**
     * Sets the text that will get displayed in the header
     * @param text Text to display
     * @return Current object of builder
     */
    public TitleBuilder setHeader( String text ) {
        this.header = text;
        return this;
    }
    /**
     * Sets the text that will get displayed in the footer
     * @param text Text to display
     * @return Current object of builder
     */
    public TitleBuilder setFooter( String text ) {
        this.footer = text;
        return this;
    }


    /**
     * Sets the TitleType which determines the actions of the send method
     * @param type TitleType of object
     * @return Current object of builder
     */
    public TitleBuilder setType( TitleType type ) {
        this.type = type;
        return this;
    }


    /**
     * Broadcasts the title to all online players
     */
    public void broadcast() {
        for( Player receiver : Bukkit.getOnlinePlayers() ) {
            sendTo( receiver );
        }
    }

    /**
     * Sends the title to all players in that list
     * @param receivers List of players to receive
     */
    public void sendTo( List< Player > receivers ) {
        for( Player receiver : receivers ) {
            sendTo( receiver );
        }
    }

    /**
     * Sends the title to the given player
     * @param receiver Receiver-Player
     */
    public void sendTo( Player receiver ) {
        String name = receiver.getName();
        text = text.replace( "%name%", name );
        superTitle = superTitle.replace( "%name%", name );
        subTitle = subTitle.replace( "%name%", name );
        header = header.replace( "%name%", name );
        footer = footer.replace( "%name%", name );

        Class< ? > icbcClass = getNMSClass( "IChatBaseComponent" );
        Class< ? > csClass = null;
        try {
            csClass = getNMSClassE( "ChatSerializer" );
        } catch( Exception ex ) {
            csClass = icbcClass.getDeclaredClasses()[ 0 ];
        }

        if( type.equals( TitleType.ACTION_BAR ) ) {
            try {

                Object icbc = csClass.getMethod( "a", String.class ).invoke( null, "{"text": "" + text + ""}" );
                Constructor< ? > packetConstructor = getNMSClass( "PacketPlayOutChat" ).getConstructor(
                    icbcClass, byte.class
                );
                Object packet = packetConstructor.newInstance( icbc, ( byte ) 2 );

                sendPacket( receiver, packet );
            } catch( Exception ex ) {
                ex.printStackTrace();
            }

            return;
        }

        if( type.equals( TitleType.TAB ) ) {
            try {

                Object icbcH = csClass.getMethod( "a", String.class ).invoke( null, "{"text": "" + header + ""}" );
                Object icbcF = csClass.getMethod( "a", String.class ).invoke( null, "{"text": "" + footer + ""}" );

                Constructor< ? > packetConstructor = getNMSClass( "PacketPlayOutPlayerListHeaderFooter" ).getConstructor();
                Object packet = packetConstructor.newInstance();

                setField( packet, "a", icbcH );
                setField( packet, "b", icbcF );

                sendPacket( receiver, packet );
            } catch( Exception ex ) {
                ex.printStackTrace();
            }

            return;
        }

        if( type.equals( TitleType.TITLE ) ) {
            try {

                Class< ? > etaClass = null;
                try {
                    etaClass = getNMSClassE( "EnumTitleAction" );
                } catch( Exception ex ) {
                    etaClass = getNMSClassE( "PacketPlayOutTitle" ).getDeclaredClasses()[ 0 ];
                }

                Constructor< ? > titleConstructor = getNMSClass( "PacketPlayOutTitle" ).getConstructor( etaClass, icbcClass );

                Object aArray = Array.newInstance( etaClass, 3 );
                Array.set( aArray, 0, etaClass.getField( "TITLE" ).get( null ) );
                Array.set( aArray, 1, etaClass.getField( "SUBTITLE" ).get( null ) );
                Array.set( aArray, 2, etaClass.getField( "TIMES" ).get( null ) );

                for( int i = 0; i <= 2; i++ ) {
                    String txt = i == 0 ? superTitle : subTitle;
                    Object action = Array.get( aArray, i );
                    Object icbc = csClass.getMethod( "a", String.class ).invoke( null, "{"text": "" + txt + ""}" );

                    if( i == 2 ) {
                        titleConstructor = getNMSClass( "PacketPlayOutTitle" ).getConstructor(
                                etaClass, icbcClass, int.class, int.class, int.class
                        );
                        Object packet = titleConstructor.newInstance( action, icbc, fadeDur, stayDur, fadeDur );
                        sendPacket( receiver, packet );

                        continue;
                    }

                    Object packet = titleConstructor.newInstance( action, icbc );
                    sendPacket( receiver, packet );
                }

            } catch( Exception ex ) {
                ex.printStackTrace();
            }

            return;
        }
    }
}


Und nun ein kleines Beispiel in meiner Main-Klasse:

Spoiler Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package me.blvckbytes.lobbysystem;

import me.blvckbytes.lobbysystem.utils.TitleBuilder;
import me.blvckbytes.lobbysystem.utils.TitleType;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * Created by BlvckBytes on 28.12.2017.
 */
public class Main extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents( this, this );
    }

    @EventHandler
    public void onJoin( PlayerJoinEvent event ) {
        new TitleBuilder()
            .setType( TitleType.TITLE )
            .setSuperTitle( "§3Willkommen, §b%name%§3!" )
            .setSubTitle( "§eViel Spaß auf Server.de" )
            .setFadeDuration( 15 )
            .setStayDuration( 35 )
            .sendTo( event.getPlayer() );

        new TitleBuilder()
            .setType( TitleType.ACTION_BAR )
            .setText( "§cDas ist nur ein §eBeispiel§6!" )
            .sendTo( event.getPlayer() );

        new TitleBuilder()
            .setType( TitleType.TAB )
            .setHeader( "§cZeile 1\n§bZeile 2, geht das? Natürlich! :D\n§8§m-------------\n" )
            .setFooter( "\n§8§m-------------\n§bNa, wie gehts, §c%name%§b?" )
            .sendTo( event.getPlayer() );
    }
}


Falls es Fragen gibt, können diese jederzeit hier gestellt werden! Und btw, sry, dass dieser Thread so langweilig aussieht, aber ich hatte irgendwie keine Lust mir das ganze Styling um diese
Uhrzeit anzutun, xD.

LG BlvckBytes

xtrumb

User / Kunde

  • "xtrumb" is male

Posts: 2,295

Location: hier

Occupation: Fachinformatiker für Systemintegration

Thanks: 16

  • Send private message

2

Friday, December 29th 2017, 2:15am

Sieht interessant aus ^^ Mal wieder Respekt, dass du es mit uns teilst :*
Mit freundlichen Grüßen
TrueMB

Dienstleistungs Service
Unser Csgo Team

BlvckBytes

User / Kunde

  • "BlvckBytes" is male
  • "BlvckBytes" started this thread

Posts: 797

Location: (*blvckbytes).home

Occupation: Schüler -> HTL für Informationstechnologie und Netzwerktechnik

  • Send private message

3

Friday, December 29th 2017, 2:52am

@xtrumb

Gerne doch :P.
Immer noch wach, huh? Ebenfalls keinen Schlafrhythmus? D:

xtrumb

User / Kunde

  • "xtrumb" is male

Posts: 2,295

Location: hier

Occupation: Fachinformatiker für Systemintegration

Thanks: 16

  • Send private message

4

Saturday, December 30th 2017, 3:21am

#Offtopic

Wie kommste darauf :D

Ich finde 3 Uhr ist eine normale Uhrzeit :'D
Mit freundlichen Grüßen
TrueMB

Dienstleistungs Service
Unser Csgo Team

xtrumb

User / Kunde

  • "xtrumb" is male

Posts: 2,295

Location: hier

Occupation: Fachinformatiker für Systemintegration

Thanks: 16

  • Send private message

5

Sunday, April 1st 2018, 11:11pm

Habe gerade mal deine Klassen versucht zu benutzen, bekommen allerdings etwas Rot an makiert :?

Mit freundlichen Grüßen
TrueMB

Dienstleistungs Service
Unser Csgo Team

MrAngel08

User / Kunde

  • "MrAngel08" is male

Posts: 339

Location: Deutschland

Occupation: Schüler

Thanks: 1

  • Send private message

6

Tuesday, April 3rd 2018, 2:55pm

Da fehlen die Escape-Sequenzen. Nitrado entfernt sie irgendwie immer in den Code-Tags. Es müsste ungefähr so aussehen:

This post has been edited 2 times, last edit by "MrAngel08" (Apr 3rd 2018, 3:00pm)


xtrumb

User / Kunde

  • "xtrumb" is male

Posts: 2,295

Location: hier

Occupation: Fachinformatiker für Systemintegration

Thanks: 16

  • Send private message

7

Tuesday, April 3rd 2018, 10:44pm

Oh alles klar :)

Ich danke dir erstmal vielmals und werde es morgen mal testen :)
Mit freundlichen Grüßen
TrueMB

Dienstleistungs Service
Unser Csgo Team

Bitte besucht unser neues Forum unter https://forum.nitrado.net| Please visit our new Forum at https://forum.nitrado.net