Files
zzz_REBIRTH__Utils/Scripts/Blocks/BlockFarmPlotRebirth.cs
2025-06-04 16:44:53 +09:30

259 lines
10 KiB
C#

using Audio;
using System.Globalization;
public class BlockFarmPlotRebirth : Block
{
private float TakeDelay = RebirthVariables.takeDelay;
private int waterMax = 6;
private bool canPickUpBlock = true;
public ulong TickRate = 1200UL;
public override bool HasBlockActivationCommands(WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, EntityAlive _entityFocusing)
{
return true;
}
public override int OnBlockDamaged(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, int _damagePoints, int _entityIdThatDamaged, ItemActionAttack.AttackHitInfo _attackHitInfo, bool _bUseHarvestTool, bool _bBypassMaxDamage, int _recDepth = 0)
{
EntityAlive entityAlive = _world.GetEntity(_entityIdThatDamaged) as EntityAlive;
if (entityAlive is EntityPlayer)
{
EntityPlayer entityPlayer = (EntityPlayer)entityAlive;
if (entityPlayer)
{
if (entityPlayer.inventory.holdingItem.GetItemName().ToLower() == "meleehandplayer")
{
//return 0;
}
}
}
return base.OnBlockDamaged(_world, _clrIdx, _blockPos, _blockValue, _damagePoints, _entityIdThatDamaged, _attackHitInfo, _bUseHarvestTool, _bBypassMaxDamage, 0);
}
private BlockActivationCommand[] cmds = new BlockActivationCommand[]
{
new BlockActivationCommand("cancel", "x", false, false),
new BlockActivationCommand("take", "hand", false)
};
public override ulong GetTickRate()
{
this.TickRate = (ulong)StringParsers.ParseFloat(this.Properties.Values["TickRate"], 0, -1, NumberStyles.Any) * 20UL;
//Log.Out("BlockFarmPlotRebirth-GetTickRate this.TickRate: " + this.TickRate);
return this.TickRate;
}
public override void Init()
{
base.Init();
this.canPickUpBlock = true;
bool flag = this.Properties.Values.ContainsKey("TakeDelay");
if (flag)
{
this.TakeDelay = StringParsers.ParseFloat(this.Properties.Values["TakeDelay"], 0, -1, NumberStyles.Any);
}
bool flag2 = this.Properties.Values.ContainsKey("canPickUpBlock");
if (flag2)
{
StringParsers.TryParseBool(this.Properties.Values["canPickUpBlock"], out this.canPickUpBlock, 0, -1, true);
}
}
protected virtual void addTileEntity(WorldBase world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue)
{
TileEntityFarmPlotRebirth tileEntityFarmPlot = new TileEntityFarmPlotRebirth(_chunk);
tileEntityFarmPlot.localChunkPos = World.toBlock(_blockPos);
tileEntityFarmPlot.waterCount = 0;
_chunk.AddTileEntity(tileEntityFarmPlot);
//Log.Out("BlockFarmPlotRebirth-addTileEntity _blockPos: " + _blockPos);
}
protected virtual void removeTileEntity(WorldBase world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue)
{
//Log.Out("BlockFarmPlotRebirth-removeTileEntity START");
_chunk.RemoveTileEntityAt<TileEntityFarmPlotRebirth>((World)world, World.toBlock(_blockPos));
}
public override void OnBlockRemoved(WorldBase world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue)
{
//Log.Out("BlockFarmPlotRebirth-OnBlockRemoved START");
base.OnBlockRemoved(world, _chunk, _blockPos, _blockValue);
TileEntityFarmPlotRebirth tileEntityFarmPlot = world.GetTileEntity(_chunk.ClrIdx, _blockPos) as TileEntityFarmPlotRebirth;
if (tileEntityFarmPlot != null)
{
tileEntityFarmPlot.OnDestroy();
}
this.removeTileEntity(world, _chunk, _blockPos, _blockValue);
}
public override void OnBlockAdded(WorldBase _world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue)
{
//Log.Out("BlockFarmPlotRebirth-OnBlockAdded START");
base.OnBlockAdded(_world, _chunk, _blockPos, _blockValue);
this.addTileEntity(_world, _chunk, _blockPos, _blockValue);
if (!SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer)
{
//Log.Out("BlockFarmPlotRebirth-OnBlockAdded 1");
return;
}
if (GameManager.Instance.IsEditMode())
{
//Log.Out("BlockFarmPlotRebirth-OnBlockAdded 2");
return;
}
_blockValue.meta = 0;
//Log.Out("BlockFarmPlotRebirth-OnBlockAdded Tick");
_world.GetWBT().AddScheduledBlockUpdate(_chunk.ClrIdx, _blockPos, this.blockID, this.GetTickRate());
}
public override string GetActivationText(WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, EntityAlive _entityFocusing)
{
string activationText = ""; //Localization.Get("ttNoRainWater");
TileEntityFarmPlotRebirth tileEntity = _world.GetTileEntity(_clrIdx, _blockPos) as TileEntityFarmPlotRebirth;
if (tileEntity != null)
{
string color = "e3dc5b";
string colorMax = "5ed680";
if (tileEntity.waterCount == tileEntity.waterMax)
{
color = colorMax;
}
if (RebirthVariables.customAdvancedFarming)
{
activationText = Localization.Get("ttWaterContent") + " [[" + color + "]" + tileEntity.waterCount + "[-]/[" + colorMax + "]" + tileEntity.waterMax + "[-]]";
}
}
else
{
//Log.Out("BlockFarmPlotRebirth-GetActivationText ADD TILE ENTITY");
Chunk chunk = (Chunk)_world.GetChunkFromWorldPos(_blockPos);
this.addTileEntity(_world, chunk, _blockPos, _blockValue);
_world.GetWBT().AddScheduledBlockUpdate(_clrIdx, _blockPos, this.blockID, this.GetTickRate());
}
return (activationText);
}
public override bool OnBlockActivated(string _commandName, WorldBase _world, int _cIdx, Vector3i _blockPos, BlockValue _blockValue, EntityPlayerLocal _player)
{
bool ischild = _blockValue.ischild;
bool result = false;
if (ischild)
{
Vector3i parentPos = _blockValue.Block.multiBlockPos.GetParentPos(_blockPos, _blockValue);
BlockValue block = _world.GetBlock(parentPos);
result = this.OnBlockActivated(_commandName, _world, _cIdx, parentPos, block, _player);
}
else
{
TileEntityFarmPlotRebirth tileEntity = _world.GetTileEntity(_cIdx, _blockPos) as TileEntityFarmPlotRebirth;
if (tileEntity != null)
{
if (_commandName == this.cmds[0].text)
{
result = false;
}
else if (_commandName == this.cmds[1].text)
{
if (((World)_world).IsWithinTraderArea(_blockPos))
{
Manager.PlayInsidePlayerHead("ui_denied");
GameManager.ShowTooltip(_player as EntityPlayerLocal, Localization.Get("ttBelongsToTrader"), string.Empty, "ui_denied", null);
return false;
}
RebirthUtilities.TakeItemWithTimer(_world, _cIdx, _blockPos, _blockValue, _player, this.TakeDelay, null, "", 1, false);
result = true;
}
else
{
result = false;
}
return result;
}
}
return result;
}
public BlockFarmPlotRebirth()
{
this.HasTileEntity = true;
}
public override BlockActivationCommand[] GetBlockActivationCommands(WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, EntityAlive _entityFocusing)
{
this.cmds[0].enabled = false; // this.canPickUpBlock;
this.cmds[1].enabled = false; // this.canPickUpBlock;
return this.cmds;
}
public override bool UpdateTick(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, bool _bRandomTick, ulong _ticksIfLoaded, GameRandom _rnd)
{
//Log.Out("BlockFarmPlotRebirth-UpdateTick START");
if (!SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer)
{
//Log.Out("BlockFarmPlotRebirth-UpdateTick 0");
return false;
}
TileEntityFarmPlotRebirth tileEntity = _world.GetTileEntity(_clrIdx, _blockPos) as TileEntityFarmPlotRebirth;
if (tileEntity != null)
{
//Log.Out("BlockFarmPlotRebirth-UpdateTick 1");
bool flag3 = this.Properties.Values.ContainsKey("WaterMax");
if (flag3)
{
this.Properties.ParseInt(this.Properties.Values["WaterMax"], ref this.waterMax);
tileEntity.waterMax = this.waterMax;
}
float num = WeatherManager.Instance.GetCurrentTemperatureValue();
float numRain = 0;
if (num == 0)
{
//Log.Out("BlockFarmPlotRebirth-UpdateTick 1a");
numRain = RebirthUtilities.GetWeatherInfo(_blockPos, "wet") * 10;
}
else
{
//Log.Out("BlockFarmPlotRebirth-UpdateTick 1b");
numRain = 10 * WeatherManager.Instance.GetCurrentRainfallValue();
}
//Log.Out("BlockFarmPlotRebirth-UpdateTick numRain: " + numRain);
if (numRain > 0)
{
tileEntity.waterCount++;
//Log.Out("BlockFarmPlotRebirth-UpdateTick tileEntity.waterCount: " + tileEntity.waterCount);
if (tileEntity.waterCount > tileEntity.waterMax)
{
//Log.Out("BlockFarmPlotRebirth-UpdateTick 3");
tileEntity.waterCount = tileEntity.waterMax;
}
SingletonMonoBehaviour<ConnectionManager>.Instance.SendPackage((NetPackage)NetPackageManager.GetPackage<NetPackageUpdateFarmPlotRebirth>().Setup(_clrIdx, _blockPos, tileEntity.waterCount));
}
}
else
{
//Log.Out("BlockFarmPlotRebirth-UpdateTick 4");
}
_world.GetWBT().AddScheduledBlockUpdate(_clrIdx, _blockPos, this.blockID, this.GetTickRate());
return false;
}
}