Skip to content

Commit

Permalink
Implement Jukebox circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed May 2, 2024
1 parent bf5a252 commit 687b0bd
Show file tree
Hide file tree
Showing 31 changed files with 428 additions and 14 deletions.
51 changes: 51 additions & 0 deletions src/main/java/cn/nukkit/block/BlockJukebox.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.utils.BlockColor;

import javax.annotation.Nullable;

import static cn.nukkit.GameVersion.*;

/**
Expand Down Expand Up @@ -60,6 +62,11 @@ public boolean onActivate(Item item, BlockFace face, Player player) {
} else if (item instanceof ItemRecord) {
jukebox.setRecordItem((ItemRecord) item);
jukebox.play();

if (level.isRedstoneEnabled()) {
level.updateAroundRedstone(this, null);
}

player.getInventory().decreaseCount(player.getInventory().getHeldItemIndex());
}

Expand Down Expand Up @@ -114,4 +121,48 @@ public float getHardness() {
public float getResistance() {
return 30;
}

@Override
public boolean isPowerSource() {
return true;
}

@Override
public int getWeakPower(BlockFace side) {
BlockEntityJukebox blockEntity = getBlockEntity();
if (blockEntity == null) {
return 0;
}
return blockEntity.isRecordPlaying() ? 15 : 0;
}

@Override
public boolean hasComparatorInputOverride() {
return true;
}

@Override
public int getComparatorInputOverride() {
BlockEntityJukebox blockEntity = getBlockEntity();
if (blockEntity == null) {
return 0;
}
ItemRecord record = blockEntity.getRecordItem();
if (record == null) {
return 0;
}
return record.getComparatorSignal();
}

@Nullable
protected BlockEntityJukebox getBlockEntity() {
if (level == null) {
return null;
}
BlockEntity blockEntity = level.getBlockEntity(this);
if (blockEntity instanceof BlockEntityJukebox jukebox) {
return jukebox;
}
return null;
}
}
22 changes: 18 additions & 4 deletions src/main/java/cn/nukkit/blockentity/BlockEntityJukebox.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ protected void initBlockEntity() {
this.ticksPlaying = 0;
}

// this.scheduleUpdate();
if (!finishedRecording) {
this.scheduleUpdate();
}
} else {
this.recordItem = null;
this.finishedRecording = true;
Expand Down Expand Up @@ -80,7 +82,7 @@ public void setRecordItem(@Nullable ItemRecord recordItem) {
}

@Nullable
public Item getRecordItem() {
public ItemRecord getRecordItem() {
return recordItem;
}

Expand All @@ -98,13 +100,17 @@ public void play() {
player.sendJukeboxPopup(new TranslationContainer("record.nowPlaying", "%item." + recordItem.getTranslationIdentifier() + ".desc"));
}

// this.scheduleUpdate();
this.scheduleUpdate();
}

public void stop() {
this.finishedRecording = true;
this.ticksPlaying = 0;
this.getLevel().addLevelSoundEvent(this.blockCenter(), LevelSoundEventPacket.SOUND_STOP_RECORD);

if (level.isRedstoneEnabled()) {
level.updateAroundRedstone(this, null);
}
}

public void dropItem() {
Expand Down Expand Up @@ -164,7 +170,15 @@ public boolean onUpdate() {
return false;
}

++this.ticksPlaying;
if (recordItem != null && ++this.ticksPlaying > recordItem.getDuration()) {
stop();
return false;
}

return true;
}

public boolean isRecordPlaying() {
return !finishedRecording;
}
}
15 changes: 15 additions & 0 deletions src/main/java/cn/nukkit/entity/item/EntityPainting.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,21 @@ public enum Motive {
PIG_SCENE("Pigscene", 4, 4),
BURNING_SKULL("BurningSkull", 4, 4),
// UNPACKED("Unpacked", 4, 4),
// BOUQUET("Bouquet", 4, 4),
// CAVEBIRD("Cavebird", 4, 4),
// COTAN("Cotan", 4, 4),
// ENDBOSS("Endboss", 4, 4),
// FERN("Fern", 4, 4),
// ORB("Orb", 4, 4),
// PASSAGE("Passage", 4, 4),
// TIDES("Tides", 4, 4),
// SUNFLOWERS("Sunflowers", 4, 4),
// BACKYARD("Backyard", 4, 5),
// POND("Pond", 4, 5),
// CHAINGING("Chainging", 5, 3),
// FINDING("Finding", 5, 3),
// LOWMIST("Lowmist", 5, 3),
// OWLEMONS("Owlemons", 5, 3),
;

public final String title;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/cn/nukkit/item/ItemID.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,25 @@ public interface ItemID extends BlockID, ItemRuntimeID {
@Deprecated
int RECORD_RELIC = 780;

int MUSIC_DISC_CREATOR = 783;
/**
* @deprecated use {@link #MUSIC_DISC_CREATOR} instead
*/
@Deprecated
int RECORD_CREATOR = 783;
int MUSIC_DISC_CREATOR_MUSIC_BOX = 784;
/**
* @deprecated use {@link #MUSIC_DISC_CREATOR_MUSIC_BOX} instead
*/
@Deprecated
int RECORD_CREATOR_MUSIC_BOX = 784;
int MUSIC_DISC_PRECIPICE = 785;
/**
* @deprecated use {@link #MUSIC_DISC_PRECIPICE} instead
*/
@Deprecated
int RECORD_PRECIPICE = 785;

