Skip to content

Commit

Permalink
fix: Fix vanilla teleportTo test
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveplays28 committed Mar 15, 2024
1 parent c42b0c0 commit b889276
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
import org.mockito.Mockito;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Entity.class)
public abstract class MixinEntityTest {
@Redirect(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/Entity;forgeFluidTypeOnEyes:Lnet/neoforged/neoforge/fluids/FluidType;", shift = At.Shift.BY, by = -3))
private Holder<FluidType> cubic_chunks_3$fixNeoForgeRegistryError() {
return Mockito.mock();
}

@Inject(method = "toString", at = @At(value = "HEAD"), cancellable = true)
private void cubic_chunks_3$fixNullPointerException(CallbackInfoReturnable<String> cir) {
cir.setReturnValue("MockedEntity");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import static io.github.opencubicchunks.cubicchunks.testutils.Misc.setupServerLevel;
import static io.github.opencubicchunks.cubicchunks.testutils.Setup.setupTests;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

import io.github.opencubicchunks.cubicchunks.testutils.CloseableReference;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.TicketType;
import net.minecraft.world.level.ChunkPos;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.mockito.Mockito;

/**
* This test class is for testing {@link io.github.opencubicchunks.cubicchunks.mixin.core.common.server.level.MixinServerPlayer}
Expand All @@ -28,12 +31,20 @@ private ServerPlayer setupServerPlayer(ServerLevel serverLevel) {
return new ServerPlayer(mock(RETURNS_DEEP_STUBS), serverLevel, mock(RETURNS_DEEP_STUBS), mock(RETURNS_DEEP_STUBS));
}

// TODO: Stub. Test fails.
@Disabled @Test public void testTeleportToVanilla() throws Exception {
@Test public void testTeleportToVanilla() throws Exception {
try (CloseableReference<ServerLevel> serverLevelReference = setupServerLevel()) {
ServerPlayer player = setupServerPlayer(serverLevelReference.value());
player.teleportTo(player.serverLevel(), 0, 0, 0, mock(), 0, 0);
assertTrue(player.serverLevel().getChunkSource().hasChunk(0, 0));

// teleportTo takes the destination level to teleport to as a param, so we mock it
ServerLevel serverLevelReferenceSpy = spy(serverLevelReference.value());
ServerChunkCache serverChunkCacheMock = mock(ServerChunkCache.class);
Mockito.when(serverLevelReferenceSpy.getChunkSource()).thenReturn(serverChunkCacheMock);

player.teleportTo(serverLevelReferenceSpy, 0, 0, 0, mock(), 0, 0);

// Verify that serverChunkCacheMock.addRegionTicket(...) is called once
Mockito.verify(serverChunkCacheMock, Mockito.times(1))
.addRegionTicket(TicketType.POST_TELEPORT, new ChunkPos(0, 0), 1, player.getId());
}
}
}

0 comments on commit b889276

Please sign in to comment.