Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Fix block placing sound and noteblock sound (#1525)
Browse files Browse the repository at this point in the history
Now noteblocks are almost 100% like in singleplayer, except for not recognizing all of the blocks underneath them, therefore not playing the matching instrument sound.
  • Loading branch information
Tee7even authored and Tee7even committed Mar 18, 2017
1 parent 53b20da commit 0751e9b
Show file tree
Hide file tree
Showing 34 changed files with 217 additions and 184 deletions.
9 changes: 5 additions & 4 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cn.nukkit;

import cn.nukkit.block.Block;
import cn.nukkit.block.BlockAir;
import cn.nukkit.block.BlockDoor;
import cn.nukkit.block.BlockEnderChest;
import cn.nukkit.block.*;
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.blockentity.BlockEntityItemFrame;
import cn.nukkit.blockentity.BlockEntitySign;
Expand Down Expand Up @@ -2447,6 +2444,10 @@ public void handleDataPacket(DataPacket packet) {
this.inventory.sendHeldItem(this);
break;
}
if (target.getId() == Block.NOTEBLOCK) {
((BlockNoteblock)target).emitSound();
break;
}
Block block = target.getSide(((PlayerActionPacket) packet).face);
if (block.getId() == Block.FIRE) {
this.level.setBlock(block, new BlockAir(), true);
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/cn/nukkit/block/BlockNoteblock.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cn.nukkit.item.ItemTool;
import cn.nukkit.level.sound.NoteBoxSound;
import cn.nukkit.math.Vector3;
import cn.nukkit.network.protocol.BlockEventPacket;

/**
* Created by Snake1999 on 2016/1/17.
Expand Down Expand Up @@ -50,12 +51,17 @@ public boolean canBeActivated() {
}

public int getStrength() {
if (this.meta < 24) this.meta++;
else this.meta = 0;
this.getLevel().setBlock(this, this);
return this.meta;
}

public void increaseStrength() {
if (this.meta < 24) {
this.meta++;
} else {
this.meta = 0;
}
}

public int getInstrument() {
Block below = this.getSide(Vector3.SIDE_DOWN);
switch (below.getId()) {
Expand Down Expand Up @@ -85,14 +91,27 @@ public int getInstrument() {
}
}

public void emitSound() {
BlockEventPacket pk = new BlockEventPacket();
pk.x = (int) this.x;
pk.y = (int) this.y;
pk.z = (int) this.z;
pk.case1 = this.getInstrument();
pk.case2 = this.getStrength();
this.getLevel().addChunkPacket((int) this.x >> 4, (int) this.z >> 4, pk);

this.getLevel().addSound(new NoteBoxSound(this, this.getInstrument(), this.getStrength()));
}

public boolean onActivate(Item item) {
return this.onActivate(item, null);
}

public boolean onActivate(Item item, Player player) {
Block up = this.getSide(Vector3.SIDE_UP);
if (up.getId() == Block.AIR) {
this.getLevel().addSound(new NoteBoxSound(this, this.getInstrument(), this.getStrength()));
this.increaseStrength();
this.emitSound();
return true;
} else {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ public Item useItemOn(Vector3 vector, Item item, int face, float fx, float fy, f
}

if (player != null) {
BlockPlaceSound sound = new BlockPlaceSound(block.add(0.5, 0.5, 0.5));
BlockPlaceSound sound = new BlockPlaceSound(block.add(0.5, 0.5, 0.5), item.getId());
Map<Integer, Player> players = getChunkPlayers((int) block.x >> 4, (int) block.z >> 4);
addSound(sound, players.values());

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/AnvilBreakSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class AnvilBreakSound extends GenericSound {
public class AnvilBreakSound extends LevelEventSound {
public AnvilBreakSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/AnvilFallSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class AnvilFallSound extends GenericSound {
public class AnvilFallSound extends LevelEventSound {
public AnvilFallSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/AnvilUseSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class AnvilUseSound extends GenericSound {
public class AnvilUseSound extends LevelEventSound {
public AnvilUseSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/BlazeShootSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class BlazeShootSound extends GenericSound {
public class BlazeShootSound extends LevelEventSound {
public BlazeShootSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/cn/nukkit/level/sound/BlockPlaceSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import cn.nukkit.math.Vector3;
import cn.nukkit.network.protocol.LevelEventPacket;
import cn.nukkit.network.protocol.LevelSoundEventPacket;

/**
* Created by CreeperFace on 13. 11. 2016.
* @author CreeperFace
*/
public class BlockPlaceSound extends GenericSound {

public BlockPlaceSound(Vector3 pos) {
this(pos, 0);
}

public BlockPlaceSound(Vector3 pos, float pitch) {
super(pos, LevelEventPacket.EVENT_SOUND_BLOCK_PLACE, pitch);
public class BlockPlaceSound extends LevelSoundEventSound {
public BlockPlaceSound(Vector3 pos, int blockId) {
super(pos, LevelSoundEventPacket.SOUND_PLACE, blockId, 1);
}
}
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/ButtonClickSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* author: MagicDroidX
* Nukkit Project
*/
public class ButtonClickSound extends GenericSound {
public class ButtonClickSound extends LevelEventSound {
public ButtonClickSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/ClickFailSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Created by Pub4Game on 28.06.2016.
*/
public class ClickFailSound extends GenericSound {
public class ClickFailSound extends LevelEventSound {
public ClickFailSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/ClickSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class ClickSound extends GenericSound {
public class ClickSound extends LevelEventSound {
public ClickSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/DoorBumpSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class DoorBumpSound extends GenericSound {
public class DoorBumpSound extends LevelEventSound {
public DoorBumpSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/DoorCrashSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class DoorCrashSound extends GenericSound {
public class DoorCrashSound extends LevelEventSound {
public DoorCrashSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/DoorSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class DoorSound extends GenericSound {
public class DoorSound extends LevelEventSound {
public DoorSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class EndermanTeleportSound extends GenericSound {
public class EndermanTeleportSound extends LevelEventSound {
public EndermanTeleportSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Created by Pub4Game on 28.06.2016.
*/
public class ExperienceOrbSound extends GenericSound {
public class ExperienceOrbSound extends LevelEventSound {
public ExperienceOrbSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/ExplodeSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.nukkit.math.Vector3;
import cn.nukkit.network.protocol.LevelEventPacket;

public class ExplodeSound extends GenericSound {
public class ExplodeSound extends LevelEventSound {

public ExplodeSound(Vector3 pos) {
this(pos, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/FizzSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class FizzSound extends GenericSound {
public class FizzSound extends LevelEventSound {
public FizzSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/GhastShootSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class GhastShootSound extends GenericSound {
public class GhastShootSound extends LevelEventSound {
public GhastShootSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/GhastSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class GhastSound extends GenericSound {
public class GhastSound extends LevelEventSound {
public GhastSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Created by Pub4Game on 03.07.2016.
*/
public class ItemFrameItemAddedSound extends GenericSound {
public class ItemFrameItemAddedSound extends LevelEventSound {
public ItemFrameItemAddedSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Created by Pub4Game on 03.07.2016.
*/
public class ItemFrameItemRemovedSound extends GenericSound {
public class ItemFrameItemRemovedSound extends LevelEventSound {
public ItemFrameItemRemovedSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Created by Pub4Game on 03.07.2016.
*/
public class ItemFrameItemRotated extends GenericSound {
public class ItemFrameItemRotated extends LevelEventSound {
public ItemFrameItemRotated(Vector3 pos) {
this(pos, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Created by Pub4Game on 03.07.2016.
*/
public class ItemFramePlacedSound extends GenericSound {
public class ItemFramePlacedSound extends LevelEventSound {
public ItemFramePlacedSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Created by Pub4Game on 03.07.2016.
*/
public class ItemFrameRemovedSound extends GenericSound {
public class ItemFrameRemovedSound extends LevelEventSound {
public ItemFrameRemovedSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/LaunchSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class LaunchSound extends GenericSound {
public class LaunchSound extends LevelEventSound {
public LaunchSound(Vector3 pos) {
this(pos, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.sound in project Nukkit .
*/
public class GenericSound extends Sound {
public GenericSound(Vector3 pos, int id) {
public class LevelEventSound extends Sound {
protected final int id;
protected float pitch = 0f;

public LevelEventSound(Vector3 pos, int id) {
this(pos, id, 0);
}

public GenericSound(Vector3 pos, int id, float pitch) {
public LevelEventSound(Vector3 pos, int id, float pitch) {
super(pos.x, pos.y, pos.z);
this.id = id;
this.pitch = pitch * 1000f;
}

protected float pitch = 0f;
protected final int id;

public float getPitch() {
return this.pitch / 1000f;
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/cn/nukkit/level/sound/LevelSoundEventSound.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.nukkit.level.sound;

import cn.nukkit.math.Vector3;
import cn.nukkit.network.protocol.DataPacket;
import cn.nukkit.network.protocol.LevelSoundEventPacket;

/**
* @author Tee7even
*/
public class LevelSoundEventSound extends Sound {
protected final byte type;
protected int extraData;
protected int pitch;

public LevelSoundEventSound(Vector3 pos, byte type) {
this(pos, type, -1, -1);
}

public LevelSoundEventSound(Vector3 pos, byte type, int extraData) {
this(pos, type, extraData, -1);
}

public LevelSoundEventSound(Vector3 pos, byte type, int extraData, int pitch) {
super(pos.x, pos.y, pos.z);
this.type = type;
this.extraData = extraData;
this.pitch = pitch;
}

@Override
public DataPacket[] encode() {
LevelSoundEventPacket pk = new LevelSoundEventPacket();
pk.type = this.type;
pk.x = (float) this.x;
pk.y = (float) this.y;
pk.z = (float) this.z;
pk.extraData = this.extraData;
pk.pitch = this.pitch;

return new DataPacket[]{pk};
}
}
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/level/sound/LeverSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* author: MagicDroidX
* Nukkit Project
*/
public class LeverSound extends GenericSound {
public class LeverSound extends LevelEventSound {

public LeverSound(Vector3 pos, boolean isPowerOn) {
super(pos, LevelEventPacket.EVENT_SOUND_BUTTON_CLICK, isPowerOn ? 0.7f : 0.5f);
Expand Down
Loading

1 comment on commit 0751e9b

@Shock95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.