-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a hook system and add a new viaversion hook
- Loading branch information
1 parent
a4872f3
commit dc71bd1
Showing
10 changed files
with
245 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/ViaHook.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/Hook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.kaspiandev.antipopup.spigot.hook; | ||
|
||
public interface Hook { | ||
|
||
String getPluginName(); | ||
|
||
void register(); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/HookManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.kaspiandev.antipopup.spigot.hook; | ||
|
||
import com.github.kaspiandev.antipopup.spigot.AntiPopup; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.plugin.PluginManager; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HookManager { | ||
|
||
private final List<Hook> hooks; | ||
|
||
public HookManager() { | ||
this.hooks = new ArrayList<>(); | ||
} | ||
|
||
public void addHook(Hook hook) { | ||
hooks.add(hook); | ||
} | ||
|
||
public void load() { | ||
PluginManager pluginManager = Bukkit.getServer().getPluginManager(); | ||
for (Hook hook : hooks) { | ||
if (pluginManager.isPluginEnabled(hook.getPluginName())) { | ||
AntiPopup.getInstance().getLogger().info("Loaded a hook for " + hook.getPluginName() + "."); | ||
hook.register(); | ||
} | ||
} | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/ViaProtocolModifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.github.kaspiandev.antipopup.spigot.hook.viaversion; | ||
|
||
import com.github.retrooper.packetevents.manager.server.ServerVersion; | ||
import com.github.retrooper.packetevents.manager.server.VersionComparison; | ||
import com.viaversion.viaversion.api.Via; | ||
import com.viaversion.viaversion.api.protocol.Protocol; | ||
|
||
public abstract class ViaProtocolModifier<T extends Protocol<?, ?, ?, ?>> { | ||
|
||
protected final T protocol; | ||
protected final VersionComparison comparison; | ||
protected final ServerVersion version; | ||
|
||
protected ViaProtocolModifier(VersionComparison comparison, ServerVersion version) throws NullPointerException { | ||
T protocol = Via.getManager().getProtocolManager().getProtocol(getProtocolClass()); | ||
if (protocol == null) throw new NullPointerException(); | ||
this.protocol = protocol; | ||
this.comparison = comparison; | ||
this.version = version; | ||
} | ||
|
||
protected abstract void modify(); | ||
|
||
protected abstract Class<T> getProtocolClass(); | ||
|
||
public VersionComparison getComparison() { | ||
return comparison; | ||
} | ||
|
||
public ServerVersion getVersion() { | ||
return version; | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
.../src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/ViaVersionHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.github.kaspiandev.antipopup.spigot.hook.viaversion; | ||
|
||
import com.github.kaspiandev.antipopup.spigot.AntiPopup; | ||
import com.github.kaspiandev.antipopup.spigot.hook.Hook; | ||
import com.github.retrooper.packetevents.PacketEvents; | ||
import com.github.retrooper.packetevents.manager.server.ServerVersion; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.StringJoiner; | ||
|
||
public class ViaVersionHook implements Hook { | ||
|
||
private final List<Class<? extends ViaProtocolModifier<?>>> modifiers; | ||
private final List<ViaProtocolModifier<?>> registeredModifiers; | ||
|
||
public ViaVersionHook() { | ||
this.modifiers = new ArrayList<>(); | ||
this.registeredModifiers = new ArrayList<>(); | ||
} | ||
|
||
public void addModifier(Class<? extends ViaProtocolModifier<?>> modifier) { | ||
modifiers.add(modifier); | ||
} | ||
|
||
@Override | ||
public String getPluginName() { | ||
return "ViaVersion"; | ||
} | ||
|
||
@Override | ||
public void register() { | ||
ServerVersion serverVersion = PacketEvents.getAPI().getServerManager().getVersion(); | ||
for (Class<? extends ViaProtocolModifier<?>> modifier : modifiers) { | ||
try { | ||
ViaProtocolModifier<?> modifierInstance = modifier.getDeclaredConstructor().newInstance(); | ||
|
||
if (serverVersion.is(modifierInstance.getComparison(), modifierInstance.getVersion())) { | ||
modifierInstance.modify(); | ||
registeredModifiers.add(modifierInstance); | ||
modifiers.remove(modifier); | ||
AntiPopup.getInstance().getLogger().info(() -> getRegisteredMessage(modifierInstance)); | ||
} | ||
} catch (IllegalAccessException | InvocationTargetException | ||
| NoSuchMethodException | InstantiationException ignored) {} | ||
} | ||
} | ||
|
||
private String getRegisteredMessage(ViaProtocolModifier<?> modifier) { | ||
String comparison = switch (modifier.getComparison()) { | ||
case EQUALS -> "equal to"; | ||
case OLDER_THAN -> "older than"; | ||
case NEWER_THAN -> "newer than"; | ||
case NEWER_THAN_OR_EQUALS -> "newer than or equal to"; | ||
case OLDER_THAN_OR_EQUALS -> "older than or equal to"; | ||
}; | ||
return "Registered a modifier targeted for versions " | ||
+ comparison | ||
+ " " | ||
+ modifier.getVersion().getReleaseName() | ||
+ "."; | ||
} | ||
|
||
public List<ViaProtocolModifier<?>> getRegisteredModifiers() { | ||
return registeredModifiers; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
.../main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/Via_1_19_to_1_19_1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.kaspiandev.antipopup.spigot.hook.viaversion; | ||
|
||
import com.github.retrooper.packetevents.manager.server.ServerVersion; | ||
import com.github.retrooper.packetevents.manager.server.VersionComparison; | ||
import com.viaversion.viaversion.api.type.Type; | ||
import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.Protocol1_19_1To1_19; | ||
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ClientboundPackets1_19; | ||
|
||
public class Via_1_19_to_1_19_1 extends ViaProtocolModifier<Protocol1_19_1To1_19> { | ||
|
||
public Via_1_19_to_1_19_1() { | ||
super(VersionComparison.EQUALS, ServerVersion.V_1_19); | ||
} | ||
|
||
@Override | ||
public void modify() { | ||
protocol.appendClientbound(ClientboundPackets1_19.SERVER_DATA, (wrapper) -> { | ||
wrapper.passthrough(Type.OPTIONAL_COMPONENT); | ||
wrapper.passthrough(Type.OPTIONAL_STRING); | ||
wrapper.passthrough(Type.BOOLEAN); | ||
wrapper.write(Type.BOOLEAN, true); | ||
}); | ||
} | ||
|
||
@Override | ||
public Class<Protocol1_19_1To1_19> getProtocolClass() { | ||
return Protocol1_19_1To1_19.class; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...ain/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/Via_1_20_4_to_1_20_5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.github.kaspiandev.antipopup.spigot.hook.viaversion; | ||
|
||
import com.github.retrooper.packetevents.manager.server.ServerVersion; | ||
import com.github.retrooper.packetevents.manager.server.VersionComparison; | ||
import com.viaversion.viaversion.api.type.Type; | ||
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3; | ||
import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.Protocol1_20_5To1_20_3; | ||
|
||
public class Via_1_20_4_to_1_20_5 extends ViaProtocolModifier<Protocol1_20_5To1_20_3> { | ||
|
||
public Via_1_20_4_to_1_20_5() { | ||
super(VersionComparison.OLDER_THAN, ServerVersion.V_1_20_5); | ||
} | ||
|
||
@Override | ||
public void modify() { | ||
protocol.appendClientbound(ClientboundPackets1_20_3.JOIN_GAME, (wrapper) -> { | ||
wrapper.passthrough(Type.INT); // Entity ID | ||
wrapper.passthrough(Type.BOOLEAN); // Hardcore | ||
wrapper.passthrough(Type.STRING_ARRAY); // World List | ||
wrapper.passthrough(Type.VAR_INT); // Max players | ||
wrapper.passthrough(Type.VAR_INT); // View distance | ||
wrapper.passthrough(Type.VAR_INT); // Simulation distance | ||
wrapper.passthrough(Type.BOOLEAN); // Reduced debug info | ||
wrapper.passthrough(Type.BOOLEAN); // Show death screen | ||
wrapper.passthrough(Type.BOOLEAN); // Limited crafting | ||
wrapper.passthrough(Type.VAR_INT); // Dimension | ||
wrapper.passthrough(Type.STRING); // World | ||
wrapper.passthrough(Type.LONG); // Seed | ||
wrapper.passthrough(Type.BYTE); // Gamemode | ||
wrapper.passthrough(Type.BYTE); // Previous gamemode | ||
wrapper.passthrough(Type.BOOLEAN); // Debug | ||
wrapper.passthrough(Type.BOOLEAN); // Flat | ||
wrapper.passthrough(Type.OPTIONAL_GLOBAL_POSITION); // Last death location | ||
wrapper.passthrough(Type.VAR_INT); // Portal cooldown | ||
wrapper.write(Type.BOOLEAN, true); // Enforces secure chat | ||
}); | ||
} | ||
|
||
@Override | ||
public Class<Protocol1_20_5To1_20_3> getProtocolClass() { | ||
return Protocol1_20_5To1_20_3.class; | ||
} | ||
|
||
} |