Skip to content

Commit

Permalink
Fixed sound when using firestone
Browse files Browse the repository at this point in the history
  • Loading branch information
Edivad99 committed Jan 1, 2024
1 parent d204592 commit 57fdc96
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 80 deletions.
7 changes: 6 additions & 1 deletion src/api/java/mods/railcraft/api/core/RailcraftConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ This work (the API) is licensed under the "MIT" License,
-----------------------------------------------------------------------------*/
package mods.railcraft.api.core;

import java.util.UUID;
import com.mojang.authlib.GameProfile;

public final class RailcraftConstants {

public static final String ID = "railcraft";
public static final String NAME = "Railcraft Reborn";
public static final String RAILCRAFT_PLAYER = "[railcraft]";
private static final String RAILCRAFT_PLAYER = "[" + ID + "]";
public static final String UNKNOWN_PLAYER = "[unknown]";
public static final GameProfile FAKE_GAMEPROFILE =
new GameProfile(UUID.nameUUIDFromBytes(RAILCRAFT_PLAYER.getBytes()), RAILCRAFT_PLAYER);
}
34 changes: 7 additions & 27 deletions src/api/java/mods/railcraft/api/core/RailcraftFakePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,26 @@ This work (the API) is licensed under the "MIT" License,
-----------------------------------------------------------------------------*/
package mods.railcraft.api.core;

import java.util.UUID;
import com.mojang.authlib.GameProfile;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.util.FakePlayerFactory;

