Skip to content

Commit 043daa7

Browse files
committed
Add test for Note Blocks in the Minecraft Mod Integration
1 parent 1e96a64 commit 043daa7

File tree

6 files changed

+380
-6
lines changed

6 files changed

+380
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dan200.computercraft.gametest.core;
2+
3+
import net.minecraft.core.BlockPos;
4+
import net.minecraftforge.event.level.NoteBlockEvent;
5+
import net.minecraftforge.eventbus.api.SubscribeEvent;
6+
import net.minecraftforge.fml.common.Mod;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
@Mod.EventBusSubscriber(modid = TestMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
12+
public class TestEvents {
13+
public static final Map<BlockPos, Integer> triggeredNoteBlocks = new HashMap<>();
14+
15+
@SubscribeEvent
16+
public static void onNoteBlockTrigger(NoteBlockEvent.Play event) {
17+
if (event.getLevel().isClientSide()) return;
18+
triggeredNoteBlocks.put(event.getPos(), triggeredNoteBlocks.getOrDefault(event.getPos(), 0) + 1);
19+
}
20+
21+
}

src/testMod/java/dan200/computercraft/gametest/core/TestMod.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232
import java.util.Locale;
3333
import java.util.function.Consumer;
3434

35-
@Mod("advancedperipheralstest")
35+
@Mod(TestMod.MOD_ID)
3636
public class TestMod {
37+
38+
public static final String MOD_ID = "advancedperipheralstest";
39+
3740
public TestMod() {
3841
TestHooks.init();
3942

src/testMod/kotlin/de/srendi/advancedperipherals/test/ModIntegrTest.kt

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package de.srendi.advancedperipherals.test
22

3-
import dan200.computercraft.gametest.api.GameTestHolder
4-
import dan200.computercraft.gametest.api.sequence
5-
import dan200.computercraft.gametest.api.thenComputerOk
6-
import net.minecraft.gametest.framework.GameTest
7-
import net.minecraft.gametest.framework.GameTestHelper
3+
import dan200.computercraft.gametest.api.*
4+
import dan200.computercraft.gametest.core.TestEvents
5+
import net.minecraft.core.BlockPos
6+
import net.minecraft.gametest.framework.*
7+
import net.minecraft.world.InteractionHand
8+
import net.minecraft.world.entity.EntityType
9+
import net.minecraft.world.entity.animal.allay.Allay
10+
import net.minecraft.world.item.ItemStack
11+
import net.minecraft.world.item.Items
812

913
@GameTestHolder
1014
class ModIntegrTest {
@@ -29,4 +33,47 @@ class ModIntegrTest {
2933
thenComputerOk();
3034
}
3135

36+
@GameTest
37+
fun minecraftNoteBlock(context: GameTestHelper) = context.sequence {
38+
thenExecute { TestEvents.triggeredNoteBlocks.clear() }
39+
thenComputerOk()
40+
thenExecute {
41+
val successBlock = BlockPos(2, 2, 1)
42+
val failBlock = BlockPos(2, 2, 3)
43+
44+
if (TestEvents.triggeredNoteBlocks.getOrDefault(context.absolutePos(successBlock), 0) != 1)
45+
context.fail("Note Block should have played one time", successBlock)
46+
47+
if (TestEvents.triggeredNoteBlocks.getOrDefault(context.absolutePos(failBlock), 0) != 0)
48+
context.fail("Note Block should not have played", failBlock)
49+
}
50+
}
51+
52+
@GameTest(timeoutTicks = 300)
53+
fun minecraftNoteBlock_Triggering_Allay(context: GameTestHelper) = context.sequence {
54+
// test if playNote triggers an allay
55+
// related issue: https://github.com/IntelligenceModding/AdvancedPeripherals/issues/603
56+
57+
val item = Items.DIAMOND
58+
var allay: Allay? = null
59+
thenExecute {
60+
allay = context.spawn(EntityType.ALLAY, 2, 3, 2)
61+
allay?.setItemInHand(InteractionHand.MAIN_HAND, ItemStack(item))
62+
63+
context.spawnItem(item, 2f, 3f, 2f)
64+
}
65+
66+
thenWaitUntil { context.assertEntityNotPresent(EntityType.ITEM) }
67+
thenWaitUntil {
68+
if (allay?.inventory?.getItem(0)?.count != 1)
69+
context.fail("Expected Allay to pick up item")
70+
}
71+
thenOnComputer { callPeripheral("left", "playNote") }
72+
thenWaitUntil { context.assertEntityPresent(EntityType.ITEM) }
73+
thenWaitUntil {
74+
if (allay?.inventory?.getItem(0)?.count != 0)
75+
context.fail("Expected Allay to drop item")
76+
}
77+
}
78+
3279
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
--- Advanced Peripherals tests for the Minecraft integration on Note Blocks
3+
--- Covers `playNote`, `getNote`, `changeNoteBy`, `changeNote`
4+
---
5+
6+
test.eq("noteBlock", peripheral.getType("left"), "Peripheral should be noteBlock")
7+
noteBlock = peripheral.wrap("left")
8+
test.assert(noteBlock, "Peripheral not found")
9+
10+
test.eq(4, noteBlock.getNote(), "Note should be 4")
11+
test.eq(24, noteBlock.changeNoteBy(24), "Note should be 24 after setting it to 24")
12+
test.eq(24, noteBlock.getNote(), "Note should be 24")
13+
test.eq(0, noteBlock.changeNote(), "Note should be 0 after cycling it")
14+
test.eq(0, noteBlock.getNote(), "Note should be 0")
15+
test.eq(1, noteBlock.changeNote(), "Note should be 1 after cycling it")
16+
test.eq(1, noteBlock.getNote(), "Note should be 1")
17+
noteBlock.playNote()
18+
19+
-- this note block has a block above it, so it should not play a note
20+
test.eq("noteBlock", peripheral.getType("right"), "Peripheral should be noteBlock")
21+
silentNoteBlock = peripheral.wrap("right")
22+
test.assert(silentNoteBlock, "Peripheral not found")
23+
24+
silentNoteBlock.playNote()
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
DataVersion: 3120,
3+
size: [5, 5, 5],
4+
data: [
5+
{pos: [0, 0, 0], state: "minecraft:polished_andesite"},
6+
{pos: [0, 0, 1], state: "minecraft:polished_andesite"},
7+
{pos: [0, 0, 2], state: "minecraft:polished_andesite"},
8+
{pos: [0, 0, 3], state: "minecraft:polished_andesite"},
9+
{pos: [0, 0, 4], state: "minecraft:polished_andesite"},
10+
{pos: [1, 0, 0], state: "minecraft:polished_andesite"},
11+
{pos: [1, 0, 1], state: "minecraft:polished_andesite"},
12+
{pos: [1, 0, 2], state: "minecraft:polished_andesite"},
13+
{pos: [1, 0, 3], state: "minecraft:polished_andesite"},
14+
{pos: [1, 0, 4], state: "minecraft:polished_andesite"},
15+
{pos: [2, 0, 0], state: "minecraft:polished_andesite"},
16+
{pos: [2, 0, 1], state: "minecraft:polished_andesite"},
17+
{pos: [2, 0, 2], state: "minecraft:polished_andesite"},
18+
{pos: [2, 0, 3], state: "minecraft:polished_andesite"},
19+
{pos: [2, 0, 4], state: "minecraft:polished_andesite"},
20+
{pos: [3, 0, 0], state: "minecraft:polished_andesite"},
21+
{pos: [3, 0, 1], state: "minecraft:polished_andesite"},
22+
{pos: [3, 0, 2], state: "minecraft:polished_andesite"},
23+
{pos: [3, 0, 3], state: "minecraft:polished_andesite"},
24+
{pos: [3, 0, 4], state: "minecraft:polished_andesite"},
25+
{pos: [4, 0, 0], state: "minecraft:polished_andesite"},
26+
{pos: [4, 0, 1], state: "minecraft:polished_andesite"},
27+
{pos: [4, 0, 2], state: "minecraft:polished_andesite"},
28+
{pos: [4, 0, 3], state: "minecraft:polished_andesite"},
29+
{pos: [4, 0, 4], state: "minecraft:polished_andesite"},
30+
{pos: [0, 1, 0], state: "minecraft:air"},
31+
{pos: [0, 1, 1], state: "minecraft:air"},
32+
{pos: [0, 1, 2], state: "minecraft:air"},
33+
{pos: [0, 1, 3], state: "minecraft:air"},
34+
{pos: [0, 1, 4], state: "minecraft:air"},
35+
{pos: [1, 1, 0], state: "minecraft:air"},
36+
{pos: [1, 1, 1], state: "minecraft:air"},
37+
{pos: [1, 1, 2], state: "minecraft:air"},
38+
{pos: [1, 1, 3], state: "minecraft:air"},
39+
{pos: [1, 1, 4], state: "minecraft:air"},
40+
{pos: [2, 1, 0], state: "minecraft:air"},
41+
{pos: [2, 1, 1], state: "minecraft:note_block{instrument:basedrum,note:4,powered:false}"},
42+
{pos: [2, 1, 2], state: "computercraft:computer_advanced{facing:west,state:blinking}", nbt: {ComputerId: 0, ForgeCaps: {}, Label: "modintegrtest.minecraftnoteblock", On: 1b, id: "computercraft:computer_advanced"}},
43+
{pos: [2, 1, 3], state: "minecraft:note_block{instrument:basedrum,note:0,powered:false}"},
44+
{pos: [2, 1, 4], state: "minecraft:air"},
45+
{pos: [3, 1, 0], state: "minecraft:air"},
46+
{pos: [3, 1, 1], state: "minecraft:air"},
47+
{pos: [3, 1, 2], state: "minecraft:air"},
48+
{pos: [3, 1, 3], state: "minecraft:air"},
49+
{pos: [3, 1, 4], state: "minecraft:air"},
50+
{pos: [4, 1, 0], state: "minecraft:air"},
51+
{pos: [4, 1, 1], state: "minecraft:air"},
52+
{pos: [4, 1, 2], state: "minecraft:air"},
53+
{pos: [4, 1, 3], state: "minecraft:air"},
54+
{pos: [4, 1, 4], state: "minecraft:air"},
55+
{pos: [0, 2, 0], state: "minecraft:air"},
56+
{pos: [0, 2, 1], state: "minecraft:air"},
57+
{pos: [0, 2, 2], state: "minecraft:air"},
58+
{pos: [0, 2, 3], state: "minecraft:air"},
59+
{pos: [0, 2, 4], state: "minecraft:air"},
60+
{pos: [1, 2, 0], state: "minecraft:air"},
61+
{pos: [1, 2, 1], state: "minecraft:air"},
62+
{pos: [1, 2, 2], state: "minecraft:air"},
63+
{pos: [1, 2, 3], state: "minecraft:air"},
64+
{pos: [1, 2, 4], state: "minecraft:air"},
65+
{pos: [2, 2, 0], state: "minecraft:air"},
66+
{pos: [2, 2, 1], state: "minecraft:air"},
67+
{pos: [2, 2, 2], state: "minecraft:air"},
68+
{pos: [2, 2, 3], state: "minecraft:polished_andesite"},
69+
{pos: [2, 2, 4], state: "minecraft:air"},
70+
{pos: [3, 2, 0], state: "minecraft:air"},
71+
{pos: [3, 2, 1], state: "minecraft:air"},
72+
{pos: [3, 2, 2], state: "minecraft:air"},
73+
{pos: [3, 2, 3], state: "minecraft:air"},
74+
{pos: [3, 2, 4], state: "minecraft:air"},
75+
{pos: [4, 2, 0], state: "minecraft:air"},
76+
{pos: [4, 2, 1], state: "minecraft:air"},
77+
{pos: [4, 2, 2], state: "minecraft:air"},
78+
{pos: [4, 2, 3], state: "minecraft:air"},
79+
{pos: [4, 2, 4], state: "minecraft:air"},
80+
{pos: [0, 3, 0], state: "minecraft:air"},
81+
{pos: [0, 3, 1], state: "minecraft:air"},
82+
{pos: [0, 3, 2], state: "minecraft:air"},
83+
{pos: [0, 3, 3], state: "minecraft:air"},
84+
{pos: [0, 3, 4], state: "minecraft:air"},
85+
{pos: [1, 3, 0], state: "minecraft:air"},
86+
{pos: [1, 3, 1], state: "minecraft:air"},
87+
{pos: [1, 3, 2], state: "minecraft:air"},
88+
{pos: [1, 3, 3], state: "minecraft:air"},
89+
{pos: [1, 3, 4], state: "minecraft:air"},
90+
{pos: [2, 3, 0], state: "minecraft:air"},
91+
{pos: [2, 3, 1], state: "minecraft:air"},
92+
{pos: [2, 3, 2], state: "minecraft:air"},
93+
{pos: [2, 3, 3], state: "minecraft:air"},
94+
{pos: [2, 3, 4], state: "minecraft:air"},
95+
{pos: [3, 3, 0], state: "minecraft:air"},
96+
{pos: [3, 3, 1], state: "minecraft:air"},
97+
{pos: [3, 3, 2], state: "minecraft:air"},
98+
{pos: [3, 3, 3], state: "minecraft:air"},
99+
{pos: [3, 3, 4], state: "minecraft:air"},
100+
{pos: [4, 3, 0], state: "minecraft:air"},
101+
{pos: [4, 3, 1], state: "minecraft:air"},
102+
{pos: [4, 3, 2], state: "minecraft:air"},
103+
{pos: [4, 3, 3], state: "minecraft:air"},
104+
{pos: [4, 3, 4], state: "minecraft:air"},
105+
{pos: [0, 4, 0], state: "minecraft:air"},
106+
{pos: [0, 4, 1], state: "minecraft:air"},
107+
{pos: [0, 4, 2], state: "minecraft:air"},
108+
{pos: [0, 4, 3], state: "minecraft:air"},
109+
{pos: [0, 4, 4], state: "minecraft:air"},
110+
{pos: [1, 4, 0], state: "minecraft:air"},
111+
{pos: [1, 4, 1], state: "minecraft:air"},
112+
{pos: [1, 4, 2], state: "minecraft:air"},
113+
{pos: [1, 4, 3], state: "minecraft:air"},
114+
{pos: [1, 4, 4], state: "minecraft:air"},
115+
{pos: [2, 4, 0], state: "minecraft:air"},
116+
{pos: [2, 4, 1], state: "minecraft:air"},
117+
{pos: [2, 4, 2], state: "minecraft:air"},
118+
{pos: [2, 4, 3], state: "minecraft:air"},
119+
{pos: [2, 4, 4], state: "minecraft:air"},
120+
{pos: [3, 4, 0], state: "minecraft:air"},
121+
{pos: [3, 4, 1], state: "minecraft:air"},
122+
{pos: [3, 4, 2], state: "minecraft:air"},
123+
{pos: [3, 4, 3], state: "minecraft:air"},
124+
{pos: [3, 4, 4], state: "minecraft:air"},
125+
{pos: [4, 4, 0], state: "minecraft:air"},
126+
{pos: [4, 4, 1], state: "minecraft:air"},
127+
{pos: [4, 4, 2], state: "minecraft:air"},
128+
{pos: [4, 4, 3], state: "minecraft:air"},
129+
{pos: [4, 4, 4], state: "minecraft:air"}
130+
],
131+
entities: [],
132+
palette: [
133+
"minecraft:polished_andesite",
134+
"minecraft:note_block{instrument:basedrum,note:4,powered:false}",
135+
"minecraft:note_block{instrument:basedrum,note:0,powered:false}",
136+
"minecraft:air",
137+
"computercraft:computer_advanced{facing:west,state:blinking}"
138+
]
139+
}

0 commit comments

Comments
 (0)