2022-02-01 01:14:21 +01:00
|
|
|
package com.simibubi.create.content.logistics.trains.track;
|
|
|
|
|
2022-02-08 01:49:35 +01:00
|
|
|
import com.simibubi.create.AllBlocks;
|
2022-05-04 01:05:03 +02:00
|
|
|
import com.simibubi.create.AllSoundEvents;
|
2023-05-09 18:23:47 +02:00
|
|
|
import com.simibubi.create.AllTags;
|
2022-02-01 01:14:21 +01:00
|
|
|
import com.simibubi.create.content.logistics.trains.ITrackBlock;
|
|
|
|
import com.simibubi.create.content.logistics.trains.track.TrackPlacement.PlacementInfo;
|
2022-04-16 20:30:11 +02:00
|
|
|
import com.simibubi.create.foundation.networking.AllPackets;
|
2022-02-01 01:14:21 +01:00
|
|
|
import com.simibubi.create.foundation.utility.Lang;
|
2022-02-09 02:05:48 +01:00
|
|
|
import com.simibubi.create.foundation.utility.Pair;
|
2022-02-01 01:14:21 +01:00
|
|
|
import com.simibubi.create.foundation.utility.VecHelper;
|
|
|
|
|
2022-04-14 02:23:55 +02:00
|
|
|
import net.minecraft.ChatFormatting;
|
2022-04-16 20:30:11 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
2022-02-01 01:14:21 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
2022-02-09 02:05:48 +01:00
|
|
|
import net.minecraft.core.Direction.AxisDirection;
|
2022-02-01 01:14:21 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.nbt.NbtUtils;
|
2022-05-04 01:05:03 +02:00
|
|
|
import net.minecraft.sounds.SoundEvents;
|
|
|
|
import net.minecraft.sounds.SoundSource;
|
2022-04-16 20:30:11 +02:00
|
|
|
import net.minecraft.world.InteractionHand;
|
2022-02-01 01:14:21 +01:00
|
|
|
import net.minecraft.world.InteractionResult;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.item.BlockItem;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
|
|
|
import net.minecraft.world.item.context.UseOnContext;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
|
|
import net.minecraft.world.level.block.Block;
|
2022-05-04 01:05:03 +02:00
|
|
|
import net.minecraft.world.level.block.SoundType;
|
2022-02-01 01:14:21 +01:00
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.phys.Vec3;
|
2022-04-16 20:30:11 +02:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
|
|
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
|
2022-02-01 01:14:21 +01:00
|
|
|
|
2022-04-16 20:30:11 +02:00
|
|
|
@EventBusSubscriber(Dist.CLIENT)
|
2022-02-01 01:14:21 +01:00
|
|
|
public class TrackBlockItem extends BlockItem {
|
|
|
|
|
|
|
|
public TrackBlockItem(Block pBlock, Properties pProperties) {
|
|
|
|
super(pBlock, pProperties);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InteractionResult useOn(UseOnContext pContext) {
|
|
|
|
ItemStack stack = pContext.getItemInHand();
|
|
|
|
BlockPos pos = pContext.getClickedPos();
|
|
|
|
Level level = pContext.getLevel();
|
|
|
|
BlockState state = level.getBlockState(pos);
|
|
|
|
Player player = pContext.getPlayer();
|
|
|
|
|
|
|
|
if (player == null)
|
|
|
|
return super.useOn(pContext);
|
2022-05-04 01:05:03 +02:00
|
|
|
if (pContext.getHand() == InteractionHand.OFF_HAND)
|
|
|
|
return super.useOn(pContext);
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
Vec3 lookAngle = player.getLookAngle();
|
|
|
|
|
|
|
|
if (!isFoil(stack)) {
|
2022-05-04 01:05:03 +02:00
|
|
|
if (state.getBlock()instanceof TrackBlock track && track.getTrackAxes(level, pos, state)
|
2022-04-14 02:23:55 +02:00
|
|
|
.size() > 1) {
|
|
|
|
if (!level.isClientSide)
|
2022-07-05 16:49:55 +02:00
|
|
|
player.displayClientMessage(Lang.translateDirect("track.junction_start")
|
2022-04-14 02:23:55 +02:00
|
|
|
.withStyle(ChatFormatting.RED), true);
|
|
|
|
return InteractionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-05-04 01:05:03 +02:00
|
|
|
if (select(level, pos, lookAngle, stack)) {
|
|
|
|
level.playSound(null, pos, SoundEvents.ITEM_FRAME_ADD_ITEM, SoundSource.BLOCKS, 0.75f, 1);
|
2022-02-01 01:14:21 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2022-05-04 01:05:03 +02:00
|
|
|
}
|
2022-02-01 01:14:21 +01:00
|
|
|
return super.useOn(pContext);
|
|
|
|
|
|
|
|
} else if (player.isSteppingCarefully()) {
|
|
|
|
if (!level.isClientSide) {
|
2022-07-05 16:49:55 +02:00
|
|
|
player.displayClientMessage(Lang.translateDirect("track.selection_cleared"), true);
|
2022-02-01 01:14:21 +01:00
|
|
|
stack.setTag(null);
|
2022-05-04 01:05:03 +02:00
|
|
|
} else
|
|
|
|
level.playSound(player, pos, SoundEvents.ITEM_FRAME_REMOVE_ITEM, SoundSource.BLOCKS, 0.75f, 1);
|
2022-02-01 01:14:21 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean placing = !(state.getBlock() instanceof ITrackBlock);
|
2022-04-16 20:30:11 +02:00
|
|
|
CompoundTag tag = stack.getTag();
|
|
|
|
boolean extend = tag.getBoolean("ExtendCurve");
|
|
|
|
tag.remove("ExtendCurve");
|
|
|
|
|
2022-03-11 23:37:41 +01:00
|
|
|
if (placing) {
|
|
|
|
if (!state.getMaterial()
|
|
|
|
.isReplaceable())
|
|
|
|
pos = pos.relative(pContext.getClickedFace());
|
2022-02-01 01:14:21 +01:00
|
|
|
state = getPlacementState(pContext);
|
|
|
|
if (state == null)
|
|
|
|
return InteractionResult.FAIL;
|
|
|
|
}
|
|
|
|
|
2022-02-08 01:49:35 +01:00
|
|
|
ItemStack offhandItem = player.getOffhandItem();
|
2022-04-16 20:30:11 +02:00
|
|
|
boolean hasGirder = AllBlocks.METAL_GIRDER.isIn(offhandItem);
|
2022-05-04 01:05:03 +02:00
|
|
|
PlacementInfo info = TrackPlacement.tryConnect(level, player, pos, state, stack, hasGirder, extend);
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
if (info.message != null && !level.isClientSide)
|
2022-07-05 16:49:55 +02:00
|
|
|
player.displayClientMessage(Lang.translateDirect(info.message), true);
|
2022-05-04 01:05:03 +02:00
|
|
|
if (!info.valid) {
|
|
|
|
AllSoundEvents.DENY.playFrom(player, 1, 1);
|
2022-02-01 01:14:21 +01:00
|
|
|
return InteractionResult.FAIL;
|
2022-05-04 01:05:03 +02:00
|
|
|
}
|
2022-02-01 01:14:21 +01:00
|
|
|
|
2022-05-04 01:05:03 +02:00
|
|
|
if (level.isClientSide)
|
2022-02-01 01:14:21 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
|
|
|
|
2022-05-04 01:05:03 +02:00
|
|
|
stack = player.getMainHandItem();
|
2023-05-09 18:23:47 +02:00
|
|
|
if (AllTags.AllBlockTags.TRACKS.matches(stack)) {
|
2022-05-04 01:05:03 +02:00
|
|
|
stack.setTag(null);
|
|
|
|
player.setItemInHand(pContext.getHand(), stack);
|
2022-02-01 01:14:21 +01:00
|
|
|
}
|
|
|
|
|
2022-05-04 01:05:03 +02:00
|
|
|
SoundType soundtype = state.getSoundType();
|
|
|
|
if (soundtype != null)
|
|
|
|
level.playSound(null, pos, soundtype.getPlaceSound(), SoundSource.BLOCKS,
|
|
|
|
(soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
|
|
|
|
|
2022-02-01 01:14:21 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockState getPlacementState(UseOnContext pContext) {
|
|
|
|
return getPlacementState(updatePlacementContext(new BlockPlaceContext(pContext)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean select(LevelAccessor world, BlockPos pos, Vec3 lookVec, ItemStack heldItem) {
|
|
|
|
BlockState blockState = world.getBlockState(pos);
|
|
|
|
Block block = blockState.getBlock();
|
|
|
|
if (!(block instanceof ITrackBlock))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ITrackBlock track = (ITrackBlock) block;
|
2022-02-09 02:05:48 +01:00
|
|
|
Pair<Vec3, AxisDirection> nearestTrackAxis = track.getNearestTrackAxis(world, pos, blockState, lookVec);
|
2022-02-09 21:13:33 +01:00
|
|
|
Vec3 axis = nearestTrackAxis.getFirst()
|
|
|
|
.scale(nearestTrackAxis.getSecond() == AxisDirection.POSITIVE ? -1 : 1);
|
|
|
|
Vec3 end = track.getCurveStart(world, pos, blockState, axis);
|
2022-02-01 01:14:21 +01:00
|
|
|
Vec3 normal = track.getUpNormal(world, pos, blockState)
|
|
|
|
.normalize();
|
|
|
|
|
|
|
|
CompoundTag compoundTag = heldItem.getOrCreateTagElement("ConnectingFrom");
|
|
|
|
compoundTag.put("Pos", NbtUtils.writeBlockPos(pos));
|
|
|
|
compoundTag.put("Axis", VecHelper.writeNBT(axis));
|
|
|
|
compoundTag.put("Normal", VecHelper.writeNBT(normal));
|
|
|
|
compoundTag.put("End", VecHelper.writeNBT(end));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:30:11 +02:00
|
|
|
@SubscribeEvent
|
|
|
|
@OnlyIn(Dist.CLIENT)
|
|
|
|
public static void sendExtenderPacket(PlayerInteractEvent.RightClickBlock event) {
|
|
|
|
ItemStack stack = event.getItemStack();
|
2023-05-09 18:23:47 +02:00
|
|
|
if (!AllTags.AllBlockTags.TRACKS.matches(stack) || !stack.hasTag())
|
2022-04-16 20:30:11 +02:00
|
|
|
return;
|
|
|
|
if (Minecraft.getInstance().options.keySprint.isDown())
|
|
|
|
AllPackets.channel
|
|
|
|
.sendToServer(new PlaceExtendedCurvePacket(event.getHand() == InteractionHand.MAIN_HAND, true));
|
|
|
|
}
|
|
|
|
|
2022-02-01 01:14:21 +01:00
|
|
|
@Override
|
|
|
|
public boolean isFoil(ItemStack stack) {
|
|
|
|
return stack.hasTag() && stack.getTag()
|
|
|
|
.contains("ConnectingFrom");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|