1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-09 18:03:08 +00:00

Merge branch 'mc-1.19.x' into mc-1.20.x

This commit is contained in:
Jonathan Coates
2023-08-27 19:08:59 +01:00
153 changed files with 2038 additions and 1032 deletions

View File

@@ -21,6 +21,7 @@ import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
@@ -51,6 +52,7 @@ import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
@@ -69,30 +71,41 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
throw new UnsupportedOperationException("Cannot create config file inside tests");
}
@Override
public <T> RegistryWrappers.RegistryWrapper<T> wrap(ResourceKey<Registry<T>> registry) {
throw new UnsupportedOperationException("Cannot query registry inside tests");
}
@Override
public <T> RegistrationHelper<T> createRegistrationHelper(ResourceKey<Registry<T>> registry) {
throw new UnsupportedOperationException("Cannot query registry inside tests");
}
@Override
public <K> ResourceLocation getRegistryKey(ResourceKey<Registry<K>> registry, K object) {
throw new UnsupportedOperationException("Cannot query registry inside tests");
@SuppressWarnings("unchecked")
private static <T> Registry<T> getRegistry(ResourceKey<Registry<T>> id) {
var registry = (Registry<T>) BuiltInRegistries.REGISTRY.get(id.location());
if (registry == null) throw new IllegalArgumentException("Unknown registry " + id);
return registry;
}
@Override
public <K> K getRegistryObject(ResourceKey<Registry<K>> registry, ResourceLocation id) {
throw new UnsupportedOperationException("Cannot query registry inside tests");
public <T> ResourceLocation getRegistryKey(ResourceKey<Registry<T>> registry, T object) {
var key = getRegistry(registry).getKey(object);
if (key == null) throw new IllegalArgumentException(object + " was not registered in " + registry);
return key;
}
@Override
public <T> T getRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) {
var value = getRegistry(registry).get(id);
if (value == null) throw new IllegalArgumentException(id + " was not registered in " + registry);
return value;
}
@Override
public <T> RegistryWrappers.RegistryWrapper<T> wrap(ResourceKey<Registry<T>> registry) {
return new RegistryWrapperImpl<>(registry.location(), getRegistry(registry));
}
@Nullable
@Override
public <T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) {
throw new UnsupportedOperationException("Cannot query registries");
return getRegistry(registry).get(id);
}
@Override
@@ -245,4 +258,48 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
public String getInstalledVersion() {
return "1.0";
}
private record RegistryWrapperImpl<T>(
ResourceLocation name, Registry<T> registry
) implements RegistryWrappers.RegistryWrapper<T> {
@Override
public int getId(T object) {
return registry.getId(object);
}
@Override
public ResourceLocation getKey(T object) {
var key = registry.getKey(object);
if (key == null) throw new IllegalArgumentException(object + " was not registered in " + name);
return key;
}
@Override
public T get(ResourceLocation location) {
var object = registry.get(location);
if (object == null) throw new IllegalArgumentException(location + " was not registered in " + name);
return object;
}
@Nullable
@Override
public T tryGet(ResourceLocation location) {
return registry.get(location);
}
@Override
public @Nullable T byId(int id) {
return registry.byId(id);
}
@Override
public int size() {
return registry.size();
}
@Override
public Iterator<T> iterator() {
return registry.iterator();
}
}
}

View File

