Player detector got radius config

This commit is contained in:
drcrazy 2021-06-18 23:05:43 +03:00
parent 88d5ee015a
commit 8ab3b3a7be
7 changed files with 180 additions and 24 deletions

View file

@ -29,8 +29,12 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import reborncore.api.IToolDrop;
import reborncore.client.screen.BuiltScreenHandlerProvider;
import reborncore.client.screen.builder.BuiltScreenHandler;
import reborncore.client.screen.builder.ScreenHandlerBuilder;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.powerSystem.PowerAcceptorBlockEntity;
import reborncore.common.util.WorldUtils;
@ -41,13 +45,14 @@ import techreborn.config.TechRebornConfig;
import techreborn.init.TRBlockEntities;
import techreborn.init.TRContent;
public class PlayerDectectorBlockEntity extends PowerAcceptorBlockEntity implements IToolDrop {
public class PlayerDetectorBlockEntity extends PowerAcceptorBlockEntity implements IToolDrop, BuiltScreenHandlerProvider {
public String owenerUdid = "";
public String ownerUdid = "";
boolean redstone = false;
int radius = 16;
public PlayerDectectorBlockEntity(BlockPos pos, BlockState state) {
public PlayerDetectorBlockEntity(BlockPos pos, BlockState state) {
super(TRBlockEntities.PLAYER_DETECTOR, pos, state);
}
@ -55,16 +60,23 @@ public class PlayerDectectorBlockEntity extends PowerAcceptorBlockEntity impleme
return redstone;
}
public void handleGuiInputFromClient(int amount) {
radius += amount;
if (radius > TechRebornConfig.playerDetectorMaxRadius) {
radius = TechRebornConfig.playerDetectorMaxRadius;
}
if (radius <= 1) {
radius = 1;
}
}
// PowerAcceptorBlockEntity
@Override
public void tick(World world, BlockPos pos, BlockState state, MachineBaseBlockEntity blockEntity) {
super.tick(world, pos, state, blockEntity);
if (world == null) {
return;
}
if (world.isClient) {
if (world == null || world.isClient) {
return;
}
@ -76,16 +88,16 @@ public class PlayerDectectorBlockEntity extends PowerAcceptorBlockEntity impleme
redstone = false;
if (getStored(EnergySide.UNKNOWN) > TechRebornConfig.playerDetectorEuPerTick) {
for (PlayerEntity player : world.getPlayers()) {
if (player.distanceTo(player) <= 256.0D) {
if (MathHelper.sqrt((float)player.squaredDistanceTo(pos.getX(), pos.getY(), pos.getZ())) <= (float)radius ) {
PlayerDetectorType type = world.getBlockState(pos).get(PlayerDetectorBlock.TYPE);
if (type == PlayerDetectorType.ALL) {// ALL
redstone = true;
} else if (type == PlayerDetectorType.OTHERS) {// Others
if (!owenerUdid.isEmpty() && !owenerUdid.equals(player.getUuid().toString())) {
if (!ownerUdid.isEmpty() && !ownerUdid.equals(player.getUuid().toString())) {
redstone = true;
}
} else {// You
if (!owenerUdid.isEmpty() && owenerUdid.equals(player.getUuid().toString())) {
if (!ownerUdid.isEmpty() && ownerUdid.equals(player.getUuid().toString())) {
redstone = true;
}
}
@ -122,19 +134,52 @@ public class PlayerDectectorBlockEntity extends PowerAcceptorBlockEntity impleme
@Override
public void readNbt(NbtCompound tag) {
super.readNbt(tag);
owenerUdid = tag.getString("ownerID");
ownerUdid = tag.getString("ownerID");
radius = tag.getInt("radius");
}
@Override
public NbtCompound writeNbt(NbtCompound tag) {
super.writeNbt(tag);
tag.putString("ownerID", owenerUdid);
tag.putString("ownerID", ownerUdid);
tag.putInt("radius", radius);
return tag;
}
// MachineBaseBlockEntity
@Override
public boolean hasSlotConfig() {
return false;
}
@Override
public boolean canBeUpgraded() {
return false;
}
// IToolDrop
@Override
public ItemStack getToolDrop(PlayerEntity p0) {
return TRContent.Machine.PLAYER_DETECTOR.getStack();
}
// BuiltScreenHandlerProvider
@Override
public BuiltScreenHandler createScreenHandler(int syncID, PlayerEntity player) {
return new ScreenHandlerBuilder("player_detector")
.player(player.getInventory())
.inventory().hotbar().addInventory()
.blockEntity(this)
.syncEnergyValue()
.sync(this::getCurrentRadius, this::setCurrentRadius)
.addInventory().create(this, syncID);
}
public int getCurrentRadius() {
return radius;
}
public void setCurrentRadius(int radius) {
this.radius = radius;
}
}

View file

@ -51,7 +51,8 @@ import reborncore.api.blockentity.IMachineGuiHandler;
import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.util.ChatUtils;
import reborncore.common.util.StringUtils;
import techreborn.blockentity.machine.tier1.PlayerDectectorBlockEntity;
import techreborn.blockentity.machine.tier1.PlayerDetectorBlockEntity;
import techreborn.client.GuiType;
import techreborn.utils.MessageIDs;
public class PlayerDetectorBlock extends BlockMachineBase {
@ -66,15 +67,15 @@ public class PlayerDetectorBlock extends BlockMachineBase {
// BlockMachineBase
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new PlayerDectectorBlockEntity(pos, state);
return new PlayerDetectorBlockEntity(pos, state);
}
@Override
public void onPlaced(World worldIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.onPlaced(worldIn, pos, state, placer, stack);
BlockEntity blockEntity = worldIn.getBlockEntity(pos);
if (blockEntity instanceof PlayerDectectorBlockEntity) {
((PlayerDectectorBlockEntity) blockEntity).owenerUdid = placer.getUuid().toString();
if (blockEntity instanceof PlayerDetectorBlockEntity) {
((PlayerDetectorBlockEntity) blockEntity).ownerUdid = placer.getUuid().toString();
}
}
@ -133,12 +134,18 @@ public class PlayerDetectorBlock extends BlockMachineBase {
)
);
}
if (getGui() != null && !playerIn.isSneaking()) {
getGui().open(playerIn, pos, worldIn);
return ActionResult.SUCCESS;
}
return ActionResult.SUCCESS;
}
@Override
public IMachineGuiHandler getGui() {
return null;
return GuiType.PLAYER_DETECTOR;
}
@Override
@ -157,8 +164,8 @@ public class PlayerDetectorBlock extends BlockMachineBase {
@Override
public int getWeakRedstonePower(BlockState blockState, BlockView blockAccess, BlockPos pos, Direction side) {
BlockEntity entity = blockAccess.getBlockEntity(pos);
if (entity instanceof PlayerDectectorBlockEntity) {
return ((PlayerDectectorBlockEntity) entity).isProvidingPower() ? 15 : 0;
if (entity instanceof PlayerDetectorBlockEntity) {
return ((PlayerDetectorBlockEntity) entity).isProvidingPower() ? 15 : 0;
}
return 0;
}
@ -167,8 +174,8 @@ public class PlayerDetectorBlock extends BlockMachineBase {
@Override
public int getStrongRedstonePower(BlockState blockState, BlockView blockAccess, BlockPos pos, Direction side) {
BlockEntity entity = blockAccess.getBlockEntity(pos);
if (entity instanceof PlayerDectectorBlockEntity) {
return ((PlayerDectectorBlockEntity) entity).isProvidingPower() ? 15 : 0;
if (entity instanceof PlayerDetectorBlockEntity) {
return ((PlayerDetectorBlockEntity) entity).isProvidingPower() ? 15 : 0;
}
return 0;
}

View file

@ -125,6 +125,7 @@ public final class GuiType<T extends BlockEntity> implements IMachineGuiHandler
public static final GuiType<WireMillBlockEntity> WIRE_MILL = register("wire_mill", () -> () -> GuiWireMill::new);
public static final GuiType<GreenhouseControllerBlockEntity> GREENHOUSE_CONTROLLER = register("greenhouse_controller", () -> () -> GuiGreenhouseController::new);
public static final GuiType<FluidReplicatorBlockEntity> FLUID_REPLICATOR = register("fluid_replicator", () -> () -> GuiFluidReplicator::new);
public static final GuiType<PlayerDetectorBlockEntity> PLAYER_DETECTOR = register("player_detector", () -> () -> GuiPlayerDetector::new);
public static final GuiType<DataDrivenBEProvider.DataDrivenBlockEntity> DATA_DRIVEN = register("data_driven", () -> () -> DataDrivenGui::new);

View file

@ -0,0 +1,80 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2020 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.gui;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import reborncore.client.gui.builder.GuiBase;
import reborncore.client.gui.builder.widget.GuiButtonUpDown;
import reborncore.client.screen.builder.BuiltScreenHandler;
import reborncore.common.network.NetworkManager;
import techreborn.blockentity.machine.tier1.PlayerDetectorBlockEntity;
import techreborn.packets.ServerboundPackets;
public class GuiPlayerDetector extends GuiBase<BuiltScreenHandler> {
PlayerDetectorBlockEntity blockEntity;
public GuiPlayerDetector(int syncID, final PlayerEntity player, final PlayerDetectorBlockEntity blockEntity) {
super(player, blockEntity, blockEntity.createScreenHandler(syncID, player));
this.blockEntity = blockEntity;
}
private void onClick(int amount) {
NetworkManager.sendToServer(ServerboundPackets.createPacketPlayerDetector(amount, blockEntity));
}
@Override
public void init() {
super.init();
addDrawableChild(new GuiButtonUpDown(x + 64, y + 40, this, b -> onClick(16), GuiButtonUpDown.UpDownButtonType.FASTFORWARD));
addDrawableChild(new GuiButtonUpDown(x + 64 + 12, y + 40, this, b -> onClick(1), GuiButtonUpDown.UpDownButtonType.FORWARD));
addDrawableChild(new GuiButtonUpDown(x + 64 + 24, y + 40, this, b -> onClick(-1), GuiButtonUpDown.UpDownButtonType.REWIND));
addDrawableChild(new GuiButtonUpDown(x + 64 + 36, y + 40, this, b -> onClick(-16), GuiButtonUpDown.UpDownButtonType.FASTREWIND));
}
@Override
protected void drawBackground(MatrixStack matrixStack, float partialTicks, int mouseX, int mouseY) {
super.drawBackground(matrixStack, partialTicks, mouseX, mouseY);
final Layer layer = Layer.BACKGROUND;
if (hideGuiElements()) return;
Text text = new LiteralText("Radius: ").append(String.valueOf(blockEntity.getCurrentRadius()));
drawCentredText(matrixStack, text, 25, 4210752, layer);
}
@Override
protected void drawForeground(MatrixStack matrixStack, final int mouseX, final int mouseY) {
super.drawForeground(matrixStack, mouseX, mouseY);
final Layer layer = Layer.FOREGROUND;
builder.drawMultiEnergyBar(matrixStack, this, 9, 19, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxStoredPower(), mouseX, mouseY, 0, layer);
}
}

View file

@ -333,9 +333,12 @@ public class TechRebornConfig {
@Config(config = "machines", category = "player_detector", key = "PlayerDetectorMaxEnergy", comment = "Player Detector Max Energy")
public static int playerDetectorMaxEnergy = 10000;
@Config(config = "machines", category = "player_detector", key = "PlayerDetectorEUPerSecond", comment = "Player Detector Energy Consumption per tick")
@Config(config = "machines", category = "player_detector", key = "PlayerDetectorEnergyUsage", comment = "Player Detector Energy Consumption per second")
public static int playerDetectorEuPerTick = 1;
@Config(config = "machines", category = "player_detector", key = "PlayerDetectorMaxRadius", comment = "Player Detector maximum detection radius")
public static int playerDetectorMaxRadius = 128;
@Config(config = "machines", category = "Distillation_tower", key = "DistillationTowerMaxInput", comment = "Distillation Tower Max Input (Energy per tick)")
public static int distillationTowerMaxInput = 128;

View file

@ -92,7 +92,7 @@ public class TRBlockEntities {
public static final BlockEntityType<MatterFabricatorBlockEntity> MATTER_FABRICATOR = register(MatterFabricatorBlockEntity::new, "matter_fabricator", TRContent.Machine.MATTER_FABRICATOR);
public static final BlockEntityType<ChunkLoaderBlockEntity> CHUNK_LOADER = register(ChunkLoaderBlockEntity::new, "chunk_loader", TRContent.Machine.CHUNK_LOADER);
public static final BlockEntityType<ChargeOMatBlockEntity> CHARGE_O_MAT = register(ChargeOMatBlockEntity::new, "charge_o_mat", TRContent.Machine.CHARGE_O_MAT);
public static final BlockEntityType<PlayerDectectorBlockEntity> PLAYER_DETECTOR = register(PlayerDectectorBlockEntity::new, "player_detector", TRContent.Machine.PLAYER_DETECTOR);
public static final BlockEntityType<PlayerDetectorBlockEntity> PLAYER_DETECTOR = register(PlayerDetectorBlockEntity::new, "player_detector", TRContent.Machine.PLAYER_DETECTOR);
public static final BlockEntityType<CableBlockEntity> CABLE = register(CableBlockEntity::new, "cable", TRContent.Cables.values());
public static final BlockEntityType<MachineCasingBlockEntity> MACHINE_CASINGS = register(MachineCasingBlockEntity::new, "machine_casing", TRContent.MachineBlocks.getCasings());
public static final BlockEntityType<DragonEggSyphonBlockEntity> DRAGON_EGG_SYPHON = register(DragonEggSyphonBlockEntity::new, "dragon_egg_syphon", TRContent.Machine.DRAGON_EGG_SYPHON);

View file

@ -35,6 +35,7 @@ import techreborn.TechReborn;
import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity;
import techreborn.blockentity.machine.multiblock.FusionControlComputerBlockEntity;
import techreborn.blockentity.machine.tier1.AutoCraftingTableBlockEntity;
import techreborn.blockentity.machine.tier1.PlayerDetectorBlockEntity;
import techreborn.blockentity.machine.tier1.RollingMachineBlockEntity;
import techreborn.blockentity.machine.tier3.ChunkLoaderBlockEntity;
import techreborn.blockentity.storage.energy.AdjustableSUBlockEntity;
@ -52,6 +53,7 @@ public class ServerboundPackets {
public static final Identifier REFUND = new Identifier(TechReborn.MOD_ID, "refund");
public static final Identifier CHUNKLOADER = new Identifier(TechReborn.MOD_ID, "chunkloader");
public static final Identifier EXPERIENCE = new Identifier(TechReborn.MOD_ID, "experience");
public static final Identifier DETECTOR_RADIUS = new Identifier(TechReborn.MOD_ID, "detector_radius");
public static void init() {
NetworkManager.registerServerBoundHandler(AESU, (server, player, handler, buf, responseSender) -> {
@ -157,6 +159,18 @@ public class ServerboundPackets {
}
});
});
NetworkManager.registerServerBoundHandler(DETECTOR_RADIUS, ((server, player, handler, buf, responseSender) -> {
BlockPos pos = buf.readBlockPos();
int buttonAmount = buf.readInt();
server.execute(() -> {
BlockEntity blockEntity = player.world.getBlockEntity(pos);
if (blockEntity instanceof PlayerDetectorBlockEntity) {
((PlayerDetectorBlockEntity) blockEntity).handleGuiInputFromClient(buttonAmount);
}
});
}));
}
public static IdentifiedPacket createPacketAesu(int buttonID, boolean shift, boolean ctrl, AdjustableSUBlockEntity blockEntity) {
@ -215,4 +229,10 @@ public class ServerboundPackets {
return NetworkManager.createServerBoundPacket(EXPERIENCE, extendedPacketBuffer -> extendedPacketBuffer.writeBlockPos(blockEntity.getPos()));
}
public static IdentifiedPacket createPacketPlayerDetector(int buttonAmount, PlayerDetectorBlockEntity blockEntity) {
return NetworkManager.createServerBoundPacket(DETECTOR_RADIUS, buf -> {
buf.writeBlockPos(blockEntity.getPos());
buf.writeInt(buttonAmount);
});
}
}