Skip to content

Commit 2b52d38

Browse files
committed
Merge branch 'bugfix/fix-inventory-snapshot-test' into develop
2 parents c0406e3 + 8f5198a commit 2b52d38

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

src/main/java/stevebot/data/player/PlayerSnapshot.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package stevebot.data.player;
22

3-
import net.minecraft.client.entity.EntityPlayerSP;
43
import net.minecraft.item.Item;
54
import net.minecraft.item.ItemTool;
65
import stevebot.data.blocks.BlockLibrary;
@@ -16,8 +15,8 @@
1615
import stevebot.data.modification.BlockPlaceModification;
1716
import stevebot.data.modification.HealthChangeModification;
1817
import stevebot.data.modification.Modification;
18+
import stevebot.minecraft.MinecraftAdapter;
1919
import stevebot.pathfinding.actions.ActionCosts;
20-
import stevebot.player.PlayerUtils;
2120

2221
public class PlayerSnapshot {
2322

@@ -62,8 +61,7 @@ public PlayerSnapshot(PlayerSnapshot snapshot) {
6261
public void applyModification(Modification modification) {
6362

6463
if (modification instanceof BlockPlaceModification) {
65-
final EntityPlayerSP player = PlayerUtils.getPlayer();
66-
if (player.isCreative()) {
64+
if (MinecraftAdapter.get().isPlayerCreativeMode()) {
6765
return;
6866
}
6967

src/main/java/stevebot/minecraft/MinecraftAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public static MinecraftAdapter get() {
5959
*/
6060
public abstract InventoryPlayer getPlayerInventory();
6161

62+
63+
/**
64+
* @return true, if the player is in creative mode
65+
*/
66+
public abstract boolean isPlayerCreativeMode();
67+
6268
/**
6369
* @param pos the position of the block
6470
* @return the block at the given position

src/main/java/stevebot/minecraft/MinecraftAdapterImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public InventoryPlayer getPlayerInventory() {
5151

5252

5353

54+
@Override
55+
public boolean isPlayerCreativeMode() {
56+
return getPlayer().isCreative();
57+
}
58+
59+
60+
61+
5462
@Override
5563
public Block getBlock(BlockPos pos) {
5664
return getWorld().getBlockState(pos).getBlock();

src/main/java/stevebot/minecraft/UnsupportedMinecraftAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public InventoryPlayer getPlayerInventory() {
3939

4040

4141

42+
@Override
43+
public boolean isPlayerCreativeMode() {
44+
throw new UnsupportedOperationException();
45+
}
46+
47+
48+
49+
4250
@Override
4351
public Block getBlock(BlockPos pos) {
4452
throw new UnsupportedOperationException();

src/test/java/stevebot/InventorySnapshotTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import stevebot.data.items.wrapper.ItemWrapper;
1010
import stevebot.data.modification.Modification;
1111
import stevebot.data.player.PlayerSnapshot;
12+
import stevebot.minecraft.MinecraftAdapter;
13+
import stevebot.minecraft.UnsupportedMinecraftAdapter;
1214

1315
import static org.assertj.core.api.Assertions.assertThat;
1416

@@ -44,6 +46,13 @@ public class InventorySnapshotTest {
4446
@Test
4547
void testApplyModificationPlace() {
4648

49+
MinecraftAdapter.initialize(new UnsupportedMinecraftAdapter() {
50+
@Override
51+
public boolean isPlayerCreativeMode() {
52+
return false;
53+
}
54+
});
55+
4756
final PlayerSnapshot snapshot = new PlayerSnapshot();
4857
snapshot.setHotbarItemStack(3, ITEM_DIRT, 2);
4958
snapshot.setHotbarItemStack(6, ITEM_DIA_PICKAXE, 1);
@@ -67,6 +76,35 @@ void testApplyModificationPlace() {
6776

6877

6978

79+
@Test
80+
void testApplyModificationPlaceCreative() {
81+
82+
MinecraftAdapter.initialize(new UnsupportedMinecraftAdapter() {
83+
@Override
84+
public boolean isPlayerCreativeMode() {
85+
return true;
86+
}
87+
});
88+
89+
final PlayerSnapshot snapshot = new PlayerSnapshot();
90+
snapshot.setHotbarItemStack(3, ITEM_DIRT, 2);
91+
snapshot.setHotbarItemStack(6, ITEM_DIA_PICKAXE, 1);
92+
snapshot.setHotbarItemStack(7, ITEM_STONE, 10);
93+
94+
snapshot.applyModification(Modification.placeBlock(new BaseBlockPos(), BLOCK_STONE));
95+
snapshot.applyModification(Modification.placeBlock(new BaseBlockPos(), BLOCK_DIRT));
96+
97+
assertThat(snapshot.getStackSize(3)).isEqualTo(2);
98+
assertThat(snapshot.getItem(3).getId()).isEqualTo(ITEM_DIRT.getId());
99+
100+
assertThat(snapshot.getStackSize(7)).isEqualTo(10);
101+
assertThat(snapshot.getItem(7).getId()).isEqualTo(ITEM_STONE.getId());
102+
103+
}
104+
105+
106+
107+
70108
@Test
71109
void testFindThrowawayBlock() {
72110

0 commit comments

Comments
 (0)