-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
6 changed files
with
214 additions
and
6 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
fabric/src/main/java/com/lx862/jcm/mod/block/SoundLooperBlock.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,20 @@ | ||
package com.lx862.jcm.mod.block; | ||
|
||
import com.lx862.jcm.mod.block.base.JCMBlock; | ||
import com.lx862.jcm.mod.block.entity.SoundLooperBlockEntity; | ||
import org.mtr.mapping.holder.BlockPos; | ||
import org.mtr.mapping.holder.BlockSettings; | ||
import org.mtr.mapping.holder.BlockState; | ||
import org.mtr.mapping.mapper.BlockEntityExtension; | ||
import org.mtr.mapping.mapper.BlockWithEntity; | ||
|
||
public class SoundLooperBlock extends JCMBlock implements BlockWithEntity { | ||
public SoundLooperBlock(BlockSettings settings) { | ||
super(settings); | ||
} | ||
|
||
@Override | ||
public BlockEntityExtension createBlockEntity(BlockPos blockPos, BlockState blockState) { | ||
return new SoundLooperBlockEntity(blockPos, blockState); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
fabric/src/main/java/com/lx862/jcm/mod/block/entity/SoundLooperBlockEntity.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,118 @@ | ||
package com.lx862.jcm.mod.block.entity; | ||
|
||
import com.lx862.jcm.mod.data.JCMServerStats; | ||
import com.lx862.jcm.mod.registry.BlockEntities; | ||
import org.mtr.mapping.holder.*; | ||
|
||
public class SoundLooperBlockEntity extends JCMBlockEntityBase { | ||
private String soundID = ""; | ||
private BlockPos pos1 = new BlockPos(0, 0, 0); | ||
private BlockPos pos2 = new BlockPos(0, 0, 0); | ||
private int repeatTick = 20; | ||
private float volume = 1; | ||
private int soundCategory = 0; | ||
private boolean needRedstone = false; | ||
private boolean limitRange = false; | ||
private static final SoundCategory[] SOURCE_LIST = {SoundCategory.MASTER, SoundCategory.MUSIC, SoundCategory.WEATHER, SoundCategory.AMBIENT, SoundCategory.PLAYERS, SoundCategory.BLOCKS, SoundCategory.VOICE}; | ||
|
||
public SoundLooperBlockEntity(BlockPos blockPos, BlockState blockState) { | ||
super(BlockEntities.SOUND_LOOPER.get(), blockPos, blockState); | ||
} | ||
|
||
@Override | ||
public void readCompoundTag(CompoundTag compoundTag) { | ||
super.readCompoundTag(compoundTag); | ||
this.repeatTick = compoundTag.getInt("repeat_tick"); | ||
this.soundID = compoundTag.getString("sound_id"); | ||
this.soundCategory = compoundTag.getInt("sound_category"); | ||
this.volume = compoundTag.getFloat("volume"); | ||
this.needRedstone = compoundTag.getBoolean("need_redstone"); | ||
this.limitRange = compoundTag.getBoolean("limit_range"); | ||
this.pos1 = BlockPos.fromLong(compoundTag.getLong("pos_1")); | ||
this.pos2 = BlockPos.fromLong(compoundTag.getLong("pos_2")); | ||
} | ||
|
||
@Override | ||
public void writeCompoundTag(CompoundTag compoundTag) { | ||
super.writeCompoundTag(compoundTag); | ||
compoundTag.putInt("repeat_tick", repeatTick); | ||
compoundTag.putString("sound_id", soundID); | ||
compoundTag.putInt("sound_category", soundCategory); | ||
compoundTag.putFloat("volume", volume); | ||
compoundTag.putBoolean("need_redstone", needRedstone); | ||
compoundTag.putBoolean("limit_range", limitRange); | ||
compoundTag.putLong("pos_1", pos1.asLong()); | ||
compoundTag.putLong("pos_2", pos2.asLong()); | ||
} | ||
|
||
@Override | ||
public void blockEntityTick() { | ||
if(repeatTick > 0 && !soundID.isEmpty() && world != null && !world.isClient() && JCMServerStats.getGameTick() % repeatTick == 0) { | ||
if(needRedstone && !world.isReceivingRedstonePower(pos)) return; | ||
|
||
if(!limitRange) { | ||
world.getPlayers().forEach(player -> { | ||
Identifier identifier = null; | ||
try { | ||
identifier = new Identifier(soundID); | ||
} catch (Exception ignored) { | ||
} | ||
|
||
if(identifier == null) return; | ||
final SoundCategory category = SOURCE_LIST[soundCategory]; | ||
final long seed = world.getRandom().nextLong(); | ||
// TODO: Implement the rest, need SoundEvent.of | ||
}); | ||
} else { | ||
|
||
} | ||
} | ||
} | ||
|
||
public void setData(String soundId, int soundCategory, int interval, float volume, boolean needRedstone, boolean limitRange, BlockPos pos1, BlockPos pos2) { | ||
this.soundID = soundId; | ||
this.repeatTick = interval; | ||
this.soundCategory = soundCategory; | ||
this.volume = volume; | ||
this.needRedstone = needRedstone; | ||
this.pos1 = pos1; | ||
this.pos2 = pos2; | ||
this.limitRange = limitRange; | ||
markDirty2(); | ||
} | ||
|
||
public String getSoundId() { | ||
return soundID == null ? "" : soundID; | ||
} | ||
|
||
public int getLoopInterval() { | ||
return repeatTick; | ||
} | ||
|
||
public int getSoundCategory() { | ||
if (soundCategory > SOURCE_LIST.length) { | ||
soundCategory = 0; | ||
} | ||
return soundCategory; | ||
} | ||
|
||
public float getVolume() { | ||
return volume; | ||
} | ||
|
||
public boolean needRedstone() { | ||
return needRedstone; | ||
} | ||
|
||
public boolean getLimitRange() { | ||
return limitRange; | ||
} | ||
|
||
public BlockPos getPos1() { | ||
return pos1; | ||
} | ||
|
||
public BlockPos getPos2() { | ||
return pos2; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
fabric/src/main/java/com/lx862/jcm/mod/network/block/SoundLooperUpdatePacket.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,71 @@ | ||
package com.lx862.jcm.mod.network.block; | ||
|
||
import com.lx862.jcm.mod.block.base.JCMBlock; | ||
import com.lx862.jcm.mod.block.entity.SoundLooperBlockEntity; | ||
import com.lx862.jcm.mod.util.BlockUtil; | ||
import org.mtr.mapping.holder.*; | ||
import org.mtr.mapping.registry.PacketHandler; | ||
import org.mtr.mapping.tool.PacketBufferReceiver; | ||
import org.mtr.mapping.tool.PacketBufferSender; | ||
|
||
public class SoundLooperUpdatePacket extends PacketHandler { | ||
private final BlockPos blockPos; | ||
private final BlockPos pos1; | ||
private final BlockPos pos2; | ||
private final String soundId; | ||
private final int soundCategory; | ||
private final int interval; | ||
private final float volume; | ||
private final boolean needRedstone; | ||
private final boolean limitRange; | ||
|
||
public SoundLooperUpdatePacket(PacketBufferReceiver packetBufferReceiver) { | ||
this.blockPos = BlockPos.fromLong(packetBufferReceiver.readLong()); | ||
this.pos1 = BlockPos.fromLong(packetBufferReceiver.readLong()); | ||
this.pos2 = BlockPos.fromLong(packetBufferReceiver.readLong()); | ||
this.soundId = packetBufferReceiver.readString(); | ||
this.soundCategory = packetBufferReceiver.readInt(); | ||
this.interval = packetBufferReceiver.readInt(); | ||
this.volume = packetBufferReceiver.readFloat(); | ||
this.needRedstone = packetBufferReceiver.readBoolean(); | ||
this.limitRange = packetBufferReceiver.readBoolean(); | ||
} | ||
|
||
public SoundLooperUpdatePacket(BlockPos blockPos, BlockPos pos1, BlockPos pos2, String soundId, int soundCategory, int interval, float volume, boolean needRedstone, boolean limitRange) { | ||
this.blockPos = blockPos; | ||
this.pos1 = pos1; | ||
this.pos2 = pos2; | ||
this.soundId = soundId; | ||
this.soundCategory = soundCategory; | ||
this.interval = interval; | ||
this.volume = volume; | ||
this.needRedstone = needRedstone; | ||
this.limitRange = limitRange; | ||
} | ||
|
||
@Override | ||
public void runServer(MinecraftServer minecraftServer, ServerPlayerEntity serverPlayerEntity) { | ||
World world = serverPlayerEntity.getEntityWorld(); | ||
BlockState state = BlockUtil.getBlockState(world, blockPos); | ||
if(state == null || !(state.getBlock().data instanceof JCMBlock)) return; | ||
|
||
((JCMBlock)state.getBlock().data).forEachBlockEntity(state, world, blockPos, be -> { | ||
if(be.data instanceof SoundLooperBlockEntity) { | ||
((SoundLooperBlockEntity)be.data).setData(soundId, soundCategory, interval, volume, needRedstone, limitRange, pos1, pos2); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void write(PacketBufferSender packetBufferSender) { | ||
packetBufferSender.writeLong(blockPos.asLong()); | ||
packetBufferSender.writeLong(pos1.asLong()); | ||
packetBufferSender.writeLong(pos2.asLong()); | ||
packetBufferSender.writeString(soundId); | ||
packetBufferSender.writeInt(soundCategory); | ||
packetBufferSender.writeInt(interval); | ||
packetBufferSender.writeFloat(volume); | ||
packetBufferSender.writeBoolean(needRedstone); | ||
packetBufferSender.writeBoolean(limitRange); | ||
} | ||
} |
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