@@ -18,22 +18,11 @@ import static org.junit.jupiter.api.Assertions.*;
*/
public class TerminalStateTest {
@RepeatedTest(5)
public void testCompressed() {
public void testRoundTrip() {
var terminal = randomTerminal();
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
new TerminalState(terminal, true).write(buffer);
checkEqual(terminal, read(buffer));
assertEquals(0, buffer.readableBytes());
}
@RepeatedTest(5)
public void testUncompressed() {
var terminal = randomTerminal();
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
new TerminalState(terminal, false).write(buffer);
new TerminalState(terminal).write(buffer);
checkEqual(terminal, read(buffer));
assertEquals(0, buffer.readableBytes());

View File

@@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.network.client;
import dan200.computercraft.test.core.StructuralEquality;
import dan200.computercraft.test.shared.MinecraftArbitraries;
import dan200.computercraft.test.shared.WithMinecraft;
import io.netty.buffer.Unpooled;
import net.jqwik.api.*;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.sounds.SoundEvent;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@WithMinecraft
class PlayRecordClientMessageTest {
static {
WithMinecraft.Setup.bootstrap(); // @Property doesn't run test lifecycle methods.
}
/**
* Sends packets on a roundtrip, ensuring that their contents are reassembled on the other end.
*
* @param message The message to send.
*/
@Property
public void testRoundTrip(@ForAll("message") PlayRecordClientMessage message) {
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
message.toBytes(buffer);
var converted = new PlayRecordClientMessage(buffer);
assertEquals(buffer.readableBytes(), 0, "Whole packet was read");
assertThat("Messages are equal", converted, equality.asMatcher(PlayRecordClientMessage.class, message));
}
@Provide
Arbitrary<PlayRecordClientMessage> message() {
return Combinators.combine(
MinecraftArbitraries.blockPos(),
MinecraftArbitraries.soundEvent().injectNull(0.3),
Arbitraries.strings().ofMaxLength(1000).injectNull(0.3)
).as(PlayRecordClientMessage::new);
}
private static final StructuralEquality<PlayRecordClientMessage> equality = StructuralEquality.all(
StructuralEquality.field(PlayRecordClientMessage.class, "pos"),
StructuralEquality.field(PlayRecordClientMessage.class, "name"),
StructuralEquality.field(PlayRecordClientMessage.class, "soundEvent", StructuralEquality.all(
StructuralEquality.at("location", SoundEvent::getLocation),
StructuralEquality.field(SoundEvent.class, "range"),
StructuralEquality.field(SoundEvent.class, "newSystem")
))
);
}

View File

@@ -0,0 +1,82 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.turtle.upgrades;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleToolDurability;
import dan200.computercraft.test.core.StructuralEquality;
import dan200.computercraft.test.shared.MinecraftArbitraries;
import dan200.computercraft.test.shared.WithMinecraft;
import io.netty.buffer.Unpooled;
import net.jqwik.api.*;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.item.ItemStack;
import org.hamcrest.Description;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@WithMinecraft
class TurtleToolSerialiserTest {
static {
WithMinecraft.Setup.bootstrap(); // @Property doesn't run test lifecycle methods.
}
/**
* Sends turtle tools on a roundtrip, ensuring that their contents are reassembled on the other end.
*
* @param tool The message to send.
*/
@Property
public void testRoundTrip(@ForAll("tool") TurtleTool tool) {
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
TurtleToolSerialiser.INSTANCE.toNetwork(buffer, tool);
var converted = TurtleToolSerialiser.INSTANCE.fromNetwork(tool.getUpgradeID(), buffer);
assertEquals(buffer.readableBytes(), 0, "Whole packet was read");
if (!equality.equals(tool, converted)) {
System.out.println("Break");
}
assertThat("Messages are equal", converted, equality.asMatcher(TurtleTool.class, tool));
}
@Provide
Arbitrary<TurtleTool> tool() {
return Combinators.combine(
MinecraftArbitraries.resourceLocation(),
Arbitraries.strings().ofMaxLength(100),
MinecraftArbitraries.item(),
MinecraftArbitraries.itemStack(),
Arbitraries.floats(),
Arbitraries.of(true, false),
Arbitraries.of(TurtleToolDurability.values()),
MinecraftArbitraries.tagKey(Registries.BLOCK)
).as(TurtleTool::new);
}
private static final StructuralEquality<ItemStack> stackEquality = new StructuralEquality<>() {
@Override
public boolean equals(ItemStack left, ItemStack right) {
return ItemStack.isSameItemSameTags(left, right) && left.getCount() == right.getCount();
}
@Override
public void describe(Description description, ItemStack object) {
description.appendValue(object).appendValue(object.getTag());
}
};
private static final StructuralEquality<TurtleTool> equality = StructuralEquality.all(
StructuralEquality.at("id", ITurtleUpgrade::getUpgradeID),
StructuralEquality.at("craftingItem", ITurtleUpgrade::getCraftingItem, stackEquality),
StructuralEquality.at("tool", x -> x.item, stackEquality),
StructuralEquality.at("damageMulitiplier", x -> x.damageMulitiplier),
StructuralEquality.at("allowEnchantments", x -> x.allowEnchantments),
StructuralEquality.at("consumeDurability", x -> x.consumeDurability),
StructuralEquality.at("breakable", x -> x.breakable)
);
}