Skip to content

Commit 773e24d

Browse files
Merge pull request CloudburstMC#1080 from PowerNukkit/v1.4/item_frame/improvement
Frame getFacing returning null and allows placing on c…
2 parents e1a2217 + 2649bbc commit 773e24d

File tree

7 files changed

+143
-26
lines changed

7 files changed

+143
-26
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*.xml text eol=lf
66
*.yml text eol=lf
77
*.ini text=auto
8+
*.csv text=auto
89
*.txt text=auto
910
*.bat text eol=crlf
1011
*.cmd text eol=crlf

runtime_block_states.dat.dump.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14683,39 +14683,51 @@ minecraft:flowing_water;liquid_depth=15
1468314683

1468414684
minecraft:frame;facing_direction=0;item_frame_map_bit=0
1468514685
[]
14686+
[]
1468614687

1468714688
minecraft:frame;facing_direction=0;item_frame_map_bit=1
1468814689
[]
14690+
[]
1468914691

1469014692
minecraft:frame;facing_direction=1;item_frame_map_bit=0
1469114693
[]
14694+
[]
1469214695

1469314696
minecraft:frame;facing_direction=1;item_frame_map_bit=1
1469414697
[]
14698+
[]
1469514699

1469614700
minecraft:frame;facing_direction=2;item_frame_map_bit=0
1469714701
[199:3]
14702+
[]
1469814703

1469914704
minecraft:frame;facing_direction=2;item_frame_map_bit=1
1470014705
[199:7]
14706+
[]
1470114707

1470214708
minecraft:frame;facing_direction=3;item_frame_map_bit=0
1470314709
[199:2]
14710+
[]
1470414711

1470514712
minecraft:frame;facing_direction=3;item_frame_map_bit=1
1470614713
[199:6]
14714+
[]
1470714715

1470814716
minecraft:frame;facing_direction=4;item_frame_map_bit=0
1470914717
[199:1]
14718+
[]
1471014719

1471114720
minecraft:frame;facing_direction=4;item_frame_map_bit=1
1471214721
[199:5]
14722+
[]
1471314723

1471414724
minecraft:frame;facing_direction=5;item_frame_map_bit=0
1471514725
[199:0]
14726+
[]
1471614727

1471714728
minecraft:frame;facing_direction=5;item_frame_map_bit=1
1471814729
[199:4]
14730+
[]
1471914731

1472014732
minecraft:frosted_ice;age=0
1472114733
[207:0]

src/main/java/cn/nukkit/block/BlockItemFrame.java

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import cn.nukkit.api.Since;
77
import cn.nukkit.blockentity.BlockEntity;
88
import cn.nukkit.blockentity.BlockEntityItemFrame;
9+
import cn.nukkit.blockproperty.BlockProperties;
10+
import cn.nukkit.blockproperty.BooleanBlockProperty;
911
import cn.nukkit.event.player.PlayerInteractEvent.Action;
1012
import cn.nukkit.item.Item;
13+
import cn.nukkit.item.ItemID;
1114
import cn.nukkit.item.ItemItemFrame;
1215
import cn.nukkit.level.Level;
1316
import cn.nukkit.level.Sound;
@@ -17,23 +20,28 @@
1720
import cn.nukkit.nbt.tag.CompoundTag;
1821
import cn.nukkit.nbt.tag.Tag;
1922
import cn.nukkit.network.protocol.LevelEventPacket;
23+
import cn.nukkit.utils.Faceable;
2024

2125
import javax.annotation.Nonnull;
2226
import javax.annotation.Nullable;
2327
import java.util.concurrent.ThreadLocalRandom;
2428

29+
import static cn.nukkit.blockproperty.CommonBlockProperties.FACING_DIRECTION;
2530
import static cn.nukkit.math.BlockFace.AxisDirection.POSITIVE;
2631

