Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix registry/attribute errors in tests mocking the server player #35

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ test {
events "passed", "skipped", "failed"
}
jvmArgs("-ea", "-Dmixin.debug.verbose=true", "-Dmixin.debug.export=true", "-Dmixin.checks.interfaces=true")
exclude "**/mixin*"
}

tasks.withType(ProcessResources).configureEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.opencubicchunks.cubicchunks.mixin.test.common;

import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.DefaultAttributes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(DefaultAttributes.class)
public abstract class MixinDefaultAttributesTest {
@Redirect(method = "<clinit>", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/ai/attributes/DefaultAttributes;SUPPLIERS:Ljava/util/Map;", shift = At.Shift.BY, by = -1))
private static ImmutableMap<?, ?> cubic_chunks_3$fixNeoForgeRegistryError(ImmutableMap.Builder<?, ?> instance) {
return ImmutableMap.builder().build();
}

@Redirect(method = "<clinit>", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;createLivingAttributes()Lnet/minecraft/world/entity/ai/attributes/AttributeSupplier$Builder;"))
private static AttributeSupplier.Builder cubic_chunks_3$fixNeoForgeRegistryError() {
return AttributeSupplier.builder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.opencubicchunks.cubicchunks.mixin.test.common;

import net.minecraft.core.Holder;
import net.minecraft.world.entity.Entity;
import net.neoforged.neoforge.fluids.FluidType;
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
@@ -0,0 +1,23 @@
package io.github.opencubicchunks.cubicchunks.mixin.test.common;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
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.callback.CallbackInfoReturnable;

@Mixin(LivingEntity.class)
public abstract class MixinLivingEntityTest {
@Inject(method = "createLivingAttributes", at = @At(value = "HEAD"), cancellable = true)
private static void cubic_chunks_3$mockLivingAttributesInitialization(CallbackInfoReturnable<AttributeSupplier.Builder> cir) {
cir.setReturnValue(Mockito.mock());
}

@Inject(method = "getAttributeValue(Lnet/minecraft/world/entity/ai/attributes/Attribute;)D", at = @At(value = "HEAD"), cancellable = true)
private void cubic_chunks_3$fixNeoForgeErrors(Attribute pAttribute, CallbackInfoReturnable<Double> cir) {
cir.setReturnValue(1d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.opencubicchunks.cubicchunks.mixin.test.common;

import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
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.callback.CallbackInfoReturnable;

@Mixin(Mob.class)
public abstract class MixinMobTest {
@Inject(method = "createMobAttributes", at = @At(value = "HEAD"), cancellable = true)
private static void cubic_chunks_3$fixNeoForgeErrors(CallbackInfoReturnable<AttributeSupplier.Builder> cir) {
cir.setReturnValue(AttributeSupplier.builder());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.opencubicchunks.cubicchunks.mixin.test.common;

import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.player.Player;
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.callback.CallbackInfoReturnable;

@Mixin(Player.class)
public abstract class MixinPlayerTest {
@Inject(method = "createAttributes", at = @At(value = "HEAD"), cancellable = true)
private static void cubic_chunks_3$fixNeoForgeErrors(CallbackInfoReturnable<AttributeSupplier.Builder> cir) {
cir.setReturnValue(AttributeSupplier.builder());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package io.github.opencubicchunks.cubicchunks.mixin.test.common;

import javax.annotation.ParametersAreNonnullByDefault;

import io.github.opencubicchunks.cc_core.annotation.MethodsReturnNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.opencubicchunks.cubicchunks.mixin.test.common.server;

import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import org.mockito.Mockito;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(ServerPlayer.class)
public abstract class MixinServerPlayerTest {
@Shadow
@SuppressWarnings("unused")
public ServerGamePacketListenerImpl connection = Mockito.mock();
Steveplays28 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package io.github.opencubicchunks.cubicchunks.mixin.test.common.server.level;

import javax.annotation.ParametersAreNonnullByDefault;

import io.github.opencubicchunks.cc_core.annotation.MethodsReturnNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package io.github.opencubicchunks.cubicchunks.mixin.test.common.server;

import javax.annotation.ParametersAreNonnullByDefault;

import io.github.opencubicchunks.cc_core.annotation.MethodsReturnNonnullByDefault;
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. This test needs a mixin from MixinEntityTest to ignore forgeFluidTypeOnEyes from NeoForge.
@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 serverLevelSpy = spy(serverLevelReference.value());
ServerChunkCache serverChunkCacheMock = mock(ServerChunkCache.class);
Mockito.when(serverLevelSpy.getChunkSource()).thenReturn(serverChunkCacheMock);

player.teleportTo(serverLevelSpy, 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());
}
}
}
Loading