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.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState; import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids; import net.minecraft.fluid.Fluids;
import net.minecraft.item.DyeItem;
import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.loot.context.LootContext; 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.BooleanProperty;
import net.minecraft.state.property.IntProperty; import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties; import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.BlockMirror; import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation; import net.minecraft.util.BlockRotation;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.SignType; import net.minecraft.util.SignType;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper; 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 IntProperty ROTATION = Properties.ROTATION;
public static final BooleanProperty FLOOR = BooleanProperty.of("floor"); public static final BooleanProperty FLOOR = BooleanProperty.of("floor");
private static final VoxelShape[] WALL_SHAPES = new VoxelShape[] { 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, 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, 2.0D, 12.5D, 16.0D),
Block.createCuboidShape(0.0D, 4.5D, 0.0D, 16.0D, 12.5D, 2.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(14.0D, 4.5D, 0.0D, 16.0D, 12.5D, 16.0D)
};
private final Block parent; private final Block parent;
public EndSignBlock(Block source) { public EndSignBlock(Block source) {
super(FabricBlockSettings.copyOf(source).strength(1.0F, 1.0F).noCollision().nonOpaque(), SignType.OAK); 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; this.parent = source;
} }
@ -78,29 +75,6 @@ public class EndSignBlock extends AbstractSignBlock implements BlockPatterned {
return new ESignBlockEntity(); 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 @Override
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) { public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
if (placer != null && placer instanceof PlayerEntity) { if (placer != null && placer instanceof PlayerEntity) {
@ -108,8 +82,10 @@ public class EndSignBlock extends AbstractSignBlock implements BlockPatterned {
if (!world.isClient) { if (!world.isClient) {
sign.setEditor((PlayerEntity) placer); sign.setEditor((PlayerEntity) placer);
((ServerPlayerEntity) placer).networkHandler.sendPacket(new SignEditorOpenS2CPacket(pos)); ((ServerPlayerEntity) placer).networkHandler.sendPacket(new SignEditorOpenS2CPacket(pos));
} else }
else {
sign.setEditable(true); sign.setEditable(true);
}
} }
} }
@ -149,14 +125,12 @@ public class EndSignBlock extends AbstractSignBlock implements BlockPatterned {
WorldView worldView = ctx.getWorld(); WorldView worldView = ctx.getWorld();
BlockPos blockPos = ctx.getBlockPos(); BlockPos blockPos = ctx.getBlockPos();
Direction[] directions = ctx.getPlacementDirections(); Direction[] directions = ctx.getPlacementDirections();
Direction[] var7 = directions;
int var8 = directions.length;
for (int var9 = 0; var9 < var8; ++var9) { for (int i = 0; i < directions.length; ++i) {
Direction direction = var7[var9]; Direction direction = directions[i];
if (direction.getAxis().isHorizontal()) { if (direction.getAxis().isHorizontal()) {
Direction direction2 = direction.getOpposite(); Direction dir = direction.getOpposite();
int rot = MathHelper.floor((180.0 + direction2.asRotation() * 16.0 / 360.0) + 0.5 + 4) & 15; int rot = MathHelper.floor((180.0 + dir.asRotation() * 16.0 / 360.0) + 0.5 + 4) & 15;
blockState = blockState.with(ROTATION, rot); blockState = blockState.with(ROTATION, rot);
if (blockState.canPlaceAt(worldView, blockPos)) { if (blockState.canPlaceAt(worldView, blockPos)) {
return blockState.with(FLOOR, false).with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER); return blockState.with(FLOOR, false).with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER);

View file

@ -1,168 +1,16 @@
package ru.betterend.blocks.entities; package ru.betterend.blocks.entities;
import java.util.function.Function; import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.SignBlockEntity;
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 ru.betterend.registry.EndBlockEntities; import ru.betterend.registry.EndBlockEntities;
public class ESignBlockEntity extends BlockEntity { public class ESignBlockEntity extends SignBlockEntity {
private final Text[] text;
private boolean editable;
private PlayerEntity editor;
private final OrderedText[] textBeingEdited;
private DyeColor textColor;
public ESignBlockEntity() { public ESignBlockEntity() {
super(EndBlockEntities.SIGN); super();
this.text = new Text[] { LiteralText.EMPTY, LiteralText.EMPTY, LiteralText.EMPTY, LiteralText.EMPTY };
this.editable = true;
this.textBeingEdited = new OrderedText[4];
this.textColor = DyeColor.BLACK;
} }
public CompoundTag toTag(CompoundTag tag) { @Override
super.toTag(tag); public BlockEntityType<?> getType() {
return EndBlockEntities.SIGN;
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;
}
} }
} }

View file

@ -11,12 +11,9 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.world.ClientWorld; import net.minecraft.client.world.ClientWorld;
import net.minecraft.network.NetworkThreadUtils; import net.minecraft.network.NetworkThreadUtils;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.SignEditorOpenS2CPacket; import net.minecraft.network.packet.s2c.play.SignEditorOpenS2CPacket;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.thread.ThreadExecutor; import net.minecraft.util.thread.ThreadExecutor;
import ru.betterend.blocks.entities.ESignBlockEntity; import ru.betterend.blocks.entities.ESignBlockEntity;
import ru.betterend.blocks.entities.PedestalBlockEntity;
import ru.betterend.client.gui.BlockSignEditScreen; import ru.betterend.client.gui.BlockSignEditScreen;
@Mixin(ClientPlayNetworkHandler.class) @Mixin(ClientPlayNetworkHandler.class)
@ -30,8 +27,7 @@ public class ClientPlayNetworkHandlerMixin
@Inject(method = "onSignEditorOpen", at = @At(value = "HEAD"), cancellable = true) @Inject(method = "onSignEditorOpen", at = @At(value = "HEAD"), cancellable = true)
public void be_openSignEditor(SignEditorOpenS2CPacket packet, CallbackInfo info) { public void be_openSignEditor(SignEditorOpenS2CPacket packet, CallbackInfo info) {
NetworkThreadUtils.forceMainThread(packet, (ClientPlayNetworkHandler) (Object) this, NetworkThreadUtils.forceMainThread(packet, (ClientPlayNetworkHandler) (Object) this, (ThreadExecutor<?>) client);
(ThreadExecutor<?>) client);
BlockEntity blockEntity = this.world.getBlockEntity(packet.getPos()); BlockEntity blockEntity = this.world.getBlockEntity(packet.getPos());
if (blockEntity instanceof ESignBlockEntity) { if (blockEntity instanceof ESignBlockEntity) {
ESignBlockEntity sign = (ESignBlockEntity) blockEntity; ESignBlockEntity sign = (ESignBlockEntity) blockEntity;
@ -39,16 +35,4 @@ public class ClientPlayNetworkHandlerMixin
info.cancel(); info.cancel();
} }
} }
@Inject(method = "onBlockEntityUpdate", at = @At(value = "HEAD"), cancellable = true)
public void be_onEntityUpdate(BlockEntityUpdateS2CPacket packet, CallbackInfo info) {
NetworkThreadUtils.forceMainThread(packet, (ClientPlayNetworkHandler) (Object) this,
(ThreadExecutor<?>) client);
BlockPos blockPos = packet.getPos();
BlockEntity blockEntity = this.client.world.getBlockEntity(blockPos);
if (blockEntity instanceof ESignBlockEntity || blockEntity instanceof PedestalBlockEntity) {
blockEntity.fromTag(this.client.world.getBlockState(blockPos), packet.getCompoundTag());
info.cancel();
}
}
} }

View file

@ -1,60 +0,0 @@
package ru.betterend.mixin.common;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.network.NetworkThreadUtils;
import net.minecraft.network.packet.c2s.play.UpdateSignC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.BlockPos;
import ru.betterend.blocks.entities.ESignBlockEntity;
@Mixin(ServerPlayNetworkHandler.class)
public class ServerPlayNetworkHandlerMixin {
@Shadow
private static final Logger LOGGER = LogManager.getLogger();
@Shadow
public ServerPlayerEntity player;
@Inject(method = "onSignUpdate", at = @At(value = "HEAD"), cancellable = true)
private void be_signUpdate(UpdateSignC2SPacket packet, CallbackInfo info) {
NetworkThreadUtils.forceMainThread(packet, ServerPlayNetworkHandler.class.cast(this), player.getServerWorld());
this.player.updateLastActionTime();
ServerWorld serverWorld = this.player.getServerWorld();
BlockPos blockPos = packet.getPos();
if (serverWorld.isChunkLoaded(blockPos)) {
BlockState blockState = serverWorld.getBlockState(blockPos);
BlockEntity blockEntity = serverWorld.getBlockEntity(blockPos);
if (blockEntity instanceof ESignBlockEntity) {
ESignBlockEntity signBlockEntity = (ESignBlockEntity) blockEntity;
if (!signBlockEntity.isEditable() || signBlockEntity.getEditor() != this.player) {
LOGGER.warn("Player {} just tried to change non-editable sign", this.player.getName().getString());
return;
}
String[] strings = packet.getText();
for (int i = 0; i < strings.length; ++i) {
signBlockEntity.setTextOnRow(i, new LiteralText(Formatting.strip(strings[i])));
}
signBlockEntity.markDirty();
serverWorld.updateListeners(blockPos, blockState, blockState, 3);
info.cancel();
}
}
}
}

View file

@ -103,6 +103,7 @@ import ru.betterend.blocks.basis.EndCropBlock;
import ru.betterend.blocks.basis.EndFurnaceBlock; import ru.betterend.blocks.basis.EndFurnaceBlock;
import ru.betterend.blocks.basis.EndLeavesBlock; import ru.betterend.blocks.basis.EndLeavesBlock;
import ru.betterend.blocks.basis.EndOreBlock; import ru.betterend.blocks.basis.EndOreBlock;
import ru.betterend.blocks.basis.EndSignBlock;
import ru.betterend.blocks.basis.EndUnderwaterWallPlantBlock; import ru.betterend.blocks.basis.EndUnderwaterWallPlantBlock;
import ru.betterend.blocks.basis.EndWallPlantBlock; import ru.betterend.blocks.basis.EndWallPlantBlock;
import ru.betterend.blocks.basis.FurBlock; import ru.betterend.blocks.basis.FurBlock;
@ -375,7 +376,8 @@ public class EndBlocks {
return block; return block;
} }
Registry.register(Registry.BLOCK, id, block); Registry.register(Registry.BLOCK, id, block);
EndItems.registerBlockItem(id, new BlockItem(block, EndItems.makeBlockItemSettings())); int maxCount = block instanceof EndSignBlock ? 16 : 64;
EndItems.registerBlockItem(id, new BlockItem(block, EndItems.makeBlockItemSettings().maxCount(maxCount)));
return block; return block;
} }

View file

@ -6,7 +6,6 @@
"mixins": [ "mixins": [
"EnchantmentScreenHandlerMixin", "EnchantmentScreenHandlerMixin",
"PlayerAdvancementTrackerMixin", "PlayerAdvancementTrackerMixin",
"ServerPlayNetworkHandlerMixin",
"CraftingScreenHandlerMixin", "CraftingScreenHandlerMixin",
"GenerationSettingsAccessor", "GenerationSettingsAccessor",
"NoiseChunkGeneratorMixin", "NoiseChunkGeneratorMixin",