use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
the class ItemScrollGuardHelp method useOn.
@Override
@NotNull
public ActionResultType useOn(ItemUseContext ctx) {
final ActionResultType result = super.useOn(ctx);
if (ctx.getLevel().isClientSide) {
return result;
}
final TileEntity te = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
if (te instanceof TileEntityColonyBuilding && ctx.getPlayer() != null) {
final IBuilding building = ((TileEntityColonyBuilding) te).getColony().getBuildingManager().getBuilding(ctx.getClickedPos());
if (!(building instanceof AbstractBuildingGuards)) {
LanguageHandler.sendPlayerMessage(ctx.getPlayer(), "minecolonies.scroll.noguardbuilding");
}
}
return result;
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
the class ItemScepterGuard method handleItemUsage.
/**
* Handles the usage of the item.
*
* @param worldIn the world it is used in.
* @param pos the position.
* @param compound the compound.
* @param playerIn the player using it.
* @return if it has been successful.
*/
@NotNull
private static ActionResultType handleItemUsage(final World worldIn, final BlockPos pos, final CompoundNBT compound, final PlayerEntity playerIn) {
if (!compound.getAllKeys().contains(TAG_ID)) {
return ActionResultType.FAIL;
}
final IColony colony = IColonyManager.getInstance().getColonyByWorld(compound.getInt(TAG_ID), worldIn);
if (colony == null) {
return ActionResultType.FAIL;
}
final BlockPos guardTower = BlockPosUtil.read(compound, TAG_POS);
final IBuilding hut = colony.getBuildingManager().getBuilding(guardTower);
if (!(hut instanceof AbstractBuildingGuards)) {
return ActionResultType.FAIL;
}
final IGuardBuilding tower = (IGuardBuilding) hut;
if (BlockPosUtil.getDistance2D(pos, guardTower) > tower.getPatrolDistance()) {
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolClickGuardTooFar");
return ActionResultType.FAIL;
}
if (hut.getSetting(AbstractBuildingGuards.GUARD_TASK).getValue().equals(GuardTaskSetting.GUARD)) {
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolclickguard", pos);
tower.setGuardPos(pos);
playerIn.inventory.removeItemNoUpdate(playerIn.inventory.selected);
} else {
if (!compound.getAllKeys().contains(TAG_LAST_POS)) {
tower.resetPatrolTargets();
}
tower.addPatrolTargets(pos);
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolclickpatrol", pos);
}
BlockPosUtil.write(compound, TAG_LAST_POS, pos);
return ActionResultType.SUCCESS;
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
the class BuildingManager method read.
@Override
public void read(@NotNull final CompoundNBT compound) {
buildings = ImmutableMap.of();
maxChunkX = colony.getCenter().getX() >> 4;
minChunkX = colony.getCenter().getX() >> 4;
maxChunkZ = colony.getCenter().getZ() >> 4;
minChunkZ = colony.getCenter().getZ() >> 4;
// Buildings
final ListNBT buildingTagList = compound.getList(TAG_BUILDINGS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < buildingTagList.size(); ++i) {
final CompoundNBT buildingCompound = buildingTagList.getCompound(i);
@Nullable final IBuilding b = IBuildingDataManager.getInstance().createFrom(colony, buildingCompound);
if (b != null) {
addBuilding(b);
setMaxChunk(b);
}
}
if (compound.getAllKeys().contains(TAG_NEW_FIELDS)) {
// Fields before Buildings, because the Farmer needs them.
final ListNBT fieldTagList = compound.getList(TAG_NEW_FIELDS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < fieldTagList.size(); ++i) {
addField(BlockPosUtil.read(fieldTagList.getCompound(i), TAG_POS));
}
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
the class BuildingManager method removeBuilding.
@Override
public void removeBuilding(@NotNull final IBuilding building, final Set<ServerPlayerEntity> subscribers) {
if (buildings.containsKey(building.getID())) {
final ImmutableMap.Builder<BlockPos, IBuilding> builder = new ImmutableMap.Builder<>();
for (final IBuilding tbuilding : buildings.values()) {
if (tbuilding != building) {
builder.put(tbuilding.getID(), tbuilding);
}
}
buildings = builder.build();
for (final ServerPlayerEntity player : subscribers) {
Network.getNetwork().sendToPlayer(new ColonyViewRemoveBuildingMessage(colony, building.getID()), player);
}
Log.getLogger().info(String.format("Colony %d - removed AbstractBuilding %s of type %s", colony.getID(), building.getID(), building.getSchematicName()));
}
if (building instanceof BuildingTownHall) {
townHall = null;
} else if (building instanceof BuildingWareHouse) {
wareHouses.remove(building);
} else if (building instanceof BuildingMysticalSite) {
mysticalSites.remove(building);
}
// Allow Citizens to fix up any data that wasn't fixed up by the AbstractBuilding's own onDestroyed
for (@NotNull final ICitizenData citizen : colony.getCitizenManager().getCitizens()) {
citizen.onRemoveBuilding(building);
building.cancelAllRequestsOfCitizen(citizen);
}
colony.getRequestManager().onProviderRemovedFromColony(building);
colony.getRequestManager().onRequesterRemovedFromColony(building.getRequester());
colony.getCitizenManager().calculateMaxCitizens();
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
the class BuildingManager method sendBuildingPackets.
/**
* Sends packages to update the buildings.
*
* @param closeSubscribers the current event subscribers.
* @param newSubscribers the new event subscribers.
*/
private void sendBuildingPackets(final Set<ServerPlayerEntity> closeSubscribers, final Set<ServerPlayerEntity> newSubscribers) {
if (isBuildingsDirty || !newSubscribers.isEmpty()) {
final Set<ServerPlayerEntity> players = new HashSet<>();
if (isBuildingsDirty) {
players.addAll(closeSubscribers);
}
players.addAll(newSubscribers);
for (@NotNull final IBuilding building : buildings.values()) {
if (building.isDirty() || !newSubscribers.isEmpty()) {
players.forEach(player -> Network.getNetwork().sendToPlayer(new ColonyViewBuildingViewMessage(building), player));
}
}
}
}
Aggregations