-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathModSettingModuleImpl.java
More file actions
143 lines (125 loc) · 5.51 KB
/
Copy pathModSettingModuleImpl.java
File metadata and controls
143 lines (125 loc) · 5.51 KB
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
/*
* This file is part of Apollo, licensed under the MIT License.
*
* Copyright (c) 2023 Moonsworth
*
* 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 com.lunarclient.apollo.module.modsettings;
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Value;
import com.lunarclient.apollo.ApolloManager;
import com.lunarclient.apollo.configurable.v1.ConfigurableSettings;
import com.lunarclient.apollo.configurable.v1.OverrideConfigurableSettingsMessage;
import com.lunarclient.apollo.event.ApolloReceivePacketEvent;
import com.lunarclient.apollo.event.EventBus;
import com.lunarclient.apollo.event.modsetting.ApolloUpdateModOptionEvent;
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
import com.lunarclient.apollo.network.NetworkOptions;
import com.lunarclient.apollo.option.Option;
import com.lunarclient.apollo.option.StatusOptionsImpl;
import com.lunarclient.apollo.player.ApolloPlayer;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.NonNull;
import org.jetbrains.annotations.NotNull;
/**
* Provides the mod settings module.
*
* @since 1.2.1
*/
public final class ModSettingModuleImpl extends ModSettingModule {
/**
* Creates a new instance of {@link ModSettingModuleImpl}.
*
* @since 1.2.1
*/
public ModSettingModuleImpl() {
super();
this.registerOptions(ApolloManager.getModsManager().getContainer().getModSettingsOptions());
this.handle(ApolloReceivePacketEvent.class, this::onReceivePacket);
}
@Override
public <T, C extends Option<T, ?, ?>> T getStatus(@NotNull ApolloPlayer player, @NonNull C option) {
return ApolloManager.getModsManager().getPlayerOptions().get(player, option);
}
private void onReceivePacket(ApolloReceivePacketEvent event) {
ApolloPlayer player = event.getPlayer();
Any packet = event.getPacket();
if(packet.is(OverrideConfigurableSettingsMessage.class) || packet.is(ConfigurableSettings.class)) {
this.handleConfiguration(player, packet);
}
}
private void handleConfiguration(ApolloPlayer player, Any any) {
// Unpack the settings first.
List<ConfigurableSettings> settings;
try {
if (any.is(OverrideConfigurableSettingsMessage.class)) {
OverrideConfigurableSettingsMessage message = any.unpack(OverrideConfigurableSettingsMessage.class);
settings = message.getConfigurableSettingsList();
} else {
settings = Collections.singletonList(any.unpack(ConfigurableSettings.class));
}
} catch (InvalidProtocolBufferException exception) {
throw new RuntimeException(exception);
}
for (ConfigurableSettings setting : settings) {
if (!setting.hasApolloModule()) {
continue;
}
if (!setting.getApolloModule().equals(this.getId())) {
continue;
}
this.updateOptions(player, setting.getPropertiesMap(), true);
}
}
/**
* Updates the {@link Option}s for a specific {@link ApolloPlayer} using the properties
* received from the client.
*
* @param player the apollo player
* @param properties a map of option keys and values
* @param callEvent whether to call the update mod option event
* @since 1.2.1
*/
public void updateOptions(ApolloPlayer player, Map<String, Value> properties, boolean callEvent) {
StatusOptionsImpl statusOptions = ApolloManager.getModsManager().getPlayerOptions();
for (Map.Entry<String, Value> entry : properties.entrySet()) {
Option<?, ?, ?> option = statusOptions.getRegistry().get(entry.getKey());
// Option exists on the client but doesn't exist in the API yet.
if (option == null) {
continue;
}
Object unwrappedValue = NetworkOptions.unwrapValue(
entry.getValue(),
option.getTypeToken().getType()
);
statusOptions.set(player, option, unwrappedValue);
if (callEvent) {
EventBus.EventResult<ApolloUpdateModOptionEvent> eventResult = EventBus.getBus()
.post(new ApolloUpdateModOptionEvent(player, option, unwrappedValue));
for (Throwable throwable : eventResult.getThrowing()) {
throwable.printStackTrace();
}
}
}
}
}