Add support for modifying identifiers through the AvailableEntityIdentifiersPacket

This is the first of many commits that address adding support for custom entities through the Geyser API.
This commit is contained in:
RednedEpic 2022-05-01 12:25:24 -05:00
parent 4ae9bdf4b9
commit 59d5a6469c
18 changed files with 702 additions and 18 deletions

View file

@ -70,6 +70,7 @@ import org.geysermc.geyser.network.RemoteServerImpl;
import org.geysermc.geyser.pack.ResourcePack;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.registry.provider.GeyserProviderManager;
import org.geysermc.geyser.scoreboard.ScoreboardUpdater;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.PendingMicrosoftAuthentication;
@ -145,6 +146,7 @@ public class GeyserImpl implements GeyserApi {
private final EventBus eventBus;
private final GeyserExtensionManager extensionManager;
private final GeyserProviderManager providerManager = new GeyserProviderManager();
private final RemoteServer remoteServer;
private final BedrockListener bedrockListener;
@ -576,6 +578,11 @@ public class GeyserImpl implements GeyserApi {
return this.bootstrap.getGeyserCommandManager();
}
@Override
public GeyserProviderManager providerManager() {
return this.providerManager;
}
@Override
public EventBus eventBus() {
return this.eventBus;

View file

@ -148,11 +148,6 @@ public abstract class GeyserCommandManager extends CommandManager {
*/
public abstract String description(String command);
@Override
protected <T extends CommandSource> Command.Builder<T> provideBuilder(Class<T> sourceType) {
return new CommandBuilder<>(sourceType);
}
@RequiredArgsConstructor
public static class CommandBuilder<T extends CommandSource> implements Command.Builder<T> {
private final Class<T> sourceType;

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.entity;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtMapBuilder;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.entity.EntityIdentifier;
import java.util.concurrent.atomic.AtomicInteger;
public record GeyserEntityIdentifier(NbtMap nbt) implements EntityIdentifier {
private static final AtomicInteger RUNTIME_ID_ALLOCATORS = new AtomicInteger(100000);
@Override
public boolean hasSpawnEgg() {
return this.nbt.getBoolean("hasspawnegg");
}
@NonNull
@Override
public String identifier() {
return this.nbt.getString("id");
}
@Override
public boolean isSummonable() {
return this.nbt.getBoolean("summonable");
}
public static class EntityIdentifierBuilder implements EntityIdentifier.Builder {
private final NbtMapBuilder nbt = NbtMap.builder();
@Override
public Builder spawnEgg(boolean spawnEgg) {
this.nbt.putBoolean("hasspawnegg", spawnEgg);
return this;
}
@Override
public Builder identifier(String identifier) {
this.nbt.putString("id", identifier);
return this;
}
@Override
public Builder summonable(boolean summonable) {
this.nbt.putBoolean("summonable", summonable);
return this;
}
@Override
public EntityIdentifier build() {
// Vanilla registry information
this.nbt.putString("bid", "");
this.nbt.putInt("rid", RUNTIME_ID_ALLOCATORS.getAndIncrement());
this.nbt.putBoolean("experimental", false);
return new GeyserEntityIdentifier(this.nbt.build());
}
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.registry;
import org.geysermc.geyser.api.provider.Provider;
import org.geysermc.geyser.registry.loader.ProviderRegistryLoader;
import org.geysermc.geyser.registry.provider.GeyserBuilderProvider;
import org.geysermc.geyser.registry.provider.ProviderSupplier;
/**
* Holds registries for the available {@link Provider}s
*/
public class ProviderRegistries {
/**
* A registry containing all the providers for builders.
*/
public static final SimpleMappedRegistry<Class<?>, ProviderSupplier> BUILDERS = SimpleMappedRegistry.create(GeyserBuilderProvider.INSTANCE, ProviderRegistryLoader::new);
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.registry.loader;
import org.geysermc.geyser.registry.provider.AbstractProvider;
import org.geysermc.geyser.registry.provider.ProviderSupplier;
import java.util.IdentityHashMap;
import java.util.Map;
/**
* Registers the provider data from the provider.
*/
public class ProviderRegistryLoader implements RegistryLoader<AbstractProvider, Map<Class<?>, ProviderSupplier>> {
@Override
public Map<Class<?>, ProviderSupplier> load(AbstractProvider input) {
Map<Class<?>, ProviderSupplier> providers = new IdentityHashMap<>();
input.registerProviders(providers);
return providers;
}
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.registry.provider;
import lombok.RequiredArgsConstructor;
import org.geysermc.geyser.api.provider.Provider;
import org.geysermc.geyser.registry.SimpleMappedRegistry;
import java.util.Map;
@RequiredArgsConstructor
public abstract class AbstractProvider implements Provider {
public abstract void registerProviders(Map<Class<?>, ProviderSupplier> providers);
public abstract SimpleMappedRegistry<Class<?>, ProviderSupplier> providerRegistry();
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.registry.provider;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.api.command.Command;
import org.geysermc.geyser.api.command.CommandSource;
import org.geysermc.geyser.api.entity.EntityIdentifier;
import org.geysermc.geyser.api.provider.BuilderProvider;
import org.geysermc.geyser.command.GeyserCommandManager;
import org.geysermc.geyser.entity.GeyserEntityIdentifier;
import org.geysermc.geyser.registry.ProviderRegistries;
import org.geysermc.geyser.registry.SimpleMappedRegistry;
import java.util.Map;
public class GeyserBuilderProvider extends AbstractProvider implements BuilderProvider {
public static GeyserBuilderProvider INSTANCE = new GeyserBuilderProvider();
private GeyserBuilderProvider() {
}
@SuppressWarnings("unchecked")
@Override
public void registerProviders(Map<Class<?>, ProviderSupplier> providers) {
providers.put(Command.Builder.class, args -> new GeyserCommandManager.CommandBuilder<>((Class<? extends CommandSource>) args[0]));
providers.put(EntityIdentifier.Builder.class, args -> new GeyserEntityIdentifier.EntityIdentifierBuilder());
}
@Override
public SimpleMappedRegistry<Class<?>, ProviderSupplier> providerRegistry() {
return ProviderRegistries.BUILDERS;
}
@SuppressWarnings("unchecked")
@Override
public <B extends T, T> @NonNull B provideBuilder(@NonNull Class<T> builderClass, @Nullable Object... args) {
return (B) this.providerRegistry().get(builderClass).create(args);
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.registry.provider;
import org.geysermc.geyser.api.provider.ProviderManager;
public class GeyserProviderManager implements ProviderManager {
@Override
public GeyserBuilderProvider builderProvider() {
return GeyserBuilderProvider.INSTANCE;
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.registry.provider;
public interface ProviderSupplier {
Object create(Object... args);
}

View file

@ -61,6 +61,9 @@ import com.github.steveice10.packetlib.tcp.TcpClientSession;
import com.github.steveice10.packetlib.tcp.TcpSession;
import com.nukkitx.math.GenericMath;
import com.nukkitx.math.vector.*;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtMapBuilder;
import com.nukkitx.nbt.NbtType;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
import com.nukkitx.protocol.bedrock.data.*;
@ -88,9 +91,12 @@ import org.geysermc.floodgate.util.BedrockData;
import org.geysermc.geyser.Constants;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.connection.GeyserConnection;
import org.geysermc.geyser.api.entity.EntityIdentifier;
import org.geysermc.geyser.api.event.entity.DefineEntitiesEvent;
import org.geysermc.geyser.api.network.RemoteServer;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.configuration.EmoteOffhandWorkaroundOption;
import org.geysermc.geyser.entity.GeyserEntityIdentifier;
import org.geysermc.geyser.entity.attribute.GeyserAttributeType;
import org.geysermc.geyser.entity.type.Entity;
import org.geysermc.geyser.entity.type.ItemFrameEntity;
@ -129,6 +135,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@Getter
public class GeyserSession implements GeyserConnection, GeyserCommandSource {
@ -595,9 +602,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
biomeDefinitionListPacket.setDefinitions(Registries.BIOMES_NBT.get());
upstream.sendPacket(biomeDefinitionListPacket);
AvailableEntityIdentifiersPacket entityPacket = new AvailableEntityIdentifiersPacket();
entityPacket.setIdentifiers(Registries.BEDROCK_ENTITY_IDENTIFIERS.get());
upstream.sendPacket(entityPacket);
this.sendAvailableEntityIdentifiers();
CreativeContentPacket creativePacket = new CreativeContentPacket();
creativePacket.setContents(this.itemMappings.getCreativeItems());
@ -627,6 +632,29 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
upstream.sendPacket(gamerulePacket);
}
public void sendAvailableEntityIdentifiers() {
NbtMap nbt = Registries.BEDROCK_ENTITY_IDENTIFIERS.get();
List<NbtMap> idlist = nbt.getList("idlist", NbtType.COMPOUND);
List<EntityIdentifier> identifiers = new ArrayList<>(idlist.size());
for (NbtMap identifier : idlist) {
identifiers.add(new GeyserEntityIdentifier(identifier));
}
DefineEntitiesEvent event = new DefineEntitiesEvent(this, identifiers);
this.geyser.eventBus().fire(event);
NbtMapBuilder builder = nbt.toBuilder();
builder.putList("idlist", NbtType.COMPOUND, event.identifiers()
.stream()
.map(identifer -> ((GeyserEntityIdentifier) identifer).nbt())
.collect(Collectors.toList())
);
AvailableEntityIdentifiersPacket entityPacket = new AvailableEntityIdentifiersPacket();
entityPacket.setIdentifiers(builder.build());
upstream.sendPacket(entityPacket);
}
public void authenticate(String username) {
authenticate(username, "");
}