Skip to content

Commit f63673a

Browse files
Messenger & WorldManager Rework (#60)
* Rework Messenger and WorldManager * fix errors * Make ChatType extendable * moved some parts of the implementation to the api --------- Co-authored-by: _tud <mmbakkar06@gmail.com>
1 parent 0ccd250 commit f63673a

File tree

12 files changed

+529
-315
lines changed

12 files changed

+529
-315
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* This file is part of Machine.
3+
*
4+
* Machine is free software: you can redistribute it and/or modify it under the terms of the
5+
* GNU General Public License as published by the Free Software Foundation,
6+
* either version 3 of the License, or (at your option) any later version.
7+
*
8+
* Machine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10+
* See the GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License along with Machine.
13+
* If not, see https://www.gnu.org/licenses/.
14+
*/
15+
package org.machinemc.api.chat;
16+
17+
import lombok.AllArgsConstructor;
18+
import lombok.Getter;
19+
import org.jetbrains.annotations.Nullable;
20+
import org.machinemc.api.server.NBTSerializable;
21+
import org.machinemc.api.utils.NamespacedKey;
22+
import org.machinemc.nbt.NBTCompound;
23+
import org.machinemc.nbt.NBTList;
24+
import org.machinemc.scriptive.style.ChatStyle;
25+
import org.machinemc.scriptive.style.TextFormat;
26+
27+
import java.util.*;
28+
29+
public interface ChatType extends NBTSerializable {
30+
31+
/**
32+
* @return namespaced key of the chat type
33+
*/
34+
NamespacedKey getName();
35+
36+
/**
37+
* @return chat element of the chat type
38+
*/
39+
Element getChatElement();
40+
41+
/**
42+
* @return narration element of the chat type
43+
*/
44+
Element getNarrationElement();
45+
46+
/**
47+
* Chat and Narration types of chat types, contain information
48+
* about their parameters, translation key and chat format.
49+
* @param type type of the element
50+
* @param parameters parameters of the element
51+
* @param translationKey translation key of the element
52+
* @param format format of the element
53+
* @param font font of the element
54+
*/
55+
record Element(ElementType type,
56+
Collection<Parameter> parameters,
57+
String translationKey,
58+
@Nullable TextFormat format,
59+
@Nullable NamespacedKey font) implements NBTSerializable {
60+
61+
/**
62+
* Creates new element of type chat.
63+
* @param parameters parameters of the element
64+
* @param translationKey translation key of the element
65+
* @param format chat format of the element
66+
* @param font font of the element
67+
* @return created chat type element
68+
*/
69+
public static Element chat(final Collection<Parameter> parameters,
70+
final String translationKey,
71+
final @Nullable TextFormat format,
72+
final @Nullable NamespacedKey font) {
73+
return new Element(ElementType.CHAT, parameters, translationKey, format, font);
74+
}
75+
/**
76+
* Creates new element of type narration.
77+
* @param parameters parameters of the element
78+
* @param translationKey translation key of the element
79+
* @param format chat format of the element
80+
* @param font font of the element
81+
* @return created chat type element
82+
*/
83+
public static Element narration(final Collection<Parameter> parameters,
84+
final String translationKey,
85+
final @Nullable TextFormat format,
86+
final @Nullable NamespacedKey font) {
87+
return new Element(ElementType.NARRATION, parameters, translationKey, format, font);
88+
}
89+
90+
@Override
91+
public NBTCompound toNBT() {
92+
final NBTList parameters = new NBTList(this.parameters.stream().map(Parameter::getName).toList());
93+
final Map<String, String> styleMap = new HashMap<>();
94+
if (format != null) {
95+
final Map<ChatStyle, Boolean> styles = format.getStyles();
96+
for (final Map.Entry<ChatStyle, Boolean> entry : styles.entrySet()) {
97+
if (entry.getValue() != null)
98+
styleMap.put(entry.getKey().name().toLowerCase(Locale.ENGLISH), entry.getValue().toString());
99+
}
100+
format.getColor().ifPresent(color -> styleMap.put("color", color.getName()));
101+
if (font != null)
102+
styleMap.put("font", font.toString());
103+
}
104+
final NBTCompound style = new NBTCompound();
105+
for (final String key : styleMap.keySet())
106+
style.set(key, styleMap.get(key));
107+
return new NBTCompound(Map.of(
108+
"translation_key", translationKey,
109+
"parameters", parameters,
110+
"style", style
111+
));
112+
}
113+
114+
}
115+
116+
/**
117+
* Type of chat type element.
118+
*/
119+
@AllArgsConstructor
120+
enum ElementType {
121+
CHAT("chat"),
122+
NARRATION("narration");
123+
124+
@Getter
125+
private final String name;
126+
}
127+
128+
/**
129+
* Parameters used by chat type elements.
130+
*/
131+
@AllArgsConstructor
132+
enum Parameter {
133+
SENDER("sender"),
134+
TARGET("target"),
135+
CONTENT("content");
136+
137+
@Getter
138+
private final String name;
139+
}
140+
141+
}

api/src/main/java/org/machinemc/api/chat/Messenger.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,91 @@
1414
*/
1515
package org.machinemc.api.chat;
1616

17+
import org.jetbrains.annotations.Nullable;
18+
import org.jetbrains.annotations.Unmodifiable;
1719
import org.machinemc.api.entities.Player;
1820
import org.machinemc.api.server.ServerProperty;
1921
import org.machinemc.api.server.codec.CodecPart;
22+
import org.machinemc.api.utils.NamespacedKey;
23+
import org.machinemc.nbt.NBTCompound;
2024
import org.machinemc.scriptive.components.Component;
2125

26+
import java.util.Set;
27+
2228
public interface Messenger extends CodecPart, ServerProperty {
2329

30+
/**
31+
* Registers new chat type to the messenger if it's not registered already
32+
* in a different one.
33+
* @param chatType chat type to register
34+
*/
35+
void addChatType(ChatType chatType);
36+
37+
/**
38+
* Removed a chat type with given name if it's registered in this messenger.
39+
* @param name name of the chat type
40+
* @return if the chat type with given name was successfully removed
41+
*/
42+
default boolean removeChatType(NamespacedKey name) {
43+
final ChatType chatType = getChatType(name);
44+
if (chatType == null)
45+
return false;
46+
return removeChatType(chatType);
47+
}
48+
49+
/**
50+
* Removes the chat type from the messenger if it's registered in this messenger.
51+
* @param chatType chat type to remove
52+
* @return if the chat type was successfully removed
53+
*/
54+
boolean removeChatType(ChatType chatType);
55+
56+
/**
57+
* Checks if chat type with given name is registered in
58+
* the messenger.
59+
* @param name name of the chat type
60+
* @return if the chat type with given name is registered in this messenger
61+
*/
62+
default boolean isRegistered(NamespacedKey name) {
63+
final ChatType chatType = getChatType(name);
64+
if (chatType == null)
65+
return false;
66+
return isRegistered(chatType);
67+
}
68+
69+
/**
70+
* Checks if the chat type is registered in this messenger.
71+
* @param chatType chat type to check
72+
* @return if the chat type is registered in this messenger
73+
*/
74+
boolean isRegistered(ChatType chatType);
75+
76+
/**
77+
* Returns chat type with the given name registered in this messenger.
78+
* @param name name of the chat type
79+
* @return chat type with given name in this messenger
80+
*/
81+
@Nullable ChatType getChatType(NamespacedKey name);
82+
83+
/**
84+
* Returns chat type with given id registered in this messenger.
85+
* @param id id of the chat type
86+
* @return chat type with given id in this messenger
87+
*/
88+
@Nullable ChatType getById(int id);
89+
90+
/**
91+
* Returns the id associated with the given chat type registered in this messenger.
92+
* @param chatType the chat type
93+
* @return the id of the chat type, or -1 if it's not registered
94+
*/
95+
int getChatTypeId(ChatType chatType);
96+
97+
/**
98+
* @return unmodifiable set of all dimensions registered in this messenger
99+
*/
100+
@Unmodifiable Set<ChatType> getChatTypes();
101+
24102
/**
25103
* Checks if player can receive a message.
26104
* @param player player to check
@@ -66,4 +144,11 @@ static boolean accepts(Player player, MessageType messageType) {
66144
*/
67145
void sendRejectionMessage(Player player);
68146

147+
/**
148+
* Returns the NBT compound of the given chat type.
149+
* @param chatType the chat type
150+
* @return NBT of the given chat type
151+
*/
152+
NBTCompound getChatTypeNBT(ChatType chatType);
153+
69154
}

api/src/main/java/org/machinemc/api/world/World.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package org.machinemc.api.world;
1616

17+
import org.jetbrains.annotations.Unmodifiable;
1718
import org.machinemc.api.chunk.Chunk;
1819
import org.machinemc.api.entities.Entity;
1920
import org.machinemc.api.server.ServerProperty;
@@ -23,14 +24,10 @@
2324
import org.machinemc.api.world.blocks.WorldBlock;
2425
import org.machinemc.api.world.dimensions.DimensionType;
2526
import org.machinemc.api.world.generation.Generator;
26-
import org.jetbrains.annotations.ApiStatus;
27-
import org.jetbrains.annotations.Nullable;
28-
import org.jetbrains.annotations.Unmodifiable;
2927

3028
import java.io.IOException;
3129
import java.util.Set;
3230
import java.util.UUID;
33-
import java.util.concurrent.atomic.AtomicReference;
3431

3532
import static org.machinemc.api.chunk.Chunk.CHUNK_SIZE_BITS;
3633

@@ -39,19 +36,6 @@
3936
*/
4037
public interface World extends ServerProperty {
4138

42-
/**
43-
* @return atomic reference of the manager
44-
*/
45-
@ApiStatus.Internal
46-
AtomicReference<WorldManager> getManagerReference();
47-
48-
/**
49-
* @return manager of the world
50-
*/
51-
default @Nullable WorldManager getManager() {
52-
return getManagerReference().get();
53-
}
54-
5539
/**
5640
* @return name of the world
5741
*/

api/src/main/java/org/machinemc/api/world/WorldManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public interface WorldManager extends ServerProperty {
5454
* @return if the world is registered in this manager
5555
*/
5656
default boolean isRegistered(World world) {
57-
return this.equals(world.getManager()) && isRegistered(world.getName());
57+
return isRegistered(world.getName());
5858
}
5959

6060
/**

server/src/main/java/org/machinemc/server/Machine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ private Machine(final String[] args) throws Exception {
244244
}
245245
console.info("Registered " + dimensionTypeManager.getDimensions().size() + " dimension types");
246246

247-
messenger = new MessengerImpl(this);
247+
messenger = MessengerImpl.createDefault(this);
248+
console.info("Registered " + messenger.getChatTypes().size() + " chat types");
248249

249250
// Loading biomes json file
250251
biomeManager = new BiomeManagerImpl(this);

0 commit comments

Comments
 (0)