Skip to content

Commit

Permalink
Add Sound Looper (No function)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Mar 20, 2024
1 parent a6da5fa commit 07ecc40
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 6 deletions.
20 changes: 20 additions & 0 deletions fabric/src/main/java/com/lx862/jcm/mod/block/SoundLooperBlock.java
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);
}
}
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;
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class BlockEntities {
public static final BlockEntityTypeRegistryObject<StaticSignalLightBlockEntity> SIGNAL_LIGHT_BLUE = RegistryHelper.registerBlockEntity("signal_light_blue", (blockPos, blockState) -> new StaticSignalLightBlockEntity(StaticSignalLightBlockEntity.SignalType.BLUE, blockPos, blockState), Blocks.STATIC_SIGNAL_LIGHT_BLUE);
public static final BlockEntityTypeRegistryObject<StaticSignalLightBlockEntity> SIGNAL_LIGHT_GREEN = RegistryHelper.registerBlockEntity("signal_light_green", (blockPos, blockState) -> new StaticSignalLightBlockEntity(StaticSignalLightBlockEntity.SignalType.GREEN, blockPos, blockState), Blocks.STATIC_SIGNAL_LIGHT_GREEN);
public static final BlockEntityTypeRegistryObject<SubsidyMachineBlockEntity> SUBSIDY_MACHINE = RegistryHelper.registerBlockEntity("subsidy_machine", SubsidyMachineBlockEntity::new, Blocks.SUBSIDY_MACHINE);
public static final BlockEntityTypeRegistryObject<SoundLooperBlockEntity> SOUND_LOOPER = RegistryHelper.registerBlockEntity("sound_looper", SoundLooperBlockEntity::new, Blocks.SOUND_LOOPER);
public static final BlockEntityTypeRegistryObject<ButterflyLightBlockEntity> BUTTERFLY_LIGHT = RegistryHelper.registerBlockEntity("butterfly_light", ButterflyLightBlockEntity::new, Blocks.BUTTERFLY_LIGHT);
public static final BlockEntityTypeRegistryObject<DepartureTimerBlockEntity> DEPARTURE_TIMER = RegistryHelper.registerBlockEntity("departure_timer", DepartureTimerBlockEntity::new, Blocks.DEPARTURE_TIMER);
public static final BlockEntityTypeRegistryObject<KCRStationNameSignBlockEntity> KCR_STATION_NAME_SIGN = RegistryHelper.registerBlockEntity("kcr_name_sign", (blockPos, blockState) -> new KCRStationNameSignBlockEntity(blockPos, blockState, false), Blocks.KCR_STATION_NAME_SIGN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public final class Blocks {
public static final BlockRegistryObject OPERATOR_BUTTON = RegistryHelper.registerBlockItem("operator_button", new OperatorButtonBlock(BlockHelper.createBlockSettings(false, state -> 5).nonOpaque(), 40), ItemGroups.JCM_MAIN);
public static final BlockRegistryObject SPOT_LAMP = RegistryHelper.registerBlockItem("spot_lamp", new SpotLampBlock(BlockHelper.createBlockSettings(false, state -> 15).nonOpaque()), ItemGroups.JCM_MAIN);
public static final BlockRegistryObject SUBSIDY_MACHINE = RegistryHelper.registerBlockItem("subsidy_machine", new SubsidyMachineBlock(BlockHelper.createBlockSettings(false).nonOpaque()), ItemGroups.JCM_MAIN);
public static final BlockRegistryObject SOUND_LOOPER = RegistryHelper.registerBlockItem("sound_looper", new SoundLooperBlock(BlockHelper.createBlockSettings(false)), ItemGroups.JCM_MAIN);
public static final BlockRegistryObject STATION_NAME_STANDING = RegistryHelper.registerBlockItem("station_name_standing", new StationNameStandingBlock(BlockHelper.createBlockSettings(false).strength(4.0f).nonOpaque()), ItemGroups.JCM_MAIN);
public static final BlockRegistryObject STATION_CEILING_WRL = RegistryHelper.registerBlockItem("station_ceiling_wrl", new StationCeilingWRL2Block(BlockHelper.createBlockSettings(false).strength(4.0f).nonOpaque()), ItemGroups.JCM_CEILING);
public static final BlockRegistryObject STATION_CEILING_WRL_SINGLE = RegistryHelper.registerBlockItem("station_ceiling_wrl_single", new StationCeilingWRLBlock(BlockHelper.createBlockSettings(false).strength(4.0f).nonOpaque()), ItemGroups.JCM_CEILING);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.lx862.jcm.mod.registry;

import com.lx862.jcm.mod.network.block.ButterflyLightUpdatePacket;
import com.lx862.jcm.mod.network.block.FareSaverUpdatePacket;
import com.lx862.jcm.mod.network.block.PIDSUpdatePacket;
import com.lx862.jcm.mod.network.block.SubsidyMachineUpdatePacket;
import com.lx862.jcm.mod.network.block.*;
import com.lx862.jcm.mod.network.gui.ButterflyLightGUIPacket;
import com.lx862.jcm.mod.network.gui.FareSaverGUIPacket;
import com.lx862.jcm.mod.network.gui.PIDSGUIPacket;
import com.lx862.jcm.mod.network.gui.SubsidyMachineGUIPacket;
import com.lx862.jcm.mod.util.JCMLogger;
import org.mtr.mapping.holder.PacketBuffer;
import org.mtr.mapping.holder.PlayerEntity;
import org.mtr.mapping.holder.ServerPlayerEntity;
import org.mtr.mapping.registry.PacketHandler;
Expand All @@ -28,8 +24,9 @@ public static void register() {
// Block Entity Update
registerPacket(ButterflyLightUpdatePacket.class, ButterflyLightUpdatePacket::new);
registerPacket(FareSaverUpdatePacket.class, FareSaverUpdatePacket::new);
registerPacket(SubsidyMachineUpdatePacket.class, SubsidyMachineUpdatePacket::new);
registerPacket(PIDSUpdatePacket.class, PIDSUpdatePacket::new);
registerPacket(SoundLooperUpdatePacket.class, SoundLooperUpdatePacket::new);
registerPacket(SubsidyMachineUpdatePacket.class, SubsidyMachineUpdatePacket::new);

// GUI Screen
registerPacket(ButterflyLightGUIPacket.class, ButterflyLightGUIPacket::new);
Expand Down

0 comments on commit 07ecc40

Please sign in to comment.