2732
/**
2833
* @author Pub4Game
2934
* @since 03.07.2016
3035
*/
31-
@PowerNukkitDifference(since = "1.4.0.0-PN", info = "Implements BlockEntityHolder only in PowerNukkit")
32-
public class BlockItemFrame extends BlockTransparentMeta implements BlockEntityHolder<BlockEntityItemFrame> {
33-
private final static int[] FACING = new int[]{4, 5, 3, 2, 1, 0}; // TODO when 1.13 support arrives, add UP/DOWN facings
36+
@PowerNukkitDifference(since = "1.4.0.0-PN", info = "Implements BlockEntityHolder and Faceable only in PowerNukkit")
37+
public class BlockItemFrame extends BlockTransparentMeta implements BlockEntityHolder<BlockEntityItemFrame>, Faceable {
38+
@PowerNukkitOnly
39+
@Since("1.4.0.0-PN")
40+
public static final BooleanBlockProperty HAS_MAP = new BooleanBlockProperty("item_frame_map_bit", false);
3441

35-
private final static int FACING_BITMASK = 0b0111;
36-
private final static int MAP_BIT = 0b1000;
42+
@PowerNukkitOnly
43+
@Since("1.4.0.0-PN")
44+
public static final BlockProperties PROPERTIES = new BlockProperties(FACING_DIRECTION, HAS_MAP);
3745

3846
public BlockItemFrame() {
3947
this(0);
@@ -48,6 +56,41 @@ public int getId() {
4856
return ITEM_FRAME_BLOCK;
4957
}
5058

59+
@Since("1.4.0.0-PN")
60+
@PowerNukkitOnly
61+
@Nonnull
62+
@Override
63+
public BlockProperties getProperties() {
64+
return PROPERTIES;
65+
}
66+
67+
@Since("1.4.0.0-PN")
68+
@PowerNukkitOnly
69+
@Nonnull
70+
@Override
71+
public BlockFace getBlockFace() {
72+
return getPropertyValue(FACING_DIRECTION);
73+
}
74+
75+
@Since("1.4.0.0-PN")
76+
@PowerNukkitOnly
77+
@Override
78+
public void setBlockFace(@Nonnull BlockFace face) {
79+
setPropertyValue(FACING_DIRECTION, face);
80+
}
81+
82+
@PowerNukkitOnly
83+
@Since("1.4.0.0-PN")
84+
public boolean isStoringMap() {
85+
return getBooleanValue(HAS_MAP);
86+
}
87+
88+
@PowerNukkitOnly
89+
@Since("1.4.0.0-PN")
90+
public void setStoringMap(boolean map) {
91+
setBooleanValue(HAS_MAP, map);
92+
}
93+
5194
@PowerNukkitOnly
5295
@Since("1.4.0.0-PN")
5396
@Nonnull
@@ -73,7 +116,7 @@ public String getName() {
73116
@Override
74117
public int onUpdate(int type) {
75118
if (type == Level.BLOCK_UPDATE_NORMAL) {
76-
Block support = this.getSideAtLayer(0, getFacing());
119+
Block support = this.getSideAtLayer(0, getFacing().getOpposite());
77120
if (!support.isSolid() && support.getId() != COBBLE_WALL) {
78121
this.level.useBreakOn(this);
79122
return type;
@@ -115,9 +158,17 @@ public boolean onActivate(@Nonnull Item item, Player player) {
115158
}
116159
itemOnFrame.setCount(1);
117160
itemFrame.setItem(itemOnFrame);
161+
if (itemOnFrame.getId() == ItemID.MAP) {
162+
setStoringMap(true);
163+
this.getLevel().setBlock(this, this, true);
164+
}
118165
this.getLevel().addLevelEvent(this, LevelEventPacket.EVENT_SOUND_ITEM_FRAME_ITEM_ADDED);
119166
} else {
120167
itemFrame.setItemRotation((itemFrame.getItemRotation() + 1) % 8);
168+
if (isStoringMap()) {
169+
setStoringMap(false);
170+
this.getLevel().setBlock(this, this, true);
171+
}
121172
this.getLevel().addLevelEvent(this, LevelEventPacket.EVENT_SOUND_ITEM_FRAME_ITEM_ROTATED);
122173
}
123174
return true;
@@ -126,12 +177,12 @@ public boolean onActivate(@Nonnull Item item, Player player) {
126177
@PowerNukkitDifference(info = "Allow to place on walls", since = "1.3.0.0-PN")
127178
@Override
128179
public boolean place(@Nonnull Item item, @Nonnull Block block, @Nonnull Block target, @Nonnull BlockFace face, double fx, double fy, double fz, @Nullable Player player) {
129-
if (face.getHorizontalIndex() == -1
130-
|| (target.getId() != COBBLE_WALL && (!target.isSolid() || (block.isSolid() && !block.canBeReplaced())))) {
180+
if (target.getId() != COBBLE_WALL && (!target.isSolid() || (block.isSolid() && !block.canBeReplaced()))) {
131181
return false;
132182
}
133183

134-
this.setDamage(FACING[face.getIndex()]);
184+
setBlockFace(face);
185+
setStoringMap(item.getId() == ItemID.MAP);
135186
CompoundTag nbt = new CompoundTag()
136187
.putByte("ItemRotation", 0)
137188
.putFloat("ItemDropChance", 1.0f);
@@ -197,18 +248,7 @@ public int getComparatorInputOverride() {
197248
}
198249

199250
public BlockFace getFacing() {
200-
switch (this.getDamage() & FACING_BITMASK) {
201-
case 0:
202-
return BlockFace.WEST;
203-
case 1:
204-
return BlockFace.EAST;
205-
case 2:
206-
return BlockFace.NORTH;
207-
case 3:
208-
return BlockFace.SOUTH;
209-
}
210-
211-
return null;
251+
return getBlockFace();
212252
}
213253

214254
@Override

src/main/java/cn/nukkit/level/format/updater/ChunkUpdater.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ public class ChunkUpdater {
2727
* <dt>9</dt><dd>Re-render cobblestone walls to connect to glass, stained glass, and other wall types like border and blackstone wall</dd>
2828
* <dt>10</dt><dd>Re-render snow layers to make them cover grass blocks and fix leaves2 issue: https://github.com/PowerNukkit/PowerNukkit/issues/482</dd>
2929
* <dt>11</dt><dd>The debug block property was removed from stripped_warped_hyphae, stripped_warped_stem, stripped_crimson_hyphae, and stripped_crimson_stem</dd>
30+
* <dt>12</dt><dd>Upgraded the block frame data values to match the vanilla data, allowing to place up and down and have map</dd>
3031
* </dl>
3132
*/
3233
@PowerNukkitOnly
3334
@Since("1.4.0.0-PN")
3435
@SuppressWarnings("java:S3400")
3536
public int getCurrentContentVersion() {
36-
return 11;
37+
return 12;
3738
}
3839

3940
@PowerNukkitOnly
@@ -61,13 +62,22 @@ public void backwardCompatibilityUpdate(Level level, BaseChunk chunk) {
6162
if (section.getContentVersion() == 10) {
6263
updated = upgradeStrippedStemsFromV10toV11(chunk, updated, section);
6364
}
65+
if (section.getContentVersion() == 11) {
66+
updated = upgradeFrameFromV11toV12(chunk, section, updated);
67+
}
6468
}
6569

6670
if (updated) {
6771
chunk.setChanged();
6872
}
6973
}
7074

75+
private static boolean upgradeFrameFromV11toV12(BaseChunk chunk, ChunkSection section, boolean updated) {
76+
updated = walk(chunk, section, new FrameUpdater(section)) || updated;
77+
section.setContentVersion(12);
78+
return updated;
79+
}
80+
7181
private boolean upgradeStrippedStemsFromV10toV11(BaseChunk chunk, boolean updated, ChunkSection section) {
7282
updated = walk(chunk, section, new StemStrippedUpdater(section)) || updated;
7383
section.setContentVersion(11);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cn.nukkit.level.format.updater;
2+
3+
import cn.nukkit.api.PowerNukkitOnly;
4+
import cn.nukkit.api.Since;
5+
import cn.nukkit.block.BlockID;
6+
import cn.nukkit.blockstate.BlockState;
7+
import cn.nukkit.level.format.ChunkSection;
8+
import lombok.RequiredArgsConstructor;
9+
10+
/**
11+
* @author joserobjr
12+
* @since 2021-05-25
13+
*/
14+
@PowerNukkitOnly
15+
@Since("1.4.0.0-PN")
16+
@RequiredArgsConstructor
17+
class FrameUpdater implements Updater {
18+
private final ChunkSection section;
19+
20+
@Override
21+
public boolean update(int offsetX, int offsetY, int offsetZ, int x, int y, int z, BlockState state) {
22+
if (state.getBlockId() != BlockID.ITEM_FRAME_BLOCK) {
23+
return false;
24+
}
25+
26+
return section.setBlockStateAtLayer(x, y, z, 0, state.withData(getNewData(state.getExactIntStorage())));
27+
}
28+
29+
private int getNewData(int fromData) {
30+
switch (fromData) {
31+
case 0: // [199:0]
32+
return 5; //minecraft:frame;facing_direction=5;item_frame_map_bit=0
33+
case 1: // [199:1]
34+
return 4; //minecraft:frame;facing_direction=4;item_frame_map_bit=0
35+
case 2: // [199:2]
36+
return 3; //minecraft:frame;facing_direction=3;item_frame_map_bit=0
37+
case 3: // [199:3]
38+
return 2; //minecraft:frame;facing_direction=2;item_frame_map_bit=0
39+
case 4: // [199:4]
40+
return 8 + 5; //minecraft:frame;facing_direction=5;item_frame_map_bit=1
41+
case 5: // [199:5]
42+
return 8 + 4; //minecraft:frame;facing_direction=4;item_frame_map_bit=1
43+
case 6: // [199:6]
44+
return 8 + 3; //minecraft:frame;facing_direction=3;item_frame_map_bit=1
45+
case 7: // [199:7]
46+
return 8 + 2; //minecraft:frame;facing_direction=2;item_frame_map_bit=1
47+
default:
48+
return fromData;
49+
}
50+
}
51+
}
Binary file not shown.

src/test/java/org/powernukkit/tools/OverridesUpdater.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
2929
import lombok.Data;
3030
import lombok.NonNull;
31+
import org.powernukkit.HumanStringComparator;
3132

3233
import java.io.*;
3334
import java.nio.ByteOrder;
3435
import java.util.LinkedHashMap;
3536
import java.util.Map;
37+
import java.util.SortedMap;
38+
import java.util.TreeMap;
3639

3740
public class OverridesUpdater {
3841
public static void main(String[] args) throws IOException {
@@ -135,23 +138,23 @@ public static void main(String[] args) throws IOException {
135138
newOverrides.add(override);
136139
}
137140

138-
/*SortedMap<String, CompoundTag> sorted = new TreeMap<>(new HumanStringComparator());
141+
SortedMap<String, CompoundTag> sorted = new TreeMap<>(new HumanStringComparator());
139142
for (CompoundTag tag : originalTags.values()) {
140143
sorted.put(new BlockInfo(tag.getCompound("block"), tag, new ListTag<>(), new ListTag<>()).getStateName(), tag);
141144
}
142145

143146
for (CompoundTag tag : sorted.values()) {
144147
String name = tag.getCompound("block").getString("name");
145148

146-
if (!name.startsWith("minecraft:beehive") || !name.startsWith("minecraft:bee_nest")) {
149+
if (!name.startsWith("minecraft:frame")) {
147150
continue;
148151
}
149152

150153
CompoundTag override = new CompoundTag();
151154
override.putCompound("block", tag.getCompound("block").remove("version"));
152-
override.putList(new ListTag<>("LegacyStates")*//*.add(new CompoundTag().putInt("id", blockId).putInt("val", 0))*//*);
155+
override.putList(new ListTag<>("LegacyStates")/*.add(new CompoundTag().putInt("id", blockId).putInt("val", 0))*/);
153156
newOverrides.add(override);
154-
}*/
157+
}
155158

156159
byte[] bytes = NBTIO.write(new CompoundTag().putList(newOverrides));
157160
try(FileOutputStream fos = new FileOutputStream("runtime_block_states_overrides.dat")) {

0 commit comments

Comments
 (0)