Skip to content

Commit

Permalink
wip: mojang mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulloy2 committed Jul 23, 2024
1 parent 4aa344b commit 84e7b75
Show file tree
Hide file tree
Showing 28 changed files with 434 additions and 357 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description = 'Provides access to the Minecraft protocol'
def isSnapshot = version.endsWith('-SNAPSHOT')

repositories {
// mavenLocal() // can speed up build, but may fail in CI
mavenLocal() // can speed up build, but may fail in CI
mavenCentral()

maven {
Expand All @@ -35,7 +35,7 @@ repositories {
dependencies {
implementation 'net.bytebuddy:byte-buddy:1.14.14'
compileOnly 'org.spigotmc:spigot-api:1.20.6-R0.1-SNAPSHOT'
compileOnly 'org.spigotmc:spigot:1.21-R0.1-SNAPSHOT'
compileOnly 'org.spigotmc:spigot:1.21-R0.1-SNAPSHOT:remapped-mojang'
compileOnly 'io.netty:netty-all:4.0.23.Final'
compileOnly 'net.kyori:adventure-text-serializer-gson:4.14.0'
compileOnly 'com.googlecode.json-simple:json-simple:1.1.1'
Expand All @@ -46,7 +46,7 @@ dependencies {
testImplementation 'org.mockito:mockito-core:5.6.0'
testImplementation 'io.netty:netty-common:4.1.97.Final'
testImplementation 'io.netty:netty-transport:4.1.97.Final'
testImplementation 'org.spigotmc:spigot:1.21-R0.1-SNAPSHOT'
testImplementation 'org.spigotmc:spigot:1.21-R0.1-SNAPSHOT:remapped-mojang'
testImplementation 'net.kyori:adventure-text-serializer-gson:4.14.0'
testImplementation 'net.kyori:adventure-text-serializer-plain:4.14.0'
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/comphenix/protocol/PacketType.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,12 @@ public static class Client extends PacketTypeEnum {
public static final PacketType UPDATE_SIGN = new PacketType(PROTOCOL, SENDER, 0x35, "SignUpdate", "UpdateSign", "CPacketUpdateSign");
public static final PacketType ARM_ANIMATION = new PacketType(PROTOCOL, SENDER, 0x36, "Swing", "ArmAnimation", "CPacketAnimation");
public static final PacketType SPECTATE = new PacketType(PROTOCOL, SENDER, 0x37, "TeleportToEntity", "Spectate", "CPacketSpectate");
public static final PacketType USE_ITEM_ON = new PacketType(PROTOCOL, SENDER, 0x38, "UseItemOn", "BlockPlace", "CPacketPlayerTryUseItemOnBlock");
public static final PacketType USE_ITEM = new PacketType(PROTOCOL, SENDER, 0x39, "UseItem", "CPacketPlayerTryUseItem");

public static final PacketType USE_ITEM_ON = new PacketType(PROTOCOL, SENDER, 0x38, "UseItemOn");
public static final PacketType USE_ITEM = new PacketType(PROTOCOL, SENDER, 0x39, "UseItem");

// public static final PacketType USE_ITEM_ON = new PacketType(PROTOCOL, SENDER, 0x38, "UseItemOn", "BlockPlace", "CPacketPlayerTryUseItemOnBlock");
// public static final PacketType USE_ITEM = new PacketType(PROTOCOL, SENDER, 0x39, "UseItem", "CPacketPlayerTryUseItem");

/**
* @deprecated Removed in 1.17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public long getLastSeen() {
return lastSeen;
}

@Override
public Location getRespawnLocation() {
return null;
}
Expand Down Expand Up @@ -241,11 +240,21 @@ public UUID getUniqueId() {
return uuid;
}

/**
* @return
*/
@NotNull
@Override
public @NotNull PlayerProfile getPlayerProfile() {
public PlayerProfile getPlayerProfile() {
return null;
}

/*
public com.destroystokyo.paper.profile.PlayerProfile getPlayerProfile() {
return null;
}
*/

@Override
public String getName() {
return name;
Expand Down Expand Up @@ -288,6 +297,11 @@ public boolean isOnline() {
return online;
}

// @Override
public boolean isConnected() {
return false;
}

@Override
public boolean isWhitelisted() {
return whitelisted;
Expand Down
92 changes: 49 additions & 43 deletions src/test/java/com/comphenix/protocol/BukkitInitialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

import com.google.common.util.concurrent.MoreExecutors;
import net.minecraft.SharedConstants;
import net.minecraft.commands.CommandDispatcher;
import net.minecraft.core.IRegistry;
import net.minecraft.core.IRegistryCustom;
import net.minecraft.commands.Commands.CommandSelection;
import net.minecraft.core.LayeredRegistryAccess;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.RegistryDataLoader;
import net.minecraft.server.DataPackResources;
import net.minecraft.server.DispenserRegistry;
import net.minecraft.server.Bootstrap;
import net.minecraft.server.RegistryLayer;
import net.minecraft.server.ReloadableServerResources;
import net.minecraft.server.WorldLoader;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.server.level.WorldServer;
import net.minecraft.server.packs.EnumResourcePackType;
import net.minecraft.server.packs.repository.ResourcePackLoader;
import net.minecraft.server.packs.repository.ResourcePackRepository;
import net.minecraft.server.packs.repository.ResourcePackSourceVanilla;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.repository.PackRepository;
import net.minecraft.server.packs.repository.ServerPacksSource;
import net.minecraft.server.packs.resources.CloseableResourceManager;
import net.minecraft.server.packs.resources.MultiPackResourceManager;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.item.enchantment.Enchantments;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -81,6 +81,8 @@ private void initialize() {
synchronized (initLock) {
if (initialized) {
return;
} else {
initialized = true;
}

try {
Expand All @@ -93,33 +95,33 @@ private void initialize() {
instance.setPackage();

// Minecraft Data Init
SharedConstants.a(); // .tryDetectVersion()
DispenserRegistry.a(); // .bootStrap()

ResourcePackRepository resourcePackRepository = ResourcePackSourceVanilla.c(); // .createVanillaTrustedRepository()
resourcePackRepository.a(); // .reload()

ResourceManager resourceManager = new ResourceManager(
EnumResourcePackType.b /* SERVER_DATA */,
resourcePackRepository.c() /* getAvailablePacks() */ .stream().map(ResourcePackLoader::f /* openFull() */).collect(Collectors.toList()));
LayeredRegistryAccess<RegistryLayer> layeredRegistryAccess = RegistryLayer.a(); // .createRegistryAccess()
layeredRegistryAccess = WorldLoader.b(resourceManager, layeredRegistryAccess, RegistryLayer.b /* WORLDGEN */, RegistryDataLoader.a /* WORLDGEN_REGISTRIES */); // .loadAndReplaceLayer()
IRegistryCustom.Dimension registryCustom = layeredRegistryAccess.a().d(); // .compositeAccess().freeze()
// IRegistryCustom.Dimension registryCustom = layeredRegistryAccess.a().c(); // .compositeAccess().freeze()

DataPackResources dataPackResources = DataPackResources.a(
resourceManager,
layeredRegistryAccess,
FeatureFlags.d.a() /* REGISTRY.allFlags() */,
CommandDispatcher.ServerType.b /* DEDICATED */,
0,
MoreExecutors.directExecutor(),
MoreExecutors.directExecutor()
SharedConstants.tryDetectVersion();
Bootstrap.bootStrap();

PackRepository resourcePackRepository = ServerPacksSource.createVanillaTrustedRepository();
resourcePackRepository.reload();

CloseableResourceManager resourceManager = new MultiPackResourceManager(
PackType.SERVER_DATA,
resourcePackRepository.getAvailablePacks().stream().map(Pack::open).collect(Collectors.toList())
);
LayeredRegistryAccess<RegistryLayer> layeredRegistryAccess = RegistryLayer.createRegistryAccess();
layeredRegistryAccess = WorldLoader.loadAndReplaceLayer(resourceManager, layeredRegistryAccess, RegistryLayer.WORLDGEN, RegistryDataLoader.WORLDGEN_REGISTRIES);
RegistryAccess.Frozen registryCustom = layeredRegistryAccess.compositeAccess().freeze();

ReloadableServerResources dataPackResources = ReloadableServerResources.loadResources(
resourceManager,
layeredRegistryAccess,
FeatureFlags.REGISTRY.allFlags(),
CommandSelection.DEDICATED,
0,
MoreExecutors.directExecutor(),
MoreExecutors.directExecutor()
).join();
dataPackResources.g();
dataPackResources.updateRegistryTags();

try {
IRegistry.class.getName();
RegistryAccess.class.getName();
} catch (Throwable ex) {
ex.printStackTrace();
}
Expand All @@ -131,7 +133,7 @@ private void initialize() {
CraftServer mockedServer = mock(CraftServer.class);
DedicatedServer mockedGameServer = mock(DedicatedServer.class);

when(mockedGameServer.bc()/*registryAccess*/).thenReturn(registryCustom);
when(mockedGameServer.registryAccess()).thenReturn(registryCustom);

when(mockedServer.getLogger()).thenReturn(java.util.logging.Logger.getLogger("Minecraft"));
when(mockedServer.getName()).thenReturn("Mock Server");
Expand All @@ -144,14 +146,20 @@ private void initialize() {
when(mockedServer.getUnsafe()).thenReturn(CraftMagicNumbers.INSTANCE);
when(mockedServer.getLootTable(any())).thenAnswer(invocation -> {
NamespacedKey key = invocation.getArgument(0);
return new CraftLootTable(key, dataPackResources.b().b(CraftLootTable.bukkitKeyToMinecraft(key)));
return new CraftLootTable(key, dataPackResources.fullRegistries().getLootTable(CraftLootTable.bukkitKeyToMinecraft(key)));
});

when(mockedServer.getRegistry(any())).thenAnswer(invocation -> {
Class<Keyed> registryType = invocation.getArgument(0);
return CraftRegistry.createRegistry(registryType, registryCustom);
Class<? extends Keyed> registryType = invocation.getArgument(0);

try {
return CraftRegistry.createRegistry(registryType, registryCustom);
} catch (Exception ignored) {
return mock(org.bukkit.Registry.class);
}
});

WorldServer nmsWorld = mock(WorldServer.class);
ServerLevel nmsWorld = mock(ServerLevel.class);
SpigotWorldConfig mockWorldConfig = mock(SpigotWorldConfig.class);

try {
Expand All @@ -172,10 +180,8 @@ private void initialize() {
CraftRegistry.setMinecraftRegistry(registryCustom);

// Init Enchantments
Enchantments.A.getClass();
Enchantments.AQUA_AFFINITY.getClass();
// Enchantment.stopAcceptingRegistrations();

initialized = true;
}
}

Expand Down
45 changes: 7 additions & 38 deletions src/test/java/com/comphenix/protocol/PacketTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,22 @@
*/
package com.comphenix.protocol;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.PacketType.Protocol;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.injector.packet.PacketRegistry;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.wrappers.WrappedChatComponent;

import net.minecraft.network.protocol.login.PacketLoginInStart;
import net.minecraft.network.protocol.login.ServerboundHelloPacket;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author dmulloy2
Expand Down Expand Up @@ -260,7 +253,7 @@ public void testFindCurrent() {
@Test
public void testLoginStart() {
// This packet is critical for handleLoin
assertEquals(PacketLoginInStart.class, PacketType.Login.Client.START.getPacketClass());
assertEquals(ServerboundHelloPacket.class, PacketType.Login.Client.START.getPacketClass());
}

@Test
Expand All @@ -287,24 +280,6 @@ public void ensureRegistryInitializes() throws Exception {
}
}

@Test
@Disabled // TODO -- lots of constructor parameters :(
public void testCreateMapChunk() {
new PacketContainer(PacketType.Play.Server.MAP_CHUNK);
}

@Test
@Disabled // TODO -- ScoreboardObjective parameter in constructor is causing this to fail
public void testCreateScoreboardObjective() {
new PacketContainer(PacketType.Play.Server.SCOREBOARD_OBJECTIVE);
}

@Test
@Disabled // TODO -- Entity parameter in constructor is causing this to fail
public void testCreateEntitySound() {
new PacketContainer(PacketType.Play.Server.ENTITY_SOUND);
}

@Test
public void testPacketCreation() {
List<PacketType> failed = new ArrayList<>();
Expand All @@ -313,12 +288,6 @@ public void testPacketCreation() {
continue;
}

if (type == PacketType.Play.Server.ENTITY_SOUND
|| type == PacketType.Play.Server.SCOREBOARD_OBJECTIVE
|| type == PacketType.Play.Server.MAP_CHUNK) {
continue;
}

try {
new PacketContainer(type);
} catch (Exception ex) {
Expand Down
Loading

0 comments on commit 84e7b75

Please sign in to comment.