Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from 3arthqu4ke/0.3.0
Browse files Browse the repository at this point in the history
[0.3.0] Cleaned up Session subscribers a bit, some convenience methods
  • Loading branch information
3arthqu4ke authored Mar 11, 2024
2 parents 5d916a0 + 27802a3 commit e4c181e
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 44 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
org.gradle.jvmargs = -Xmx2G

version = 0.2.0
version = 0.3.0
minecraft_version = 1.20.4
mapping_version = 1
neoforge_version = 1-beta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.earth.pingbypass.api.event.SubscriberImpl;
import me.earth.pingbypass.api.event.listeners.generic.Listener;
import me.earth.pingbypass.api.event.chat.ChatEvent;
import me.earth.pingbypass.api.event.chat.CommandSuggestionEvent;
import net.minecraft.network.chat.ComponentUtils;
import me.earth.pingbypass.api.event.listeners.generic.Listener;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;

public class CommandEventService extends SubscriberImpl {
public CommandEventService(CommandManager commandManager, CommandSource source) {
Expand All @@ -18,7 +19,7 @@ public void onEvent(ChatEvent event) {
try {
commandManager.execute(event.getMessage().substring(prefix.length()), source);
} catch (CommandSyntaxException e) {
source.getMinecraft().gui.getChat().addMessage(ComponentUtils.fromMessage(e.getRawMessage()));
source.getMinecraft().gui.getChat().addMessage(Component.literal(e.getMessage()).withStyle(ChatFormatting.RED));
}

event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package me.earth.pingbypass.api.command;

import com.mojang.brigadier.arguments.ArgumentType;
import me.earth.pingbypass.PingBypass;
import me.earth.pingbypass.api.command.impl.arguments.StringArgument;
import me.earth.pingbypass.api.command.impl.builder.ExtendedLiteralArgumentBuilder;
import me.earth.pingbypass.api.command.impl.builder.ExtendedRequiredArgumentBuilder;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.SharedSuggestionProvider;

Expand All @@ -13,4 +17,16 @@ default Chat getChat() {
return getPingBypass().getChat();
}

static <T> ExtendedRequiredArgumentBuilder<CommandSource, T> argument(String name, ArgumentType<T> type) {
return new ExtendedRequiredArgumentBuilder<>(type, name);
}

static ExtendedLiteralArgumentBuilder<CommandSource> literal(String literal) {
return new ExtendedLiteralArgumentBuilder<>(literal);
}

static ExtendedRequiredArgumentBuilder<CommandSource, String> greedy(String name) {
return new ExtendedRequiredArgumentBuilder<>(StringArgument.greedy(name), name);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.earth.pingbypass.api.event.impl;

import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import me.earth.pingbypass.api.event.api.EventBus;

@RequiredArgsConstructor
public class DelegatingEventBus implements EventBus {
@Delegate
private final EventBus eventBus;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package me.earth.pingbypass.api.event.listeners.generic;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Synchronized;
import me.earth.pingbypass.api.event.Lockable;
import me.earth.pingbypass.api.event.api.EventBus;
import me.earth.pingbypass.api.event.api.EventListener;
import me.earth.pingbypass.api.event.api.Subscriber;

Expand All @@ -18,4 +21,26 @@ public Object getLock(Object requester) {
return lock;
}

/**
* Listens for an Event once, then unsubscribes itself from the given {@link EventBus}.
*
* @param <E> the type of event this Listener is listening to.
*/
@RequiredArgsConstructor
public static abstract class OneTime<E> extends SubscriberListener<E> {
private final EventBus eventBus;

public abstract void onOneTimeEvent(E event);

@Override
public void onEvent(E event) {
synchronized (getLock(eventBus)) {
if (eventBus.isSubscribed(this)) {
onOneTimeEvent(event);
eventBus.unsubscribe(this);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import me.earth.pingbypass.api.event.event.CancellableEvent;
import net.minecraft.client.player.LocalPlayer;

@Getter
@RequiredArgsConstructor
public class LocalPlayerUpdateEvent {
public class LocalPlayerUpdateEvent extends CancellableEvent {
private final LocalPlayer player;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ public abstract class MixinLocalPlayer {
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/player/AbstractClientPlayer;tick()V",
shift = At.Shift.BEFORE))
shift = At.Shift.BEFORE),
cancellable = true)
private void tickHook(CallbackInfo ci) {
PingBypassApi.getEventBus().post(new LocalPlayerUpdateEvent(LocalPlayer.class.cast(this)));
var event = new LocalPlayerUpdateEvent(LocalPlayer.class.cast(this));
PingBypassApi.getEventBus().post(event);
if (event.isCancelled()) {
ci.cancel();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package me.earth.pingbypass.api.plugin.impl;

import lombok.RequiredArgsConstructor;
import lombok.Getter;
import me.earth.pingbypass.PingBypass;
import me.earth.pingbypass.api.command.Command;
import me.earth.pingbypass.api.command.impl.module.ModuleCommand;
import me.earth.pingbypass.api.event.api.EventBus;
import me.earth.pingbypass.api.event.api.EventListener;
import me.earth.pingbypass.api.event.api.Subscriber;
import me.earth.pingbypass.api.module.Module;
Expand All @@ -14,21 +15,28 @@
import me.earth.pingbypass.api.traits.Nameable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* Makes it easier to write {@link Plugin}s that support unloading.
*/
@RequiredArgsConstructor
public class PluginUnloadingService {
private final Map<Registry<?>, List<Nameable>> registries = new HashMap<>();
private final Map<Subscriber, List<EventListener<?>>> listeners = new HashMap<>();
private final List<ModuleAddOn<?>> addOns = new ArrayList<>();
private final List<Subscriber> subscribers = new ArrayList<>();
private final List<Runnable> runOnUnload = new ArrayList<>();
private final PingBypass pingBypass;
private final Map<Registry<?>, List<Nameable>> registries = new ConcurrentHashMap<>();
private final Map<Subscriber, List<EventListener<?>>> listeners = new ConcurrentHashMap<>();
private final List<ModuleAddOn<?>> addOns = new CopyOnWriteArrayList<>();
private final List<Object> subscribers = new CopyOnWriteArrayList<>();
private final List<Runnable> runOnUnload = new CopyOnWriteArrayList<>();
@Getter
private final EventBus eventBus;
final PingBypass pingBypass;

public PluginUnloadingService(PingBypass pingBypass) {
this.pingBypass = pingBypass;
this.eventBus = new UnloadableEventBus(this);
}

public boolean registerCommand(Command command) {
return register(pingBypass.getCommandManager(), command);
Expand Down Expand Up @@ -60,11 +68,11 @@ public boolean register(Module module) {
return register(pingBypass.getModuleManager(), module);
}

public void registerSubscriber(Subscriber subscriber) {
public void registerSubscriber(Object subscriber) {
subscribers.add(subscriber);
}

public void subscribe(Subscriber subscriber) {
public void subscribe(Object subscriber) {
pingBypass.getEventBus().subscribe(subscriber);
this.registerSubscriber(subscriber);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.earth.pingbypass.api.plugin.impl;

import me.earth.pingbypass.api.event.impl.DelegatingEventBus;

class UnloadableEventBus extends DelegatingEventBus {
private final PluginUnloadingService unloadingService;

public UnloadableEventBus(PluginUnloadingService unloadingService) {
super(unloadingService.pingBypass.getEventBus());
this.unloadingService = unloadingService;
}

@Override
public void subscribe(Object subscriber) {
unloadingService.subscribe(subscriber);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package me.earth.pingbypass.api.setting;

import java.util.Collection;

public interface PreAndPostObservable<O> {
void addPreObserver(O observer);

void addPostObserver(O observer);

void removeObserver(O observer);

Collection<O> getPreObservers();

Collection<O> getPostObservers();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.earth.pingbypass.api.setting.SettingEvent;
import me.earth.pingbypass.api.setting.SettingObserver;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -39,5 +40,15 @@ public void removeObserver(SettingObserver<T> observer) {
preObservers.remove(observer);
}

@Override
public Collection<SettingObserver<T>> getPreObservers() {
return preObservers;
}

@Override
public Collection<SettingObserver<T>> getPostObservers() {
return postObservers;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import me.earth.pingbypass.server.handlers.play.PlayPacketHandler;
import me.earth.pingbypass.server.session.Session;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.multiplayer.MultiPlayerGameMode;
import net.minecraft.client.player.LocalPlayer;
Expand All @@ -36,8 +35,7 @@ public void build(LiteralArgumentBuilder<ServerCommandSource> builder) {
if (player != null && level != null && gameMode != null) {
session.getPipeline().lock();
// TODO: when is the best time to allow proxying packets from client to server?
session.addSubscriber(new GameProfileTranslation(session));
session.subscribe();
session.whenMadePrimarySession();
session.setListener(new PlayPacketHandler(server, session, session.getCookie()));
new JoinWorldService().join(session, player, gameMode, level);
session.getPipeline().unlockAndFlush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ protected CommonListenerCookie createCookie(ClientInformation clientInformation)
return new CommonListenerCookie(playerProfile(), latency, clientInformation);
}

// TODO: when we schedule we still need to handle the packet!
protected void scheduleSafely(NullabilityUtil.PlayerLevelAndGameModeConsumer action) {
mc.submit(() -> NullabilityUtil.safe(mc, action));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void handleMovePlayer(ServerboundMovePlayerPacket packet) {
player.setYRot(packet.getYRot(player.getYRot()));
player.setXRot(packet.getXRot(player.getXRot()));
player.setOnGround(packet.isOnGround());
session.getPlayerUpdateHandler().update(player);
});
// TODO: handle! Fire event!
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.earth.pingbypass.server.handlers.play;

import me.earth.pingbypass.api.event.SubscriberImpl;
import me.earth.pingbypass.api.event.listeners.generic.Listener;
import me.earth.pingbypass.api.event.loop.LocalPlayerUpdateEvent;
import net.minecraft.client.player.LocalPlayer;

public class PlayerUpdateHandler extends SubscriberImpl {
private final ThreadLocal<Boolean> inUpdate = ThreadLocal.withInitial(() -> false);

public PlayerUpdateHandler() {
listen(new Listener<LocalPlayerUpdateEvent>() {
@Override
public void onEvent(LocalPlayerUpdateEvent event) {
if (!inUpdate.get()) {
event.setCancelled(true);
}
}
});
}

public void update(LocalPlayer player) {
try {
inUpdate.set(true);
player.tick();
} finally {
inUpdate.set(false);
}
}

}
Loading

0 comments on commit e4c181e

Please sign in to comment.