/
UniversalSkinFactory.java
146 lines (121 loc) · 5.96 KB
/
UniversalSkinFactory.java
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
/*
* #%L
* SkinsRestorer
* %%
* Copyright (C) 2021 SkinsRestorer
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package net.skinsrestorer.bukkit.skinfactory;
import io.papermc.lib.PaperLib;
import lombok.RequiredArgsConstructor;
import net.skinsrestorer.bukkit.SkinsRestorer;
import net.skinsrestorer.shared.storage.Config;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.io.File;
import java.util.function.Consumer;
import java.util.logging.Level;
@RequiredArgsConstructor
public class UniversalSkinFactory implements SkinFactory {
private final SkinsRestorer plugin;
private final Consumer<Player> refresh = detectRefresh();
private boolean checkOptFileChecked = false;
private boolean disableDismountPlayer;
private boolean enableDismountEntities;
private boolean enableRemountPlayer;
private static Consumer<Player> detectRefresh() {
// Giving warning when using java 9+ regarding illegal reflection access
final String version = System.getProperty("java.version");
if (!version.startsWith("1."))
System.out.println("[SkinsRestorer] [!] WARNING [!] \n[SkinsRestorer] Below message about \"Illegal reflective access\" can be IGNORED, we will fix this in a later release!");
// force OldSkinRefresher for unsupported plugins (ViaVersion & other ProtocolHack).
// todo: reuse code
// No need to check for all three Vias as ViaVersion has to be installed for the other two to work.
// Ran with getPlugin != null instead of isPluginEnabled as older Spigot builds return false during the login process even if enabled
boolean viaVersion = SkinsRestorer.getInstance().getServer().getPluginManager().getPlugin("ViaVersion") != null;
boolean protocolSupportExists = SkinsRestorer.getInstance().getServer().getPluginManager().getPlugin("ProtocolSupport") != null;
if (viaVersion || protocolSupportExists) {
SkinsRestorer.getInstance().getLogger().log(Level.INFO, "Unsupported plugin (ViaVersion or ProtocolSupport) detected, forcing OldSkinRefresher");
return new OldSkinRefresher();
}
if (PaperLib.isPaper()) {
try {
return new PaperSkinRefresher();
} catch (ExceptionInInitializerError ignored) {
}
}
return new OldSkinRefresher();
}
@Override
public void updateSkin(Player player) {
if (!player.isOnline())
return;
if (checkOptFileChecked)
checkOptFile();
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
Entity vehicle = player.getVehicle();
//dismounts a player on refreshing, which prevents desync caused by riding a horse, or plugins that allow sitting
if ((Config.DISMOUNT_PLAYER_ON_UPDATE || !disableDismountPlayer) && vehicle != null) {
vehicle.removePassenger(player);
if (Config.REMOUNT_PLAYER_ON_UPDATE || enableRemountPlayer) {
//this is delayed to next tick to allow the accepter to propagate if necessary (IE: Paper's health update)
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
//this is not really necessary, as addPassenger on vanilla despawned vehicles won't do anything, but better to be safe in case the server has plugins that do strange things
if (vehicle.isValid()) {
vehicle.addPassenger(player);
}
}, 1);
}
}
//dismounts all entities riding the player, preventing desync from plugins that allow players to mount each other
if (Config.DISMOUNT_PASSENGERS_ON_UPDATE || enableDismountEntities) {
if (!player.isEmpty()) {
for (Entity passenger : player.getPassengers()) {
player.removePassenger(passenger);
}
}
}
for (Player ps : Bukkit.getOnlinePlayers()) {
// Some older spigot versions only support hidePlayer(player)
try {
ps.hidePlayer(plugin, player);
} catch (NoSuchMethodError ignored) {
ps.hidePlayer(player);
}
try {
ps.showPlayer(plugin, player);
} catch (NoSuchMethodError ignored) {
ps.showPlayer(player);
}
}
refresh.accept(player);
});
}
private void checkOptFile() {
File fileDisableDismountPlayer = new File("plugins" + File.separator + "SkinsRestorer" + File.separator + "disablesdismountplayer");
File fileEnableDismountEntities = new File("plugins" + File.separator + "SkinsRestorer" + File.separator + "enablesdismountentities");
File fileDisableRemountPlayer = new File("plugins" + File.separator + "SkinsRestorer" + File.separator + "disablesremountplayer");
if (fileDisableDismountPlayer.exists())
disableDismountPlayer = true;
if (fileEnableDismountEntities.exists())
enableDismountEntities = true;
if (fileDisableRemountPlayer.exists())
enableRemountPlayer = false;
checkOptFileChecked = true;
}
}