End signs remake

This commit is contained in:
paulevsGitch 2021-03-10 20:18:16 +03:00
parent ee6dd6f7a8
commit c8378435bd
6 changed files with 25 additions and 278 deletions

View file

@ -15,7 +15,6 @@ import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.DyeItem;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.context.LootContext;
@ -25,13 +24,10 @@ import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.SignType;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
@ -50,16 +46,17 @@ public class EndSignBlock extends AbstractSignBlock implements BlockPatterned {
public static final IntProperty ROTATION = Properties.ROTATION;
public static final BooleanProperty FLOOR = BooleanProperty.of("floor");
private static final VoxelShape[] WALL_SHAPES = new VoxelShape[] {
Block.createCuboidShape(0.0D, 4.5D, 14.0D, 16.0D, 12.5D, 16.0D),
Block.createCuboidShape(0.0D, 4.5D, 0.0D, 2.0D, 12.5D, 16.0D),
Block.createCuboidShape(0.0D, 4.5D, 0.0D, 16.0D, 12.5D, 2.0D),
Block.createCuboidShape(14.0D, 4.5D, 0.0D, 16.0D, 12.5D, 16.0D) };
Block.createCuboidShape(0.0D, 4.5D, 14.0D, 16.0D, 12.5D, 16.0D),
Block.createCuboidShape(0.0D, 4.5D, 0.0D, 2.0D, 12.5D, 16.0D),
Block.createCuboidShape(0.0D, 4.5D, 0.0D, 16.0D, 12.5D, 2.0D),
Block.createCuboidShape(14.0D, 4.5D, 0.0D, 16.0D, 12.5D, 16.0D)
};
private final Block parent;
public EndSignBlock(Block source) {
super(FabricBlockSettings.copyOf(source).strength(1.0F, 1.0F).noCollision().nonOpaque(), SignType.OAK);
this.setDefaultState(this.stateManager.getDefaultState().with(ROTATION, 0).with(FLOOR, true).with(WATERLOGGED, false));
this.setDefaultState(this.stateManager.getDefaultState().with(ROTATION, 0).with(FLOOR, false).with(WATERLOGGED, false));
this.parent = source;
}
@ -77,30 +74,7 @@ public class EndSignBlock extends AbstractSignBlock implements BlockPatterned {
public BlockEntity createBlockEntity(BlockView world) {
return new ESignBlockEntity();
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ItemStack itemStack = player.getStackInHand(hand);
boolean bl = itemStack.getItem() instanceof DyeItem && player.abilities.allowModifyWorld;
if (world.isClient) {
return bl ? ActionResult.SUCCESS : ActionResult.CONSUME;
} else {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof ESignBlockEntity) {
ESignBlockEntity signBlockEntity = (ESignBlockEntity) blockEntity;
if (bl) {
boolean bl2 = signBlockEntity.setTextColor(((DyeItem) itemStack.getItem()).getColor());
if (bl2 && !player.isCreative()) {
itemStack.decrement(1);
}
}
return signBlockEntity.onActivate(player) ? ActionResult.SUCCESS : ActionResult.PASS;
} else {
return ActionResult.PASS;
}
}
}
@Override
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
if (placer != null && placer instanceof PlayerEntity) {
@ -108,8 +82,10 @@ public class EndSignBlock extends AbstractSignBlock implements BlockPatterned {
if (!world.isClient) {
sign.setEditor((PlayerEntity) placer);
((ServerPlayerEntity) placer).networkHandler.sendPacket(new SignEditorOpenS2CPacket(pos));
} else
}
else {
sign.setEditable(true);
}
}
}
@ -149,14 +125,12 @@ public class EndSignBlock extends AbstractSignBlock implements BlockPatterned {
WorldView worldView = ctx.getWorld();
BlockPos blockPos = ctx.getBlockPos();
Direction[] directions = ctx.getPlacementDirections();
Direction[] var7 = directions;
int var8 = directions.length;
for (int var9 = 0; var9 < var8; ++var9) {
Direction direction = var7[var9];
for (int i = 0; i < directions.length; ++i) {
Direction direction = directions[i];
if (direction.getAxis().isHorizontal()) {
Direction direction2 = direction.getOpposite();
int rot = MathHelper.floor((180.0 + direction2.asRotation() * 16.0 / 360.0) + 0.5 + 4) & 15;
Direction dir = direction.getOpposite();
int rot = MathHelper.floor((180.0 + dir.asRotation() * 16.0 / 360.0) + 0.5 + 4) & 15;
blockState = blockState.with(ROTATION, rot);
if (blockState.canPlaceAt(worldView, blockPos)) {
return blockState.with(FLOOR, false).with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER);

View file

@ -1,168 +1,16 @@
package ru.betterend.blocks.entities;
import java.util.function.Function;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.server.command.CommandOutput;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.LiteralText;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.Texts;
import net.minecraft.util.DyeColor;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.SignBlockEntity;
import ru.betterend.registry.EndBlockEntities;
public class ESignBlockEntity extends BlockEntity {
private final Text[] text;
private boolean editable;
private PlayerEntity editor;
private final OrderedText[] textBeingEdited;
private DyeColor textColor;
public class ESignBlockEntity extends SignBlockEntity {
public ESignBlockEntity() {
super(EndBlockEntities.SIGN);
this.text = new Text[] { LiteralText.EMPTY, LiteralText.EMPTY, LiteralText.EMPTY, LiteralText.EMPTY };
this.editable = true;
this.textBeingEdited = new OrderedText[4];
this.textColor = DyeColor.BLACK;
super();
}
public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
for (int i = 0; i < 4; ++i) {
String string = Text.Serializer.toJson(this.text[i]);
tag.putString("Text" + (i + 1), string);
}
tag.putString("Color", this.textColor.getName());
return tag;
}
public void fromTag(BlockState state, CompoundTag tag) {
this.editable = false;
super.fromTag(state, tag);
this.textColor = DyeColor.byName(tag.getString("Color"), DyeColor.BLACK);
for (int i = 0; i < 4; ++i) {
String string = tag.getString("Text" + (i + 1));
Text text = Text.Serializer.fromJson(string.isEmpty() ? "\"\"" : string);
if (this.world instanceof ServerWorld) {
try {
this.text[i] = Texts.parse(this.getCommandSource((ServerPlayerEntity) null), text, (Entity) null,
0);
} catch (CommandSyntaxException var7) {
this.text[i] = text;
}
} else {
this.text[i] = text;
}
this.textBeingEdited[i] = null;
}
}
public void setTextOnRow(int row, Text text) {
this.text[row] = text;
this.textBeingEdited[row] = null;
}
@Environment(EnvType.CLIENT)
public OrderedText getTextBeingEditedOnRow(int row, Function<Text, OrderedText> function) {
if (this.textBeingEdited[row] == null && this.text[row] != null) {
this.textBeingEdited[row] = (OrderedText) function.apply(this.text[row]);
}
return this.textBeingEdited[row];
}
public BlockEntityUpdateS2CPacket toUpdatePacket() {
return new BlockEntityUpdateS2CPacket(this.pos, 9, this.toInitialChunkDataTag());
}
public CompoundTag toInitialChunkDataTag() {
return this.toTag(new CompoundTag());
}
public boolean copyItemDataRequiresOperator() {
return true;
}
public boolean isEditable() {
return this.editable;
}
@Environment(EnvType.CLIENT)
public void setEditable(boolean bl) {
this.editable = bl;
if (!bl) {
this.editor = null;
}
}
public void setEditor(PlayerEntity player) {
this.editor = player;
}
public PlayerEntity getEditor() {
return this.editor;
}
public boolean onActivate(PlayerEntity player) {
Text[] var2 = this.text;
int var3 = var2.length;
for (int var4 = 0; var4 < var3; ++var4) {
Text text = var2[var4];
Style style = text == null ? null : text.getStyle();
if (style != null && style.getClickEvent() != null) {
ClickEvent clickEvent = style.getClickEvent();
if (clickEvent.getAction() == ClickEvent.Action.RUN_COMMAND) {
player.getServer().getCommandManager().execute(this.getCommandSource((ServerPlayerEntity) player),
clickEvent.getValue());
}
}
}
return true;
}
public ServerCommandSource getCommandSource(ServerPlayerEntity player) {
String string = player == null ? "Sign" : player.getName().getString();
Text text = player == null ? new LiteralText("Sign") : player.getDisplayName();
return new ServerCommandSource(CommandOutput.DUMMY, Vec3d.ofCenter(this.pos), Vec2f.ZERO,
(ServerWorld) this.world, 2, string, (Text) text, this.world.getServer(), player);
}
public DyeColor getTextColor() {
return this.textColor;
}
public boolean setTextColor(DyeColor value) {
if (value != this.getTextColor()) {
this.textColor = value;
this.markDirty();
this.world.updateListeners(this.getPos(), this.getCachedState(), this.getCachedState(), 3);
return true;
} else {
return false;
}
@Override
public BlockEntityType<?> getType() {
return EndBlockEntities.SIGN;
}
}