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

Commit

Permalink
[1.20.4-0.0.1] Sync with private Repo, Fixed Textures, PingBypass Con…
Browse files Browse the repository at this point in the history
…figWorld not fully functional yet
  • Loading branch information
3arthqu4ke committed Feb 17, 2024
1 parent 0a1e382 commit 69f1fbb
Show file tree
Hide file tree
Showing 98 changed files with 2,436 additions and 917 deletions.
14 changes: 14 additions & 0 deletions pb-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
plugins {
id 'java-test-fixtures'
}

sourceSets {
testNoRuntime {
compileClasspath += sourceSets.test.compileClasspath
}
test {
compileClasspath += sourceSets.testNoRuntime.runtimeClasspath
}
}

dependencies {
api project(':pb-security-api')
testImplementation(testFixtures(project))
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package me.earth.pingbypass.api.command;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.earth.pingbypass.api.traits.HasDescription;
import me.earth.pingbypass.api.traits.Nameable;

public interface Command extends Nameable, HasDescription {
void build(LiteralArgumentBuilder<CommandSource> builder);
public interface Command extends GenericCommand<CommandSource> {

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package me.earth.pingbypass.api.command;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.earth.pingbypass.api.registry.Registry;

public interface CommandManager extends Registry<Command>, ProvidesSuggestions<CommandSource> {
void execute(String command, CommandSource source) throws CommandSyntaxException;

String getPrefix();

void setPrefix(String prefix);
public interface CommandManager extends GenericCommandManager<CommandSource, Command> {

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

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.earth.pingbypass.api.traits.HasDescription;
import me.earth.pingbypass.api.traits.Nameable;

public interface GenericCommand<S> extends Nameable, HasDescription {
void build(LiteralArgumentBuilder<S> builder);

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

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.RootCommandNode;
import me.earth.pingbypass.api.registry.Registry;

public interface GenericCommandManager<S, C extends GenericCommand<S>> extends Registry<C>, ProvidesSuggestions<S> {
void execute(String command, S source) throws CommandSyntaxException;

RootCommandNode<S> getRoot();

String getPrefix();

void setPrefix(String prefix);

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

import lombok.Data;
import me.earth.pingbypass.api.command.Command;
import me.earth.pingbypass.api.command.CommandSource;

@Data
public abstract class AbstractCommand implements Command {
private final String name;
private final String description;
public abstract class AbstractCommand extends AbstractGenericCommand<CommandSource> implements Command {
public AbstractCommand(String name, String description) {
super(name, description);
}

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

import com.mojang.brigadier.arguments.ArgumentType;
import lombok.Data;
import me.earth.pingbypass.api.command.GenericCommand;
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;

@Data
public abstract class AbstractGenericCommand<S> implements GenericCommand<S> {
private final String name;
private final String description;

public <T> ExtendedRequiredArgumentBuilder<S, T> arg(String name, ArgumentType<T> type) {
return new ExtendedRequiredArgumentBuilder<>(type, name);
}

public ExtendedLiteralArgumentBuilder<S> literal(String literal) {
return new ExtendedLiteralArgumentBuilder<>(literal);
}

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

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

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.tree.CommandNode;
import lombok.Getter;
import lombok.Setter;
import me.earth.pingbypass.api.command.Command;
import me.earth.pingbypass.api.command.CommandManager;
import me.earth.pingbypass.api.command.CommandSource;
import me.earth.pingbypass.api.command.impl.builder.ExtendedLiteralArgumentBuilder;
import me.earth.pingbypass.api.registry.impl.SortedRegistry;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static java.util.Collections.synchronizedMap;

public class CommandManagerImpl extends SortedRegistry<Command> implements CommandManager {
private final Map<Command, CommandNode<CommandSource>> builder2Node = synchronizedMap(new LinkedHashMap<>());
private CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
@Getter
@Setter
private String prefix = "+";

@Override
public boolean register(Command builder) {
synchronized (lock) {
if (super.register(builder)) {
var command = ExtendedLiteralArgumentBuilder.<CommandSource>literal(builder.getName());
builder.build(command);
var node = command.build();
builder2Node.put(builder, node);
dispatcher.getRoot().addChild(node);
return true;
}

return false;
}
}

@Override
public boolean unregister(Command builder) {
synchronized (lock) {
// because CommandDispatcher does not support unregistering, we have to build a new one (or reflection...)
// (or could we register a delegating node and set its delegate to null on unregistering?)
if (super.unregister(builder)) {
builder2Node.remove(builder);
CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
builder2Node.values().forEach(node -> dispatcher.getRoot().addChild(node));
this.dispatcher = dispatcher;
return true;
}

return false;
}
}

@Override
public void execute(String command, CommandSource source) throws CommandSyntaxException {
dispatcher.execute(command, source);
}

@Override
public ParseResults<CommandSource> parse(StringReader reader, CommandSource source) {
return dispatcher.parse(reader, source);
}

@Override
public CompletableFuture<Suggestions> getCompletionSuggestions(ParseResults<CommandSource> parse,
int cursor) {
// TODO: is this needed? Better be safe, a command could get unregistered between parse and getSuggestions?
if (!dispatcher.equals(parse.getContext().getDispatcher())) {
return Suggestions.empty();
}

return dispatcher.getCompletionSuggestions(parse, cursor);
}

@Override
public Map<CommandNode<CommandSource>, String> getSmartUsage(CommandNode<CommandSource> node,
CommandSource source) {
return dispatcher.getSmartUsage(node, source);
}
public class CommandManagerImpl extends GenericCommandManagerImpl<CommandSource, Command> implements CommandManager {

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

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import lombok.Getter;
import lombok.Setter;
import me.earth.pingbypass.api.command.*;
import me.earth.pingbypass.api.command.impl.builder.ExtendedLiteralArgumentBuilder;
import me.earth.pingbypass.api.registry.impl.SortedRegistry;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static java.util.Collections.synchronizedMap;

public class GenericCommandManagerImpl<S, C extends GenericCommand<S>> extends SortedRegistry<C> implements GenericCommandManager<S, C> {
private final Map<C, CommandNode<S>> builder2Node = synchronizedMap(new LinkedHashMap<>());
private CommandDispatcher<S> dispatcher = new CommandDispatcher<>();
@Getter
@Setter
private String prefix = "+";

@Override
public boolean register(C builder) {
synchronized (lock) {
if (super.register(builder)) {
var command = ExtendedLiteralArgumentBuilder.<S>literal(builder.getName());
builder.build(command);
var node = command.build();
builder2Node.put(builder, node);
dispatcher.getRoot().addChild(node);
return true;
}

return false;
}
}

@Override
public boolean unregister(C builder) {
synchronized (lock) {
// because CommandDispatcher does not support unregistering, we have to build a new one (or reflection...)
// (or could we register a delegating node and set its delegate to null on unregistering?)
if (super.unregister(builder)) {
builder2Node.remove(builder);
CommandDispatcher<S> dispatcher = new CommandDispatcher<>();
builder2Node.values().forEach(node -> dispatcher.getRoot().addChild(node));
this.dispatcher = dispatcher;
return true;
}

return false;
}
}

@Override
public void execute(String command, S source) throws CommandSyntaxException {
dispatcher.execute(command, source);
}

@Override
public RootCommandNode<S> getRoot() {
return dispatcher.getRoot();
}

@Override
public ParseResults<S> parse(StringReader reader, S source) {
return dispatcher.parse(reader, source);
}

@Override
public CompletableFuture<Suggestions> getCompletionSuggestions(ParseResults<S> parse, int cursor) {
// TODO: is this needed? Better be safe, a command could get unregistered between parse and getSuggestions?
if (!dispatcher.equals(parse.getContext().getDispatcher())) {
return Suggestions.empty();
}

return dispatcher.getCompletionSuggestions(parse, cursor);
}

@Override
public Map<CommandNode<S>, String> getSmartUsage(CommandNode<S> node, S source) {
return dispatcher.getSmartUsage(node, source);
}

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

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import me.earth.pingbypass.api.command.Command;
import me.earth.pingbypass.api.command.GenericCommand;
import me.earth.pingbypass.api.traits.Streamable;

@Getter
@RequiredArgsConstructor(staticName = "of")
public final class CommandArgument implements DescriptionArgumentType<Command> {
public final class CommandArgument<S, C extends GenericCommand<S>> implements DescriptionArgumentType<C> {
private final String type = "command";
private final Streamable<Command> nameables;
private final Streamable<C> nameables;

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

import lombok.*;
import me.earth.pingbypass.PingBypass;
import me.earth.pingbypass.api.event.Lockable;
import me.earth.pingbypass.api.event.api.EventListener;
import me.earth.pingbypass.api.event.api.Subscriber;
import me.earth.pingbypass.api.side.Side;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
* If you are writing a plugin that can be used on different pingbypass implementations but you want to target one
* specifically its easy to use events and this class. The implementation might not be available at runtime so
* this works around any {@link ClassNotFoundException} that might occur.
*
* @param <P> the type of the PingBypass implementation to target.
*/
@Data
public class SideSpecificSubscriber<P extends PingBypass> implements Subscriber, Lockable {
@ToString.Exclude
@Getter(AccessLevel.NONE)
@EqualsAndHashCode.Exclude
private final Object lock = new Object();
private final List<EventListener<?>> listeners = new ArrayList<>();
private final P pingBypass;

@Override
public Object getLock(Object requester) {
return lock;
}

public void subscribe() {
pingBypass.getEventBus().subscribe(this);
}

public void unsubscribe() {
pingBypass.getEventBus().unsubscribe(this);
}

public void addListener(EventListener<?> listener) {
listeners.add(listener);
}

@SuppressWarnings("unchecked")
public static <T extends PingBypass> List<SideSpecificSubscriber<T>> create(Stream<PingBypass> instances,
@Nullable Side side,
ClassSupplier<T> classSupplier,
Consumer<SideSpecificSubscriber<T>> initialize)
{
List<SideSpecificSubscriber<T>> result = new ArrayList<>(1);
try {
Class<? extends T> clazz = classSupplier.supply();
instances.forEach(pb -> {
if (clazz.isInstance(pb) && (side == null || side.equals(pb.getSide()))) {
var subscriber = (SideSpecificSubscriber<T>) new SideSpecificSubscriber<>(pb);
initialize.accept(subscriber);
result.add(subscriber);
}
});
} catch (ClassNotFoundException | NoClassDefFoundError ignored) { }
return result;
}

@FunctionalInterface
public interface ClassSupplier<T> {
Class<? extends T> supply() throws ClassNotFoundException;
}

}
Loading

0 comments on commit 69f1fbb

Please sign in to comment.