-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac0b552
commit 63e55a2
Showing
19 changed files
with
352 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/generated/resources/assets/biomancy/blockstates/modular_larynx.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"variants": { | ||
"": { | ||
"model": "biomancy:block/modular_larynx" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/generated/resources/assets/biomancy/models/item/modular_larynx.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"parent": "biomancy:block/modular_larynx" | ||
} |
21 changes: 21 additions & 0 deletions
21
src/generated/resources/data/biomancy/loot_tables/blocks/modular_larynx.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"type": "minecraft:block", | ||
"pools": [ | ||
{ | ||
"bonus_rolls": 0.0, | ||
"conditions": [ | ||
{ | ||
"condition": "minecraft:survives_explosion" | ||
} | ||
], | ||
"entries": [ | ||
{ | ||
"type": "minecraft:item", | ||
"name": "biomancy:modular_larynx" | ||
} | ||
], | ||
"rolls": 1.0 | ||
} | ||
], | ||
"random_sequence": "biomancy:blocks/modular_larynx" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...ain/java/com/github/elenterius/biomancy/block/modularlarynx/ModularLarynxBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.github.elenterius.biomancy.block.modularlarynx; | ||
|
||
import com.github.elenterius.biomancy.init.ModBlockEntities; | ||
import com.github.elenterius.biomancy.init.ModCapabilities; | ||
import com.github.elenterius.biomancy.inventory.itemhandler.SingleItemStackHandler; | ||
import com.github.elenterius.biomancy.item.EssenceItem; | ||
import com.github.elenterius.biomancy.util.MobSoundUtil; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.sounds.SoundEvent; | ||
import net.minecraft.sounds.SoundEvents; | ||
import net.minecraft.sounds.SoundSource; | ||
import net.minecraft.world.Containers; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraftforge.common.capabilities.Capability; | ||
import net.minecraftforge.common.util.LazyOptional; | ||
import net.minecraftforge.items.IItemHandler; | ||
import net.minecraftforge.registries.ForgeRegistries; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
public class ModularLarynxBlockEntity extends BlockEntity { | ||
|
||
public static final String INVENTORY_TAG = "inventory"; | ||
public static final String SOUND_EVENT_TAG = "sound_event"; | ||
|
||
public static final Predicate<ItemStack> VALID_ITEM = stack -> stack.getItem() instanceof EssenceItem; | ||
|
||
private final SingleItemStackHandler inventory; | ||
private LazyOptional<IItemHandler> optionalItemHandler; | ||
|
||
private SoundEvent soundEvent = SoundEvents.PLAYER_BREATH; | ||
|
||
public ModularLarynxBlockEntity(BlockPos pos, BlockState state) { | ||
super(ModBlockEntities.MODULAR_LARYNX.get(), pos, state); | ||
inventory = new SingleItemStackHandler() { | ||
@Override | ||
public int getSlotLimit(int slot) { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public boolean isItemValid(@NotNull ItemStack stack) { | ||
return VALID_ITEM.test(stack); | ||
} | ||
|
||
@Override | ||
protected void onContentsChanged() { | ||
updateSounds(); | ||
setChanged(); | ||
} | ||
}; | ||
optionalItemHandler = LazyOptional.of(() -> inventory); | ||
} | ||
|
||
public boolean isInventoryEmpty() { | ||
return inventory.isEmpty(); | ||
} | ||
|
||
public ItemStack insertItemStack(ItemStack stack) { | ||
return inventory.insertItem(stack, false); | ||
} | ||
|
||
protected void updateSounds() { | ||
ItemStack stack = inventory.getStack(); | ||
|
||
if (stack.getItem() instanceof EssenceItem essenceItem) { | ||
soundEvent = essenceItem | ||
.getEntityType(stack) | ||
.map(entityType -> MobSoundUtil.findSoundFor(entityType, MobSoundUtil.SoundType.AMBIENT)) | ||
.orElse(SoundEvents.PLAYER_BREATH); | ||
} | ||
else { | ||
soundEvent = SoundEvents.PLAYER_BREATH; | ||
} | ||
} | ||
|
||
public boolean playSound(float volume, float pitch) { | ||
if (level == null || level.isClientSide) return false; | ||
|
||
BlockPos pos = getBlockPos(); | ||
double x = pos.getX() + 0.5d; | ||
double y = pos.getY() + 0.5d; | ||
double z = pos.getZ() + 0.5d; | ||
level.playSound(null, x, y, z, soundEvent, SoundSource.RECORDS, volume, pitch); | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
protected void saveAdditional(CompoundTag tag) { | ||
super.saveAdditional(tag); | ||
tag.put(INVENTORY_TAG, inventory.serializeNBT()); | ||
tag.putString(SOUND_EVENT_TAG, soundEvent.getLocation().toString()); | ||
} | ||
|
||
@Override | ||
public void load(CompoundTag tag) { | ||
super.load(tag); | ||
inventory.deserializeNBT(tag.getCompound(INVENTORY_TAG)); | ||
soundEvent = deserializeSoundEvent(tag.getString(SOUND_EVENT_TAG)).orElse(SoundEvents.PLAYER_BREATH); | ||
} | ||
|
||
public Optional<SoundEvent> deserializeSoundEvent(String stringKey) { | ||
ResourceLocation key = ResourceLocation.tryParse(stringKey); | ||
if (key != null) { | ||
return Optional.ofNullable(ForgeRegistries.SOUND_EVENTS.getValue(key)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public void dropInventoryContents(Level level, BlockPos pos) { | ||
ItemStack stack = inventory.extractItem(inventory.getMaxAmount(), false); | ||
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), stack); | ||
} | ||
|
||
@Override | ||
public void invalidateCaps() { | ||
super.invalidateCaps(); | ||
optionalItemHandler.invalidate(); | ||
} | ||
|
||
@Override | ||
public void reviveCaps() { | ||
super.reviveCaps(); | ||
optionalItemHandler = LazyOptional.of(() -> inventory); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { | ||
if (!remove) { | ||
return ModCapabilities.ITEM_HANDLER.orEmpty(cap, optionalItemHandler); | ||
} | ||
return super.getCapability(cap, side); | ||
} | ||
|
||
} |
Oops, something went wrong.