public final class RailcraftFakePlayer {
private RailcraftFakePlayer() {}

public static final GameProfile RAILCRAFT_USER_PROFILE =
new GameProfile(UUID.nameUUIDFromBytes(RailcraftConstants.RAILCRAFT_PLAYER.getBytes()),
RailcraftConstants.RAILCRAFT_PLAYER);

public static ServerPlayer get(final ServerLevel world, final double x, final double y,
final double z) {
ServerPlayer player = FakePlayerFactory.get(world, RAILCRAFT_USER_PROFILE);
public static ServerPlayer get(ServerLevel level, double x, double y, double z) {
var player = FakePlayerFactory.get(level, RailcraftConstants.FAKE_GAMEPROFILE);
player.setPos(x, y, z);
return player;
}

public static ServerPlayer get(final ServerLevel world, final BlockPos pos) {
ServerPlayer player = FakePlayerFactory.get(world, RAILCRAFT_USER_PROFILE);
player.setPos(pos.getX(), pos.getY(), pos.getZ());
return player;
}

public static ServerPlayer get(ServerLevel world, double x, double y, double z,
ItemStack stack, InteractionHand hand) {
ServerPlayer player = get(world, x, y, z);
player.setItemInHand(hand, stack);
return player;
public static ServerPlayer get(ServerLevel level, final Vec3 pos) {
return get(level, pos.x, pos.y, pos.z);
}

public static ServerPlayer get(ServerLevel world, BlockPos pos, ItemStack stack,
InteractionHand hand) {
ServerPlayer player = get(world, pos);
player.setItemInHand(hand, stack);
return player;
public static ServerPlayer get(ServerLevel level, final BlockPos pos) {
return get(level, pos.getX(), pos.getY(), pos.getZ());
}
}
20 changes: 0 additions & 20 deletions src/main/java/mods/railcraft/util/PlayerUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package mods.railcraft.util;

import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.mojang.authlib.GameProfile;
import mods.railcraft.api.core.RailcraftConstants;
import mods.railcraft.api.item.ActivationBlockingItem;
Expand All @@ -15,15 +13,6 @@

public final class PlayerUtil {

@Nullable
public static Player getPlayer(Level level, GameProfile gameProfile) {
var playerId = gameProfile.getId();
if (playerId != null) {
return level.getPlayerByUUID(playerId);
}
return null;
}

public static Component getUsername(Level level, @NotNull GameProfile gameProfile) {
var playerId = gameProfile.getId();
if (playerId != null) {
Expand All @@ -36,15 +25,6 @@ public static Component getUsername(Level level, @NotNull GameProfile gameProfil
StringUtils.isEmpty(username) ? RailcraftConstants.UNKNOWN_PLAYER : username);
}

public static Component getUsername(Level level, @Nullable UUID playerId) {
if (playerId != null) {
var player = level.getPlayerByUUID(playerId);
if (player != null)
return player.getDisplayName();
}
return Component.literal(RailcraftConstants.UNKNOWN_PLAYER);
}

public static boolean isOwnerOrOp(GameProfile owner, Player player) {
return player.getGameProfile().equals(owner) || player.hasPermissions(2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public static void removePassengers(AbstractMinecart cart, double x, double y, d
}

public static ServerPlayer getFakePlayer(AbstractMinecart cart) {
return RailcraftFakePlayer.get((ServerLevel) cart.level(), cart.getX(), cart.getY(),
cart.getZ());
return RailcraftFakePlayer.get((ServerLevel) cart.level(), cart.position());
}

public static ServerPlayer getFakePlayerWith(AbstractMinecart cart, ItemStack stack) {
Expand Down
37 changes: 20 additions & 17 deletions src/main/java/mods/railcraft/world/item/RefinedFirestoneItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
Expand Down Expand Up @@ -92,16 +93,16 @@ public void appendHoverText(ItemStack itemStack, @Nullable Level level,

@Override
public InteractionResult useOn(UseOnContext context) {
Player player = context.getPlayer();
ItemStack stack = context.getItemInHand();
Level level = context.getLevel();
BlockPos pos = context.getClickedPos();
Direction side = context.getClickedFace();
BlockState blockState = level.getBlockState(pos);
RandomSource random = level.getRandom();
var player = context.getPlayer();
var stack = context.getItemInHand();
var level = context.getLevel();
var pos = context.getClickedPos();
var side = context.getClickedFace();
var blockState = level.getBlockState(pos);
var random = level.getRandom();

if (level.isClientSide())
return InteractionResult.CONSUME;
return InteractionResult.sidedSuccess(level.isClientSide());
if (stack.getDamageValue() == stack.getMaxDamage())
return InteractionResult.PASS;

Expand All @@ -117,7 +118,8 @@ public InteractionResult useOn(UseOnContext context) {
var newState = ContainerTools.getBlockStateFromStack(cooked, level, pos);
if (newState != null) {
level.setBlockAndUpdate(pos, newState);
player.playSound(SoundEvents.FIRECHARGE_USE, 1.0F, random.nextFloat() * 0.4F + 0.8F);
level.playSound(null, pos, SoundEvents.FIRECHARGE_USE, SoundSource.AMBIENT, 1.0F,
random.nextFloat() * 0.4F + 0.8F);
stack.hurt(1, random, serverPlayer);
return InteractionResult.SUCCESS;
}
Expand All @@ -128,13 +130,13 @@ public InteractionResult useOn(UseOnContext context) {
pos = pos.relative(side);

if (player.mayUseItemAt(pos, side, stack) && level.getBlockState(pos).isAir()) {
player.playSound(SoundEvents.FIRECHARGE_USE, 1.0F, random.nextFloat() * 0.4F + 0.8F);
level.playSound(null, pos, SoundEvents.FIRECHARGE_USE, SoundSource.AMBIENT, 1.0F,
random.nextFloat() * 0.4F + 0.8F);
level.setBlockAndUpdate(pos, Blocks.FIRE.defaultBlockState());
stack.hurt(1, random, serverPlayer);
return InteractionResult.SUCCESS;
}
}
return InteractionResult.CONSUME;
return InteractionResult.sidedSuccess(level.isClientSide());
}

@NotNull
Expand All @@ -148,16 +150,17 @@ private ItemStack cookedItem(Level level, ItemStack ingredient) {
@Override
public InteractionResult interactLivingEntity(ItemStack itemStack, Player player,
LivingEntity livingEntity, InteractionHand hand) {
if (player instanceof ServerPlayer && !livingEntity.fireImmune()) {
var level = player.level();
if (level instanceof ServerLevel && !livingEntity.fireImmune()) {
livingEntity.setSecondsOnFire(5);
itemStack.hurtAndBreak(1, player, __ -> player.broadcastBreakEvent(hand));
player.playSound(SoundEvents.FIRECHARGE_USE, 1.0F,
player.getRandom().nextFloat() * 0.4F + 0.8F);
level.playSound(null, livingEntity.blockPosition(), SoundEvents.FIRECHARGE_USE,
SoundSource.AMBIENT, 1.0F, player.getRandom().nextFloat() * 0.4F + 0.8F);
player.swing(hand);
player.level().setBlockAndUpdate(player.blockPosition(), Blocks.FIRE.defaultBlockState());
level.setBlockAndUpdate(livingEntity.blockPosition(), Blocks.FIRE.defaultBlockState());
return InteractionResult.SUCCESS;
}
return InteractionResult.CONSUME;
return InteractionResult.sidedSuccess(level.isClientSide());
}

@Override
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/mods/railcraft/world/item/SpikeMaulItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,13 @@ public InteractionResult useOn(UseOnContext context) {
level.playSound(player, blockPos, soundtype.getPlaceSound(), SoundSource.BLOCKS,
(soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);

if (level.isClientSide()) {
return InteractionResult.SUCCESS;
}

RailcraftCriteriaTriggers.SPIKE_MAUL_USE.trigger(
(ServerPlayer) player, heldStack, (ServerLevel) level, blockPos);
if (level instanceof ServerLevel serverLevel) {
RailcraftCriteriaTriggers.SPIKE_MAUL_USE.trigger(
(ServerPlayer) player, heldStack, serverLevel, blockPos);

heldStack.hurtAndBreak(1, player, __ -> player.broadcastBreakEvent(hand));
return InteractionResult.SUCCESS;
heldStack.hurtAndBreak(1, player, __ -> player.broadcastBreakEvent(hand));
}
return InteractionResult.sidedSuccess(level.isClientSide());
}

@Override
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/mods/railcraft/RoutingTrackTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mods.railcraft;

import com.mojang.authlib.GameProfile;
import mods.railcraft.api.core.RailcraftConstants;
import mods.railcraft.world.entity.RailcraftEntityTypes;
import mods.railcraft.world.entity.vehicle.locomotive.Locomotive;
Expand Down Expand Up @@ -30,8 +29,7 @@ public static void routingTrackActive(GameTestHelper helper) {
final var dest = "HOME";
if (helper.getBlockEntity(ROUTING_TRACK_POS) instanceof RoutingTrackBlockEntity routingTrack) {
var goldenTicket = new ItemStack(RailcraftItems.GOLDEN_TICKET.get());
TicketItem.setTicketData(goldenTicket, dest,
new GameProfile(null, RailcraftConstants.RAILCRAFT_PLAYER));
TicketItem.setTicketData(goldenTicket, dest, RailcraftConstants.FAKE_GAMEPROFILE);
routingTrack.setItem(0, goldenTicket);
}
var train = helper.spawn(RailcraftEntityTypes.CREATIVE_LOCOMOTIVE.get(), SPAWN_POINT);
Expand All @@ -53,8 +51,7 @@ public static void routingTrackPassive(GameTestHelper helper) {
final var dest = "HOME";
if (helper.getBlockEntity(ROUTING_TRACK_POS) instanceof RoutingTrackBlockEntity routingTrack) {
var goldenTicket = new ItemStack(RailcraftItems.GOLDEN_TICKET.get());
TicketItem.setTicketData(goldenTicket, dest,
new GameProfile(null, RailcraftConstants.RAILCRAFT_PLAYER));
TicketItem.setTicketData(goldenTicket, dest, RailcraftConstants.FAKE_GAMEPROFILE);
routingTrack.setItem(0, goldenTicket);
}
var train = helper.spawn(RailcraftEntityTypes.CREATIVE_LOCOMOTIVE.get(), SPAWN_POINT);
Expand Down

0 comments on commit 57fdc96

Please sign in to comment.