Skip to content

Commit

Permalink
Custom open/close sounds for some loot crate chests
Browse files Browse the repository at this point in the history
Prevent crash if tile entity is removed while playing sound. Partly fixes #22
  • Loading branch information
DaFuqs committed Nov 4, 2021
1 parent 8f3627e commit fb31e52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
47 changes: 36 additions & 11 deletions src/main/java/de/dafuqs/lootcrates/blocks/LootCrateBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public abstract class LootCrateBlockEntity extends LootableContainerBlockEntity {
Expand Down Expand Up @@ -284,13 +285,15 @@ public boolean isTrapped() {

public boolean doesUnlock(Item item) {
if(world != null && item instanceof LootKeyItem) {
Block block = getBlock();
LootCrateRarity itemRarity = LootCrateAtlas.getKeyRarity((LootKeyItem) item);
LootCrateRarity blockRarity = LootCrateBlock.getCrateRarity(block);
return itemRarity.equals(blockRarity);
} else {
return false;
Optional<LootCrateBlock> block = getBlock();
if(block.isPresent()) {
LootCrateRarity itemRarity = LootCrateAtlas.getKeyRarity((LootKeyItem) item);
LootCrateRarity blockRarity = LootCrateBlock.getCrateRarity(block.get());
return itemRarity.equals(blockRarity);
}
}

return false;
}

public boolean doesConsumeKeyOnUnlock() {
Expand All @@ -303,13 +306,25 @@ public void unlock(World world) {
this.playSound(LootCrates.CHEST_UNLOCKS_SOUND_EVENT);
}

public LootCrateBlock getBlock() {
return (LootCrateBlock) this.world.getBlockState(this.pos).getBlock();
public Optional<LootCrateBlock> getBlock() {
if(this.world == null) {
return Optional.empty();
} else {
Block block = this.world.getBlockState(this.pos).getBlock();
if(block instanceof LootCrateBlock lootCrateBlock) {
return Optional.of(lootCrateBlock);
} else {
return Optional.empty();
}
}
}

public ScheduledTickEvent getRandomTickEvent() {
if(this.scheduledTickEvent == null) {
scheduledTickEvent = LootCrateAtlas.getRandomTickEvent(getBlock());
Optional<LootCrateBlock> block = getBlock();
if(block.isPresent()) {
scheduledTickEvent = LootCrateAtlas.getRandomTickEvent(block.get());
}
}
return this.scheduledTickEvent;
}
Expand All @@ -323,7 +338,12 @@ public void playOpenSoundEffect() {
}

// also play custom sound, if set
SoundEvent customSoundEvent = LootCrateAtlas.getCustomOpenSoundEvent(getBlock());
SoundEvent customSoundEvent = null;
Optional<LootCrateBlock> block = getBlock();
if(block.isPresent()) {
customSoundEvent = LootCrateAtlas.getCustomOpenSoundEvent(block.get());
}

if(customSoundEvent != null) {
playSound(customSoundEvent, 0.4F);
}
Expand All @@ -338,7 +358,12 @@ public void playCloseSoundEffect() {
}

// also play custom sound, if set
SoundEvent customSoundEvent = LootCrateAtlas.getCustomCloseSoundEvent(getBlock());
SoundEvent customSoundEvent = null;
Optional<LootCrateBlock> block = getBlock();
if(block.isPresent()) {
customSoundEvent = LootCrateAtlas.getCustomCloseSoundEvent(block.get());
}

if(customSoundEvent != null) {
playSound(customSoundEvent, 0.4F);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public ChestLootCrateBlockEntity(BlockEntityType<?> blockEntityType, BlockPos bl
this.inventory = DefaultedList.ofSize(27, ItemStack.EMPTY);
this.stateManager = new ChestStateManager() {
protected void onChestOpened(World world, BlockPos pos, BlockState state) {
playSound(world, pos, state, SoundEvents.BLOCK_CHEST_OPEN);
playOpenSoundEffect();
}

protected void onChestClosed(World world, BlockPos pos, BlockState state) {
playSound(world, pos, state, SoundEvents.BLOCK_CHEST_CLOSE);
playCloseSoundEffect();
}

protected void onInteracted(World world, BlockPos pos, BlockState state, int oldViewerCount, int newViewerCount) {
Expand Down

0 comments on commit fb31e52

Please sign in to comment.