Skip to content

Commit

Permalink
feat!: Add colored portstones #814
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Jun 1, 2024
1 parent 1cec358 commit 97ef9ac
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package net.blay09.mods.waystones.api.error;

import net.blay09.mods.waystones.api.Waystone;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

public class WaystoneTeleportError {

private final String translationKey;
private final Component component;

public WaystoneTeleportError() {
this.translationKey = null;
this.component = Component.empty();
}

public WaystoneTeleportError(String translationKey) {
this.translationKey = translationKey;
public WaystoneTeleportError(Component component) {
this.component = component;
}

@Nullable
public String getTranslationKey() {
return translationKey;
public Component getComponent() {
return component;
}

public static class NotOnServer extends WaystoneTeleportError {
Expand Down Expand Up @@ -54,7 +53,7 @@ public static class MissingWaystone extends WaystoneTeleportError {
private final Waystone waystone;

public MissingWaystone(Waystone waystone) {
super("chat.waystones.waystone_missing");
super(Component.translatable("chat.waystones.waystone_missing"));
this.waystone = waystone;
}

Expand All @@ -68,21 +67,21 @@ public static class CancelledByEvent extends WaystoneTeleportError {

public static class DimensionalWarpDenied extends WaystoneTeleportError {
public DimensionalWarpDenied() {
super("chat.waystones.cannot_dimension_warp");
super(Component.translatable("chat.waystones.cannot_dimension_warp"));
}
}

public static class LeashedWarpDenied extends WaystoneTeleportError {
public LeashedWarpDenied() {
super("chat.waystones.cannot_transport_leashed");
super(Component.translatable("chat.waystones.cannot_transport_leashed"));
}
}

public static class SpecificLeashedWarpDenied extends WaystoneTeleportError {
private final Entity entity;

public SpecificLeashedWarpDenied(Entity entity) {
super("chat.waystones.cannot_transport_this_leashed");
super(Component.translatable("chat.waystones.cannot_transport_this_leashed"));
this.entity = entity;
}

Expand All @@ -93,13 +92,13 @@ public Entity getEntity() {

public static class LeashedDimensionalWarpDenied extends WaystoneTeleportError {
public LeashedDimensionalWarpDenied() {
super("chat.waystones.cannot_transport_leashed_dimensional");
super(Component.translatable("chat.waystones.cannot_transport_leashed_dimensional"));
}
}

public static class NotEnoughXp extends WaystoneTeleportError {
public NotEnoughXp() {
super("chat.waystones.not_enough_xp");
super(Component.translatable("chat.waystones.not_enough_xp"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.menu.BalmMenuProvider;
import net.blay09.mods.waystones.api.Waystone;
import net.blay09.mods.waystones.api.WaystoneTypes;
import net.blay09.mods.waystones.block.entity.PortstoneBlockEntity;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.blay09.mods.waystones.menu.ModMenus;
Expand All @@ -17,6 +18,7 @@
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
Expand Down Expand Up @@ -114,16 +116,15 @@ public class PortstoneBlock extends WaystoneBlockBase {
).optimize()
};

@Nullable
private final DyeColor color;

public PortstoneBlock(@Nullable DyeColor color, Properties properties) {
public PortstoneBlock(DyeColor color, Properties properties) {
super(properties);
this.color = color;
registerDefaultState(this.stateDefinition.any().setValue(HALF, DoubleBlockHalf.LOWER).setValue(WATERLOGGED, false));
}

public @Nullable DyeColor getColor() {
public DyeColor getColor() {
return color;
}

Expand All @@ -142,12 +143,13 @@ public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
@Override
public InteractionResult useWithoutItem(BlockState state, Level world, BlockPos pos, Player player, BlockHitResult blockHitResult) {
if (!world.isClientSide) {
final var waystones = PlayerWaystoneManager.getTargetsForPlayer(player);
final var targetWaystoneType = getTargetWaystoneType();
final var waystones = PlayerWaystoneManager.getTargetsForWaystoneType(player, targetWaystoneType);
PlayerWaystoneManager.ensureSortingIndex(player, waystones);
Balm.getNetworking().openGui(player, new BalmMenuProvider<Collection<Waystone>>() {
@Override
public Component getDisplayName() {
return Component.translatable("block.waystones.portstone");
return Component.translatable("container.waystones." + color.getSerializedName() + "_portstone");
}

@Override
Expand All @@ -169,6 +171,10 @@ public StreamCodec<RegistryFriendlyByteBuf, Collection<Waystone>> getScreenStrea
return InteractionResult.SUCCESS;
}

private ResourceLocation getTargetWaystoneType() {
return WaystoneTypes.getSharestone(color).orElse(WaystoneTypes.WAYSTONE);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
import net.blay09.mods.waystones.block.entity.WaystoneBlockEntityBase;
import net.blay09.mods.waystones.component.ModComponents;
import net.blay09.mods.waystones.core.*;
import net.blay09.mods.waystones.item.ModItems;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.IntArrayTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -44,7 +40,6 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

public abstract class WaystoneBlockBase extends BaseEntityBlock implements SimpleWaterloggedBlock {

Expand Down Expand Up @@ -288,7 +283,7 @@ protected void addWaystoneNameToTooltip(List<Component> tooltip, WaystoneProxy w
protected ItemInteractionResult useItemOn(ItemStack itemStack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult blockHitResult) {
final var blockEntity = level.getBlockEntity(pos);
if (!(blockEntity instanceof WaystoneBlockEntityBase waystoneBlockEntity)) {
return ItemInteractionResult.FAIL;
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}

return handleDebugActions(level, player, itemStack, waystoneBlockEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,8 @@ private void teleportToTarget(Entity entity, Waystone targetWaystone, ItemStack

private Consumer<WaystoneTeleportError> informRejectedTeleport(final Entity entityToInform) {
return error -> {
if (error.getTranslationKey() != null && entityToInform instanceof Player player) {
var chatComponent = Component.translatable(error.getTranslationKey());
chatComponent.withStyle(ChatFormatting.DARK_RED);
if (error.getComponent() != null && entityToInform instanceof Player player) {
var chatComponent = error.getComponent().copy().withStyle(ChatFormatting.DARK_RED);
player.displayClientMessage(chatComponent, true);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,24 @@ public static Collection<Waystone> getTargetsForItem(Player player, ItemStack it
}

public static Collection<Waystone> getTargetsForWaystone(Player player, Waystone waystone) {
final var waystoneType = waystone.getWaystoneType();
final var result = getTargetsForWaystoneType(player, waystone.getWaystoneType());

final var blockEntity = player.level().getBlockEntity(waystone.getPos());
if (blockEntity instanceof WaystoneBlockEntityBase waystoneBlockEntity) {
result.addAll(waystoneBlockEntity.getAuxiliaryTargets());
}

return result;
}

public static Collection<Waystone> getTargetsForWaystoneType(Player player, ResourceLocation waystoneType) {
final var result = new ArrayList<Waystone>();
if (WaystoneTypes.isSharestone(waystoneType)) {
result.addAll(WaystoneManagerImpl.get(player.getServer()).getWaystonesByType(waystoneType).toList());
} else {
result.addAll(PlayerWaystoneManager.getActivatedWaystones(player));
}

final var blockEntity = player.level().getBlockEntity(waystone.getPos());
if (blockEntity instanceof WaystoneBlockEntityBase waystoneBlockEntity) {
result.addAll(waystoneBlockEntity.getAuxiliaryTargets());
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public StreamCodec<RegistryFriendlyByteBuf, Collection<Waystone>> getStreamCodec
portstoneSelection = menus.registerMenu(id("portstone_selection"), new BalmMenuFactory<WaystoneSelectionMenu, Collection<Waystone>>() {
@Override
public WaystoneSelectionMenu create(int windowId, Inventory inventory, Collection<Waystone> waystones) {
return new WaystoneSelectionMenu(ModMenus.waystoneSelection.get(), null, windowId, waystones, Collections.emptySet());
return new WaystoneSelectionMenu(ModMenus.portstoneSelection.get(), null, windowId, waystones, Collections.emptySet());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.blay09.mods.waystones.core.WaystoneTeleportManager;
import net.blay09.mods.waystones.menu.WaystoneSelectionMenu;
import net.blay09.mods.waystones.core.WaystoneProxy;
import net.minecraft.ChatFormatting;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -15,7 +16,8 @@

public class SelectWaystoneMessage implements CustomPacketPayload {

public static final CustomPacketPayload.Type<SelectWaystoneMessage> TYPE = new CustomPacketPayload.Type<>(new ResourceLocation(Waystones.MOD_ID, "select_waystone"));
public static final CustomPacketPayload.Type<SelectWaystoneMessage> TYPE = new CustomPacketPayload.Type<>(new ResourceLocation(Waystones.MOD_ID,
"select_waystone"));

private final UUID waystoneUid;

Expand Down Expand Up @@ -50,7 +52,8 @@ public static void handle(final ServerPlayer player, SelectWaystoneMessage messa
it.addFlags(selectionMenu.getFlags());
})
.ifLeft(WaystonesAPI::tryTeleport)
.ifLeft(selectionMenu.getPostTeleportHandler());
.ifLeft(selectionMenu.getPostTeleportHandler())
.ifRight(error -> player.displayClientMessage(error.getComponent().copy().withStyle(ChatFormatting.DARK_RED), false));
player.closeContainer();
}

Expand Down
16 changes: 16 additions & 0 deletions common/src/main/resources/assets/waystones/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@
"container.waystones.waystone_selection": "Select your destination:",
"container.waystones.waystone_admin_selection": "Select which waystone of <%s> you would like to visit:",
"container.waystones.waystone": "Waystone",
"container.waystones.white_portstone": "Portstone",
"container.waystones.orange_portstone": "Orange Portstone",
"container.waystones.magenta_portstone": "Magenta Portstone",
"container.waystones.light_blue_portstone": "Light Blue Portstone",
"container.waystones.yellow_portstone": "Yellow Portstone",
"container.waystones.lime_portstone": "Lime Portstone",
"container.waystones.pink_portstone": "Pink Portstone",
"container.waystones.gray_portstone": "Gray Portstone",
"container.waystones.light_gray_portstone": "Light Gray Portstone",
"container.waystones.cyan_portstone": "Cyan Portstone",
"container.waystones.purple_portstone": "Purple Portstone",
"container.waystones.blue_portstone": "Blue Portstone",
"container.waystones.brown_portstone": "Brown Portstone",
"container.waystones.green_portstone": "Green Portstone",
"container.waystones.red_portstone": "Red Portstone",
"container.waystones.black_portstone": "Black Portstone",
"container.waystones.sharestone": "Sharestone",
"container.waystones.landing_stone": "Landing Stone",
"container.waystones.warp_plate": "Warp Plate",
Expand Down

0 comments on commit 97ef9ac

Please sign in to comment.