int SOUL_CAMPFIRE = 801;

int GLOW_FRAME = 850;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/item/ItemMace.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public int getMaxDurability() {

@Override
public int getAttackDamage() {
return 8;
return 5;
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/cn/nukkit/item/ItemNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,25 @@ public interface ItemNames extends ItemBlockNames {
@Deprecated
String RECORD_RELIC = "record_relic";

String MUSIC_DISC_CREATOR = "music_disc_creator";
/**
* @deprecated use {@link #MUSIC_DISC_CREATOR} instead
*/
@Deprecated
String RECORD_CREATOR = "record_creator";
String MUSIC_DISC_CREATOR_MUSIC_BOX = "music_disc_creator_music_box";
/**
* @deprecated use {@link #MUSIC_DISC_CREATOR_MUSIC_BOX} instead
*/
@Deprecated
String RECORD_CREATOR_MUSIC_BOX = "record_creator_music_box";
String MUSIC_DISC_PRECIPICE = "music_disc_precipice";
/**
* @deprecated use {@link #MUSIC_DISC_PRECIPICE} instead
*/
@Deprecated
String RECORD_PRECIPICE = "record_precipice";

String SOUL_CAMPFIRE = "soul_campfire";

String GLOW_FRAME = "glow_frame";
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ public int getMaxStackSize() {

public abstract int getSoundEvent();

public abstract int getDuration();

public abstract int getComparatorSignal();

public abstract String getTranslationIdentifier();
}
10 changes: 10 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecord11.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public int getSoundEvent() {
return LevelSoundEventPacket.SOUND_RECORD_11;
}

@Override
public int getDuration() {
return 71 * 20;
}

@Override
public int getComparatorSignal() {
return 11;
}

@Override
public String getTranslationIdentifier() {
return "record_11";
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecord13.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public int getSoundEvent() {
return LevelSoundEventPacket.SOUND_RECORD_13;
}

@Override
public int getDuration() {
return 178 * 20;
}

@Override
public int getComparatorSignal() {
return 1;
}

@Override
public String getTranslationIdentifier() {
return "record_13";
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecord5.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public int getSoundEvent() {
return LevelSoundEventPacket.SOUND_RECORD_5;
}

@Override
public int getDuration() {
return 178 * 20;
}

@Override
public int getComparatorSignal() {
return 15;
}

@Override
public String getTranslationIdentifier() {
return "record_5";
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecordBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public int getSoundEvent() {
return LevelSoundEventPacket.SOUND_RECORD_BLOCKS;
}

@Override
public int getDuration() {
return 345 * 20;
}

@Override
public int getComparatorSignal() {
return 3;
}

@Override
public String getTranslationIdentifier() {
return "record_blocks";
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecordCat.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public int getSoundEvent() {
return LevelSoundEventPacket.SOUND_RECORD_CAT;
}

@Override
public int getDuration() {
return 185 * 20;
}

@Override
public int getComparatorSignal() {
return 2;
}

@Override
public String getTranslationIdentifier() {
return "record_cat";
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecordChirp.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public int getSoundEvent() {
return LevelSoundEventPacket.SOUND_RECORD_CHIRP;
}

@Override
public int getDuration() {
return 185 * 20;
}

@Override
public int getComparatorSignal() {
return 4;
}

@Override
public String getTranslationIdentifier() {
return "record_chirp";
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecordCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cn.nukkit.item;

import cn.nukkit.network.protocol.LevelSoundEventPacket;

public class ItemRecordCreator extends ItemRecord {
public ItemRecordCreator() {
this(0, 1);
}

public ItemRecordCreator(Integer meta) {
this(meta, 1);
}

public ItemRecordCreator(Integer meta, int count) {
super(Item.MUSIC_DISC_CREATOR, meta, count);
}

@Override
public int getSoundEvent() {
return LevelSoundEventPacket.SOUND_RECORD_CREATOR;
}

@Override
public int getDuration() {
return 176 * 20;
}

@Override
public int getComparatorSignal() {
return 12;
}

@Override
public String getTranslationIdentifier() {
return "record_creator";
}
}
37 changes: 37 additions & 0 deletions src/main/java/cn/nukkit/item/ItemRecordCreatorMusicBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cn.nukkit.item;

import cn.nukkit.network.protocol.LevelSoundEventPacket;

public class ItemRecordCreatorMusicBox extends ItemRecord {
public ItemRecordCreatorMusicBox() {
this(0, 1);
}

public ItemRecordCreatorMusicBox(Integer meta) {
this(meta, 1);
}

public ItemRecordCreatorMusicBox(Integer meta, int count) {
super(Item.MUSIC_DISC_CREATOR_MUSIC_BOX, meta, count);
}

@Override
public int getSoundEvent() {
return LevelSoundEventPacket.SOUND_RECORD_CREATOR_MUSIC_BOX;
}

@Override
public int getDuration() {
return 73 * 20;
}

@Override
public int getComparatorSignal() {
return 11;
}

@Override
public String getTranslationIdentifier() {
return "record_creator_music_box";
}
}
Loading

0 comments on commit 687b0bd

Please sign in to comment.